318
304
commandline.extend(['--attach',
319
305
self._encode_path(attach_path, 'attachment')])
320
306
return commandline
321
mail_client_registry.register('xdg-email', XDGEmail,
322
help=XDGEmail.__doc__)
325
class EmacsMail(ExternalMailClient):
326
"""Call emacsclient to have a mail buffer.
328
This only work for emacs >= 22.1 due to recent -e/--eval support.
330
The good news is that this implementation will work with all mail
331
agents registered against ``mail-user-agent``. So there is no need
332
to instantiate ExternalMailClient for each and every GNU Emacs
335
Users just have to ensure that ``mail-user-agent`` is set according
339
_client_commands = ['emacsclient']
341
def _prepare_send_function(self):
342
"""Write our wrapper function into a temporary file.
344
This temporary file will be loaded at runtime in
345
_get_compose_commandline function.
347
This function does not remove the file. That's a wanted
348
behaviour since _get_compose_commandline won't run the send
349
mail function directly but return the eligible command line.
350
Removing our temporary file here would prevent our sendmail
351
function to work. (The file is deleted by some elisp code
352
after being read by Emacs.)
355
_defun = r"""(defun bzr-add-mime-att (file)
356
"Attach FILE to a mail buffer as a MIME attachment."
357
(let ((agent mail-user-agent))
358
(if (and file (file-exists-p file))
360
((eq agent 'sendmail-user-agent)
364
(if (functionp 'etach-attach)
366
(mail-attach-file file))))
367
((or (eq agent 'message-user-agent)(eq agent 'gnus-user-agent))
369
(mml-attach-file file "text/x-patch" "BZR merge" "inline")))
370
((eq agent 'mew-user-agent)
372
(mew-draft-prepare-attachments)
373
(mew-attach-link file (file-name-nondirectory file))
374
(let* ((nums (mew-syntax-nums))
375
(syntax (mew-syntax-get-entry mew-encode-syntax nums)))
376
(mew-syntax-set-cd syntax "BZR merge")
377
(mew-encode-syntax-print mew-encode-syntax))
378
(mew-header-goto-body)))
380
(message "Unhandled MUA, report it on bazaar@lists.canonical.com")))
381
(error "File %s does not exist." file))))
384
fd, temp_file = tempfile.mkstemp(prefix="emacs-bzr-send-",
389
os.close(fd) # Just close the handle but do not remove the file.
392
def _get_compose_commandline(self, to, subject, attach_path):
393
commandline = ["--eval"]
399
_to = ("\"%s\"" % self._encode_safe(to).replace('"', '\\"'))
400
if subject is not None:
401
_subject = ("\"%s\"" %
402
self._encode_safe(subject).replace('"', '\\"'))
404
# Funcall the default mail composition function
405
# This will work with any mail mode including default mail-mode
406
# User must tweak mail-user-agent variable to tell what function
407
# will be called inside compose-mail.
408
mail_cmd = "(compose-mail %s %s)" % (_to, _subject)
409
commandline.append(mail_cmd)
411
# Try to attach a MIME attachment using our wrapper function
412
if attach_path is not None:
413
# Do not create a file if there is no attachment
414
elisp = self._prepare_send_function()
415
lmmform = '(load "%s")' % elisp
416
mmform = '(bzr-add-mime-att "%s")' % \
417
self._encode_path(attach_path, 'attachment')
418
rmform = '(delete-file "%s")' % elisp
419
commandline.append(lmmform)
420
commandline.append(mmform)
421
commandline.append(rmform)
424
mail_client_registry.register('emacsclient', EmacsMail,
425
help=EmacsMail.__doc__)
428
309
class MAPIClient(ExternalMailClient):