mason/common/autohandler: Add an AGPL link to the HTML header.
[odin-cgi] / bin / shorturl.userv
1 #! /usr/bin/perl
2 ###
3 ### URL shortening userv interface for Odin
4 ###
5 ### (c) 2015 Mark Wooding
6 ###
7
8 ###----- Licensing notice ---------------------------------------------------
9 ###
10 ### This file is part of the `odin.gg' service, `odin-cgi'.
11 ###
12 ### `odin-cgi' is free software; you can redistribute it and/or modify
13 ### it under the terms of the GNU Affero General Public License as
14 ### published by the Free Software Foundation; either version 3 of the
15 ### License, or (at your option) any later version.
16 ###
17 ### `odin-cgi' is distributed in the hope that it will be useful,
18 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ### GNU Affero General Public License for more details.
21 ###
22 ### You should have received a copy of the GNU Affero General Public
23 ### License along with `odin-cgi'; if not, see
24 ### <http://www.gnu.org/licenses/>.
25
26 use lib "lib";
27
28 use Odin;
29 use DBI;
30
31 Odin::cmdline_who;
32
33 my $op = shift(@ARGV) // "help";
34 if ($op eq "help") {
35 print <<EOF;
36 Commands available:
37
38 del TAG ...
39 get TAG ...
40 help
41 list
42 new URL
43 EOF
44 } elsif ($op eq "list") {
45 @ARGV == 0 or Odin::fail "usage: list";
46 my $db = Odin::open_db;
47 for my $r (@{$db->selectall_arrayref
48 ("SELECT tag, stamp, url
49 FROM odin_shorturl WHERE owner = ?
50 ORDER BY stamp", undef, $Odin::WHO)}) {
51 my ($tag, $stamp, $url) = @$r;
52 Odin::print_columns Odin::fmt_time $stamp => 25, $tag => 12, $url =>0;
53 }
54 } elsif ($op eq "new") {
55 @ARGV == 1 or Odin::fail "usage: new URL";
56 my ($url) = @ARGV;
57 my $tag = Odin::new_shorturl $url;
58 print "$Odin::SHORTURL/$tag\n";
59 } elsif ($op eq "get") {
60 @ARGV >= 0 or Odin::fail "usage: get TAG ...";
61 if (@ARGV == 1) { print Odin::get_shorturl $ARGV[0], "\n"; }
62 else {
63 for my $tag (@ARGV)
64 { printf "%-12s %s\n", $tag, Odin::get_shorturl $tag; }
65 }
66 } elsif ($op eq "del") {
67 @ARGV >= 0 or Odin::fail "usage: del TAG ...";
68 Odin::delete_shorturl @ARGV;
69 } else {
70 Odin::fail "unknown operation `$op'";
71 }