1# Copyright (C) 2001-2018 by the Free Software Foundation, Inc.
2#
3# This program is free software; you can redistribute it and/or
4# modify it under the terms of the GNU General Public License
5# as published by the Free Software Foundation; either version 2
6# of the License, or (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software
15# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
16# USA.
17
18"""Creation/deletion hooks for manual /etc/aliases files."""
19
20import sys
21import email.Utils
22from cStringIO import StringIO
23
24from Mailman import mm_cfg
25from Mailman import Message
26from Mailman import Utils
27from Mailman.Queue.sbcache import get_switchboard
28from Mailman.i18n import _, C_
29from Mailman.MTA.Utils import makealiases
30
31try:
32    True, False
33except NameError:
34    True = 1
35    False = 0
36
37
38
39# no-ops for interface compliance
40def makelock():
41    class Dummy:
42        def lock(self):
43            pass
44        def unlock(self, unconditionally=False):
45            pass
46    return Dummy()
47
48
49def clear():
50    pass
51
52
53
54# nolock argument is ignored, but exists for interface compliance
55def create(mlist, cgi=False, nolock=False, quiet=False):
56    if mlist is None:
57        return
58    listname = mlist.internal_name()
59    fieldsz = len(listname) + len('-unsubscribe')
60    if cgi:
61        # If a list is being created via the CGI, the best we can do is send
62        # an email message to mailman-owner requesting that the proper aliases
63        # be installed.
64        sfp = StringIO()
65        if not quiet:
66            print >> sfp, _("""\
67The mailing list `%(listname)s' has been created via the through-the-web
68interface.  In order to complete the activation of this mailing list, the
69proper /etc/aliases (or equivalent) file must be updated.  The program
70`newaliases' may also have to be run.
71
72Here are the entries for the /etc/aliases file:
73""")
74        outfp = sfp
75    else:
76        if not quiet:
77            print C_("""\
78To finish creating your mailing list, you must edit your /etc/aliases (or
79equivalent) file by adding the following lines, and possibly running the
80`newaliases' program:
81""")
82        print C_("""\
83## %(listname)s mailing list""")
84        outfp = sys.stdout
85    # Common path
86    for k, v in makealiases(listname):
87        print >> outfp, k + ':', ((fieldsz - len(k)) * ' '), v
88    # If we're using the command line interface, we're done.  For ttw, we need
89    # to actually send the message to mailman-owner now.
90    if not cgi:
91        print >> outfp
92        return
93    # Send the message to the site -owner so someone can do something about
94    # this request.
95    siteowner = Utils.get_site_email(extra='owner')
96    # Should this be sent in the site list's preferred language?
97    msg = Message.UserNotification(
98        siteowner, siteowner,
99        _('Mailing list creation request for list %(listname)s'),
100        sfp.getvalue(), mm_cfg.DEFAULT_SERVER_LANGUAGE)
101    msg.send(mlist)
102
103
104
105def remove(mlist, cgi=False):
106    listname = mlist.internal_name()
107    fieldsz = len(listname) + len('-unsubscribe')
108    if cgi:
109        # If a list is being removed via the CGI, the best we can do is send
110        # an email message to mailman-owner requesting that the appropriate
111        # aliases be deleted.
112        sfp = StringIO()
113        print >> sfp, _("""\
114The mailing list `%(listname)s' has been removed via the through-the-web
115interface.  In order to complete the de-activation of this mailing list, the
116appropriate /etc/aliases (or equivalent) file must be updated.  The program
117`newaliases' may also have to be run.
118
119Here are the entries in the /etc/aliases file that should be removed:
120""")
121        outfp = sfp
122    else:
123        print C_("""
124To finish removing your mailing list, you must edit your /etc/aliases (or
125equivalent) file by removing the following lines, and possibly running the
126`newaliases' program:
127
128## %(listname)s mailing list""")
129        outfp = sys.stdout
130    # Common path
131    for k, v in makealiases(listname):
132        print >> outfp, k + ':', ((fieldsz - len(k)) * ' '), v
133    # If we're using the command line interface, we're done.  For ttw, we need
134    # to actually send the message to mailman-owner now.
135    if not cgi:
136        print >> outfp
137        return
138    siteowner = Utils.get_site_email(extra='owner')
139    # Should this be sent in the site list's preferred language?
140    msg = Message.UserNotification(
141        siteowner, siteowner,
142        _('Mailing list removal request for list %(listname)s'),
143        sfp.getvalue(), mm_cfg.DEFAULT_SERVER_LANGUAGE)
144    msg['Date'] = email.Utils.formatdate(localtime=1)
145    outq = get_switchboard(mm_cfg.OUTQUEUE_DIR)
146    outq.enqueue(msg, recips=[siteowner], nodecorate=1)
147