summaryrefslogtreecommitdiff
path: root/src/mailman/archiving/mhonarc.py
blob: 286d489f3844266edc15511434b4c645603ef1d3 (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
# Copyright (C) 2008-2017 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/>.

"""MHonArc archiver."""

import logging

from mailman.config import config
from mailman.config.config import external_configuration
from mailman.interfaces.archiver import IArchiver
from mailman.utilities.string import expand
from public import public
from subprocess import PIPE, Popen
from urllib.parse import urljoin
from zope.interface import implementer


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


@public
@implementer(IArchiver)
class MHonArc:
    """Local MHonArc archiver."""

    name = 'mhonarc'
    is_enabled = False

    def __init__(self):
        # Read our specific configuration file
        archiver_config = external_configuration(
            config.archiver.mhonarc.configuration)
        self.base_url = archiver_config.get('general', 'base_url')
        self.command = archiver_config.get('general', 'command')

    def list_url(self, mlist):
        """See `IArchiver`."""
        # XXX What about private MHonArc archives?
        return expand(self.base_url, mlist, dict(
            # For backward compatibility.
            hostname=mlist.domain.mail_host,
            fqdn_listname=mlist.fqdn_listname,
            ))

    def permalink(self, mlist, msg):
        """See `IArchiver`."""
        # XXX What about private MHonArc archives?
        #
        # It is the LMTP server's responsibility to ensure that the message has
        # a Message-ID-Hash header.  For backward compatibility, fall back to
        # X-Message-ID-Hash.  If the message has neither, then there's no
        # permalink.
        message_id_hash = msg.get('message-id-hash')
        if message_id_hash is None:
            message_id_hash = msg.get('x-message-id-hash')
        if message_id_hash is None:
            return None
        if isinstance(message_id_hash, bytes):
            message_id_hash = message_id_hash.decode('ascii')
        return urljoin(self.list_url(mlist), message_id_hash)

    def archive_message(self, mlist, msg):
        """See `IArchiver`."""
        substitutions = config.__dict__.copy()
        substitutions['listname'] = mlist.fqdn_listname
        command = expand(self.command, mlist, substitutions)
        proc = Popen(
            command,
            stdin=PIPE, stdout=PIPE, stderr=PIPE,
            universal_newlines=True, shell=True)
        stdout, stderr = proc.communicate(msg.as_string())
        if proc.returncode != 0:
            log.error('%s: mhonarc subprocess had non-zero exit code: %s' %
                      (msg['message-id'], proc.returncode))
        log.info(stdout)
        log.error(stderr)
        # Can we get more information, such as the url to the message just
        # archived, out of MHonArc?
        return None