summaryrefslogtreecommitdiff
path: root/cron/checkdbs
blob: cf1401b2fbf286271de69e64af3b405d3804c3e5 (plain)
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
#! @PYTHON@
#
# Copyright (C) 1998,1999,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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

"""Invoked by cron, this checks for pending moderation requests and mails the
list moderators if necessary.
"""

import sys
import time

import paths
# mm_cfg must be imported before the other modules, due to the side-effect of
# it hacking sys.paths to include site-packages.  Without this, running this
# script from cron with python -S will fail.
from Mailman import mm_cfg
from Mailman import Utils
from Mailman import MailList
from Mailman import Message
from Mailman import i18n

# Work around known problems with some RedHat cron daemons
import signal
signal.signal(signal.SIGCHLD, signal.SIG_DFL)

NL = '\n'

_ = i18n._
i18n.set_language(mm_cfg.DEFAULT_SERVER_LANGUAGE)



def main():
    for name in Utils.list_names():
        # the list must be locked in order to open the requests database
	mlist = MailList.MailList(name)
        try:
            count = mlist.NumRequestsPending()
            # Don't save the list since we've done nothing to modify its state
        finally:
            mlist.Unlock()
            
        if count:
            i18n.set_language(mlist.preferred_language)
            realname = mlist.real_name
            text = Utils.maketext(
                'checkdbs.txt',
                {'count'    : count,
                 'host_name': mlist.host_name,
                 'adminDB'  : mlist.GetScriptURL('admindb', absolute=1),
                 'real_name': realname,
                 }, mlist=mlist)
            text += '\n' + pending_requests(mlist)
            subject = _('%(count)d %(realname)s moderator request(s) waiting')
            msg = Message.UserNotification(mlist.GetOwnerEmail(),
                                           mlist.GetBouncesEmail(),
                                           subject, text,
                                           mlist.preferred_language)
            msg.send(mlist, **{'tomoderators': 1})
            


def pending_requests(mlist):
    pending = []
    first = 1
    for id in mlist.GetSubscriptionIds():
        if first:
            pending.append(_('Pending subscriptions:'))
            first = 0
        when, addr, fullname, passwd, digest, lang = mlist.GetRecord(id)
        if fullname:
            fullname = ' (%s)' % fullname
        pending.append('    %s%s %s' % (addr, fullname, time.ctime(when)))
    first = 1
    for id in mlist.GetHeldMessageIds():
        if first:
            pending.append(_('\nPending posts:'))
            first = 0
        info = mlist.GetRecord(id)
        when, sender, subject, reason, text, msgdata = mlist.GetRecord(id)
        date = time.ctime(when)
        pending.append(_(
            '     From: %(sender)s on %(date)s\n    Cause: %(reason)s'))
        pending.append('')
    return NL.join(pending)



if __name__ == '__main__':
    main()