6 use Digest
::SHA
qw(sha256_hex
);
9 ###--------------------------------------------------------------------------
12 sub merge_hash
(\
%%) {
13 my ($hashref, %defaults) = @_;
14 for my $k (keys %defaults)
15 { $hashref->{$k} = $defaults{$k} unless exists $hashref->{$k}; }
18 ###--------------------------------------------------------------------------
21 our $DSN = "dbi:Pg(pg_enable_utf8=>1):host=db";
23 our @BACKOFF = (0.1, 10, 1.5, 0.5, 2.0);
25 our $BASEURL = "http://odin.gg/";
26 our $STATIC = "http://odin.gg/";
28 our $SHORTURL_PATH = "u";
29 our $PASTEBIN_PATH = "p";
31 our $URLMAXLEN = 1024;
36 our $PASTEMAXLEN = 1024*1024;
38 our %COOKIE_DEFAULTS = (
45 our ($SCHEME, $DOMAIN, $BASEPATH) = $BASEURL =~ m!^([^:]+)://([^/]+)(/.*)$!;
46 merge_hash
%COOKIE_DEFAULTS, -domain
=> $DOMAIN, -path
=> $BASEPATH;
47 merge_hash
%COOKIE_DEFAULTS, -secure
=> undef if $SCHEME eq "https";
49 our $SHORTURL = "$BASEURL$SHORTURL_PATH";
50 our $PASTEBIN = "$BASEURL$PASTEBIN_PATH";
52 ###--------------------------------------------------------------------------
53 ### Miscellaneous utilities.
56 sub update_now
() { $NOW = time; }
59 (our $PROG = $0) =~ s
:^.*/::;
61 sub fail_cmdline
($$%) {
62 my ($msg, $label, %args) = @_;
63 print STDERR
"$PROG: $msg\n";
67 our $FAILPROC = \
&fail_cmdline
;
70 my ($msg, $label, %args) = @_;
71 $FAILPROC->($msg, $label, %args);
74 sub set_mason_failproc
($) {
77 my ($msg, $label, %args) = @_;
79 $m->comp($label, %args);
90 ###--------------------------------------------------------------------------
91 ### Database utilities.
95 my $db = DBI
->connect_cached($DSN, undef, undef, {
101 my $drv = $db->{Driver
}{Name
};
103 $db->{private_odin_retry_p
} = sub { $db->state =~ /^40[0P]01$/ };
104 } elsif ($drv eq "SQLite") {
105 $db->{private_odin_retry_p
} = sub { $db->err == 5 };
107 $db->{private_odin_retry_p
} = sub { 0 };
114 my ($body, $db) = @_;
118 my ($sleep, $maxsleep, $mult, $minvar, $maxvar) = @BACKOFF;
119 for (my $i = 0; $i < $RETRY; $i++) {
121 eval { @rv = $body->(); $db->commit; };
123 return @rv unless $exc;
124 my $retryp = $db->{private_odin_retry_p
}();
125 eval { $db->rollback; };
126 die $exc unless $retryp;
127 my $t = $sleep * ($minvar + rand($maxvar - $minvar));
128 $sleep *= $mult; $sleep = $max if $sleep > $max;
129 select undef, undef, undef, $t;
135 ###--------------------------------------------------------------------------
136 ### Sequence numbers and tagging.
139 my ($db, $table) = @_;
140 my ($seq) = $db->selectrow_array("SELECT seq FROM $table");
141 die "no sequence number in $table" unless defined $seq;
142 $db->do("UPDATE $table SET seq = ?", undef, $seq + 1);
147 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
148 my $NALPHA = length $ALPHABET;
154 $tag .= substr($ALPHABET, $seq % $NALPHA, 1);
155 $seq = int $seq/$NALPHA;
160 ###--------------------------------------------------------------------------
164 sub fetch_cookies
($) {
168 my $cookies = $r->header_in("Cookie");
169 if (defined $cookies) {
170 for my $kv (split /;/, $cookies) {
171 my ($k, $v) = split /=/, $kv, 2;
172 $k =~ s/^\s*(|\S|\S.*\S)\s*$/$1/;
173 $v =~ s/^\s*(|\S|\S.*\S)\s*$/$1/;
175 $v =~ s/\%([0-9a-f][0-9a-f])/chr hex $1/eg;
181 sub bake_cookie
($$%) {
182 my ($r, $cookie, %attr) = @_;
183 merge_hash
%attr, %COOKIE_DEFAULTS;
185 my $v = $attr{$_}; tr/_-/-/d;
186 defined $v ?
"$_=$v" : $_
188 $r->headers_out->add("Set-Cookie", join "; ", $cookie, @attr);
193 return $ENV{PATH_INFO
} // $r->path_info;
196 ###--------------------------------------------------------------------------
199 sub escapify
($$;$) {
200 my ($m, $s, $mode) = @_;
201 return $m->interp->apply_escapes($s, $mode // "h");
204 ###--------------------------------------------------------------------------
207 our ($WHO, $WHOSURE);
208 our ($WHOMATCH, $WHOCMP, $WHOPAT);
212 my $raddr = $ENV{REMOTE_ADDR
} // $r->connection->remote_ip;
213 $WHO = ":NET-$raddr"; $WHOSURE = 0;
214 $WHOMATCH = "LIKE"; $WHOCMP = ":NET-\%"; $WHOPAT = qr/^:NET-/;
218 $WHO = $ENV{USERV_USER
}
219 // ($< == $> && $ENV{USER
})
220 // @
{[getpwuid $<]}[0]
221 // die "nameless user";
222 $WHOMATCH = "="; $WHOCMP = $WHO; $WHOPAT = qr/^\Q$WHO\E$/;
227 open my $fh, "/dev/urandom" or die "open urandom: $!";
228 sysread $fh, my $rand, 16;
229 (my $edit = encode_base64
$rand) =~ tr
:+/=\n:.-:d
;
230 return $edit, sha256_hex
$edit;
233 ###--------------------------------------------------------------------------
236 sub get_shorturl
($) {
240 my ($url) = $db->selectrow_array
241 ("SELECT url FROM odin_shorturl WHERE tag = ?", undef, $tag);
242 fail
"tag `$tag' not found", ".notfound", tag
=> $tag unless defined $url;
246 sub valid_url_p
($) {
249 length $url < $URLMAXLEN &&
250 scalar grep { $url =~ /$_/ } @URLPAT;
253 sub new_shorturl
($) {
256 valid_url_p
$url or fail
"invalid url", ".badurl", u
=> $url;
261 ($tag) = $db->selectrow_array
262 ("SELECT tag FROM odin_shorturl WHERE owner $WHOMATCH ? AND url = ?",
263 undef, $WHOCMP, $url);
264 unless (defined $tag) {
265 $tag = encode_tag
(next_seq
($db, "odin_shorturl_seq"));
266 $db->do("INSERT INTO odin_shorturl (tag, stamp, owner, url)
267 VALUES (?, ?, ?, ?)", undef,
268 $tag, $NOW, $WHO, $url);
274 sub check_shorturl_owner
($$) {
277 my ($owner) = $db->selectrow_array
278 ("SELECT owner FROM odin_shorturl WHERE tag = ?", undef, $tag);
279 fail
"tag `$tag' not found", ".notfound", tag
=> $tag
280 unless defined $owner;
281 fail
"not owner of `$tag'", ".notowner", tag
=> $tag
282 unless $owner =~ /$WHOPAT/;
285 sub update_shorturl
($$) {
286 my ($tag, $url) = @_;
290 check_shorturl_owner
$db, $tag;
291 $db->do("UPDATE odin_shorturl SET url = ? WHERE tag = ?",
296 sub delete_shorturl
(@
) {
301 for my $tag (@tags) {
302 check_shorturl_owner
$db, $tag;
303 $db->do("DELETE FROM odin_shorturl WHERE tag = ?", undef, $tag);
308 ###--------------------------------------------------------------------------
311 our %PASTEBIN_DEFAULTS = (
312 title
=> "(untitled)",
316 our @PASTEBIN_PROPS = keys %PASTEBIN_DEFAULTS;
317 our $PASTEBIN_PROPCOLS = join ", ", @PASTEBIN_PROPS;
318 our $PASTEBIN_PROPPLACES = join ", ", map "?", @PASTEBIN_PROPS;
320 sub new_pastebin
(\
%) {
324 my ($editkey, $hash) = new_editkey
;
327 merge_hash
%$new, %PASTEBIN_DEFAULTS;
329 $tag = encode_tag next_seq
$db, "odin_pastebin_seq";
330 $db->do("INSERT INTO odin_pastebin
331 (tag, stamp, edithash, owner, $PASTEBIN_PROPCOLS)
332 VALUES (?, ?, ?, ?, $PASTEBIN_PROPPLACES)", undef,
333 $tag, $NOW, $hash, $WHO, @
{$new}{@PASTEBIN_PROPS});
335 return $tag, $editkey;
338 sub get_pastebin
($$\
%) {
339 my ($db, $tag, $props) = @_;
341 (my $owner, my $hash, @
{$props}{@PASTEBIN_PROPS}) =
342 $db->selectrow_array("SELECT owner, edithash, $PASTEBIN_PROPCOLS
343 FROM odin_pastebin WHERE tag = ?",
345 fail
"tag `$tag' not found", ".notfound", tag
=> $tag
346 unless defined $owner;
347 return $owner, $hash;
350 sub get_pastebin_check_owner
($$\
%) {
351 my ($db, $tag, $props) = @_;
353 my ($owner, $hash) = get_pastebin
$db, $tag, %$props;
354 fail
"not owner of `$tag'", ".notowner", tag
=> $tag
355 unless $WHOSURE && $WHO eq $owner;
358 sub get_pastebin_check_editkey_or_owner
($$$\
%) {
359 my ($db, $tag, $editkey, $props) = @_;
361 if (!defined $editkey) { get_pastebin_check_owner
$db, $tag, %$props; }
363 my ($owner, $hash) = get_pastebin
$db, $tag, %$props;
364 fail
"incorrect edit key for `$tag'", ".badhash", tag
=> $tag
365 unless $hash eq sha256_hex
$editkey;
369 sub rekey_pastebin
($) {
375 get_pastebin_check_owner
$db, $tag, my %hunoz;
376 ($editkey, my $hash) = new_editkey
;
377 $db->do("UPDATE odin_pastebin SET edithash = ? WHERE tag = ?",
383 sub claim_pastebin
($$) {
384 my ($tag, $editkey) = @_;
387 $WHOSURE or fail
"you can't claim pastes", ".notsure";
389 get_pastebin_check_editkey_or_owner
$db, $tag, $editkey, my %hunoz;
390 $db->do("UPDATE odin_pastebin SET owner = ? WHERE tag = ?",
395 sub update_pastebin
($$\
%) {
396 my ($tag, $editkey, $new) = @_;
401 get_pastebin_check_editkey_or_owner
$db, $tag, $editkey, my %old;
402 for my $p (@PASTEBIN_PROPS) {
403 if (!defined $new->{$p}) { $new->{$p} = $old{$p}; }
405 $db->do("UPDATE odin_pastebin SET $p = ? WHERE tag = ?",
406 undef, $new->{$p}, $tag)
407 unless $new->{$p} eq $old{$p};
415 sub delete_pastebin
(@
) {
420 (my $tag, my $editkey, @a) = @a;
421 get_pastebin_check_editkey_or_owner
$db, $tag, $editkey, my %hunoz;
422 $db->do("DELETE FROM odin_pastebin WHERE tag = ?", undef, $tag);
427 sub tidy_pastebin_content
($) {
429 return undef unless defined $content;
430 $content =~ tr/\r//d;
431 $content =~ s/([^\n])\z/$1\n/;
432 length $content <= $PASTEMAXLEN or
433 fail
"invalid paste content", ".badpaste";
437 ###--------------------------------------------------------------------------
438 ### Simple option parser.
440 package Odin
::OptParse
;
443 my ($cls, @args) = @_;
454 if (!length $me->{cur
}) {
455 my $args = $me->{args
};
456 if (!@
$args) { return undef; }
457 elsif ($args->[0] =~ /^[^-]|^-$/) { return undef; }
458 elsif ($args->[0] eq "--") { shift @
$args; return undef; }
459 $me->{cur
} = substr shift @
$args, 1;
461 my $o = $me->{opt
} = substr $me->{cur
}, 0, 1;
462 $me->{cur
} = substr $me->{cur
}, 1;
469 if (length $me->{cur
}) { $a = $me->{cur
}; $me->{cur
} = ""; }
470 elsif (@
{$me->{args
}}) { $a = shift @
{$me->{args
}}; }
471 else { $a = undef; $me->err("option `-$me->{opt}' requires an argument"); }
475 sub rest
{ return @
{$_[0]->{args
}}; }
476 sub ok
{ return $_[0]->{ok
}; }
477 sub bad
{ $_[0]->{ok
} = 0; }
478 sub err
{ $_[0]->bad; print STDERR
"$PROG: $_[1]\n"; }
479 sub unk
{ $_[0]->err("unknown option `-$_[0]->{opt}'"); }
481 ###----- That's all, folks --------------------------------------------------