| 1 | #! /usr/bin/perl |
| 2 | ### |
| 3 | ### Pastebin userv interface for Odin |
| 4 | ### |
| 5 | ### (c) 2015 Mark Wooding |
| 6 | ### |
| 7 | |
| 8 | ###----- Licensing notice --------------------------------------------------- |
| 9 | ### |
| 10 | ### This file is part of the `odin.gg' service, `odin-cgi'. |
| 11 | ### |
| 12 | ### `odin-cgi' is free software; you can redistribute it and/or modify |
| 13 | ### it under the terms of the GNU Affero General Public License as |
| 14 | ### published by the Free Software Foundation; either version 3 of the |
| 15 | ### License, or (at your option) any later version. |
| 16 | ### |
| 17 | ### `odin-cgi' is distributed in the hope that it will be useful, |
| 18 | ### but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | ### GNU Affero General Public License for more details. |
| 21 | ### |
| 22 | ### You should have received a copy of the GNU Affero General Public |
| 23 | ### License along with `odin-cgi'; if not, see |
| 24 | ### <http://www.gnu.org/licenses/>. |
| 25 | |
| 26 | use lib "lib"; |
| 27 | |
| 28 | use Odin; |
| 29 | use DBI; |
| 30 | use Encode; |
| 31 | use Encode::Locale; |
| 32 | |
| 33 | my $BAD = 0; |
| 34 | |
| 35 | sub bad ($) { |
| 36 | my ($m) = @_; |
| 37 | $BAD = 1; |
| 38 | print STDERR "$Odin::PROG: $m\n"; |
| 39 | } |
| 40 | |
| 41 | Odin::cmdline_who; |
| 42 | |
| 43 | sub read_content () { |
| 44 | my $c = ""; |
| 45 | while (read STDIN, my $buf, 8192) { $c .= $buf; } |
| 46 | return Odin::tidy_pastebin_content decode locale => $c; |
| 47 | } |
| 48 | |
| 49 | my $op = shift(@ARGV) // "help"; |
| 50 | if ($op eq "help") { |
| 51 | print <<EOF; |
| 52 | Commands available: |
| 53 | |
| 54 | claim TAG EDITKEY |
| 55 | del TAG ... |
| 56 | get TAG |
| 57 | help |
| 58 | langs |
| 59 | list |
| 60 | new [-l LANG] [-t TITLE] |
| 61 | rekey TAG |
| 62 | update [-c] [-l LANG] [-t TITLE] TAG |
| 63 | EOF |
| 64 | } elsif ($op eq "langs") { |
| 65 | @ARGV == 0 or Odin::fail "usage: list"; |
| 66 | my $db = Odin::open_db; |
| 67 | for my $r (@{$db->selectall_arrayref |
| 68 | ("SELECT lang, descr FROM odin_pastebin_lang |
| 69 | ORDER BY lang", undef)}) { |
| 70 | my ($lang, $descr) = @$r; |
| 71 | Odin::print_columns $lang => 16, $descr => 0; |
| 72 | } |
| 73 | } elsif ($op eq "list") { |
| 74 | @ARGV == 0 or Odin::fail "usage: list"; |
| 75 | my $db = Odin::open_db; |
| 76 | for my $r (@{$db->selectall_arrayref |
| 77 | ("SELECT tag, stamp, lang, title |
| 78 | FROM odin_pastebin WHERE owner = ? |
| 79 | ORDER BY stamp", undef, $Odin::WHO)}) { |
| 80 | my ($tag, $stamp, $lang, $title) = @$r; |
| 81 | Odin::print_columns Odin::fmt_time $stamp => 25, |
| 82 | $tag => 12, $lang => 16, (encode locale => $title) => 0; |
| 83 | } |
| 84 | } elsif ($op eq "new") { |
| 85 | my $op = Odin::OptParse->new(@ARGV); |
| 86 | my $p = (title => undef, lang => "txt"); |
| 87 | while (my $o = $op->get) { |
| 88 | if ($o eq "l") { $p{lang} = $op->arg; } |
| 89 | elsif ($o eq "t") { $p{title} = decode locale => $op->arg; } |
| 90 | else { $op->unk; } |
| 91 | } |
| 92 | @ARGV = $op->rest; |
| 93 | $op->bad if @ARGV; |
| 94 | $op->ok or Odin::fail "usage: new [-l LANG] [-t TITLE]"; |
| 95 | $p{content} = read_content; |
| 96 | my ($tag, $edit) = Odin::new_pastebin %p; |
| 97 | print "$Odin::PASTEBIN/$tag $edit\n"; |
| 98 | } elsif ($op eq "get") { |
| 99 | @ARGV == 1 or Odin::fail "usage: get TAG"; |
| 100 | my ($tag) = @ARGV; |
| 101 | Odin::get_pastebin Odin::open_db, $tag, my %p; |
| 102 | print encode locale => $p{content}; |
| 103 | } elsif ($op eq "claim") { |
| 104 | @ARGV == 2 or Odin::fail "usage: claim TAG EDITKEY"; |
| 105 | my ($tag, $key) = @ARGV; |
| 106 | Odin::claim_pastebin $tag, $key; |
| 107 | } elsif ($op eq "rekey") { |
| 108 | @ARGV == 1 or Odin::fail "usage: rekey TAG"; |
| 109 | my ($tag) = @ARGV; |
| 110 | my $key = Odin::rekey_pastebin $tag; |
| 111 | print $key, "\n"; |
| 112 | } elsif ($op eq "del") { |
| 113 | @ARGV or Odin::fail "usage: del TAG ..."; |
| 114 | Odin::delete_pastebin map { $_, undef } @ARGV; |
| 115 | } elsif ($op eq "update") { |
| 116 | my $op = Odin::OptParse->new(@ARGV); |
| 117 | my %p = (); |
| 118 | my $contentp = 0; |
| 119 | while (my $o = $op->get) { |
| 120 | if ($o eq "c") { $contentp = 1; } |
| 121 | elsif ($o eq "l") { $p{lang} = $op->arg; } |
| 122 | elsif ($o eq "t") { $p{title} = decode locale => $op->arg; } |
| 123 | else { $op->unk; } |
| 124 | } |
| 125 | @ARGV = $op->rest; |
| 126 | $op->bad if @ARGV != 1; |
| 127 | $op->ok or Odin::fail "usage: update [-c] [-l LANG] [-t TITLE] TAG"; |
| 128 | my $tag = shift @ARGV; |
| 129 | $p{content} = read_content if $contentp; |
| 130 | Odin::update_pastebin $tag, undef, %p or Odin::fail "nothing changed"; |
| 131 | } else { |
| 132 | Odin::fail "unknown operation `$op'"; |
| 133 | } |