X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/odin-cgi/blobdiff_plain/be24e9afafe0d740652d962fd5531de901a9cd09..fc8074b35d1b16d35726247fcc0dc86d59b83a3a:/lib/Odin.pm diff --git a/lib/Odin.pm b/lib/Odin.pm index e7786f5..26a2f80 100644 --- a/lib/Odin.pm +++ b/lib/Odin.pm @@ -33,6 +33,8 @@ our @URLPAT = ( qr{^https?://} ); +our $PASTEMAXLEN = 1024*1024; + our %COOKIE_DEFAULTS = ( -httponly => undef, -max_age => 3600 @@ -50,6 +52,10 @@ our $PASTEBIN = "$BASEURL$PASTEBIN_PATH"; ###-------------------------------------------------------------------------- ### Miscellaneous utilities. +our $NOW; +sub update_now () { $NOW = time; } +update_now; + (our $PROG = $0) =~ s:^.*/::; sub fail_cmdline ($$%) { @@ -95,12 +101,10 @@ sub open_db (@) { my $drv = $db->{Driver}{Name}; if ($drv eq "Pg") { $db->{private_odin_retry_p} = sub { $db->state =~ /^40[0P]01$/ }; - $db->{private_odin_unixstamp} = sub { "extract(epoch from $_[0])" }; } elsif ($drv eq "SQLite") { $db->{private_odin_retry_p} = sub { $db->err == 5 }; - $db->{private_odin_unixstamp} = sub { "strftime('%s', $_[0])" }; } else { - fail "unsupported database driver `$drv' (patches welcome)", undef; + $db->{private_odin_retry_p} = sub { 0 }; } return $db; @@ -127,10 +131,6 @@ sub xact (&$) { die $exc; } -sub sql_timestamp ($$) { - my ($db, $col) = @_; - return $db->{private_odin_unixstamp}->($col); -} ###-------------------------------------------------------------------------- ### Sequence numbers and tagging. @@ -263,8 +263,9 @@ sub new_shorturl ($) { undef, $WHOCMP, $url); unless (defined $tag) { $tag = encode_tag(next_seq($db, "odin_shorturl_seq")); - $db->do("INSERT INTO odin_shorturl (tag, owner, url) VALUES (?, ?, ?)", - undef, $tag, $WHO, $url); + $db->do("INSERT INTO odin_shorturl (tag, stamp, owner, url) + VALUES (?, ?, ?, ?)", undef, + $tag, $NOW, $WHO, $url); } } $db; return $tag; @@ -309,7 +310,7 @@ sub delete_shorturl (@) { our %PASTEBIN_DEFAULTS = ( title => "(untitled)", - lang => "plain-text", + lang => "txt", content => "" ); our @PASTEBIN_PROPS = keys %PASTEBIN_DEFAULTS; @@ -327,9 +328,9 @@ sub new_pastebin (\%) { xact { $tag = encode_tag next_seq $db, "odin_pastebin_seq"; $db->do("INSERT INTO odin_pastebin - (tag, edithash, owner, $PASTEBIN_PROPCOLS) - VALUES (?, ?, ?, $PASTEBIN_PROPPLACES)", undef, - $tag, $hash, $WHO, @{$new}{@PASTEBIN_PROPS}); + (tag, stamp, edithash, owner, $PASTEBIN_PROPCOLS) + VALUES (?, ?, ?, ?, $PASTEBIN_PROPPLACES)", undef, + $tag, $NOW, $hash, $WHO, @{$new}{@PASTEBIN_PROPS}); } $db; return $tag, $editkey; } @@ -428,9 +429,55 @@ sub tidy_pastebin_content ($) { return undef unless defined $content; $content =~ tr/\r//d; $content =~ s/([^\n])\z/$1\n/; + length $content <= $PASTEMAXLEN or + fail "invalid paste content", ".badpaste"; return $content; } +###-------------------------------------------------------------------------- +### Simple option parser. + +package Odin::OptParse; + +sub new { + my ($cls, @args) = @_; + return bless { + cur => "", + args => \@args, + opt => undef, + ok => 1 + }, $cls; +} + +sub get { + my ($me) = @_; + if (!length $me->{cur}) { + my $args = $me->{args}; + if (!@$args) { return undef; } + elsif ($args->[0] =~ /^[^-]|^-$/) { return undef; } + elsif ($args->[0] eq "--") { shift @$args; return undef; } + $me->{cur} = substr shift @$args, 1; + } + my $o = $me->{opt} = substr $me->{cur}, 0, 1; + $me->{cur} = substr $me->{cur}, 1; + return $o; +} + +sub arg { + my ($me) = @_; + my $a; + if (length $me->{cur}) { $a = $me->{cur}; $me->{cur} = ""; } + elsif (@{$me->{args}}) { $a = shift @{$me->{args}}; } + else { $a = undef; $me->err("option `-$me->{opt}' requires an argument"); } + return $a; +} + +sub rest { return @{$_[0]->{args}}; } +sub ok { return $_[0]->{ok}; } +sub bad { $_[0]->{ok} = 0; } +sub err { $_[0]->bad; print STDERR "$PROG: $_[1]\n"; } +sub unk { $_[0]->err("unknown option `-$_[0]->{opt}'"); } + ###----- That's all, folks -------------------------------------------------- 1;