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; | |
9 | use Getopt::Std; | |
10 | use POSIX; | |
11 | ||
12 | my $BAD = 0; | |
13 | ||
14 | sub bad ($) { | |
15 | my ($m) = @_; | |
16 | $BAD = 1; | |
17 | print STDERR "$Odin::PROG: $m\n"; | |
18 | } | |
19 | ||
20 | Odin::cmdline_who; | |
21 | ||
22 | sub read_content () { | |
23 | my $c = ""; | |
24 | while (read STDIN, my $buf, 8192) { $c .= $buf; } | |
25 | return Odin::tidy_pastebin_content decode locale => $c; | |
26 | } | |
27 | ||
28 | my $op = shift(@ARGV) // "help"; | |
29 | if ($op eq "help") { | |
30 | print <<EOF; | |
31 | Commands available: | |
32 | ||
33 | claim TAG EDITKEY | |
34 | del TAG ... | |
35 | get TAG | |
36 | help | |
37 | list | |
38 | new [-l LANG] [-t TITLE] | |
39 | rekey TAG | |
40 | update [-c] [-l LANG] [-t TITLE] TAG | |
41 | EOF | |
42 | } elsif ($op eq "list") { | |
43 | @ARGV == 0 or Odin::fail "usage: list"; | |
44 | my $db = Odin::open_db; | |
45 | for my $r (@{$db->selectall_arrayref | |
46 | ("SELECT " . Odin::sql_timestamp($db, "stamp") . | |
47 | ", tag, lang, title | |
48 | FROM odin_pastebin WHERE owner = ? | |
49 | ORDER BY stamp", undef, $Odin::WHO)}) { | |
50 | my ($stamp, $tag, $lang, $title) = @$r; | |
51 | my $t = strftime "%Y-%m-%d %H:%M:%S %z", localtime $stamp; | |
52 | printf "%-25s %-12s %-16s %s\n", | |
53 | $t, $tag, $lang, encode locale => $title; | |
54 | } | |
55 | } elsif ($op eq "new") { | |
56 | my %o; | |
57 | getopts "l:t:", \%o and @ARGV == 0 | |
58 | or Odin::fail "usage: new [-l LANG] [-t TITLE]"; | |
97a33b9c | 59 | my %p = (title => decode(locale => $o{t}), lang => $o{l} // "txt", |
be24e9af MW |
60 | content => read_content); |
61 | my $db = Odin::open_db; | |
62 | my $c = ""; | |
63 | while (read STDIN, my $buf, 8192) { $c .= $buf; } | |
64 | $p{content} = read_content; | |
65 | @{$db->selectall_arrayref | |
66 | ("SELECT lang FROM odin_pastebin_lang WHERE lang = ?", undef, $p{lang})} | |
67 | or Odin::fail "unknown language `$p{lang}'"; | |
68 | my ($tag, $edit) = Odin::new_pastebin %p; | |
69 | print "$Odin::PASTEBIN/$url $edit\n"; | |
70 | } elsif ($op eq "get") { | |
71 | @ARGV == 1 or Odin::fail "usage: get TAG"; | |
72 | my ($tag) = @ARGV; | |
73 | Odin::get_pastebin Odin::open_db, $tag, my %p; | |
74 | print encode locale => $p{content}; | |
75 | } elsif ($op eq "claim") { | |
76 | @ARGV == 2 or Odin::fail "usage: claim TAG EDITKEY"; | |
77 | my ($tag, $key) = @ARGV; | |
78 | Odin::claim_pastebin $tag, $key; | |
79 | } elsif ($op eq "rekey") { | |
80 | @ARGV == 1 or Odin::fail "usage: rekey TAG"; | |
81 | my ($tag) = @ARGV; | |
82 | my $key = Odin::rekey_pastebin $tag; | |
83 | print $key, "\n"; | |
84 | } elsif ($op eq "del") { | |
85 | @ARGV or Odin::fail "usage: del TAG ..."; | |
86 | Odin::delete_pastebin map { $_, undef } @ARGV; | |
87 | } elsif ($op eq "update") { | |
88 | my %o; | |
89 | getopts "cl:t:", \%o and @ARGV == 1 | |
90 | or Odin::fail "usage: update [-c] [-l LANG] [-t TITLE] TAG"; | |
91 | my ($tag) = @ARGV; | |
92 | my %p = (title => decode(locale => $o{t}), lang => $o{l}); | |
93 | if ($o{c}) { $p{content} = read_content; } | |
94 | Odin::update_pastebin $tag, undef, %p or Odin::fail "nothing changed"; | |
95 | } else { | |
96 | Odin::fail "unknown operation `$op'"; | |
97 | } |