1
# Copyright (C) 2007 Canonical Ltd
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.
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.
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
32
class MailClient(object):
33
"""A mail client that can send messages with attachements."""
35
def __init__(self, config):
38
def compose(self, prompt, to, subject, attachment, mime_subtype,
40
raise NotImplementedError
42
def compose_merge_request(self, to, subject, directive):
43
prompt = self._get_merge_prompt("Please describe these changes:", to,
45
self.compose(prompt, to, subject, directive,
48
def _get_merge_prompt(self, prompt, to, subject, attachment):
52
class Editor(MailClient):
53
"""DIY mail client that uses commit message editor"""
55
def _get_merge_prompt(self, prompt, to, subject, attachment):
56
return "%s\n\nTo: %s\nSubject: %s\n\n%s" % (prompt, to, subject,
57
attachment.decode('utf-8', 'replace'))
59
def compose(self, prompt, to, subject, attachment, mime_subtype,
61
body = msgeditor.edit_commit_message(prompt)
63
raise errors.NoMessageSupplied()
64
email_message.EmailMessage.send(self.config,
65
self.config.username(),
70
attachment_mime_subtype=mime_subtype)
73
class ExternalMailClient(MailClient):
74
"""An external mail client."""
76
def compose(self, prompt, to, subject, attachment, mime_subtype,
78
fd, pathname = tempfile.mkstemp(extension, 'bzr-mail-')
80
os.write(fd, attachment)
83
self._compose(prompt, to, subject, pathname, mime_subtype, extension)
85
def _compose(self, prompt, to, subject, attach_path, mime_subtype,
87
for name in self._client_commands:
89
cmdline.extend(self._get_compose_commandline(to, subject,
92
subprocess.call(cmdline)
94
if e.errno != errno.ENOENT:
99
raise errors.MailClientNotFound(self._client_commands)
101
def _get_compose_commandline(self, to, subject, attach_path):
102
raise NotImplementedError
105
class Evolution(ExternalMailClient):
106
"""Evolution mail client."""
108
_client_commands = ['evolution']
110
def _get_compose_commandline(self, to, subject, attach_path):
112
if subject is not None:
113
message_options['subject'] = subject
114
if attach_path is not None:
115
message_options['attach'] = attach_path
116
options_list = ['%s=%s' % (k, urlutils.escape(v)) for (k, v) in
117
message_options.iteritems()]
118
return ['mailto:%s?%s' % (to or '', '&'.join(options_list))]
121
class Thunderbird(ExternalMailClient):
122
"""Mozilla Thunderbird (or Icedove)
124
Note that Thunderbird 1.5 is buggy and does not support setting
125
"to" simultaneously with including a attachment.
127
There is a workaround if no attachment is present, but we always need to
131
_client_commands = ['thunderbird', 'mozilla-thunderbird', 'icedove']
133
def _get_compose_commandline(self, to, subject, attach_path):
136
message_options['to'] = to
137
if subject is not None:
138
message_options['subject'] = subject
139
if attach_path is not None:
140
message_options['attachment'] = urlutils.local_path_to_url(
142
options_list = ["%s='%s'" % (k, v) for k, v in
143
sorted(message_options.iteritems())]
144
return ['-compose', ','.join(options_list)]
147
class XDGEmail(ExternalMailClient):
148
"""xdg-email attempts to invoke the user's preferred mail client"""
150
_client_commands = ['xdg-email']
152
def _get_compose_commandline(self, to, subject, attach_path):
154
if subject is not None:
155
commandline.extend(['--subject', subject])
156
if attach_path is not None:
157
commandline.extend(['--attach', attach_path])
161
class MAPIClient(ExternalMailClient):
162
"""Default Windows mail client launched using MAPI."""
164
def _compose(self, prompt, to, subject, attach_path, mime_subtype,
166
from bzrlib.util import simplemapi
168
simplemapi.SendMail(to or '', subject or '', '', attach_path)
169
except simplemapi.MAPIError, e:
170
if e.code != simplemapi.MAPI_USER_ABORT:
171
raise errors.MailClientNotFound(['MAPI supported mail client'
172
' (error %d)' % (e.code,)])
175
class DefaultMail(MailClient):
176
"""Default mail handling. Tries XDGEmail (or MAPIClient on Windows),
177
falls back to Editor"""
179
def _mail_client(self):
180
if osutils.supports_mapi():
181
return MAPIClient(self.config)
183
return XDGEmail(self.config)
185
def compose(self, prompt, to, subject, attachment, mime_subtype,
188
return self._mail_client().compose(prompt, to, subject,
189
attachment, mimie_subtype,
191
except errors.MailClientNotFound:
192
return Editor(self.config).compose(prompt, to, subject,
193
attachment, mimie_subtype, extension)
195
def compose_merge_request(self, to, subject, directive):
197
return self._mail_client().compose_merge_request(to, subject,
199
except errors.MailClientNotFound:
200
return Editor(self.config).compose_merge_request(to, subject,