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
20
from bzrlib.lazy_import import lazy_import
21
lazy_import(globals(), '''
32
from bzrlib.util import simplemapi
36
class MailClient(object):
37
"""A mail client that can send messages with attachements."""
39
def __init__(self, config):
42
def compose(self, prompt, to, subject, attachment, mime_subtype,
44
raise NotImplementedError
46
def compose_merge_request(self, to, subject, directive):
47
prompt = self._get_merge_prompt("Please describe these changes:", to,
49
self.compose(prompt, to, subject, directive,
52
def _get_merge_prompt(self, prompt, to, subject, attachment):
56
class Editor(MailClient):
57
"""DIY mail client that uses commit message editor"""
59
def _get_merge_prompt(self, prompt, to, subject, attachment):
60
return "%s\n\nTo: %s\nSubject: %s\n\n%s" % (prompt, to, subject,
61
attachment.decode('utf-8', 'replace'))
63
def compose(self, prompt, to, subject, attachment, mime_subtype,
65
body = msgeditor.edit_commit_message(prompt)
67
raise errors.NoMessageSupplied()
68
email_message.EmailMessage.send(self.config,
69
self.config.username(),
74
attachment_mime_subtype=mime_subtype)
77
class ExternalMailClient(MailClient):
78
"""An external mail client."""
82
def compose(self, prompt, to, subject, attachment, mime_subtype,
84
fd, pathname = tempfile.mkstemp(extension, 'bzr-mail-')
86
os.write(fd, attachment)
89
self._compose(prompt, to, subject, pathname, mime_subtype, extension)
91
def _compose(self, prompt, to, subject, attach_path, mime_subtype,
93
for name in self._client_commands:
95
cmdline.extend(self._get_compose_commandline(to, subject,
98
subprocess.call(cmdline)
100
if e.errno != errno.ENOENT:
105
raise errors.MailClientNotFound(self._client_commands)
107
def _get_compose_commandline(self, to, subject, attach_path):
109
if subject is not None:
110
message_options['subject'] = subject
111
if attach_path is not None:
112
message_options['attach'] = attach_path
113
options_list = ['%s=%s' % (k, urlutils.escape(v)) for (k, v) in
114
message_options.iteritems()]
115
return ['mailto:%s?%s' % (to or '', '&'.join(options_list))]
118
class Evolution(ExternalMailClient):
119
"""Evolution mail client."""
121
_client_commands = ['evolution']
123
def _get_compose_commandline(self, to, subject, attach_path):
125
if subject is not None:
126
message_options['subject'] = subject
127
if attach_path is not None:
128
message_options['attach'] = attach_path
129
options_list = ['%s=%s' % (k, urlutils.escape(v)) for (k, v) in
130
message_options.iteritems()]
131
return ['mailto:%s?%s' % (to or '', '&'.join(options_list))]
134
class Thunderbird(ExternalMailClient):
135
"""Mozilla Thunderbird (or Icedove)
137
Note that Thunderbird 1.5 is buggy and does not support setting
138
"to" simultaneously with including a attachment.
140
There is a workaround if no attachment is present, but we always need to
144
_client_commands = ['thunderbird', 'mozilla-thunderbird', 'icedove']
146
def _get_compose_commandline(self, to, subject, attach_path):
149
message_options['to'] = to
150
if subject is not None:
151
message_options['subject'] = subject
152
if attach_path is not None:
153
message_options['attachment'] = urlutils.local_path_to_url(
155
options_list = ["%s='%s'" % (k, v) for k, v in
156
sorted(message_options.iteritems())]
157
return ['-compose', ','.join(options_list)]
160
class XDGEmail(ExternalMailClient):
161
"""xdg-email attempts to invoke the user's preferred mail client"""
163
_client_commands = ['xdg-email']
165
def _get_compose_commandline(self, to, subject, attach_path):
167
if subject is not None:
168
commandline.extend(['--subject', subject])
169
if attach_path is not None:
170
commandline.extend(['--attach', attach_path])
174
class MAPIClient(ExternalMailClient):
175
"""Default Windows mail client launched using MAPI."""
177
def _compose(self, prompt, to, subject, attach_path, mime_subtype,
179
simplemapi.SendMail(to or '', subject or '', '', attach_path)
182
class DefaultMail(MailClient):
183
"""Default mail handling. Tries XDGEmail (or MAPIClient on Windows),
184
falls back to Editor"""
186
def _mail_client(self):
187
if sys.platform == 'win32':
188
return MAPIClient(self.config)
190
return XDGEmail(self.config)
192
def compose(self, prompt, to, subject, attachment, mime_subtype,
195
return self._mail_client().compose(prompt, to, subject,
196
attachment, mimie_subtype,
198
except errors.MailClientNotFound:
199
return Editor(self.config).compose(prompt, to, subject,
200
attachment, mimie_subtype, extension)
202
def compose_merge_request(self, to, subject, directive):
204
return self._mail_client().compose_merge_request(to, subject,
206
except errors.MailClientNotFound:
207
return Editor(self.config).compose_merge_request(to, subject,