summaryrefslogtreecommitdiff
path: root/mailman/app/lifecycle.py
blob: 5401c1e1201174956a13eda3e529ef3fbb8a16ce (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Copyright (C) 2007-2009 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman 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 3 of the License, or (at your option)
# any later version.
#
# GNU Mailman 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
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.

"""Application level list creation."""

__metaclass__ = type
__all__ = [
    'create_list',
    'remove_list',
    ]


import os
import sys
import shutil
import logging

from mailman import Utils
from mailman.Utils import ValidateEmail
from mailman.config import config
from mailman.core import errors
from mailman.core.plugins import get_plugin
from mailman.core.styles import style_manager
from mailman.interfaces import MemberRole


log = logging.getLogger('mailman.error')



def create_list(fqdn_listname, owners=None):
    """Create the named list and apply styles."""
    if owners is None:
        owners = []
    ValidateEmail(fqdn_listname)
    listname, domain = Utils.split_listname(fqdn_listname)
    if domain not in config.domains:
        raise errors.BadDomainSpecificationError(domain)
    mlist = config.db.list_manager.create(fqdn_listname)
    for style in style_manager.lookup(mlist):
        # XXX FIXME.  When we get rid of the wrapper object, this hack won't
        # be necessary.  Until then, setattr on the MailList instance won't
        # set the database column values, so pass the underlying database
        # object to .apply() instead.
        style.apply(mlist)
    # Coordinate with the MTA, which should be defined by plugins.
    # XXX FIXME
##     mta_plugin = get_plugin('mailman.mta')
##     mta_plugin().create(mlist)
    # Create any owners that don't yet exist, and subscribe all addresses as
    # owners of the mailing list.
    usermgr = config.db.user_manager
    for owner_address in owners:
        addr = usermgr.get_address(owner_address)
        if addr is None:
            # XXX Make this use an IRegistrar instead, but that requires
            # sussing out the IDomain stuff.  For now, fake it.
            user = usermgr.create_user(owner_address)
            addr = list(user.addresses)[0]
        addr.subscribe(mlist, MemberRole.owner)
    return mlist



def remove_list(fqdn_listname, mailing_list=None, archives=True):
    """Remove the list and all associated artifacts and subscriptions."""
    removeables = []
    # mailing_list will be None when only residual archives are being removed.
    if mailing_list:
        # Remove all subscriptions, regardless of role.
        for member in mailing_list.subscribers.members:
            member.unsubscribe()
        # Delete the mailing list from the database.
        config.db.list_manager.delete(mailing_list)
        # Do the MTA-specific list deletion tasks
##         if config.MTA:
##             modname = 'mailman.MTA.' + config.MTA
##             __import__(modname)
##             sys.modules[modname].remove(mailing_list)
        # Remove the list directory.
        removeables.append(os.path.join(config.LIST_DATA_DIR, fqdn_listname))
    # Remove any stale locks associated with the list.
    for filename in os.listdir(config.LOCK_DIR):
        fn_listname = filename.split('.')[0]
        if fn_listname == fqdn_listname:
            removeables.append(os.path.join(config.LOCK_DIR, filename))
    if archives:
        private_dir = config.PRIVATE_ARCHIVE_FILE_DIR
        public_dir  = config.PUBLIC_ARCHIVE_FILE_DIR
        removeables.extend([
            os.path.join(private_dir, fqdn_listname),
            os.path.join(private_dir, fqdn_listname + '.mbox'),
            os.path.join(public_dir, fqdn_listname),
            os.path.join(public_dir, fqdn_listname + '.mbox'),
            ])
    # Now that we know what files and directories to delete, delete them.
    for target in removeables:
        if os.path.islink(target):
            os.unlink(target)
        elif os.path.isdir(target):
            shutil.rmtree(target)
        elif os.path.isfile(target):
            os.unlink(target)
        else:
            log.error('Could not delete list artifact: %s', target)