Commit | Line | Data |
---|---|---|
be24e9af MW |
1 | #! /usr/bin/perl |
2 | ||
3 | use lib "lib"; | |
4 | ||
5 | use Odin; | |
6 | use DBI; | |
7 | use Encode; | |
8 | use Encode::Locale; | |
be24e9af MW |
9 | |
10 | my $BAD = 0; | |
11 | ||
12 | sub bad ($) { | |
13 | my ($m) = @_; | |
14 | $BAD = 1; | |
15 | print STDERR "$Odin::PROG: $m\n"; | |
16 | } | |
17 | ||
18 | Odin::cmdline_who; | |
19 | ||
20 | sub read_content () { | |
21 | my $c = ""; | |
22 | while (read STDIN, my $buf, 8192) { $c .= $buf; } | |
23 | return Odin::tidy_pastebin_content decode locale => $c; | |
24 | } | |
25 | ||
26 | my $op = shift(@ARGV) // "help"; | |
27 | if ($op eq "help") { | |
28 | print <<EOF; | |
29 | Commands available: | |
30 | ||
31 | claim TAG EDITKEY | |
32 | del TAG ... | |
33 | get TAG | |
34 | help | |
35 | list | |
36 | new [-l LANG] [-t TITLE] | |
37 | rekey TAG | |
38 | update [-c] [-l LANG] [-t TITLE] TAG | |
39 | EOF | |
40 | } elsif ($op eq "list") { | |
41 | @ARGV == 0 or Odin::fail "usage: list"; | |
42 | my $db = Odin::open_db; | |
43 | for my $r (@{$db->selectall_arrayref | |
3300e9a2 | 44 | ("SELECT tag, stamp, lang, title |
be24e9af MW |
45 | FROM odin_pastebin WHERE owner = ? |
46 | ORDER BY stamp", undef, $Odin::WHO)}) { | |
3300e9a2 | 47 | my ($tag, $stamp, $lang, $title) = @$r; |
cc346ee1 MW |
48 | Odin::print_columns Odin::fmt_time $stamp => 25, |
49 | $tag => 12, $lang => 16, (encode locale => $title) => 0; | |
be24e9af MW |
50 | } |
51 | } elsif ($op eq "new") { | |
f0bcb39a MW |
52 | my $op = Odin::OptParse->new(@ARGV); |
53 | my $p = (title => undef, lang => "txt"); | |
54 | while (my $o = $op->get) { | |
55 | if ($o eq "l") { $p{lang} = $op->arg; } | |
56 | elsif ($o eq "t") { $p{title} = decode locale => $op->arg; } | |
57 | else { $op->unk; } | |
58 | } | |
59 | @ARGV = $op->rest; | |
60 | $op->bad if @ARGV; | |
61 | $op->ok or Odin::fail "usage: new [-l LANG] [-t TITLE]"; | |
62 | $p{content} = read_content; | |
be24e9af | 63 | my ($tag, $edit) = Odin::new_pastebin %p; |
93ec1b86 | 64 | print "$Odin::PASTEBIN/$tag $edit\n"; |
be24e9af MW |
65 | } elsif ($op eq "get") { |
66 | @ARGV == 1 or Odin::fail "usage: get TAG"; | |
67 | my ($tag) = @ARGV; | |
68 | Odin::get_pastebin Odin::open_db, $tag, my %p; | |
69 | print encode locale => $p{content}; | |
70 | } elsif ($op eq "claim") { | |
71 | @ARGV == 2 or Odin::fail "usage: claim TAG EDITKEY"; | |
72 | my ($tag, $key) = @ARGV; | |
73 | Odin::claim_pastebin $tag, $key; | |
74 | } elsif ($op eq "rekey") { | |
75 | @ARGV == 1 or Odin::fail "usage: rekey TAG"; | |
76 | my ($tag) = @ARGV; | |
77 | my $key = Odin::rekey_pastebin $tag; | |
78 | print $key, "\n"; | |
79 | } elsif ($op eq "del") { | |
80 | @ARGV or Odin::fail "usage: del TAG ..."; | |
81 | Odin::delete_pastebin map { $_, undef } @ARGV; | |
82 | } elsif ($op eq "update") { | |
f0bcb39a MW |
83 | my $op = Odin::OptParse->new(@ARGV); |
84 | my %p = (); | |
85 | my $contentp = 0; | |
86 | while (my $o = $op->get) { | |
87 | if ($o eq "c") { $contentp = 1; } | |
88 | elsif ($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; | |
98984c9d MW |
93 | $op->bad if @ARGV != 1; |
94 | $op->ok or Odin::fail "usage: update [-c] [-l LANG] [-t TITLE] TAG"; | |
95 | my $tag = shift @ARGV; | |
f0bcb39a | 96 | $p{content} = read_content if $contentp; |
be24e9af MW |
97 | Odin::update_pastebin $tag, undef, %p or Odin::fail "nothing changed"; |
98 | } else { | |
99 | Odin::fail "unknown operation `$op'"; | |
100 | } |