2 # Copyright (C) 2015-2016 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
5 # A work-in-progress, but one day I hope this script is no longer
6 # necessary and users will all pull from public-inboxes instead
7 # of having mail pushed to them via mlmmj.
9 # This is for use with ssoma, using "command:" delivery mechanism
10 # (as opposed to normal Maildir or mbox).
11 # It assumes mlmmj-process is in /usr/bin (mlmmj requires absolute paths)
12 # and assumes FOO@domain.example.com has web archives available at:
13 # https://domain.example.com/FOO/
15 # The goal here is _anybody_ can setup a mirror of any public-inbox
16 # repository and run their own mlmmj instance to replay traffic.
17 =begin usage with ssoma:
20 URL=https://public-inbox.org/meta/
21 ssoma add $NAME $URL "command:/path/to/ssoma-replay -L /path/to/spool/$NAME"
23 ; $GIT_DIR/ssoma.state should have something like the following target:
24 ; (where GIT_DIR is ~/.ssoma/meta.git/ in the above example)
26 command = /path/to/ssoma-replay -L /path/to/spool/meta
30 use URI::Escape qw/uri_escape_utf8/;
31 use File::Temp qw/tempfile/;
32 my ($fh, $filename) = tempfile('ssoma-replay-XXXXXXXX', TMPDIR => 1);
35 Email::Simple->new(<STDIN>);
39 # Note: the archive URL makes assumptions about where the
40 # archive is hosted. It is currently true of all the domains
43 my $header_obj = $msg->header_obj;
44 my $body = $msg->body;
45 my $list_id = $header_obj->header('List-Id');
46 my ($archive_url, $user, $domain);
47 if (defined $list_id) {
48 # due to a bug in old versions of public-inbox, <user@domain> was used
49 # as the list-Id instead of <user.domain> as recommended in RFC2919
50 ($user, $domain) = ($list_id =~ /<([^\.@]+)[\.@](.+)>/g);
52 if (defined $domain) {
53 $archive_url = "https://$domain/$user/";
54 my $mid = $header_obj->header('Message-Id');
55 if ($mid =~ /\A<(.+)>\z/) {
58 $mid = uri_escape_utf8($mid,
59 '^A-Za-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@');
60 $header_obj->header_set('List-Archive', "<$archive_url>");
62 foreach my $h (qw(Help Unsubscribe Subscribe Owner)) {
64 my $v = "<mailto:$user+$lch\@$domain>";
65 $header_obj->header_set("List-$h", $v);
67 $header_obj->header_set('List-Post', "<mailto:$user\@$domain>");
70 $header_obj->header_set('Archived-At', "<$archive_url$mid/>");
71 $header_obj->header_set('X-Archived-At');
75 print $header_obj->as_string, $msg->crlf, $body;
77 # don't break inline signatures
78 goto out if ($body =~ /^-----BEGIN PGP SIG.+-----/sm);
80 # try not to break dkim/dmarc/spf crap, either
81 foreach (qw(domainkey-signature dkim-signature authentication-results)) {
82 goto out if defined $header_obj->header($_);
85 my $ct = $header_obj->header('Content-Type');
87 if (!defined($ct) || $ct =~ m{\A\s*text/plain\b}i) {
88 print "\n" unless $body =~ /\n\z/s;
89 defined $archive_url or goto out;
90 # Do not add a space after '--' as is standard for user-generated
91 # signatures, we want to preserve the "-- \n" in original user sigs
92 # for mail software which splits on that.
93 print "--\n", "unsubscribe: $user+unsubscribe\@$domain\n",
94 "archive: $archive_url\n";
98 exec '/usr/bin/mlmmj-process', @ARGV, '-m', $filename;