aboutsummaryrefslogtreecommitdiffstats
path: root/bin/clone_member
blob: e2ff0cfb3fb111dbe7b59fcf3a4254339f357003 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#! @PYTHON@
#
# Copyright (C) 1998-2018 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.

"""Clone a member address.

Cloning a member address means that a new member will be added who has all the
same options and passwords as the original member address.  Note that this
operation is fairly trusting of the user who runs it -- it does no
verification to the new address, it does not send out a welcome message, etc.

The existing member's subscription is usually not modified in any way.  If you
want to remove the old address, use the -r flag.  If you also want to change
any list admin addresses, use the -a flag.

Usage:
    clone_member [options] fromoldaddr tonewaddr

Where:

    --listname=listname
    -l listname
        Check and modify only the named mailing lists.  If -l is not given,
        then all mailing lists are scanned from the address.  Multiple -l
        options can be supplied.

    --remove
    -r
        Remove the old address from the mailing list after it's been cloned.

    --admin
    -a
        Scan the list admin addresses for the old address, and clone or change
        them too.

    --quiet
    -q
        Do the modifications quietly.

    --nomodify
    -n
        Print what would be done, but don't actually do it.  Inhibits the
        --quiet flag.

    --help
    -h
        Print this help message and exit.

 fromoldaddr (`from old address') is the old address of the user.  tonewaddr
 (`to new address') is the new address of the user.

"""

import sys
import getopt

import paths
from Mailman import MailList
from Mailman import Utils
from Mailman import Errors
from Mailman.i18n import C_



def usage(code, msg=''):
    if code:
        fd = sys.stderr
    else:
        fd = sys.stdout
    print >> fd, C_(__doc__)
    if msg:
        print >> fd, msg
    sys.exit(code)



def dolist(mlist, options):
    SPACE = ' '
    if not options.quiet:
        print C_('processing mailing list:'), mlist.internal_name()

    # scan the list owners.  TBD: mlist.owner keys should be lowercase?
    oldowners = mlist.owner[:]
    oldowners.sort()
    if options.admintoo:
        if not options.quiet:
            print C_('    scanning list owners:'), SPACE.join(oldowners)
        newowners = {}
        foundp = 0
        for owner in mlist.owner:
            if options.lfromaddr == owner.lower():
                foundp = 1
                if options.remove:
                    continue
            newowners[owner] = 1
        if foundp:
            newowners[options.toaddr] = 1 
        newowners = newowners.keys()
        newowners.sort()
        if options.modify:
            mlist.owner = newowners
        if not options.quiet:
            if newowners <> oldowners:
                print
                print C_('    new list owners:'), SPACE.join(newowners)
            else:
                print C_('(no change)')

    # see if the fromaddr is a digest member or regular member
    if options.lfromaddr in mlist.getDigestMemberKeys():
        digest = 1
    elif options.lfromaddr in mlist.getRegularMemberKeys():
        digest = 0
    else:
        if not options.quiet:
            print C_('    address not found:'), options.fromaddr
        return
    # Check for banned to address.
    pattern = mlist.GetBannedPattern(options.toaddr)
    if pattern:
        if not options.quiet:
            print '   ', C_('Banned address (matched %(pattern)s)')
        return

    # Now change the membership address
    try:
        if options.modify:
            mlist.changeMemberAddress(options.fromaddr, options.toaddr,
                                      not options.remove)
        if not options.quiet:
            print C_('    clone address added:'), options.toaddr
    except Errors.MMAlreadyAMember:
        if not options.quiet:
            print C_('    clone address is already a member:'), options.toaddr

    if options.remove:
        print C_('    original address removed:'), options.fromaddr



def main():
    # default options
    class Options:
        listnames = None
        remove = 0
        admintoo = 0
        quiet = 0
        modify = 1

    # scan sysargs
    try:
        opts, args = getopt.getopt(
            sys.argv[1:], 'arl:qnh',
            ['admin', 'remove', 'listname=', 'quiet', 'nomodify', 'help'])
    except getopt.error, msg:
        usage(1, msg)

    options = Options()
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage(0)
        elif opt in ('-q', '--quiet'):
            options.quiet = 1
        elif opt in ('-n', '--nomodify'):
            options.modify = 0
        elif opt in ('-a', '--admin'):
            options.admintoo = 1
        elif opt in ('-r', '--remove'):
            options.remove = 1
        elif opt in ('-l', '--listname'):
            if options.listnames is None:
                options.listnames = []
            options.listnames.append(arg.lower())

    # further options and argument processing
    if not options.modify:
        options.quiet = 0

    if len(args) <> 2:
        usage(1)
    fromaddr = args[0]
    toaddr = args[1]
        
    # validate and normalize the target address
    try:
        Utils.ValidateEmail(toaddr)
    except Errors.EmailAddressError:
        usage(1, C_('Not a valid email address: %(toaddr)s'))
    lfromaddr = fromaddr.lower()
    options.toaddr = toaddr
    options.fromaddr = fromaddr
    options.lfromaddr = lfromaddr

    if options.listnames is None:
        options.listnames = Utils.list_names()

    for listname in options.listnames:
        try:
            mlist = MailList.MailList(listname)
        except Errors.MMListError, e:
            print C_('Error opening list "%(listname)s", skipping.\n%(e)s')
            continue
        try:
            dolist(mlist, options)
        finally:
            mlist.Save()
            mlist.Unlock()


if __name__ == '__main__':
    main()