chiark / gitweb /
Store Unix timestamps in the database, rather than SQL ones.
[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 tag, stamp, lang, title
46                   FROM odin_pastebin WHERE owner = ?
47                   ORDER BY stamp", undef, $Odin::WHO)}) {
48     my ($tag, $stamp, $lang, $title) = @$r;
49     my $t = strftime "%Y-%m-%d %H:%M:%S %z", localtime $stamp;
50     printf "%-25s  %-12s  %-16s  %s\n",
51       $t, $tag, $lang, encode locale => $title;
52   }
53 } elsif ($op eq "new") {
54   my $op = Odin::OptParse->new(@ARGV);
55   my $p = (title => undef, lang => "txt");
56   while (my $o = $op->get) {
57     if ($o eq "l") { $p{lang} = $op->arg; }
58     elsif ($o eq "t") { $p{title} = decode locale => $op->arg; }
59     else { $op->unk; }
60   }
61   @ARGV = $op->rest;
62   $op->bad if @ARGV;
63   $op->ok or Odin::fail "usage: new [-l LANG] [-t TITLE]";
64   $p{content} = read_content;
65   my $db = Odin::open_db;
66   my $c = "";
67   while (read STDIN, my $buf, 8192) { $c .= $buf; }
68   $p{content} = read_content;
69   @{$db->selectall_arrayref
70     ("SELECT lang FROM odin_pastebin_lang WHERE lang = ?", undef, $p{lang})}
71     or Odin::fail "unknown language `$p{lang}'";
72   my ($tag, $edit) = Odin::new_pastebin %p;
73   print "$Odin::PASTEBIN/$url $edit\n";
74 } elsif ($op eq "get") {
75   @ARGV == 1 or Odin::fail "usage: get TAG";
76   my ($tag) = @ARGV;
77   Odin::get_pastebin Odin::open_db, $tag, my %p;
78   print encode locale => $p{content};
79 } elsif ($op eq "claim") {
80   @ARGV == 2 or Odin::fail "usage: claim TAG EDITKEY";
81   my ($tag, $key) = @ARGV;
82   Odin::claim_pastebin $tag, $key;
83 } elsif ($op eq "rekey") {
84   @ARGV == 1 or Odin::fail "usage: rekey TAG";
85   my ($tag) = @ARGV;
86   my $key = Odin::rekey_pastebin $tag;
87   print $key, "\n";
88 } elsif ($op eq "del") {
89   @ARGV or Odin::fail "usage: del TAG ...";
90   Odin::delete_pastebin map { $_, undef } @ARGV;
91 } elsif ($op eq "update") {
92   my $op = Odin::OptParse->new(@ARGV);
93   my %p = ();
94   my $contentp = 0;
95   while (my $o = $op->get) {
96     if ($o eq "c") { $contentp = 1; }
97     elsif ($o eq "l") { $p{lang} = $op->arg; }
98     elsif ($o eq "t") { $p{title} = decode locale => $op->arg; }
99     else { $op->unk; }
100   }
101   @ARGV = $op->rest;
102   $op->bad if @ARGV;
103   $op->ok or Odin::fail "usage: new [-l LANG] [-t TITLE]";
104   $p{content} = read_content if $contentp;
105   Odin::update_pastebin $tag, undef, %p or Odin::fail "nothing changed";
106 } else {
107   Odin::fail "unknown operation `$op'";
108 }