Commit | Line | Data |
---|---|---|
b4bddc06 CM |
1 | __copyright__ = """ |
2 | Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com> | |
3 | ||
4 | This program is free software; you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License version 2 as | |
6 | published by the Free Software Foundation. | |
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 | ||
18 | import sys, os, re, time, smtplib, email.Utils | |
19 | from optparse import OptionParser, make_option | |
20 | from time import gmtime, strftime | |
21 | ||
22 | from stgit.commands.common import * | |
23 | from stgit.utils import * | |
24 | from stgit import stack, git | |
25 | from stgit.config import config | |
26 | ||
27 | ||
28 | help = 'send a patch or series of patches by e-mail' | |
26aab5b0 CM |
29 | usage = """%prog [options] [<patch>] |
30 | ||
31 | Send a patch or a range of patches (defaulting to the applied patches) | |
2bb96902 CM |
32 | by e-mail using the 'smtpserver' configuration option. The From |
33 | address and the e-mail format are generated from the template file | |
34 | passed as argument to '--template' (defaulting to .git/patchmail.tmpl | |
35 | or /usr/share/stgit/templates/patchmail.tmpl). The To/Cc/Bcc addresses | |
36 | can either be added to the template file or passed via the | |
37 | corresponding command line options. | |
38 | ||
39 | A preamble e-mail can be sent using the '--first' option. All the | |
40 | subsequent e-mails appear as replies to the first e-mail sent (either | |
41 | the preamble or the first patch). E-mails can be seen as replies to a | |
42 | different e-mail by using the '--refid' option. | |
26aab5b0 CM |
43 | |
44 | SMTP authentication is also possible with '--smtp-user' and | |
45 | '--smtp-password' options, also available as configuration settings: | |
46 | 'smtpuser' and 'smtppassword'. | |
47 | ||
48 | The template e-mail headers and body must be separated by | |
49 | '%(endofheaders)s' variable, which is replaced by StGIT with | |
50 | additional headers and a blank line. The patch e-mail template accepts | |
51 | the following variables: | |
52 | ||
53 | %(patch)s - patch name | |
dae0f0be | 54 | %(maintainer)s - 'authname <authemail>' as read from the config file |
26aab5b0 CM |
55 | %(shortdescr)s - the first line of the patch description |
56 | %(longdescr)s - the rest of the patch description, after the first line | |
57 | %(endofheaders)s - delimiter between e-mail headers and body | |
58 | %(diff)s - unified diff of the patch | |
59 | %(diffstat)s - diff statistics | |
60 | %(date)s - current date/time | |
d0d139a3 | 61 | %(version)s - ' version' string passed on the command line (or empty) |
26aab5b0 CM |
62 | %(patchnr)s - patch number |
63 | %(totalnr)s - total number of patches to be sent | |
b8d258e5 | 64 | %(number)s - empty if only one patch is sent or ' patchnr/totalnr' |
26aab5b0 CM |
65 | %(authname)s - author's name |
66 | %(authemail)s - author's email | |
67 | %(authdate)s - patch creation date | |
68 | %(commname)s - committer's name | |
69 | %(commemail)s - committer's e-mail | |
70 | ||
dae0f0be | 71 | For the preamble e-mail template, only the %(maintainer)s, %(date)s, |
d0d139a3 CM |
72 | %(endofheaders)s, %(version)s, %(patchnr)s, %(totalnr)s and %(number)s |
73 | variables are supported.""" | |
b4bddc06 | 74 | |
9a316368 CM |
75 | options = [make_option('-a', '--all', |
76 | help = 'e-mail all the applied patches', | |
77 | action = 'store_true'), | |
b4bddc06 CM |
78 | make_option('-r', '--range', |
79 | metavar = '[PATCH1][:[PATCH2]]', | |
80 | help = 'e-mail patches between PATCH1 and PATCH2'), | |
2bb96902 | 81 | make_option('--to', |
d0d139a3 | 82 | help = 'add TO to the To: list'), |
2bb96902 | 83 | make_option('--cc', |
d0d139a3 | 84 | help = 'add CC to the Cc: list'), |
2bb96902 | 85 | make_option('--bcc', |
d0d139a3 CM |
86 | help = 'add BCC to the Bcc: list'), |
87 | make_option('-v', '--version', metavar = 'VERSION', | |
88 | help = 'add VERSION to the [PATCH ...] prefix'), | |
9a316368 CM |
89 | make_option('-t', '--template', metavar = 'FILE', |
90 | help = 'use FILE as the message template'), | |
b4bddc06 CM |
91 | make_option('-f', '--first', metavar = 'FILE', |
92 | help = 'send FILE as the first message'), | |
93 | make_option('-s', '--sleep', type = 'int', metavar = 'SECONDS', | |
94 | help = 'sleep for SECONDS between e-mails sending'), | |
95 | make_option('--refid', | |
d0d139a3 | 96 | help = 'use REFID as the reference id'), |
eb026d93 B |
97 | make_option('-u', '--smtp-user', metavar = 'USER', |
98 | help = 'username for SMTP authentication'), | |
99 | make_option('-p', '--smtp-password', metavar = 'PASSWORD', | |
100 | help = 'username for SMTP authentication')] | |
b4bddc06 CM |
101 | |
102 | ||
dae0f0be CM |
103 | def __get_maintainer(): |
104 | """Return the 'authname <authemail>' string as read from the | |
105 | configuration file | |
106 | """ | |
107 | if config.has_option('stgit', 'authname') \ | |
108 | and config.has_option('stgit', 'authemail'): | |
109 | return '%s <%s>' % (config.get('stgit', 'authname'), | |
110 | config.get('stgit', 'authemail')) | |
111 | else: | |
112 | return None | |
113 | ||
b4bddc06 CM |
114 | def __parse_addresses(string): |
115 | """Return a two elements tuple: (from, [to]) | |
116 | """ | |
117 | def __addr_list(string): | |
118 | return re.split('.*?([\w\.]+@[\w\.]+)', string)[1:-1:2] | |
119 | ||
120 | from_addr_list = [] | |
121 | to_addr_list = [] | |
122 | for line in string.split('\n'): | |
123 | if re.match('from:\s+', line, re.I): | |
124 | from_addr_list += __addr_list(line) | |
125 | elif re.match('(to|cc|bcc):\s+', line, re.I): | |
126 | to_addr_list += __addr_list(line) | |
127 | ||
128 | if len(from_addr_list) != 1: | |
129 | raise CmdException, 'No "From" address' | |
130 | if len(to_addr_list) == 0: | |
131 | raise CmdException, 'No "To/Cc/Bcc" addresses' | |
132 | ||
133 | return (from_addr_list[0], to_addr_list) | |
134 | ||
eb026d93 B |
135 | def __send_message(smtpserver, from_addr, to_addr_list, msg, sleep, |
136 | smtpuser, smtppassword): | |
b4bddc06 CM |
137 | """Send the message using the given SMTP server |
138 | """ | |
139 | try: | |
140 | s = smtplib.SMTP(smtpserver) | |
141 | except Exception, err: | |
142 | raise CmdException, str(err) | |
143 | ||
144 | s.set_debuglevel(0) | |
145 | try: | |
eb026d93 B |
146 | if smtpuser and smtppassword: |
147 | s.ehlo() | |
148 | s.login(smtpuser, smtppassword) | |
149 | ||
b4bddc06 CM |
150 | s.sendmail(from_addr, to_addr_list, msg) |
151 | # give recipients a chance of receiving patches in the correct order | |
152 | time.sleep(sleep) | |
153 | except Exception, err: | |
154 | raise CmdException, str(err) | |
155 | ||
156 | s.quit() | |
157 | ||
2bb96902 | 158 | def __build_first(tmpl, total_nr, msg_id, options): |
b4bddc06 CM |
159 | """Build the first message (series description) to be sent via SMTP |
160 | """ | |
dae0f0be CM |
161 | maintainer = __get_maintainer() |
162 | if not maintainer: | |
163 | maintainer = '' | |
164 | ||
2bb96902 CM |
165 | headers_end = '' |
166 | if options.to: | |
167 | headers_end += 'To: %s\n' % options.to | |
168 | if options.cc: | |
169 | headers_end += 'Cc: %s\n' % options.cc | |
170 | if options.bcc: | |
171 | headers_end += 'Bcc: %s\n' % options.bcc | |
172 | headers_end += 'Message-Id: %s\n' % msg_id | |
173 | ||
d0d139a3 CM |
174 | if options.version: |
175 | version_str = ' %s' % options.version | |
176 | ||
b4bddc06 | 177 | total_nr_str = str(total_nr) |
b8d258e5 CM |
178 | patch_nr_str = '0'.zfill(len(total_nr_str)) |
179 | if total_nr > 1: | |
180 | number_str = ' %s/%s' % (patch_nr_str, total_nr_str) | |
181 | else: | |
182 | number_str = '' | |
b4bddc06 | 183 | |
dae0f0be CM |
184 | tmpl_dict = {'maintainer': maintainer, |
185 | 'endofheaders': headers_end, | |
b4bddc06 | 186 | 'date': email.Utils.formatdate(localtime = True), |
d0d139a3 | 187 | 'version': version_str, |
b8d258e5 CM |
188 | 'patchnr': patch_nr_str, |
189 | 'totalnr': total_nr_str, | |
190 | 'number': number_str} | |
b4bddc06 CM |
191 | |
192 | try: | |
193 | msg = tmpl % tmpl_dict | |
194 | except KeyError, err: | |
195 | raise CmdException, 'Unknown patch template variable: %s' \ | |
196 | % err | |
197 | except TypeError: | |
198 | raise CmdException, 'Only "%(name)s" variables are ' \ | |
199 | 'supported in the patch template' | |
200 | ||
201 | return msg | |
202 | ||
2bb96902 | 203 | def __build_message(tmpl, patch, patch_nr, total_nr, msg_id, ref_id, options): |
b4bddc06 CM |
204 | """Build the message to be sent via SMTP |
205 | """ | |
206 | p = crt_series.get_patch(patch) | |
207 | ||
208 | descr = p.get_description().strip() | |
209 | descr_lines = descr.split('\n') | |
210 | ||
211 | short_descr = descr_lines[0].rstrip() | |
212 | long_descr = reduce(lambda x, y: x + '\n' + y, | |
213 | descr_lines[1:], '').lstrip() | |
214 | ||
dae0f0be CM |
215 | maintainer = __get_maintainer() |
216 | if not maintainer: | |
217 | maintainer = '%s <%s>' % (p.get_commname(), p.get_commemail()) | |
218 | ||
2bb96902 CM |
219 | headers_end = '' |
220 | if options.to: | |
221 | headers_end += 'To: %s\n' % options.to | |
222 | if options.cc: | |
223 | headers_end += 'Cc: %s\n' % options.cc | |
224 | if options.bcc: | |
225 | headers_end += 'Bcc: %s\n' % options.bcc | |
226 | headers_end += 'Message-Id: %s\n' % msg_id | |
b4bddc06 | 227 | if ref_id: |
2bb96902 CM |
228 | headers_end += "In-Reply-To: %s\n" % ref_id |
229 | headers_end += "References: %s\n" % ref_id | |
b4bddc06 | 230 | |
d0d139a3 CM |
231 | if options.version: |
232 | version_str = ' %s' % options.version | |
233 | ||
b4bddc06 CM |
234 | total_nr_str = str(total_nr) |
235 | patch_nr_str = str(patch_nr).zfill(len(total_nr_str)) | |
b8d258e5 CM |
236 | if total_nr > 1: |
237 | number_str = ' %s/%s' % (patch_nr_str, total_nr_str) | |
238 | else: | |
239 | number_str = '' | |
b4bddc06 CM |
240 | |
241 | tmpl_dict = {'patch': patch, | |
dae0f0be | 242 | 'maintainer': maintainer, |
b4bddc06 CM |
243 | 'shortdescr': short_descr, |
244 | 'longdescr': long_descr, | |
245 | 'endofheaders': headers_end, | |
246 | 'diff': git.diff(rev1 = git_id('%s/bottom' % patch), | |
247 | rev2 = git_id('%s/top' % patch)), | |
248 | 'diffstat': git.diffstat(rev1 = git_id('%s/bottom'%patch), | |
249 | rev2 = git_id('%s/top' % patch)), | |
250 | 'date': email.Utils.formatdate(localtime = True), | |
d0d139a3 | 251 | 'version': version_str, |
b4bddc06 CM |
252 | 'patchnr': patch_nr_str, |
253 | 'totalnr': total_nr_str, | |
b8d258e5 | 254 | 'number': number_str, |
b4bddc06 CM |
255 | 'authname': p.get_authname(), |
256 | 'authemail': p.get_authemail(), | |
257 | 'authdate': p.get_authdate(), | |
258 | 'commname': p.get_commname(), | |
259 | 'commemail': p.get_commemail()} | |
260 | for key in tmpl_dict: | |
261 | if not tmpl_dict[key]: | |
262 | tmpl_dict[key] = '' | |
263 | ||
264 | try: | |
265 | msg = tmpl % tmpl_dict | |
266 | except KeyError, err: | |
267 | raise CmdException, 'Unknown patch template variable: %s' \ | |
268 | % err | |
269 | except TypeError: | |
270 | raise CmdException, 'Only "%(name)s" variables are ' \ | |
271 | 'supported in the patch template' | |
272 | ||
273 | return msg | |
274 | ||
b4bddc06 CM |
275 | def func(parser, options, args): |
276 | """Send the patches by e-mail using the patchmail.tmpl file as | |
277 | a template | |
278 | """ | |
9a316368 | 279 | if len(args) > 1: |
b4bddc06 CM |
280 | parser.error('incorrect number of arguments') |
281 | ||
282 | if not config.has_option('stgit', 'smtpserver'): | |
283 | raise CmdException, 'smtpserver not defined' | |
284 | smtpserver = config.get('stgit', 'smtpserver') | |
285 | ||
eb026d93 B |
286 | smtpuser = None |
287 | smtppassword = None | |
288 | if config.has_option('stgit', 'smtpuser'): | |
289 | smtpuser = config.get('stgit', 'smtpuser') | |
290 | if config.has_option('stgit', 'smtppassword'): | |
291 | smtppassword = config.get('stgit', 'smtppassword') | |
292 | ||
b4bddc06 CM |
293 | applied = crt_series.get_applied() |
294 | ||
9a316368 CM |
295 | if len(args) == 1: |
296 | if args[0] in applied: | |
297 | patches = [args[0]] | |
298 | else: | |
299 | raise CmdException, 'Patch "%s" not applied' % args[0] | |
300 | elif options.all: | |
301 | patches = applied | |
302 | elif options.range: | |
b4bddc06 CM |
303 | boundaries = options.range.split(':') |
304 | if len(boundaries) == 1: | |
305 | start = boundaries[0] | |
306 | stop = boundaries[0] | |
307 | elif len(boundaries) == 2: | |
308 | if boundaries[0] == '': | |
309 | start = applied[0] | |
310 | else: | |
311 | start = boundaries[0] | |
312 | if boundaries[1] == '': | |
313 | stop = applied[-1] | |
314 | else: | |
315 | stop = boundaries[1] | |
316 | else: | |
317 | raise CmdException, 'incorrect parameters to "--range"' | |
318 | ||
319 | if start in applied: | |
320 | start_idx = applied.index(start) | |
321 | else: | |
322 | raise CmdException, 'Patch "%s" not applied' % start | |
323 | if stop in applied: | |
324 | stop_idx = applied.index(stop) + 1 | |
325 | else: | |
326 | raise CmdException, 'Patch "%s" not applied' % stop | |
327 | ||
328 | if start_idx >= stop_idx: | |
329 | raise CmdException, 'Incorrect patch range order' | |
9a316368 CM |
330 | |
331 | patches = applied[start_idx:stop_idx] | |
b4bddc06 | 332 | else: |
9a316368 | 333 | raise CmdException, 'Incorrect options. Unknown patches to send' |
b4bddc06 | 334 | |
eb026d93 B |
335 | if options.smtp_password: |
336 | smtppassword = options.smtp_password | |
337 | ||
338 | if options.smtp_user: | |
339 | smtpuser = options.smtp_user | |
340 | ||
341 | if (smtppassword and not smtpuser): | |
342 | raise CmdException, 'SMTP password supplied, username needed' | |
343 | if (smtpuser and not smtppassword): | |
344 | raise CmdException, 'SMTP username supplied, password needed' | |
345 | ||
b4bddc06 | 346 | total_nr = len(patches) |
9a316368 CM |
347 | if total_nr == 0: |
348 | raise CmdException, 'No patches to send' | |
b4bddc06 CM |
349 | |
350 | ref_id = options.refid | |
351 | ||
352 | if options.sleep != None: | |
353 | sleep = options.sleep | |
354 | else: | |
355 | sleep = 2 | |
356 | ||
357 | # send the first message (if any) | |
358 | if options.first: | |
359 | tmpl = file(options.first).read() | |
b4bddc06 CM |
360 | |
361 | msg_id = email.Utils.make_msgid('stgit') | |
2bb96902 CM |
362 | msg = __build_first(tmpl, total_nr, msg_id, options) |
363 | from_addr, to_addr_list = __parse_addresses(msg) | |
b4bddc06 CM |
364 | |
365 | # subsequent e-mails are seen as replies to the first one | |
366 | ref_id = msg_id | |
367 | ||
368 | print 'Sending file "%s"...' % options.first, | |
369 | sys.stdout.flush() | |
370 | ||
eb026d93 B |
371 | __send_message(smtpserver, from_addr, to_addr_list, msg, sleep, |
372 | smtpuser, smtppassword) | |
b4bddc06 CM |
373 | |
374 | print 'done' | |
375 | ||
376 | # send the patches | |
377 | if options.template: | |
2bb96902 | 378 | tfile_list = [options.template] |
b4bddc06 | 379 | else: |
2bb96902 CM |
380 | tfile_list = [] |
381 | ||
382 | tfile_list += [os.path.join(git.base_dir, 'patchmail.tmpl'), | |
383 | os.path.join(sys.prefix, | |
384 | 'share/stgit/templates/patchmail.tmpl')] | |
385 | tmpl = None | |
386 | for tfile in tfile_list: | |
387 | if os.path.isfile(tfile): | |
388 | tmpl = file(tfile).read() | |
389 | break | |
390 | if not tmpl: | |
391 | raise CmdException, 'No e-mail template file: %s or %s' \ | |
392 | % (tfile_list[-1], tfile_list[-2]) | |
b4bddc06 CM |
393 | |
394 | for (p, patch_nr) in zip(patches, range(1, len(patches) + 1)): | |
395 | msg_id = email.Utils.make_msgid('stgit') | |
2bb96902 CM |
396 | msg = __build_message(tmpl, p, patch_nr, total_nr, msg_id, ref_id, |
397 | options) | |
398 | from_addr, to_addr_list = __parse_addresses(msg) | |
399 | ||
b4bddc06 CM |
400 | # subsequent e-mails are seen as replies to the first one |
401 | if not ref_id: | |
402 | ref_id = msg_id | |
403 | ||
404 | print 'Sending patch "%s"...' % p, | |
405 | sys.stdout.flush() | |
406 | ||
eb026d93 B |
407 | __send_message(smtpserver, from_addr, to_addr_list, msg, sleep, |
408 | smtpuser, smtppassword) | |
b4bddc06 CM |
409 | |
410 | print 'done' |