X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/odin-cgi/blobdiff_plain/f0bcb39a65ff55a0cbe3fced66903c730679150a..cc346ee1adf852ba0f4322bc04a78ec64206d37a:/lib/Odin.pm diff --git a/lib/Odin.pm b/lib/Odin.pm index 28118c8..e373dcc 100644 --- a/lib/Odin.pm +++ b/lib/Odin.pm @@ -5,6 +5,7 @@ package Odin; use DBI; use Digest::SHA qw(sha256_hex); use MIME::Base64; +use POSIX; ###-------------------------------------------------------------------------- ### Early utilities. @@ -33,6 +34,8 @@ our @URLPAT = ( qr{^https?://} ); +our $PASTEMAXLEN = 1024*1024; + our %COOKIE_DEFAULTS = ( -httponly => undef, -max_age => 3600 @@ -50,6 +53,10 @@ our $PASTEBIN = "$BASEURL$PASTEBIN_PATH"; ###-------------------------------------------------------------------------- ### Miscellaneous utilities. +our $NOW; +sub update_now () { $NOW = time; } +update_now; + (our $PROG = $0) =~ s:^.*/::; sub fail_cmdline ($$%) { @@ -81,6 +88,26 @@ sub nice_name ($) { return lc $s; } +sub print_columns (@) { + my @col = reverse @_; + my @fmt = (); + my @val = (); + while (@col && $col[1] eq "") { splice @col, 0, 2; } + my ($wd, $v) = splice @col, 0, 2; + push @fmt, "%s"; push @val, $v; + while (@col) { + my ($wd, $v) = splice @col, 0, 2; + push @fmt, "%-${wd}s"; + push @val, $v; + } + printf join(" ", reverse @fmt) . "\n", reverse @val; +} + +sub fmt_time ($) { + my ($t) = @_; + return $t == -1 ? "--" : strftime "%Y-%m-%d %H:%M:%S %z", localtime $t; +} + ###-------------------------------------------------------------------------- ### Database utilities. @@ -95,12 +122,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 +152,6 @@ sub xact (&$) { die $exc; } -sub sql_timestamp ($$) { - my ($db, $col) = @_; - return $db->{private_odin_unixstamp}->($col); -} ###-------------------------------------------------------------------------- ### Sequence numbers and tagging. @@ -263,8 +284,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; @@ -327,9 +349,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,6 +450,8 @@ 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; }