bin/pastebin.userv: New `langs' subcommand prints language names.
[odin-cgi] / bin / pastebin.userv
CommitLineData
be24e9af
MW
1#! /usr/bin/perl
2
3use lib "lib";
4
5use Odin;
6use DBI;
7use Encode;
8use Encode::Locale;
be24e9af
MW
9
10my $BAD = 0;
11
12sub bad ($) {
13 my ($m) = @_;
14 $BAD = 1;
15 print STDERR "$Odin::PROG: $m\n";
16}
17
18Odin::cmdline_who;
19
20sub 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
26my $op = shift(@ARGV) // "help";
27if ($op eq "help") {
28 print <<EOF;
29Commands available:
30
31 claim TAG EDITKEY
32 del TAG ...
33 get TAG
34 help
c24aba68 35 langs
be24e9af
MW
36 list
37 new [-l LANG] [-t TITLE]
38 rekey TAG
39 update [-c] [-l LANG] [-t TITLE] TAG
40EOF
c24aba68
MW
41} elsif ($op eq "langs") {
42 @ARGV == 0 or Odin::fail "usage: list";
43 my $db = Odin::open_db;
44 for my $r (@{$db->selectall_arrayref
45 ("SELECT lang, descr FROM odin_pastebin_lang
46 ORDER BY lang", undef)}) {
47 my ($lang, $descr) = @$r;
48 Odin::print_columns $lang => 16, $descr => 0;
49 }
be24e9af
MW
50} elsif ($op eq "list") {
51 @ARGV == 0 or Odin::fail "usage: list";
52 my $db = Odin::open_db;
53 for my $r (@{$db->selectall_arrayref
3300e9a2 54 ("SELECT tag, stamp, lang, title
be24e9af
MW
55 FROM odin_pastebin WHERE owner = ?
56 ORDER BY stamp", undef, $Odin::WHO)}) {
3300e9a2 57 my ($tag, $stamp, $lang, $title) = @$r;
cc346ee1
MW
58 Odin::print_columns Odin::fmt_time $stamp => 25,
59 $tag => 12, $lang => 16, (encode locale => $title) => 0;
be24e9af
MW
60 }
61} elsif ($op eq "new") {
f0bcb39a
MW
62 my $op = Odin::OptParse->new(@ARGV);
63 my $p = (title => undef, lang => "txt");
64 while (my $o = $op->get) {
65 if ($o eq "l") { $p{lang} = $op->arg; }
66 elsif ($o eq "t") { $p{title} = decode locale => $op->arg; }
67 else { $op->unk; }
68 }
69 @ARGV = $op->rest;
70 $op->bad if @ARGV;
71 $op->ok or Odin::fail "usage: new [-l LANG] [-t TITLE]";
72 $p{content} = read_content;
be24e9af 73 my ($tag, $edit) = Odin::new_pastebin %p;
93ec1b86 74 print "$Odin::PASTEBIN/$tag $edit\n";
be24e9af
MW
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") {
f0bcb39a
MW
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;
98984c9d
MW
103 $op->bad if @ARGV != 1;
104 $op->ok or Odin::fail "usage: update [-c] [-l LANG] [-t TITLE] TAG";
105 my $tag = shift @ARGV;
f0bcb39a 106 $p{content} = read_content if $contentp;
be24e9af
MW
107 Odin::update_pastebin $tag, undef, %p or Odin::fail "nothing changed";
108} else {
109 Odin::fail "unknown operation `$op'";
110}