#! /usr/bin/perl
###
### Pastebin userv interface for Odin
###
### (c) 2015 Mark Wooding
###
###----- Licensing notice ---------------------------------------------------
###
### This file is part of the `odin.gg' service, `odin-cgi'.
###
### `odin-cgi' is free software; you can redistribute it and/or modify
### it under the terms of the GNU Affero General Public License as
### published by the Free Software Foundation; either version 3 of the
### License, or (at your option) any later version.
###
### `odin-cgi' is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
### GNU Affero General Public License for more details.
###
### You should have received a copy of the GNU Affero General Public
### License along with `odin-cgi'; if not, see
### .
use lib "lib";
use Odin;
use DBI;
use Encode;
use Encode::Locale;
my $BAD = 0;
sub bad ($) {
my ($m) = @_;
$BAD = 1;
print STDERR "$Odin::PROG: $m\n";
}
Odin::cmdline_who;
sub read_content () {
my $c = "";
while (read STDIN, my $buf, 8192) { $c .= $buf; }
return Odin::tidy_pastebin_content decode locale => $c;
}
my $op = shift(@ARGV) // "help";
if ($op eq "help") {
print <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 tag, stamp, lang, title
FROM odin_pastebin WHERE owner = ?
ORDER BY stamp", undef, $Odin::WHO)}) {
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 $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;
my ($tag, $edit) = Odin::new_pastebin %p;
print "$Odin::PASTEBIN/$tag $edit\n";
} elsif ($op eq "get") {
@ARGV == 1 or Odin::fail "usage: get TAG";
my ($tag) = @ARGV;
Odin::get_pastebin Odin::open_db, $tag, my %p;
print encode locale => $p{content};
} elsif ($op eq "claim") {
@ARGV == 2 or Odin::fail "usage: claim TAG EDITKEY";
my ($tag, $key) = @ARGV;
Odin::claim_pastebin $tag, $key;
} elsif ($op eq "rekey") {
@ARGV == 1 or Odin::fail "usage: rekey TAG";
my ($tag) = @ARGV;
my $key = Odin::rekey_pastebin $tag;
print $key, "\n";
} elsif ($op eq "del") {
@ARGV or Odin::fail "usage: del TAG ...";
Odin::delete_pastebin map { $_, undef } @ARGV;
} elsif ($op eq "update") {
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'";
}