lib/Odin.pm, bin/pastebin.userv: Use our own simple option parser.
[odin-cgi] / bin / pastebin.userv
1 #! /usr/bin/perl
2
3 use lib "lib";
4
5 use Odin;
6 use DBI;
7 use Encode;
8 use Encode::Locale;
9 use POSIX;
10
11 my $BAD = 0;
12
13 sub bad ($) {
14 my ($m) = @_;
15 $BAD = 1;
16 print STDERR "$Odin::PROG: $m\n";
17 }
18
19 Odin::cmdline_who;
20
21 sub read_content () {
22 my $c = "";
23 while (read STDIN, my $buf, 8192) { $c .= $buf; }
24 return Odin::tidy_pastebin_content decode locale => $c;
25 }
26
27 my $op = shift(@ARGV) // "help";
28 if ($op eq "help") {
29 print <<EOF;
30 Commands available:
31
32 claim TAG EDITKEY
33 del TAG ...
34 get TAG
35 help
36 list
37 new [-l LANG] [-t TITLE]
38 rekey TAG
39 update [-c] [-l LANG] [-t TITLE] TAG
40 EOF
41 } elsif ($op eq "list") {
42 @ARGV == 0 or Odin::fail "usage: list";
43 my $db = Odin::open_db;
44 for my $r (@{$db->selectall_arrayref
45 ("SELECT " . Odin::sql_timestamp($db, "stamp") .
46 ", tag, lang, title
47 FROM odin_pastebin WHERE owner = ?
48 ORDER BY stamp", undef, $Odin::WHO)}) {
49 my ($stamp, $tag, $lang, $title) = @$r;
50 my $t = strftime "%Y-%m-%d %H:%M:%S %z", localtime $stamp;
51 printf "%-25s %-12s %-16s %s\n",
52 $t, $tag, $lang, encode locale => $title;
53 }
54 } elsif ($op eq "new") {
55 my $op = Odin::OptParse->new(@ARGV);
56 my $p = (title => undef, lang => "txt");
57 while (my $o = $op->get) {
58 if ($o eq "l") { $p{lang} = $op->arg; }
59 elsif ($o eq "t") { $p{title} = decode locale => $op->arg; }
60 else { $op->unk; }
61 }
62 @ARGV = $op->rest;
63 $op->bad if @ARGV;
64 $op->ok or Odin::fail "usage: new [-l LANG] [-t TITLE]";
65 $p{content} = read_content;
66 my $db = Odin::open_db;
67 my $c = "";
68 while (read STDIN, my $buf, 8192) { $c .= $buf; }
69 $p{content} = read_content;
70 @{$db->selectall_arrayref
71 ("SELECT lang FROM odin_pastebin_lang WHERE lang = ?", undef, $p{lang})}
72 or Odin::fail "unknown language `$p{lang}'";
73 my ($tag, $edit) = Odin::new_pastebin %p;
74 print "$Odin::PASTEBIN/$url $edit\n";
75 } elsif ($op eq "get") {
76 @ARGV == 1 or Odin::fail "usage: get TAG";
77 my ($tag) = @ARGV;
78 Odin::get_pastebin Odin::open_db, $tag, my %p;
79 print encode locale => $p{content};
80 } elsif ($op eq "claim") {
81 @ARGV == 2 or Odin::fail "usage: claim TAG EDITKEY";
82 my ($tag, $key) = @ARGV;
83 Odin::claim_pastebin $tag, $key;
84 } elsif ($op eq "rekey") {
85 @ARGV == 1 or Odin::fail "usage: rekey TAG";
86 my ($tag) = @ARGV;
87 my $key = Odin::rekey_pastebin $tag;
88 print $key, "\n";
89 } elsif ($op eq "del") {
90 @ARGV or Odin::fail "usage: del TAG ...";
91 Odin::delete_pastebin map { $_, undef } @ARGV;
92 } elsif ($op eq "update") {
93 my $op = Odin::OptParse->new(@ARGV);
94 my %p = ();
95 my $contentp = 0;
96 while (my $o = $op->get) {
97 if ($o eq "c") { $contentp = 1; }
98 elsif ($o eq "l") { $p{lang} = $op->arg; }
99 elsif ($o eq "t") { $p{title} = decode locale => $op->arg; }
100 else { $op->unk; }
101 }
102 @ARGV = $op->rest;
103 $op->bad if @ARGV;
104 $op->ok or Odin::fail "usage: new [-l LANG] [-t TITLE]";
105 $p{content} = read_content if $contentp;
106 Odin::update_pastebin $tag, undef, %p or Odin::fail "nothing changed";
107 } else {
108 Odin::fail "unknown operation `$op'";
109 }