Add a 'pull' command
[stgit] / stgit / commands / mail.py
CommitLineData
b4bddc06
CM
1__copyright__ = """
2Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
3
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License version 2 as
6published by the Free Software Foundation.
7
8This program is distributed in the hope that it will be useful,
9but WITHOUT ANY WARRANTY; without even the implied warranty of
10MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11GNU General Public License for more details.
12
13You should have received a copy of the GNU General Public License
14along with this program; if not, write to the Free Software
15Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16"""
17
18import sys, os, re, time, smtplib, email.Utils
19from optparse import OptionParser, make_option
20from time import gmtime, strftime
21
22from stgit.commands.common import *
23from stgit.utils import *
24from stgit import stack, git
25from stgit.config import config
26
27
28help = 'send a patch or series of patches by e-mail'
29usage = """%prog [options]"""
30
31options = [make_option('-t', '--template', metavar = 'FILE',
32 help = 'use FILE as the message template'),
33 make_option('-r', '--range',
34 metavar = '[PATCH1][:[PATCH2]]',
35 help = 'e-mail patches between PATCH1 and PATCH2'),
36 make_option('-f', '--first', metavar = 'FILE',
37 help = 'send FILE as the first message'),
38 make_option('-s', '--sleep', type = 'int', metavar = 'SECONDS',
39 help = 'sleep for SECONDS between e-mails sending'),
40 make_option('--refid',
41 help = 'Use REFID as the reference id')]
42
43
44def __parse_addresses(string):
45 """Return a two elements tuple: (from, [to])
46 """
47 def __addr_list(string):
48 return re.split('.*?([\w\.]+@[\w\.]+)', string)[1:-1:2]
49
50 from_addr_list = []
51 to_addr_list = []
52 for line in string.split('\n'):
53 if re.match('from:\s+', line, re.I):
54 from_addr_list += __addr_list(line)
55 elif re.match('(to|cc|bcc):\s+', line, re.I):
56 to_addr_list += __addr_list(line)
57
58 if len(from_addr_list) != 1:
59 raise CmdException, 'No "From" address'
60 if len(to_addr_list) == 0:
61 raise CmdException, 'No "To/Cc/Bcc" addresses'
62
63 return (from_addr_list[0], to_addr_list)
64
65def __send_message(smtpserver, from_addr, to_addr_list, msg, sleep):
66 """Send the message using the given SMTP server
67 """
68 try:
69 s = smtplib.SMTP(smtpserver)
70 except Exception, err:
71 raise CmdException, str(err)
72
73 s.set_debuglevel(0)
74 try:
75 s.sendmail(from_addr, to_addr_list, msg)
76 # give recipients a chance of receiving patches in the correct order
77 time.sleep(sleep)
78 except Exception, err:
79 raise CmdException, str(err)
80
81 s.quit()
82
83def __build_first(tmpl, total_nr, msg_id):
84 """Build the first message (series description) to be sent via SMTP
85 """
86 headers_end = 'Message-Id: %s\n' % (msg_id)
87 total_nr_str = str(total_nr)
88
89 tmpl_dict = {'endofheaders': headers_end,
90 'date': email.Utils.formatdate(localtime = True),
91 'totalnr': total_nr_str}
92
93 try:
94 msg = tmpl % tmpl_dict
95 except KeyError, err:
96 raise CmdException, 'Unknown patch template variable: %s' \
97 % err
98 except TypeError:
99 raise CmdException, 'Only "%(name)s" variables are ' \
100 'supported in the patch template'
101
102 return msg
103
104
105def __build_message(tmpl, patch, patch_nr, total_nr, msg_id, ref_id = None):
106 """Build the message to be sent via SMTP
107 """
108 p = crt_series.get_patch(patch)
109
110 descr = p.get_description().strip()
111 descr_lines = descr.split('\n')
112
113 short_descr = descr_lines[0].rstrip()
114 long_descr = reduce(lambda x, y: x + '\n' + y,
115 descr_lines[1:], '').lstrip()
116
117 headers_end = 'Message-Id: %s\n' % (msg_id)
118 if ref_id:
119 headers_end += "In-Reply-To: %s\n" % (ref_id)
120 headers_end += "References: %s\n" % (ref_id)
121
122 total_nr_str = str(total_nr)
123 patch_nr_str = str(patch_nr).zfill(len(total_nr_str))
124
125 tmpl_dict = {'patch': patch,
126 'shortdescr': short_descr,
127 'longdescr': long_descr,
128 'endofheaders': headers_end,
129 'diff': git.diff(rev1 = git_id('%s/bottom' % patch),
130 rev2 = git_id('%s/top' % patch)),
131 'diffstat': git.diffstat(rev1 = git_id('%s/bottom'%patch),
132 rev2 = git_id('%s/top' % patch)),
133 'date': email.Utils.formatdate(localtime = True),
134 'patchnr': patch_nr_str,
135 'totalnr': total_nr_str,
136 'authname': p.get_authname(),
137 'authemail': p.get_authemail(),
138 'authdate': p.get_authdate(),
139 'commname': p.get_commname(),
140 'commemail': p.get_commemail()}
141 for key in tmpl_dict:
142 if not tmpl_dict[key]:
143 tmpl_dict[key] = ''
144
145 try:
146 msg = tmpl % tmpl_dict
147 except KeyError, err:
148 raise CmdException, 'Unknown patch template variable: %s' \
149 % err
150 except TypeError:
151 raise CmdException, 'Only "%(name)s" variables are ' \
152 'supported in the patch template'
153
154 return msg
155
156
157def func(parser, options, args):
158 """Send the patches by e-mail using the patchmail.tmpl file as
159 a template
160 """
161 if len(args) != 0:
162 parser.error('incorrect number of arguments')
163
164 if not config.has_option('stgit', 'smtpserver'):
165 raise CmdException, 'smtpserver not defined'
166 smtpserver = config.get('stgit', 'smtpserver')
167
168 applied = crt_series.get_applied()
169
170 if options.range:
171 boundaries = options.range.split(':')
172 if len(boundaries) == 1:
173 start = boundaries[0]
174 stop = boundaries[0]
175 elif len(boundaries) == 2:
176 if boundaries[0] == '':
177 start = applied[0]
178 else:
179 start = boundaries[0]
180 if boundaries[1] == '':
181 stop = applied[-1]
182 else:
183 stop = boundaries[1]
184 else:
185 raise CmdException, 'incorrect parameters to "--range"'
186
187 if start in applied:
188 start_idx = applied.index(start)
189 else:
190 raise CmdException, 'Patch "%s" not applied' % start
191 if stop in applied:
192 stop_idx = applied.index(stop) + 1
193 else:
194 raise CmdException, 'Patch "%s" not applied' % stop
195
196 if start_idx >= stop_idx:
197 raise CmdException, 'Incorrect patch range order'
198 else:
199 start_idx = 0
200 stop_idx = len(applied)
201
202 patches = applied[start_idx:stop_idx]
203 total_nr = len(patches)
204
205 ref_id = options.refid
206
207 if options.sleep != None:
208 sleep = options.sleep
209 else:
210 sleep = 2
211
212 # send the first message (if any)
213 if options.first:
214 tmpl = file(options.first).read()
215 from_addr, to_addr_list = __parse_addresses(tmpl)
216
217 msg_id = email.Utils.make_msgid('stgit')
218 msg = __build_first(tmpl, total_nr, msg_id)
219
220 # subsequent e-mails are seen as replies to the first one
221 ref_id = msg_id
222
223 print 'Sending file "%s"...' % options.first,
224 sys.stdout.flush()
225
226 __send_message(smtpserver, from_addr, to_addr_list, msg, sleep)
227
228 print 'done'
229
230 # send the patches
231 if options.template:
232 tfile = options.template
233 else:
234 tfile = os.path.join(git.base_dir, 'patchmail.tmpl')
235 tmpl = file(tfile).read()
236
237 from_addr, to_addr_list = __parse_addresses(tmpl)
238
239 for (p, patch_nr) in zip(patches, range(1, len(patches) + 1)):
240 msg_id = email.Utils.make_msgid('stgit')
241 msg = __build_message(tmpl, p, patch_nr, total_nr, msg_id, ref_id)
242 # subsequent e-mails are seen as replies to the first one
243 if not ref_id:
244 ref_id = msg_id
245
246 print 'Sending patch "%s"...' % p,
247 sys.stdout.flush()
248
249 __send_message(smtpserver, from_addr, to_addr_list, msg, sleep)
250
251 print 'done'