chiark / gitweb /
lib/Odin.pm, bin/*.userv: New function for printing tabular reports.
[odin-cgi] / lib / Odin.pm
index 551c51f0e83cc7248baa176513a8df201c1af71b..e373dcc8d4c3a631db60ff8ef64e67f89dd80e22 100644 (file)
@@ -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
@@ -85,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.
 
@@ -427,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;
 }