summaryrefslogtreecommitdiff
path: root/distixlib/plugins/import_mail_plugin.py
blob: c8f2d127937fb26882bfee532128e70362e07e0e (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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# Copyright 2014  Lars Wirzenius
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
# =*= License: GPL-3+ =*=


import contextlib
import email
import mailbox
import os

import cliapp
import ttystatus

import distixlib


class WrongArguments(distixlib.StructuredError):

    msg = 'Wrong number of arguments: got {count}, wanted 2 or 3'


class NotKeywordArgument(distixlib.StructuredError):

    msg = '{arg} is not a keyword argumnent (KEY=VALUE)'


class ImportMailPlugin(cliapp.Plugin):

    commit_msg = 'imported mails'

    def enable(self):
        self.app.add_subcommand(
            'import-mail', self.import_mail,
            arg_synopsis='REPO FILE [KEY=VALUE]')

        self.app.add_subcommand(
            'import-mbox', self.import_mbox,
            arg_synopsis='REPO FILE [KEY=VALUE]')

        self.app.add_subcommand(
            'import-maildir', self.import_maildir,
            arg_synopsis='REPO MAILDIR')

    def import_mail(self, args):
        repo_dirname, mail_filename, keyvalue = self._parse_command_line(args)
        key, value = self._parse_keyvalue(keyvalue)
        msg = self._read_mail_message(mail_filename)

        context = _ImportContext()
        context.set_repo(distixlib.Repository(repo_dirname))
        context.repo.require_clean_working_tree()
        context.all_ticket_ids = context.store.get_ticket_ids()

        filenames = self._import_msg_to_ticket_store(
            context, msg, key, value)
        if filenames:
            context.repo.commit_changes(filenames, self.commit_msg)

    def import_mbox(self, args):
        self._import_folder(args, mailbox.mbox)

    def import_maildir(self, args):
        def maildir_factory(filename):
            return mailbox.Maildir(filename, factory=None)
        self._import_folder(args, maildir_factory)

    def _import_folder(self, args, folder_factory):
        repo_dirname, folder_filename, keyvalue = self._parse_command_line(
            args)
        key, value = self._parse_keyvalue(keyvalue)
        folder = folder_factory(folder_filename)

        context = _ImportContext()
        context.set_repo(distixlib.Repository(repo_dirname))
        context.repo.require_clean_working_tree()
        context.all_ticket_ids = context.store.get_ticket_ids()

        if self.app.settings['quiet']:
            progress = _QuietProgressReporter()
        else:
            progress = _MboxProgressReporter(len(folder))

        filenames = []
        with contextlib.closing(folder), progress:
            for msg in folder:
                progress.next_msg()
                filenames += self._import_msg_to_ticket_store(
                    context, msg, key, value)
        filenames += context.store.save_changes()
        if filenames:
            context.repo.commit_changes(filenames, self.commit_msg)

    def _parse_command_line(self, args):
        if len(args) == 2:
            return args[0], args[1], None
        elif len(args) == 3:
            if '=' not in args[2]:
                raise NotKeywordArgument(arg=args[2])
            return args[0], args[1], args[2]
        else:
            raise WrongArguments(count=len(args))

    def _parse_keyvalue(self, keyvalue):
        if keyvalue is None:
            return None, None
        return keyvalue.split('=', 1)

    def _read_mail_message(self, mail_filename):
        with open(mail_filename) as f:
            return email.message_from_file(f)

    def _import_msg_to_ticket_store(self, context, msg, key, value):

        referenced_ids = self._find_tickets_with_mails_referenced_by_msg(
            context, msg)
        msg_ids = distixlib.get_ids_from_message(msg)
        filenames = []

        if referenced_ids:
            for ticket_id in referenced_ids:
                if not self._contains_message(context.store, ticket_id, msg):
                    ticket = context.store.get_ticket(ticket_id)
                    ticket.add_message(msg)
                    self._set_key_value(ticket, key, value)
                    context.cache.add_msg_ids_for_ticket_id(
                        ticket.get_ticket_id(), msg_ids)
        else:
            if not self._is_already_imported(context, msg):
                new_ticket = self._create_ticket_from_msg(context.repo, msg)
                self._set_key_value(new_ticket, key, value)
                context.cache.add_msg_ids_for_ticket_id(
                    new_ticket.get_ticket_id(), msg_ids)
                context.all_ticket_ids.append(new_ticket.get_ticket_id())
                filenames = context.store.add_ticket(new_ticket)

        filenames += context.store.save_changes()
        return filenames

    def _set_key_value(self, ticket, key, value):
        if key is not None and value is not None:
            metadata = ticket.get_ticket_metadata()
            if key in metadata:
                metadata.remove_all_values(key)
            metadata.add(key, value)
            ticket.set_ticket_metadata(metadata)

    def _contains_message(self, store, ticket_id, msg):
        return store.ticket_has_message_with_text(
            ticket_id, msg.as_string())

    def _is_already_imported(self, context, msg):
        for ticket_id in context.all_ticket_ids:
            if self._contains_message(context.store, ticket_id, msg):
                return True
        return False

    def _find_tickets_with_mails_referenced_by_msg(self, context, msg):
        ticket_ids = []
        msg_ids = distixlib.get_ids_from_message(msg)

        for ticket_id in context.all_ticket_ids:
            if ticket_id in context.cache:
                other_ids = contex.cache.get_msg_ids_for_ticket_id(ticket_id)
            else:
                filenames = context.store.get_message_filenames(ticket_id)
                for filename in filenames:
                    with open(filename) as f:
                        other_msg = email.message_from_file(f)
                    other_ids = distixlib.get_ids_from_message(other_msg)
                    context.cache.add_msg_ids_for_ticket_id(
                        ticket_id, other_ids)
            if other_ids.intersection(msg_ids):
                ticket_ids.append(ticket_id)

        return ticket_ids

    def _create_ticket_from_msg(self, repo, msg):
        ticket_id = repo.invent_new_ticket_id()
        subject = self._get_header(msg, 'Subject')
        ticket = self._create_ticket(ticket_id, subject)
        ticket.add_message(msg)
        return ticket

    def _get_header(self, msg, name):
        decoded = email.header.decode_header(msg[name])
        combined = u' '.join(
            self._safe_decode(value, encoding)
            for value, encoding in decoded)
        return combined

    def _safe_decode(self, text, encoding):
        try:
            return text.decode(encoding or 'us-ascii')
        except LookupError:
            return repr(text)
        except UnicodeDecodeError:
            return repr(text)

    def _create_ticket(self, ticket_id, title):
        ticket = distixlib.Ticket()
        ticket.set_ticket_id(ticket_id)
        ticket.set_title(title)
        return ticket


class _ImportContext(object):

    def __init__(self):
        self.repo = None
        self.store = None
        self.all_ticket_ids = None
        self.cache = _MessageIdCache()

    def set_repo(self, repo):
        self.repo = repo
        self.store = self.repo.open_ticket_store(distixlib.tickets_dir_name)


class _MessageIdCache(object):

    def __init__(self):
        self._dict = {}

    def __contains__(self, ticket_id):
        return ticket_id in self._dict

    def get_msg_ids_for_ticket_id(self, ticket_id):
        return self._dict.get(ticket_id, set())

    def add_msg_ids_for_ticket_id(self, ticket_id, msg_ids):
        old = self.get_msg_ids_for_ticket_id(ticket_id)
        self._dict[ticket_id] = old.union(msg_ids)


class _MboxProgressReporter(object):

    def __init__(self, total):
        self._ts = ttystatus.TerminalStatus()
        self._ts.format(
            '%ElapsedTime() '
            'importing message %Integer(current) of %Integer(total) '
            '(%PercentDone(current,total))')
        self._ts['current'] = 0
        self._ts['total'] = total

    def next_msg(self):
        self._ts['current'] += 1

    def __enter__(self):
        return self

    def __exit__(self, *args):
        self._ts.clear()
        self._ts.finish()


class _QuietProgressReporter(object):

    def next_msg(self):
        pass

    def __enter__(self):
        pass

    def __exit__(self, *args):
        pass