mason/common/autohandler: Add an AGPL link to the HTML header.
[odin-cgi] / bin / shorturl.userv
CommitLineData
be24e9af 1#! /usr/bin/perl
128543b0
MW
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/>.
be24e9af
MW
25
26use lib "lib";
27
28use Odin;
29use DBI;
be24e9af
MW
30
31Odin::cmdline_who;
32
33my $op = shift(@ARGV) // "help";
34if ($op eq "help") {
35 print <<EOF;
36Commands available:
37
38 del TAG ...
39 get TAG ...
40 help
41 list
42 new URL
43EOF
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
3300e9a2 48 ("SELECT tag, stamp, url
be24e9af
MW
49 FROM odin_shorturl WHERE owner = ?
50 ORDER BY stamp", undef, $Odin::WHO)}) {
3300e9a2 51 my ($tag, $stamp, $url) = @$r;
cc346ee1 52 Odin::print_columns Odin::fmt_time $stamp => 25, $tag => 12, $url =>0;
be24e9af
MW
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}