~bzr-pqm/bzr/bzr.dev

2681.1.8 by Aaron Bentley
Add Thunderbird support to bzr send
1
# Copyright (C) 2007 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
import os
18
import subprocess
19
import tempfile
20
2681.1.9 by Aaron Bentley
Add support for mail-from-editor
21
from bzrlib import (
22
    email_message,
23
    errors,
24
    msgeditor,
25
    urlutils,
26
    )
2681.1.8 by Aaron Bentley
Add Thunderbird support to bzr send
27
28
29
class MailClient(object):
30
2681.1.9 by Aaron Bentley
Add support for mail-from-editor
31
    def __init__(self, config):
32
        self.config = config
33
2681.1.8 by Aaron Bentley
Add Thunderbird support to bzr send
34
    def compose(self, to, subject, attachment):
35
        raise NotImplementedError
36
37
38
class Editor(MailClient):
39
2681.1.9 by Aaron Bentley
Add support for mail-from-editor
40
    def compose(self, to, subject, attachment):
41
        info = ("Please describe these changes:\n\nTo: %s\nSubject: %s\n\n%s"
42
                % (to, subject, attachment))
43
        body = msgeditor.edit_commit_message(info)
44
        if body == '':
45
            raise errors.NoMessageSupplied()
46
        email_message.EmailMessage.send(self.config,
47
                                        self.config.username(),
48
                                        to,
49
                                        subject,
50
                                        body,
51
                                        attachment,
52
                                        attachment_mime_subtype='x-patch')
2681.1.8 by Aaron Bentley
Add Thunderbird support to bzr send
53
54
55
class Thunderbird(MailClient):
56
57
    def compose(self, to, subject, attachment):
58
        fd, pathname = tempfile.mkstemp('.patch', 'bzr-mail-')
59
        try:
60
            os.write(fd, attachment)
61
        finally:
62
            os.close(fd)
63
        cmdline = ['thunderbird']
64
        cmdline.extend(self._get_compose_commandline(to, subject, pathname))
65
        subprocess.call(cmdline)
66
67
    def _get_compose_commandline(self, to, subject, attach_path):
68
        message_options = {}
69
        if to is not None:
70
            message_options['to'] = to
71
        if subject is not None:
72
            message_options['subject'] = subject
73
        if attach_path is not None:
74
            message_options['attachment'] = urlutils.local_path_to_url(
75
                attach_path)
76
        options_list = ["%s='%s'" % (k, v) for k, v in
77
                        sorted(message_options.iteritems())]
78
        return ['-compose', ','.join(options_list)]