lib/Odin.pm: Provide substitutions on mail recipient addresses.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 27 Jun 2016 16:50:00 +0000 (17:50 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Mon, 27 Jun 2016 16:50:00 +0000 (17:50 +0100)
The most useful thing is `%l' to include the original Odin local-part in
the forwarded address.  For completeness, we also have `%d' which is the
default domain-part.

lib/Odin.pm

index a575e83..90f1dc1 100644 (file)
@@ -574,7 +574,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 ($) {
@@ -586,6 +586,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;
@@ -600,7 +607,7 @@ sub new_redir ($$\%;$) {
       insert_record $db, "odin_mailredir",
        lpart => $l, dom => $dom, st => 'live',
        owner => $WHO, creator => $WHO,
-       recip => $r->{recip} // qualify_recip $Odin::WHO,
+       recip => subst_recip($l, $r->{recip} // qualify_recip $Odin::WHO),
        expire => $r->{expire} // -1,
        comment => $r->{comment} // "";
       push @l, $l;
@@ -688,7 +695,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 = ();