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
34
mail_client_registry = registry.Registry()
37
class MailClient(object):
38
"""A mail client that can send messages with attachements."""
40
def __init__(self, config):
43
def compose(self, prompt, to, subject, attachment, mime_subtype,
44
extension, basename=None):
45
"""Compose (and possibly send) an email message
47
Must be implemented by subclasses.
49
:param prompt: A message to tell the user what to do. Supported by
50
the Editor client, but ignored by others
51
:param to: The address to send the message to
52
:param subject: The contents of the subject line
53
:param attachment: An email attachment, as a bytestring
54
:param mime_subtype: The attachment is assumed to be a subtype of
55
Text. This allows the precise subtype to be specified, e.g.
56
"plain", "x-patch", etc.
57
:param extension: The file extension associated with the attachment
59
:param basename: The name to use for the attachment, e.g.
62
raise NotImplementedError
64
def compose_merge_request(self, to, subject, directive, basename=None):
65
"""Compose (and possibly send) a merge request
67
:param to: The address to send the request to
68
:param subject: The subject line to use for the request
69
:param directive: A merge directive representing the merge request, as
71
:param basename: The name to use for the attachment, e.g.
74
prompt = self._get_merge_prompt("Please describe these changes:", to,
76
self.compose(prompt, to, subject, directive,
77
'x-patch', '.patch', basename)
79
def _get_merge_prompt(self, prompt, to, subject, attachment):
80
"""Generate a prompt string. Overridden by Editor.
82
:param prompt: A string suggesting what user should do
83
:param to: The address the mail will be sent to
84
:param subject: The subject line of the mail
85
:param attachment: The attachment that will be used
90
class Editor(MailClient):
91
"""DIY mail client that uses commit message editor"""
93
def _get_merge_prompt(self, prompt, to, subject, attachment):
94
"""See MailClient._get_merge_prompt"""
98
u"%s" % (prompt, to, subject,
99
attachment.decode('utf-8', 'replace')))
101
def compose(self, prompt, to, subject, attachment, mime_subtype,
102
extension, basename=None):
103
"""See MailClient.compose"""
105
raise errors.NoMailAddressSpecified()
106
body = msgeditor.edit_commit_message(prompt)
108
raise errors.NoMessageSupplied()
109
email_message.EmailMessage.send(self.config,
110
self.config.username(),
115
attachment_mime_subtype=mime_subtype)
116
mail_client_registry.register('editor', Editor,
120
class ExternalMailClient(MailClient):
121
"""An external mail client."""
123
def _get_client_commands(self):
124
"""Provide a list of commands that may invoke the mail client"""
125
if sys.platform == 'win32':
127
return [win32utils.get_app_path(i) for i in self._client_commands]
129
return self._client_commands
131
def compose(self, prompt, to, subject, attachment, mime_subtype,
132
extension, basename=None):
133
"""See MailClient.compose.
135
Writes the attachment to a temporary file, invokes _compose.
138
basename = 'attachment'
139
pathname = osutils.mkdtemp(prefix='bzr-mail-')
140
attach_path = osutils.pathjoin(pathname, basename + extension)
141
outfile = open(attach_path, 'wb')
143
outfile.write(attachment)
146
self._compose(prompt, to, subject, attach_path, mime_subtype,
149
def _compose(self, prompt, to, subject, attach_path, mime_subtype,
151
"""Invoke a mail client as a commandline process.
153
Overridden by MAPIClient.
154
:param to: The address to send the mail to
155
:param subject: The subject line for the mail
156
:param pathname: The path to the attachment
157
:param mime_subtype: The attachment is assumed to have a major type of
158
"text", but the precise subtype can be specified here
159
:param extension: A file extension (including period) associated with
162
for name in self._get_client_commands():
163
cmdline = [self._encode_path(name, 'executable')]
164
cmdline.extend(self._get_compose_commandline(to, subject,
167
subprocess.call(cmdline)
169
if e.errno != errno.ENOENT:
174
raise errors.MailClientNotFound(self._client_commands)
176
def _get_compose_commandline(self, to, subject, attach_path):
177
"""Determine the commandline to use for composing a message
179
Implemented by various subclasses
180
:param to: The address to send the mail to
181
:param subject: The subject line for the mail
182
:param attach_path: The path to the attachment
184
raise NotImplementedError
186
def _encode_safe(self, u):
187
"""Encode possible unicode string argument to 8-bit string
188
in user_encoding. Unencodable characters will be replaced
191
:param u: possible unicode string.
192
:return: encoded string if u is unicode, u itself otherwise.
194
if isinstance(u, unicode):
195
return u.encode(osutils.get_user_encoding(), 'replace')
198
def _encode_path(self, path, kind):
199
"""Encode unicode path in user encoding.
201
:param path: possible unicode path.
202
:param kind: path kind ('executable' or 'attachment').
203
:return: encoded path if path is unicode,
204
path itself otherwise.
205
:raise: UnableEncodePath.
207
if isinstance(path, unicode):
209
return path.encode(osutils.get_user_encoding())
210
except UnicodeEncodeError:
211
raise errors.UnableEncodePath(path, kind)
215
class Evolution(ExternalMailClient):
216
"""Evolution mail client."""
218
_client_commands = ['evolution']
220
def _get_compose_commandline(self, to, subject, attach_path):
221
"""See ExternalMailClient._get_compose_commandline"""
223
if subject is not None:
224
message_options['subject'] = subject
225
if attach_path is not None:
226
message_options['attach'] = attach_path
227
options_list = ['%s=%s' % (k, urlutils.escape(v)) for (k, v) in
228
sorted(message_options.iteritems())]
229
return ['mailto:%s?%s' % (self._encode_safe(to or ''),
230
'&'.join(options_list))]
231
mail_client_registry.register('evolution', Evolution,
232
help=Evolution.__doc__)
235
class Mutt(ExternalMailClient):
236
"""Mutt mail client."""
238
_client_commands = ['mutt']
240
def _get_compose_commandline(self, to, subject, attach_path):
241
"""See ExternalMailClient._get_compose_commandline"""
243
if subject is not None:
244
message_options.extend(['-s', self._encode_safe(subject)])
245
if attach_path is not None:
246
message_options.extend(['-a',
247
self._encode_path(attach_path, 'attachment')])
249
message_options.append(self._encode_safe(to))
250
return message_options
251
mail_client_registry.register('mutt', Mutt,
255
class Thunderbird(ExternalMailClient):
256
"""Mozilla Thunderbird (or Icedove)
258
Note that Thunderbird 1.5 is buggy and does not support setting
259
"to" simultaneously with including a attachment.
261
There is a workaround if no attachment is present, but we always need to
265
_client_commands = ['thunderbird', 'mozilla-thunderbird', 'icedove',
266
'/Applications/Mozilla/Thunderbird.app/Contents/MacOS/thunderbird-bin',
267
'/Applications/Thunderbird.app/Contents/MacOS/thunderbird-bin']
269
def _get_compose_commandline(self, to, subject, attach_path):
270
"""See ExternalMailClient._get_compose_commandline"""
273
message_options['to'] = self._encode_safe(to)
274
if subject is not None:
275
message_options['subject'] = self._encode_safe(subject)
276
if attach_path is not None:
277
message_options['attachment'] = urlutils.local_path_to_url(
279
options_list = ["%s='%s'" % (k, v) for k, v in
280
sorted(message_options.iteritems())]
281
return ['-compose', ','.join(options_list)]
282
mail_client_registry.register('thunderbird', Thunderbird,
283
help=Thunderbird.__doc__)
286
class KMail(ExternalMailClient):
287
"""KDE mail client."""
289
_client_commands = ['kmail']
291
def _get_compose_commandline(self, to, subject, attach_path):
292
"""See ExternalMailClient._get_compose_commandline"""
294
if subject is not None:
295
message_options.extend(['-s', self._encode_safe(subject)])
296
if attach_path is not None:
297
message_options.extend(['--attach',
298
self._encode_path(attach_path, 'attachment')])
300
message_options.extend([self._encode_safe(to)])
301
return message_options
302
mail_client_registry.register('kmail', KMail,
306
class Claws(ExternalMailClient):
307
"""Claws mail client."""
309
_client_commands = ['claws-mail']
311
def _get_compose_commandline(self, to, subject, attach_path):
312
"""See ExternalMailClient._get_compose_commandline"""
313
compose_url = ['mailto:']
315
compose_url.append(self._encode_safe(to))
316
compose_url.append('?')
317
if subject is not None:
318
# Don't use urllib.quote_plus because Claws doesn't seem
319
# to recognise spaces encoded as "+".
321
'subject=%s' % urllib.quote(self._encode_safe(subject)))
322
# Collect command-line options.
323
message_options = ['--compose', ''.join(compose_url)]
324
if attach_path is not None:
325
message_options.extend(
326
['--attach', self._encode_path(attach_path, 'attachment')])
327
return message_options
328
mail_client_registry.register('claws', Claws,
332
class XDGEmail(ExternalMailClient):
333
"""xdg-email attempts to invoke the user's preferred mail client"""
335
_client_commands = ['xdg-email']
337
def _get_compose_commandline(self, to, subject, attach_path):
338
"""See ExternalMailClient._get_compose_commandline"""
340
raise errors.NoMailAddressSpecified()
341
commandline = [self._encode_safe(to)]
342
if subject is not None:
343
commandline.extend(['--subject', self._encode_safe(subject)])
344
if attach_path is not None:
345
commandline.extend(['--attach',
346
self._encode_path(attach_path, 'attachment')])
348
mail_client_registry.register('xdg-email', XDGEmail,
349
help=XDGEmail.__doc__)
352
class EmacsMail(ExternalMailClient):
353
"""Call emacsclient to have a mail buffer.
355
This only work for emacs >= 22.1 due to recent -e/--eval support.
357
The good news is that this implementation will work with all mail
358
agents registered against ``mail-user-agent``. So there is no need
359
to instantiate ExternalMailClient for each and every GNU Emacs
362
Users just have to ensure that ``mail-user-agent`` is set according
366
_client_commands = ['emacsclient']
368
def _prepare_send_function(self):
369
"""Write our wrapper function into a temporary file.
371
This temporary file will be loaded at runtime in
372
_get_compose_commandline function.
374
This function does not remove the file. That's a wanted
375
behaviour since _get_compose_commandline won't run the send
376
mail function directly but return the eligible command line.
377
Removing our temporary file here would prevent our sendmail
378
function to work. (The file is deleted by some elisp code
379
after being read by Emacs.)
382
_defun = r"""(defun bzr-add-mime-att (file)
383
"Attach FILE to a mail buffer as a MIME attachment."
384
(let ((agent mail-user-agent))
385
(if (and file (file-exists-p file))
387
((eq agent 'sendmail-user-agent)
391
(if (functionp 'etach-attach)
393
(mail-attach-file file))))
394
((or (eq agent 'message-user-agent)
395
(eq agent 'gnus-user-agent)
396
(eq agent 'mh-e-user-agent))
398
(mml-attach-file file "text/x-patch" "BZR merge" "inline")))
399
((eq agent 'mew-user-agent)
401
(mew-draft-prepare-attachments)
402
(mew-attach-link file (file-name-nondirectory file))
403
(let* ((nums (mew-syntax-nums))
404
(syntax (mew-syntax-get-entry mew-encode-syntax nums)))
405
(mew-syntax-set-cd syntax "BZR merge")
406
(mew-encode-syntax-print mew-encode-syntax))
407
(mew-header-goto-body)))
409
(message "Unhandled MUA, report it on bazaar@lists.canonical.com")))
410
(error "File %s does not exist." file))))
413
fd, temp_file = tempfile.mkstemp(prefix="emacs-bzr-send-",
418
os.close(fd) # Just close the handle but do not remove the file.
421
def _get_compose_commandline(self, to, subject, attach_path):
422
commandline = ["--eval"]
428
_to = ("\"%s\"" % self._encode_safe(to).replace('"', '\\"'))
429
if subject is not None:
430
_subject = ("\"%s\"" %
431
self._encode_safe(subject).replace('"', '\\"'))
433
# Funcall the default mail composition function
434
# This will work with any mail mode including default mail-mode
435
# User must tweak mail-user-agent variable to tell what function
436
# will be called inside compose-mail.
437
mail_cmd = "(compose-mail %s %s)" % (_to, _subject)
438
commandline.append(mail_cmd)
440
# Try to attach a MIME attachment using our wrapper function
441
if attach_path is not None:
442
# Do not create a file if there is no attachment
443
elisp = self._prepare_send_function()
444
lmmform = '(load "%s")' % elisp
445
mmform = '(bzr-add-mime-att "%s")' % \
446
self._encode_path(attach_path, 'attachment')
447
rmform = '(delete-file "%s")' % elisp
448
commandline.append(lmmform)
449
commandline.append(mmform)
450
commandline.append(rmform)
453
mail_client_registry.register('emacsclient', EmacsMail,
454
help=EmacsMail.__doc__)
457
class MAPIClient(ExternalMailClient):
458
"""Default Windows mail client launched using MAPI."""
460
def _compose(self, prompt, to, subject, attach_path, mime_subtype,
462
"""See ExternalMailClient._compose.
464
This implementation uses MAPI via the simplemapi ctypes wrapper
466
from bzrlib.util import simplemapi
468
simplemapi.SendMail(to or '', subject or '', '', attach_path)
469
except simplemapi.MAPIError, e:
470
if e.code != simplemapi.MAPI_USER_ABORT:
471
raise errors.MailClientNotFound(['MAPI supported mail client'
472
' (error %d)' % (e.code,)])
473
mail_client_registry.register('mapi', MAPIClient,
474
help=MAPIClient.__doc__)
477
class DefaultMail(MailClient):
478
"""Default mail handling. Tries XDGEmail (or MAPIClient on Windows),
479
falls back to Editor"""
481
def _mail_client(self):
482
"""Determine the preferred mail client for this platform"""
483
if osutils.supports_mapi():
484
return MAPIClient(self.config)
486
return XDGEmail(self.config)
488
def compose(self, prompt, to, subject, attachment, mime_subtype,
489
extension, basename=None):
490
"""See MailClient.compose"""
492
return self._mail_client().compose(prompt, to, subject,
493
attachment, mimie_subtype,
495
except errors.MailClientNotFound:
496
return Editor(self.config).compose(prompt, to, subject,
497
attachment, mimie_subtype, extension)
499
def compose_merge_request(self, to, subject, directive, basename=None):
500
"""See MailClient.compose_merge_request"""
502
return self._mail_client().compose_merge_request(to, subject,
503
directive, basename=basename)
504
except errors.MailClientNotFound:
505
return Editor(self.config).compose_merge_request(to, subject,
506
directive, basename=basename)
507
mail_client_registry.register('default', DefaultMail,
508
help=DefaultMail.__doc__)
509
mail_client_registry.default_key = 'default'