/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/mail_client.py

  • Committer: Aaron Bentley
  • Date: 2007-08-10 00:03:44 UTC
  • mto: (2681.5.3 bzr-mail)
  • mto: This revision was merged to the branch mainline in revision 2736.
  • Revision ID: aaron.bentley@utoronto.ca-20070810000344-cyfuo1igj88pwb2s
Refactor to increase code sharing, allow multiple command names for tbird

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
import errno
 
18
import os
 
19
import subprocess
 
20
import tempfile
 
21
 
 
22
from bzrlib import (
 
23
    email_message,
 
24
    errors,
 
25
    msgeditor,
 
26
    urlutils,
 
27
    )
 
28
 
 
29
 
 
30
class MailClient(object):
 
31
    """A mail client that can send messages with attachements."""
 
32
 
 
33
    def __init__(self, config):
 
34
        self.config = config
 
35
 
 
36
    def compose(self, prompt, to, subject, attachment, mime_subtype,
 
37
                extension):
 
38
        raise NotImplementedError
 
39
 
 
40
    def compose_merge_request(self, to, subject, directive):
 
41
        self.compose("Please describe these changes:", to, subject, directive,
 
42
            'x-patch', '.patch')
 
43
 
 
44
 
 
45
class Editor(MailClient):
 
46
    """DIY mail client that uses commit message editor"""
 
47
 
 
48
    def compose(self, prompt, to, subject, attachment, mime_subtype,
 
49
                extension):
 
50
        info = ("%s\n\nTo: %s\nSubject: %s\n\n%s" % (prompt, to, subject,
 
51
                attachment))
 
52
        body = msgeditor.edit_commit_message(info)
 
53
        if body == '':
 
54
            raise errors.NoMessageSupplied()
 
55
        email_message.EmailMessage.send(self.config,
 
56
                                        self.config.username(),
 
57
                                        to,
 
58
                                        subject,
 
59
                                        body,
 
60
                                        attachment,
 
61
                                        attachment_mime_subtype=mime_subtype)
 
62
 
 
63
 
 
64
class Evolution(MailClient):
 
65
    """Evolution mail client."""
 
66
 
 
67
    _client_commands = ['evolution']
 
68
 
 
69
    def compose(self, prompt, to, subject, attachment, mime_subtype,
 
70
                extension):
 
71
        fd, pathname = tempfile.mkstemp(extension, 'bzr-mail-')
 
72
        try:
 
73
            os.write(fd, attachment)
 
74
        finally:
 
75
            os.close(fd)
 
76
        for name in self._client_commands:
 
77
            cmdline = [name]
 
78
            cmdline.extend(self._get_compose_commandline(to, subject,
 
79
                                                         pathname))
 
80
            try:
 
81
                subprocess.call(cmdline)
 
82
            except OSError, e:
 
83
                if e.errno != errno.ENOENT:
 
84
                    raise
 
85
            else:
 
86
                break
 
87
        else:
 
88
            raise errors.MailClientNotFound(self._client_commands)
 
89
 
 
90
    def _get_compose_commandline(self, to, subject, attach_path):
 
91
        message_options = {}
 
92
        if subject is not None:
 
93
            message_options['subject'] = subject
 
94
        if attach_path is not None:
 
95
            message_options['attach'] = attach_path
 
96
        options_list = ['%s=%s' % (k, urlutils.escape(v)) for (k, v) in
 
97
                        message_options.iteritems()]
 
98
        return ['mailto:%s?%s' % (to or '', '&'.join(options_list))]
 
99
 
 
100
 
 
101
class Thunderbird(Evolution):
 
102
    """Mozilla Thunderbird (or Icedove)
 
103
 
 
104
    Note that Thunderbird 1.5 is buggy and does not support setting
 
105
    "to" simultaneously with including a attachment.
 
106
 
 
107
    There is a workaround if no attachment is present, but we always need to
 
108
    send attachments.
 
109
    """
 
110
 
 
111
    _client_commands = ['thunderbird', 'mozilla-thunderbird', 'icedove']
 
112
 
 
113
    def _get_compose_commandline(self, to, subject, attach_path):
 
114
        message_options = {}
 
115
        if to is not None:
 
116
            message_options['to'] = to
 
117
        if subject is not None:
 
118
            message_options['subject'] = subject
 
119
        if attach_path is not None:
 
120
            message_options['attachment'] = urlutils.local_path_to_url(
 
121
                attach_path)
 
122
        options_list = ["%s='%s'" % (k, v) for k, v in
 
123
                        sorted(message_options.iteritems())]
 
124
        return ['-compose', ','.join(options_list)]