/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: Lukáš Lalinsky
  • Date: 2007-08-09 22:30:26 UTC
  • mto: (2681.1.17 send-bundle)
  • mto: This revision was merged to the branch mainline in revision 2736.
  • Revision ID: lalinsky@gmail.com-20070809223026-c1xray4fmphhkr39
Support for Evolution mail client.

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