/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 17:22:19 UTC
  • mto: (2681.5.3 bzr-mail)
  • mto: This revision was merged to the branch mainline in revision 2736.
  • Revision ID: abentley@panoramicfeedback.com-20070810172219-fmlgv335z41f7sy5
Add support for xdg-email

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
        prompt = self._get_merge_prompt("Please describe these changes:", to,
 
42
                                        subject, directive)
 
43
        self.compose(prompt, to, subject, directive,
 
44
            'x-patch', '.patch')
 
45
 
 
46
    def _get_merge_prompt(self, prompt, to, subject, attachment):
 
47
        return ''
 
48
 
 
49
 
 
50
class Editor(MailClient):
 
51
    """DIY mail client that uses commit message editor"""
 
52
 
 
53
    def _get_merge_prompt(self, prompt, to, subject, attachment):
 
54
        return "%s\n\nTo: %s\nSubject: %s\n\n%s" % (prompt, to, subject,
 
55
                attachment.decode('utf-8', 'replace'))
 
56
 
 
57
    def compose(self, prompt, to, subject, attachment, mime_subtype,
 
58
                extension):
 
59
        body = msgeditor.edit_commit_message(prompt)
 
60
        if body == '':
 
61
            raise errors.NoMessageSupplied()
 
62
        email_message.EmailMessage.send(self.config,
 
63
                                        self.config.username(),
 
64
                                        to,
 
65
                                        subject,
 
66
                                        body,
 
67
                                        attachment,
 
68
                                        attachment_mime_subtype=mime_subtype)
 
69
 
 
70
 
 
71
class Evolution(MailClient):
 
72
    """Evolution mail client."""
 
73
 
 
74
    _client_commands = ['evolution']
 
75
 
 
76
    def compose(self, prompt, to, subject, attachment, mime_subtype,
 
77
                extension):
 
78
        fd, pathname = tempfile.mkstemp(extension, 'bzr-mail-')
 
79
        try:
 
80
            os.write(fd, attachment)
 
81
        finally:
 
82
            os.close(fd)
 
83
        for name in self._client_commands:
 
84
            cmdline = [name]
 
85
            cmdline.extend(self._get_compose_commandline(to, subject,
 
86
                                                         pathname))
 
87
            try:
 
88
                subprocess.call(cmdline)
 
89
            except OSError, e:
 
90
                if e.errno != errno.ENOENT:
 
91
                    raise
 
92
            else:
 
93
                break
 
94
        else:
 
95
            raise errors.MailClientNotFound(self._client_commands)
 
96
 
 
97
    def _get_compose_commandline(self, to, subject, attach_path):
 
98
        message_options = {}
 
99
        if subject is not None:
 
100
            message_options['subject'] = subject
 
101
        if attach_path is not None:
 
102
            message_options['attach'] = attach_path
 
103
        options_list = ['%s=%s' % (k, urlutils.escape(v)) for (k, v) in
 
104
                        message_options.iteritems()]
 
105
        return ['mailto:%s?%s' % (to or '', '&'.join(options_list))]
 
106
 
 
107
 
 
108
class Thunderbird(Evolution):
 
109
    """Mozilla Thunderbird (or Icedove)
 
110
 
 
111
    Note that Thunderbird 1.5 is buggy and does not support setting
 
112
    "to" simultaneously with including a attachment.
 
113
 
 
114
    There is a workaround if no attachment is present, but we always need to
 
115
    send attachments.
 
116
    """
 
117
 
 
118
    _client_commands = ['thunderbird', 'mozilla-thunderbird', 'icedove']
 
119
 
 
120
    def _get_compose_commandline(self, to, subject, attach_path):
 
121
        message_options = {}
 
122
        if to is not None:
 
123
            message_options['to'] = to
 
124
        if subject is not None:
 
125
            message_options['subject'] = subject
 
126
        if attach_path is not None:
 
127
            message_options['attachment'] = urlutils.local_path_to_url(
 
128
                attach_path)
 
129
        options_list = ["%s='%s'" % (k, v) for k, v in
 
130
                        sorted(message_options.iteritems())]
 
131
        return ['-compose', ','.join(options_list)]
 
132
 
 
133
 
 
134
class XDGEmail(Evolution):
 
135
    """xdg-email attempts to invoke the user's preferred mail client"""
 
136
 
 
137
    _client_commands = ['xdg-email']
 
138
 
 
139
    def _get_compose_commandline(self, to, subject, attach_path):
 
140
        commandline = [to]
 
141
        if subject is not None:
 
142
            commandline.extend(['--subject', subject])
 
143
        if attach_path is not None:
 
144
            commandline.extend(['--attach', attach_path])
 
145
        return commandline