X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=examples%2Funsubscribe.milter;h=608524cbaca40337a8ca45a1f0b7d0ba38cfd774;hb=96a5e841a169166457d9cf485e27a0d6cbe7ed75;hp=f7bf6f1d38b4f18503b8be906d6e9bc35b9c7760;hpb=da0eeb321433b70819dd417afcaf1ac86e1da5df;p=public-inbox.git
diff --git a/examples/unsubscribe.milter b/examples/unsubscribe.milter
index f7bf6f1d..608524cb 100644
--- a/examples/unsubscribe.milter
+++ b/examples/unsubscribe.milter
@@ -1,8 +1,7 @@
#!/usr/bin/perl -w
-# Copyright (C) 2016-2019 all contributors
+# Copyright (C) 2016-2021 all contributors
# License: AGPL-3.0+
use strict;
-use warnings;
use Sendmail::PMilter qw(:all);
use IO::Socket;
use Crypt::CBC;
@@ -16,6 +15,10 @@ if (read($fh, $key, 8) != 8 || read($fh, $iv, 8) != 8 ||
die "KEY_FILE must be 16 bytes\n";
}
+# optionally support unique mailto: subject in List-Unsubscribe,
+# requires a custom rule in front of mlmmj, see __END__
+my $unique_mailto = $ENV{UNIQUE_MAILTO};
+
# these parameters were chosen to generate shorter parameters
# to reduce the possibility of copy+paste errors
my $crypt = Crypt::CBC->new(-key => $key,
@@ -102,6 +105,12 @@ $cbs{eom} = sub {
next unless $k && $v && $list && $domain;
my $u = $crypt->encrypt($rcpt[0]);
$u = encode_base64url($u);
+ if ($unique_mailto) {
+ # $u needs to be in the Subject: header since
+ # +$EXTENSION is case-insensitive
+ my $s = "subject=$u";
+ $v = "";
+ }
$v .= ",\n ";
$ctx->chgheader($k, $index, $v);
@@ -118,7 +127,7 @@ my $fds = $ENV{LISTEN_FDS};
if ($fds && (($ENV{LISTEN_PID} || 0) == $$)) {
die "$0 can only listen on one FD\n" if $fds != 1;
my $start_fd = 3;
- my $s = IO::Socket->new_from_fd($start_fd, 'r') or
+ open(my $s, '<&=', $start_fd) or
die "inherited bad FD from LISTEN_FDS: $!\n";
$milter->set_socket($s);
} else {
@@ -132,3 +141,28 @@ if ($fds && (($ENV{LISTEN_PID} || 0) == $$)) {
$milter->register('unsubscribe', \%cbs, SMFI_CURR_ACTS);
$milter->main();
+__END__
+# TMPMSG comes from dc-dlvr, it's populated before the above runs:
+# TMPMSG=$(mktemp -t dc-dlvr.orig.$USER.XXXXXX || exit 1)
+# cat >$TMPMSG
+
+# I use something like this in front of mlmmj for UNIQUE_MAILTO
+# $EXTENSION and $ORIGINAL_RECIPIENT are set by postfix, $list
+# is a local mapping of addresses to mailing list names.
+case $ORIGINAL_RECIPIENT in
+foo+*) list=foo ;;
+# ...
+esac
+
+case $EXTENSION in
+unique-unsub)
+ u="$(formail -z -c -x Subject <$TMPMSG)"
+ d=$(expr "$ORIGINAL_RECIPIENT" : '^.*@\(.*\)')
+
+ # forward this to the unsubscribe.psgi service
+ curl -sSf https://$d/u/$u/$list >/dev/null
+ exit
+ ;;
+esac
+/usr/bin/mlmmj-receive -L /path/to/mlmmj-spool/$list <"$TMPMSG"
+exit