~mailman-coders/mailman/2.1

« back to all changes in this revision

Viewing changes to bin/add_members

  • Committer: Mark Sapiro
  • Date: 2018-05-25 22:11:42 UTC
  • Revision ID: mark@msapiro.net-20180525221142-0x0x0zmyrlrxkgxu
Add an option to add_members to issue invitations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#! @PYTHON@
2
2
#
3
 
# Copyright (C) 1998-2016 by the Free Software Foundation, Inc.
 
3
# Copyright (C) 1998-2018 by the Free Software Foundation, Inc.
4
4
#
5
5
# This program is free software; you can redistribute it and/or
6
6
# modify it under the terms of the GNU General Public License
15
15
# You should have received a copy of the GNU General Public License
16
16
# along with this program; if not, write to the Free Software
17
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
 
#
19
 
# argv[1] should be the name of the list.
20
 
# argv[2] should be the list of non-digested users.
21
 
# argv[3] should be the list of digested users.
22
18
 
23
19
# Make sure that the list of email addresses doesn't contain any comments,
24
20
# like majordomo may throw in.  For now, you just have to remove them manually.
34
30
    -r file
35
31
        A file containing addresses of the members to be added, one
36
32
        address per line.  This list of people become non-digest
37
 
        members.  If file is `-', read addresses from stdin.  Note that
38
 
        -n/--non-digest-members-file are deprecated synonyms for this option.
 
33
        members.  If file is `-', read addresses from stdin.
39
34
 
40
35
    --digest-members-file=file
41
36
    -d file
42
37
        Similar to above, but these people become digest members.
43
38
 
 
39
    --invite
 
40
    -i
 
41
        Specify this if you only want to invite the users to a list
 
42
        instead of subscribing them.
 
43
 
 
44
    --invite-msg-file=file
 
45
    -m file
 
46
        This will prepend the message in the file to the invite email that
 
47
        gets generated when --invite is set.
 
48
 
44
49
    --welcome-msg=<y|n>
45
50
    -w <y|n>
46
51
        Set whether or not to send the list members a welcome message,
47
 
        overriding whatever the list's `send_welcome_msg' setting is.
 
52
        overriding whatever the list's `send_welcome_msg' setting is.  This
 
53
        is ignored and the list's setting at the time of acceptance is used
 
54
        if --invite is set.
48
55
 
49
56
    --admin-notify=<y|n>
50
57
    -a <y|n>
51
58
        Set whether or not to send the list administrators a notification on
52
59
        the success/failure of these subscriptions, overriding whatever the
53
 
        list's `admin_notify_mchanges' setting is.
 
60
        list's `admin_notify_mchanges' setting is.  This is ignored and the
 
61
        list's setting at the time of acceptance is used if --invite is set.
54
62
 
55
63
    --nomail
56
64
    -n
57
 
        Set the newly added members mail delivery to disabled by admin.
 
65
        Set the newly added members mail delivery to disabled by admin.  This
 
66
        is ignored if --invite is set.
58
67
 
59
68
    --help
60
69
    -h
84
93
from Mailman import Message
85
94
from Mailman import MailList
86
95
from Mailman import MemberAdaptor
 
96
from Mailman.UserDesc import UserDesc
87
97
 
88
98
_ = i18n._
89
99
C_ = i18n.C_
117
127
 
118
128
 
119
129
 
 
130
def readmsgfile(filename):
 
131
    lines = open(filename).read()
 
132
    return lines
 
133
 
 
134
 
 
135
 
120
136
class Tee:
121
137
    def __init__(self, outfp):
122
138
        self.__outfp = outfp
126
142
        self.__outfp.write(msg)
127
143
 
128
144
 
129
 
class UserDesc: pass
130
 
 
131
 
 
132
145
 
133
 
def addall(mlist, members, digest, ack, outfp, nomail):
 
146
def addall(mlist, members, digest, ack, outfp, nomail, invite, invite_msg):
134
147
    tee = Tee(outfp)
135
148
    for member in members:
136
149
        userdesc = UserDesc()
138
151
        userdesc.digest = digest
139
152
 
140
153
        try:
141
 
            mlist.ApprovedAddMember(userdesc,
142
 
                                    ack=ack,
143
 
                                    admin_notif=False,
144
 
                                    whence='bin/add_members',
145
 
                                   )
 
154
            if invite:
 
155
                # These are needed for an invitation.
 
156
                userdesc.password = Utils.MakeRandomPassword()
 
157
                userdesc.language = mlist.preferred_language
 
158
                # Don't forget the special invite hack.
 
159
                userdesc.invitation = mlist.internal_name()
 
160
                # InviteNewMember doesn't throw Errors.MMAlreadyAMember.
 
161
                if mlist.isMember(userdesc.address):
 
162
                    print >> tee, _('Already a member: %(member)s')
 
163
                    continue
 
164
                mlist.InviteNewMember(userdesc, invite_msg)
 
165
            else:
 
166
                mlist.ApprovedAddMember(userdesc,
 
167
                                        ack=ack,
 
168
                                        admin_notif=False,
 
169
                                        whence='bin/add_members',
 
170
                                       )
146
171
        except Errors.MMAlreadyAMember:
147
172
            print >> tee, _('Already a member: %(member)s')
148
173
        except Errors.MembershipIsBanned, pattern:
156
181
        except Errors.MMHostileAddress:
157
182
            print >> tee, _('Hostile address (illegal characters): %(member)s')
158
183
        else:
159
 
            print >> tee, _('Subscribed: %(member)s')
160
 
            if nomail:
161
 
                mlist.setDeliveryStatus(member, MemberAdaptor.BYADMIN)
 
184
            if invite:
 
185
                print >> tee, _('Invited: %(member)s')
 
186
            else:
 
187
                print >> tee, _('Subscribed: %(member)s')
 
188
                if nomail:
 
189
                    mlist.setDeliveryStatus(
 
190
                        userdesc.address.lower(), MemberAdaptor.BYADMIN)
162
191
 
163
192
 
164
193
 
165
194
def main():
166
195
    try:
167
196
        opts, args = getopt.getopt(sys.argv[1:],
168
 
                                   'a:r:d:w:nh',
 
197
                                   'a:r:d:w:im:nh',
169
198
                                   ['admin-notify=',
170
199
                                    'regular-members-file=',
171
200
                                    'digest-members-file=',
172
201
                                    'welcome-msg=',
 
202
                                    'invite',
 
203
                                    'invite-msg-file=',
173
204
                                    'nomail',
174
 
                                    'help'])
 
205
                                    'help',])
175
206
    except getopt.error, msg:
176
207
        usage(1, msg)
177
208
 
183
214
    dfile = None
184
215
    send_welcome_msg = None
185
216
    admin_notif = None
 
217
    invite = False
 
218
    invite_msg_file = None
186
219
    nomail = False
187
220
    for opt, arg in opts:
188
221
        if opt in ('-h', '--help'):
191
224
            dfile = arg
192
225
        elif opt in ('-r', '--regular-members-file'):
193
226
            nfile = arg
 
227
        elif opt in ('-m', '--invite-msg-file'):
 
228
            invite_msg_file = arg
 
229
        elif opt in ('-i', '--invite'):
 
230
            invite = True
194
231
        elif opt in ('-w', '--welcome-msg'):
195
232
            if arg.lower()[0] == 'y':
196
233
                send_welcome_msg = 1
215
252
        usage(1, C_('Cannot read both digest and normal members '
216
253
                    'from standard input.'))
217
254
 
 
255
    if not invite and invite_msg_file <> None:
 
256
        usage(1, C_('Setting invite-msg-file requires --invite.'))
 
257
 
218
258
    try:
219
259
        mlist = MailList.MailList(listname)
220
260
    except Errors.MMUnknownListError:
237
277
        if nfile:
238
278
            nmembers = readfile(nfile)
239
279
 
 
280
        invite_msg = ''
 
281
        if invite_msg_file:
 
282
            invite_msg = readmsgfile(invite_msg_file)
 
283
 
240
284
        if not dmembers and not nmembers:
241
285
            usage(0, C_('Nothing to do.'))
242
286
 
243
287
        s = StringIO()
244
288
        i18n.set_language(mlist.preferred_language)
245
289
        if nmembers:
246
 
            addall(mlist, nmembers, 0, send_welcome_msg, s, nomail)
 
290
            addall(mlist, nmembers, 0, send_welcome_msg, s, nomail, invite,
 
291
                   invite_msg)
247
292
 
248
293
        if dmembers:
249
 
            addall(mlist, dmembers, 1, send_welcome_msg, s, nomail)
 
294
            addall(mlist, dmembers, 1, send_welcome_msg, s, nomail, invite,
 
295
                   invite_msg)
250
296
 
251
297
        if admin_notif:
252
298
            realname = mlist.real_name