3 ### Pastebin userv interface for Odin
5 ### (c) 2015 Mark Wooding
8 ###----- Licensing notice ---------------------------------------------------
10 ### This file is part of the `odin.gg' service, `odin-cgi'.
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.
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.
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/>.
38 print STDERR "$Odin::PROG: $m\n";
45 while (read STDIN, my $buf, 8192) { $c .= $buf; }
46 return Odin::tidy_pastebin_content decode locale => $c;
49 my $op = shift(@ARGV) // "help";
60 new [-l LANG] [-t TITLE]
62 update [-c] [-l LANG] [-t TITLE] TAG
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;
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;
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; }
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";
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";
110 my $key = Odin::rekey_pastebin $tag;
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);
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; }
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";
132 Odin::fail "unknown operation `$op'";