aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/auto
blob: eaa8ab461988bb0856c0d827015740f84c8677c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# -*- python -*-
#
# Copyright (C) 2000,2001,2002 by the Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program 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 General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

"""Automatically send a message to a mailing list.
"""

# To use with Postfix, set the following in your main.cf file:
#
# recipient_delimiter = +
# luser_relay = mm+$user@yourdomain.com
# owner_request_special = no

import sys
import os
import time

import paths
from Mailman import mm_cfg
from Mailman import Utils
from Mailman import MailList
from Mailman import Errors
from Mailman.Queue.sbcache import get_switchboard
from Mailman.Logging.Utils import LogStdErr

# Error code if it's really not a Mailman list addr destination
EX_NOUSER = 67

LogStdErr('auto', 'auto')

DISPOSE_MAP = {None     : 'tolist',
               'request': 'torequest',
               'admin'  : 'toadmin',
               'owner'  : 'toadmin',
               }



def fqdn_listname(listname, hostname):
    return ('%s@%s' % (listname, hostname)).lower()



def main():
    # Postfix sets some environment variables based on information gleaned
    # from the original message.  This is the most direct way to figure out
    # which list the message was intended for.
    extension = os.environ.get('EXTENSION', '').lower()
    i = extension.rfind('-')
    if i < 0:
        listname = extension
        subdest = 'tolist'
    else:
        missing = []
        listname = extension[:i]
        subdest = DISPOSE_MAP.get(extension[i+1:], missing)
        if not Utils.list_exists(listname) or subdest is missing:
            # must be a list that has a `-' in it's name
            listname = extension
            subdest = 'tolist'
    if not listname:
        print >> sys.stderr, 'Empty list name (someone being subversive?)'
        return EX_NOUSER
    try:
        mlist = MailList.MailList(listname, lock=0)
    except Errors.MMListError:
        print >> sys.stderr, 'List not found:', listname
        return EX_NOUSER

    # Make sure that the domain part of the incoming address matches the
    # domain of the mailing list.  Actually, it's possible that one or the
    # other is more fully qualified, and thus longer.  So we split the domains
    # by dots, reverse them and make sure that whatever parts /are/ defined
    # for both are equivalent.
    domain = os.environ.get('DOMAIN', '').lower()
    domainp = domain.split('.')
    hostname = mlist.host_name.split('.')
    domainp.reverse()
    hostname.reverse()
    for ca, cb in zip(domainp, hostname):
        if ca <> cb:
            print >> sys.stderr, 'Domain mismatch: %s@%s (expected @%s)' \
                  % (listname, domain, mlist.host_name)
            return EX_NOUSER

    if subdest is None:
        print >> sys.stderr, 'Bad sub-destination:', extension
        return EX_NOUSER

    inq = get_switchboard(mm_cfg.INQUEUE_DIR)
    inq.enqueue(sys.stdin.read(),
                listname=listname,
                received_time=time.time(),
                _plaintext=1,
                **{subdest: 1})
    return 0



if __name__ == '__main__':
    code = main()
    sys.exit(code)