X-Git-Url: https://git.distorted.org.uk/~mdw/odin-cgi/blobdiff_plain/3e12ff54361d996cd64056a76b45f69abeec278d..refs/heads/master:/lib/Odin.pm diff --git a/lib/Odin.pm b/lib/Odin.pm index a742839..3983e8f 100644 --- a/lib/Odin.pm +++ b/lib/Odin.pm @@ -1,4 +1,27 @@ ### -*-perl-*- +### +### Common library for Odin +### +### (c) 2015 Mark Wooding +### + +###----- Licensing notice --------------------------------------------------- +### +### This file is part of the `odin.gg' service, `odin-cgi'. +### +### `odin-cgi' is free software; you can redistribute it and/or modify +### it under the terms of the GNU Affero General Public License as +### published by the Free Software Foundation; either version 3 of the +### License, or (at your option) any later version. +### +### `odin-cgi' is distributed in the hope that it will be useful, +### but WITHOUT ANY WARRANTY; without even the implied warranty of +### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +### GNU Affero General Public License for more details. +### +### You should have received a copy of the GNU Affero General Public +### License along with `odin-cgi'; if not, see +### . package Odin; @@ -24,8 +47,9 @@ our $DSN = "dbi:Pg(pg_enable_utf8=>1):host=db"; our $RETRY = 10; our @BACKOFF = (0.1, 10, 1.5, 0.5, 2.0); -our $BASEURL = "http://odin.gg/"; -our $STATIC = "http://odin.gg/"; +our $BASEURL = "https://odin.gg/"; +our $STATIC = "https://odin.gg/static/"; +our $SRCURL = "https://git.distorted.org.uk/~mdw/odin-cgi/"; our $SHORTURL_PATH = "u"; our $PASTEBIN_PATH = "p"; @@ -376,6 +400,16 @@ our @PASTEBIN_PROPS = keys %PASTEBIN_DEFAULTS; our $PASTEBIN_PROPCOLS = join ", ", @PASTEBIN_PROPS; our $PASTEBIN_PROPPLACES = join ", ", map "?", @PASTEBIN_PROPS; +sub check_lang ($) { + my ($lang) = @_; + + return unless defined $lang; + my $db = open_db; + @{$db->selectall_arrayref + ("SELECT lang FROM odin_pastebin_lang WHERE lang = ?", undef, $lang)} + or fail "unknown language `$lang'"; +} + sub new_pastebin (\%) { my ($new) = @_; @@ -384,6 +418,7 @@ sub new_pastebin (\%) { my $tag; merge_hash %$new, %PASTEBIN_DEFAULTS; + check_lang $new->{lang}; xact { $tag = encode_tag next_seq $db, "odin_pastebin_seq"; insert_record $db, "odin_pastebin", @@ -455,6 +490,7 @@ sub update_pastebin ($$\%) { my $db = open_db; my $editp = 0; + check_lang $new->{lang}; xact { get_pastebin_check_editkey_or_owner $db, $tag, $editkey, my %old; for my $p (@PASTEBIN_PROPS) { @@ -550,7 +586,7 @@ sub gen_redir_name ($$) { my ($db, $gen) = @_; for (my $try = 0; $try < $MAIL_MAXGENTRY; $try++) { - my $l = $gen->gen; + my $l = lc $gen->gen; return $l unless $db->selectrow_arrayref ("SELECT 1 FROM odin_mailredir WHERE lpart = ? AND dom = ?", @@ -562,7 +598,7 @@ sub gen_redir_name ($$) { sub qualify_recip ($) { my ($r) = @_; - return $r =~ /\@/ || !defined $MAIL_QUALDOM ? $r : "$r\@$MAIL_QUALDOM"; + return $r =~ /\@/ || !defined $MAIL_QUALDOM ? $r : "$r\@\%d"; } sub check_fixup_redir ($) { @@ -574,6 +610,13 @@ sub check_fixup_redir ($) { } } +sub subst_recip ($$) { + my ($l, $r) = @_; + my %d = ('%' => '%', 'l' => $l, 'd' => $MAIL_QUALDOM); + $r =~ s{%([%a-z])}{$d{$1} // fail "undefined substitution `\%$1'"}eg; + return $r; +} + sub new_redir ($$\%;$) { my ($dom, $gen, $r, $n) = @_; my $db = open_db; @@ -586,8 +629,9 @@ sub new_redir ($$\%;$) { while (@l < $n) { my $l = Odin::gen_redir_name $db, $gen; insert_record $db, "odin_mailredir", - lpart => $l, dom => $dom, owner => $WHO, st => 'live', - recip => $r->{recip} // qualify_recip $Odin::WHO, + lpart => $l, dom => $dom, st => 'live', + owner => $WHO, creator => $WHO, + recip => subst_recip($l, $r->{recip} // qualify_recip $Odin::WHO), expire => $r->{expire} // -1, comment => $r->{comment} // ""; push @l, $l; @@ -608,7 +652,7 @@ sub reserve_redir ($$$) { for (my $i = 0; $i < $n; $i++) { push @l, gen_redir_name $db, $gen; } for my $l (@l) { insert_record $db, "odin_mailredir", - lpart => $l, dom => $dom, owner => $WHO, + lpart => $l, dom => $dom, owner => $WHO, creator => $WHO, st => 'reserved', expire => $NOW + $MAIL_AGEMAX_RESV; } check_redir_limits $db; @@ -675,7 +719,8 @@ sub modify_redir ($\%@) { WHERE lpart = ? AND dom = ? AND owner = ?", undef, $l, $dom, $WHO); if (!defined $recip) { Odin::fail "unknown local part `$l'"; } - elsif ($recip eq "") { $r{recip} //= qualify_recip $WHO; } + elsif ($recip eq "" || defined $r{recip}) + { $r{recip} = subst_recip $l, $r{recip} // qualify_recip $WHO; } if ($st ne "live") { $r{st} = "live"; $r{expire} //= -1; } my @var = (); my @val = ();