X-Git-Url: https://git.distorted.org.uk/~mdw/odin-cgi/blobdiff_plain/97a33b9cd6fbde6d8eac854354c6b1326fad2f8e..f6bbb0334e68f346b3cf3273eeabbb7a0bdfa641:/bin/pastebin.userv diff --git a/bin/pastebin.userv b/bin/pastebin.userv index 4d5d4c0..c9936ed 100755 --- a/bin/pastebin.userv +++ b/bin/pastebin.userv @@ -6,8 +6,6 @@ use Odin; use DBI; use Encode; use Encode::Locale; -use Getopt::Std; -use POSIX; my $BAD = 0; @@ -34,39 +32,46 @@ Commands available: del TAG ... get TAG help + langs list new [-l LANG] [-t TITLE] rekey TAG update [-c] [-l LANG] [-t TITLE] TAG EOF +} elsif ($op eq "langs") { + @ARGV == 0 or Odin::fail "usage: list"; + my $db = Odin::open_db; + for my $r (@{$db->selectall_arrayref + ("SELECT lang, descr FROM odin_pastebin_lang + ORDER BY lang", undef)}) { + my ($lang, $descr) = @$r; + Odin::print_columns $lang => 16, $descr => 0; + } } elsif ($op eq "list") { @ARGV == 0 or Odin::fail "usage: list"; my $db = Odin::open_db; for my $r (@{$db->selectall_arrayref - ("SELECT " . Odin::sql_timestamp($db, "stamp") . - ", tag, lang, title + ("SELECT tag, stamp, lang, title FROM odin_pastebin WHERE owner = ? ORDER BY stamp", undef, $Odin::WHO)}) { - my ($stamp, $tag, $lang, $title) = @$r; - my $t = strftime "%Y-%m-%d %H:%M:%S %z", localtime $stamp; - printf "%-25s %-12s %-16s %s\n", - $t, $tag, $lang, encode locale => $title; + my ($tag, $stamp, $lang, $title) = @$r; + Odin::print_columns Odin::fmt_time $stamp => 25, + $tag => 12, $lang => 16, (encode locale => $title) => 0; } } elsif ($op eq "new") { - my %o; - getopts "l:t:", \%o and @ARGV == 0 - or Odin::fail "usage: new [-l LANG] [-t TITLE]"; - my %p = (title => decode(locale => $o{t}), lang => $o{l} // "txt", - content => read_content); - my $db = Odin::open_db; - my $c = ""; - while (read STDIN, my $buf, 8192) { $c .= $buf; } + my $op = Odin::OptParse->new(@ARGV); + my $p = (title => undef, lang => "txt"); + while (my $o = $op->get) { + if ($o eq "l") { $p{lang} = $op->arg; } + elsif ($o eq "t") { $p{title} = decode locale => $op->arg; } + else { $op->unk; } + } + @ARGV = $op->rest; + $op->bad if @ARGV; + $op->ok or Odin::fail "usage: new [-l LANG] [-t TITLE]"; $p{content} = read_content; - @{$db->selectall_arrayref - ("SELECT lang FROM odin_pastebin_lang WHERE lang = ?", undef, $p{lang})} - or Odin::fail "unknown language `$p{lang}'"; my ($tag, $edit) = Odin::new_pastebin %p; - print "$Odin::PASTEBIN/$url $edit\n"; + print "$Odin::PASTEBIN/$tag $edit\n"; } elsif ($op eq "get") { @ARGV == 1 or Odin::fail "usage: get TAG"; my ($tag) = @ARGV; @@ -85,12 +90,20 @@ EOF @ARGV or Odin::fail "usage: del TAG ..."; Odin::delete_pastebin map { $_, undef } @ARGV; } elsif ($op eq "update") { - my %o; - getopts "cl:t:", \%o and @ARGV == 1 - or Odin::fail "usage: update [-c] [-l LANG] [-t TITLE] TAG"; - my ($tag) = @ARGV; - my %p = (title => decode(locale => $o{t}), lang => $o{l}); - if ($o{c}) { $p{content} = read_content; } + my $op = Odin::OptParse->new(@ARGV); + my %p = (); + my $contentp = 0; + while (my $o = $op->get) { + if ($o eq "c") { $contentp = 1; } + elsif ($o eq "l") { $p{lang} = $op->arg; } + elsif ($o eq "t") { $p{title} = decode locale => $op->arg; } + else { $op->unk; } + } + @ARGV = $op->rest; + $op->bad if @ARGV != 1; + $op->ok or Odin::fail "usage: update [-c] [-l LANG] [-t TITLE] TAG"; + my $tag = shift @ARGV; + $p{content} = read_content if $contentp; Odin::update_pastebin $tag, undef, %p or Odin::fail "nothing changed"; } else { Odin::fail "unknown operation `$op'";