Give the SubscriptionWorkflow a member attribute which gets set to the member
[mailman.git] / port_me / disabled.py
blobb190556c2b37d585d0285f02e3b80df726bf3e20
1 # Copyright (C) 2001-2015 by the Free Software Foundation, Inc.
3 # This file is part of GNU Mailman.
5 # GNU Mailman is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option)
8 # any later version.
10 # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 # more details.
15 # You should have received a copy of the GNU General Public License along with
16 # GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
18 import time
19 import logging
20 import optparse
22 from mailman import MailList
23 from mailman import MemberAdaptor
24 from mailman import Pending
25 from mailman import loginit
26 from mailman.Bouncer import _BounceInfo
27 from mailman.configuration import config
28 from mailman.core.i18n import _
29 from mailman.interfaces.member import NotAMemberError
30 from mailman.version import MAILMAN_VERSION
33 # Work around known problems with some RedHat cron daemons
34 import signal
35 signal.signal(signal.SIGCHLD, signal.SIG_DFL)
37 ALL = (MemberAdaptor.BYBOUNCE,
38 MemberAdaptor.BYADMIN,
39 MemberAdaptor.BYUSER,
40 MemberAdaptor.UNKNOWN,
45 def who_callback(option, opt, value, parser):
46 dest = getattr(parser.values, option.dest)
47 if opt in ('-o', '--byadmin'):
48 dest.add(MemberAdaptor.BYADMIN)
49 elif opt in ('-m', '--byuser'):
50 dest.add(MemberAdaptor.BYUSER)
51 elif opt in ('-u', '--unknown'):
52 dest.add(MemberAdaptor.UNKNOWN)
53 elif opt in ('-b', '--notbybounce'):
54 dest.discard(MemberAdaptor.BYBOUNCE)
55 elif opt in ('-a', '--all'):
56 dest.update(ALL)
57 x5o
59 def parseargs():
60 parser = optparse.OptionParser(version=MAILMAN_VERSION,
61 usage=_("""\
62 %prog [options]
64 Process disabled members, recommended once per day.
66 This script iterates through every mailing list looking for members whose
67 delivery is disabled. If they have been disabled due to bounces, they will
68 receive another notification, or they may be removed if they've received the
69 maximum number of notifications.
71 Use the --byadmin, --byuser, and --unknown flags to also send notifications to
72 members whose accounts have been disabled for those reasons. Use --all to
73 send the notification to all disabled members."""))
74 # This is the set of working flags for who to send notifications to. By
75 # default, we notify anybody who has been disable due to bounces.
76 parser.set_defaults(who=set([MemberAdaptor.BYBOUNCE]))
77 parser.add_option('-o', '--byadmin',
78 callback=who_callback, action='callback', dest='who',
79 help=_("""\
80 Also send notifications to any member disabled by the list
81 owner/administrator."""))
82 parser.add_option('-m', '--byuser',
83 callback=who_callback, action='callback', dest='who',
84 help=_("""\
85 Also send notifications to any member who has disabled themself."""))
86 parser.add_option('-u', '--unknown',
87 callback=who_callback, action='callback', dest='who',
88 help=_("""\
89 Also send notifications to any member disabled for unknown reasons
90 (usually a legacy disabled address)."""))
91 parser.add_option('-b', '--notbybounce',
92 callback=who_callback, action='callback', dest='who',
93 help=_("""\
94 Don't send notifications to members disabled because of bounces (the
95 default is to notify bounce disabled members)."""))
96 parser.add_option('-a', '--all',
97 callback=who_callback, action='callback', dest='who',
98 help=_('Send notifications to all disabled members'))
99 parser.add_option('-f', '--force',
100 default=False, action='store_true',
101 help=_("""\
102 Send notifications to disabled members even if they're not due a new
103 notification yet."""))
104 parser.add_option('-l', '--listname',
105 dest='listnames', action='append', default=[],
106 type='string', help=_("""\
107 Process only the given list, otherwise do all lists."""))
108 parser.add_option('-C', '--config',
109 help=_('Alternative configuration file to use'))
110 opts, args = parser.parse_args()
111 return opts, args, parser
115 def main():
116 opts, args, parser = parseargs()
117 config.load(opts.config)
119 loginit.initialize(propagate=True)
120 elog = logging.getLogger('mailman.error')
121 blog = logging.getLogger('mailman.bounce')
123 listnames = set(opts.listnames or config.list_manager.names)
124 who = tuple(opts.who)
126 msg = _('[disabled by periodic sweep and cull, no message available]')
127 today = time.mktime(time.localtime()[:3] + (0,) * 6)
128 for listname in listnames:
129 # List of members to notify
130 notify = []
131 mlist = MailList.MailList(listname)
132 try:
133 interval = mlist.bounce_you_are_disabled_warnings_interval
134 # Find all the members who are currently bouncing and see if
135 # they've reached the disable threshold but haven't yet been
136 # disabled. This is a sweep through the membership catching
137 # situations where they've bounced a bunch, then the list admin
138 # lowered the threshold, but we haven't (yet) seen more bounces
139 # from the member. Note: we won't worry about stale information
140 # or anything else since the normal bounce processing code will
141 # handle that.
142 disables = []
143 for member in mlist.getBouncingMembers():
144 if mlist.getDeliveryStatus(member) <> MemberAdaptor.ENABLED:
145 continue
146 info = mlist.getBounceInfo(member)
147 if info.score >= mlist.bounce_score_threshold:
148 disables.append((member, info))
149 if disables:
150 for member, info in disables:
151 mlist.disableBouncingMember(member, info, msg)
152 # Go through all the members who have delivery disabled, and find
153 # those that are due to have another notification. If they are
154 # disabled for another reason than bouncing, and we're processing
155 # them (because of the command line switch) then they won't have a
156 # bounce info record. We can piggyback on that for all disable
157 # purposes.
158 members = mlist.getDeliveryStatusMembers(who)
159 for member in members:
160 info = mlist.getBounceInfo(member)
161 if not info:
162 # See if they are bounce disabled, or disabled for some
163 # other reason.
164 status = mlist.getDeliveryStatus(member)
165 if status == MemberAdaptor.BYBOUNCE:
166 elog.error(
167 '%s disabled BYBOUNCE lacks bounce info, list: %s',
168 member, mlist.internal_name())
169 continue
170 info = _BounceInfo(
171 member, 0, today,
172 mlist.bounce_you_are_disabled_warnings,
173 mlist.pend_new(Pending.RE_ENABLE,
174 mlist.internal_name(),
175 member))
176 mlist.setBounceInfo(member, info)
177 lastnotice = time.mktime(info.lastnotice + (0,) * 6)
178 if opts.force or today >= lastnotice + interval:
179 notify.append(member)
180 # Now, send notifications to anyone who is due
181 for member in notify:
182 blog.info('Notifying disabled member %s for list: %s',
183 member, mlist.internal_name())
184 try:
185 mlist.sendNextNotification(member)
186 except NotAMemberError:
187 # There must have been some problem with the data we have
188 # on this member. Most likely it's that they don't have a
189 # password assigned. Log this and delete the member.
190 blog.info(
191 'Cannot send disable notice to non-member: %s',
192 member)
193 mlist.ApprovedDeleteMember(member, 'cron/disabled')
194 mlist.Save()
195 finally:
196 mlist.Unlock()
200 if __name__ == '__main__':
201 main()