~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/mail_client.py

Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
306
306
        return commandline
307
307
 
308
308
 
309
 
class EmacsMail(ExternalMailClient):
310
 
    """Call emacsclient to have a mail buffer.
311
 
 
312
 
    This only work for emacs >= 22.1 due to recent -e/--eval support.
313
 
 
314
 
    The good news is that this implementation will work with all mail
315
 
    agents registered against ``mail-user-agent``. So there is no need
316
 
    to instantiate ExternalMailClient for each and every GNU Emacs
317
 
    MUA.
318
 
 
319
 
    Users just have to ensure that ``mail-user-agent`` is set according
320
 
    to their tastes.
321
 
    """
322
 
 
323
 
    _client_commands = ['emacsclient']
324
 
 
325
 
    def _prepare_send_function(self):
326
 
        """Write our wrapper function into a temporary file.
327
 
 
328
 
        This temporary file will be loaded at runtime in
329
 
        _get_compose_commandline function.
330
 
 
331
 
        FIXME: this function does not remove the file. That's a wanted
332
 
        behaviour since _get_compose_commandline won't run the send
333
 
        mail function directly but return the eligible command line.
334
 
        Removing our temporary file here would prevent our sendmail
335
 
        function to work.
336
 
 
337
 
        A possible workaround could be to load the function here with
338
 
        emacsclient --eval '(load temp)' but this is not robust since
339
 
        emacs could have been stopped between here and the call to
340
 
        mail client.
341
 
        """
342
 
 
343
 
        _defun = r"""(defun bzr-add-mime-att (file)
344
 
  "Attach FILe to a mail buffer as a MIME attachment."
345
 
  (let ((agent mail-user-agent))
346
 
    (mail-text)
347
 
    (newline)
348
 
    (if (and file (file-exists-p file))
349
 
        (cond
350
 
         ((eq agent 'sendmail-user-agent)
351
 
          (etach-attach file))
352
 
         ((or (eq agent 'message-user-agent)(eq agent 'gnus-user-agent))
353
 
          (mml-attach-file file "text/x-patch" "BZR merge" "attachment"))
354
 
         (t
355
 
          (message "Unhandled MUA")))
356
 
      (error "File %s does not exist." file))))
357
 
"""
358
 
 
359
 
        fd, temp_file = tempfile.mkstemp(prefix="emacs-bzr-send-",
360
 
                                         suffix=".el")
361
 
        try:
362
 
            os.write(fd, _defun)
363
 
        finally:
364
 
            os.close(fd) # Just close the handle but do not remove the file.
365
 
        return temp_file
366
 
 
367
 
    def _get_compose_commandline(self, to, subject, attach_path):
368
 
        commandline = ["--eval"]
369
 
 
370
 
        _to = "nil"
371
 
        _subject = "nil"
372
 
 
373
 
        if to is not None:
374
 
            _to = ("\"%s\"" % self._encode_safe(to))
375
 
        if subject is not None:
376
 
            _subject = ("\"%s\"" % self._encode_safe(subject))
377
 
 
378
 
        # Funcall the default mail composition function
379
 
        # This will work with any mail mode including default mail-mode
380
 
        # User must tweak mail-user-agent variable to tell what function
381
 
        # will be called inside compose-mail.
382
 
        mail_cmd = "(compose-mail %s %s)" % (_to, _subject)
383
 
        commandline.append(mail_cmd)
384
 
 
385
 
        # Try to attach a MIME attachment using our wrapper function
386
 
        if attach_path is not None:
387
 
            # Do not create a file if there is no attachment
388
 
            lmmform = '(load "%s")' % self._prepare_send_function()
389
 
            mmform  = '(bzr-add-mime-att "%s")' % \
390
 
                self._encode_path(attach_path, 'attachment')
391
 
            commandline.append(lmmform)
392
 
            commandline.append(mmform)
393
 
 
394
 
        return commandline
395
 
 
396
 
 
397
309
class MAPIClient(ExternalMailClient):
398
310
    """Default Windows mail client launched using MAPI."""
399
311
 
434
346
            return Editor(self.config).compose(prompt, to, subject,
435
347
                          attachment, mimie_subtype, extension)
436
348
 
437
 
    def compose_merge_request(self, to, subject, directive, basename=None):
 
349
    def compose_merge_request(self, to, subject, directive):
438
350
        """See MailClient.compose_merge_request"""
439
351
        try:
440
352
            return self._mail_client().compose_merge_request(to, subject,
441
 
                    directive, basename=basename)
 
353
                                                             directive)
442
354
        except errors.MailClientNotFound:
443
355
            return Editor(self.config).compose_merge_request(to, subject,
444
 
                          directive, basename=basename)
 
356
                          directive)