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 | ||
61eed152 CM |
18 | import sys, os, re, time, datetime, smtplib |
19 | import email, email.Utils, email.Header | |
b4bddc06 | 20 | from optparse import OptionParser, make_option |
b4bddc06 CM |
21 | |
22 | from stgit.commands.common import * | |
23 | from stgit.utils import * | |
1f3bb017 | 24 | from stgit import stack, git, version, templates |
b4bddc06 CM |
25 | from stgit.config import config |
26 | ||
27 | ||
28 | help = 'send a patch or series of patches by e-mail' | |
6b1e0111 | 29 | usage = """%prog [options] [<patch1>] [<patch2>] [<patch3>..<patch4>] |
26aab5b0 | 30 | |
6b1e0111 CM |
31 | Send a patch or a range of patches by e-mail using the 'smtpserver' |
32 | configuration option. The From address and the e-mail format are | |
33 | generated from the template file passed as argument to '--template' | |
34 | (defaulting to '.git/patchmail.tmpl' or | |
35 | '~/.stgit/templates/patchmail.tmpl' or or | |
94d18868 | 36 | '/usr/share/stgit/templates/patchmail.tmpl'). The To/Cc/Bcc addresses |
2bb96902 CM |
37 | can either be added to the template file or passed via the |
38 | corresponding command line options. | |
39 | ||
0ba13ee9 KH |
40 | A preamble e-mail can be sent using the '--cover' and/or |
41 | '--edit-cover' options. The first allows the user to specify a file to | |
42 | be used as a template. The latter option will invoke the editor on the | |
43 | specified file (defaulting to '.git/covermail.tmpl' or | |
94d18868 YD |
44 | '~/.stgit/templates/covermail.tmpl' or |
45 | '/usr/share/stgit/templates/covermail.tmpl'). | |
e3e05587 CM |
46 | |
47 | All the subsequent e-mails appear as replies to the first e-mail sent | |
48 | (either the preamble or the first patch). E-mails can be seen as | |
49 | replies to a different e-mail by using the '--refid' option. | |
26aab5b0 CM |
50 | |
51 | SMTP authentication is also possible with '--smtp-user' and | |
52 | '--smtp-password' options, also available as configuration settings: | |
53 | 'smtpuser' and 'smtppassword'. | |
54 | ||
e5bdb1fe | 55 | The patch e-mail template accepts the following variables: |
26aab5b0 CM |
56 | |
57 | %(patch)s - patch name | |
901288c2 | 58 | %(sender)s - 'sender' or 'authname <authemail>' as per the config file |
26aab5b0 CM |
59 | %(shortdescr)s - the first line of the patch description |
60 | %(longdescr)s - the rest of the patch description, after the first line | |
26aab5b0 CM |
61 | %(diff)s - unified diff of the patch |
62 | %(diffstat)s - diff statistics | |
d0d139a3 | 63 | %(version)s - ' version' string passed on the command line (or empty) |
d323b5da | 64 | %(prefix)s - 'prefix ' string passed on the command line |
26aab5b0 CM |
65 | %(patchnr)s - patch number |
66 | %(totalnr)s - total number of patches to be sent | |
b8d258e5 | 67 | %(number)s - empty if only one patch is sent or ' patchnr/totalnr' |
901288c2 | 68 | %(fromauth)s - 'From: author\\n\\n' if different from sender |
26aab5b0 CM |
69 | %(authname)s - author's name |
70 | %(authemail)s - author's email | |
71 | %(authdate)s - patch creation date | |
72 | %(commname)s - committer's name | |
73 | %(commemail)s - committer's e-mail | |
74 | ||
901288c2 CM |
75 | For the preamble e-mail template, only the %(sender)s, %(version)s, |
76 | %(patchnr)s, %(totalnr)s and %(number)s variables are supported.""" | |
b4bddc06 | 77 | |
9a316368 CM |
78 | options = [make_option('-a', '--all', |
79 | help = 'e-mail all the applied patches', | |
80 | action = 'store_true'), | |
2bb96902 | 81 | make_option('--to', |
e83b3149 PO |
82 | help = 'add TO to the To: list', |
83 | action = 'append'), | |
2bb96902 | 84 | make_option('--cc', |
e83b3149 PO |
85 | help = 'add CC to the Cc: list', |
86 | action = 'append'), | |
2bb96902 | 87 | make_option('--bcc', |
e83b3149 PO |
88 | help = 'add BCC to the Bcc: list', |
89 | action = 'append'), | |
f8d1cf65 CM |
90 | make_option('--auto', |
91 | help = 'automatically cc the patch signers', | |
92 | action = 'store_true'), | |
d1ed3a12 CM |
93 | make_option('--noreply', |
94 | help = 'do not send subsequent messages as replies', | |
95 | action = 'store_true'), | |
d0d139a3 CM |
96 | make_option('-v', '--version', metavar = 'VERSION', |
97 | help = 'add VERSION to the [PATCH ...] prefix'), | |
d323b5da RR |
98 | make_option('--prefix', metavar = 'PREFIX', |
99 | help = 'add PREFIX to the [... PATCH ...] prefix'), | |
9a316368 CM |
100 | make_option('-t', '--template', metavar = 'FILE', |
101 | help = 'use FILE as the message template'), | |
e3e05587 CM |
102 | make_option('-c', '--cover', metavar = 'FILE', |
103 | help = 'send FILE as the cover message'), | |
0ba13ee9 | 104 | make_option('-e', '--edit-cover', |
e3e05587 CM |
105 | help = 'edit the cover message before sending', |
106 | action = 'store_true'), | |
0ba13ee9 KH |
107 | make_option('-E', '--edit-patches', |
108 | help = 'edit each patch before sending', | |
109 | action = 'store_true'), | |
b4bddc06 CM |
110 | make_option('-s', '--sleep', type = 'int', metavar = 'SECONDS', |
111 | help = 'sleep for SECONDS between e-mails sending'), | |
112 | make_option('--refid', | |
d0d139a3 | 113 | help = 'use REFID as the reference id'), |
eb026d93 B |
114 | make_option('-u', '--smtp-user', metavar = 'USER', |
115 | help = 'username for SMTP authentication'), | |
116 | make_option('-p', '--smtp-password', metavar = 'PASSWORD', | |
2f7c8b0b CM |
117 | help = 'username for SMTP authentication'), |
118 | make_option('-b', '--branch', | |
29f00589 CM |
119 | help = 'use BRANCH instead of the default one'), |
120 | make_option('-m', '--mbox', | |
121 | help = 'generate an mbox file instead of sending', | |
122 | action = 'store_true')] | |
b4bddc06 CM |
123 | |
124 | ||
901288c2 | 125 | def __get_sender(): |
dae0f0be CM |
126 | """Return the 'authname <authemail>' string as read from the |
127 | configuration file | |
128 | """ | |
901288c2 CM |
129 | if config.has_option('stgit', 'sender'): |
130 | return config.get('stgit', 'sender') | |
131 | elif config.has_option('stgit', 'authname') \ | |
132 | and config.has_option('stgit', 'authemail'): | |
dae0f0be CM |
133 | return '%s <%s>' % (config.get('stgit', 'authname'), |
134 | config.get('stgit', 'authemail')) | |
135 | else: | |
901288c2 | 136 | raise CmdException, 'unknown sender details' |
dae0f0be | 137 | |
7cc615f3 | 138 | def __parse_addresses(addresses): |
b4bddc06 CM |
139 | """Return a two elements tuple: (from, [to]) |
140 | """ | |
7cc615f3 CL |
141 | def __addr_list(addrs): |
142 | m = re.search('[^@\s<,]+@[^>\s,]+', addrs); | |
d60cd083 CM |
143 | if (m == None): |
144 | return [] | |
7cc615f3 | 145 | return [ m.group() ] + __addr_list(addrs[m.end():]) |
b4bddc06 CM |
146 | |
147 | from_addr_list = [] | |
148 | to_addr_list = [] | |
7cc615f3 | 149 | for line in addresses.split('\n'): |
b4bddc06 CM |
150 | if re.match('from:\s+', line, re.I): |
151 | from_addr_list += __addr_list(line) | |
152 | elif re.match('(to|cc|bcc):\s+', line, re.I): | |
153 | to_addr_list += __addr_list(line) | |
154 | ||
24aadb3f | 155 | if len(from_addr_list) == 0: |
b4bddc06 CM |
156 | raise CmdException, 'No "From" address' |
157 | if len(to_addr_list) == 0: | |
158 | raise CmdException, 'No "To/Cc/Bcc" addresses' | |
159 | ||
160 | return (from_addr_list[0], to_addr_list) | |
161 | ||
eb026d93 B |
162 | def __send_message(smtpserver, from_addr, to_addr_list, msg, sleep, |
163 | smtpuser, smtppassword): | |
b4bddc06 CM |
164 | """Send the message using the given SMTP server |
165 | """ | |
166 | try: | |
167 | s = smtplib.SMTP(smtpserver) | |
168 | except Exception, err: | |
169 | raise CmdException, str(err) | |
170 | ||
171 | s.set_debuglevel(0) | |
172 | try: | |
eb026d93 B |
173 | if smtpuser and smtppassword: |
174 | s.ehlo() | |
175 | s.login(smtpuser, smtppassword) | |
176 | ||
b4bddc06 CM |
177 | s.sendmail(from_addr, to_addr_list, msg) |
178 | # give recipients a chance of receiving patches in the correct order | |
179 | time.sleep(sleep) | |
180 | except Exception, err: | |
181 | raise CmdException, str(err) | |
182 | ||
183 | s.quit() | |
184 | ||
61eed152 | 185 | def __build_address_headers(msg, options, extra_cc = []): |
f8d1cf65 CM |
186 | """Build the address headers and check existing headers in the |
187 | template. | |
188 | """ | |
61eed152 CM |
189 | def __replace_header(header, addr): |
190 | if addr: | |
191 | crt_addr = msg[header] | |
192 | del msg[header] | |
f8d1cf65 | 193 | |
61eed152 CM |
194 | if crt_addr: |
195 | msg[header] = ', '.join([crt_addr, addr]) | |
196 | else: | |
197 | msg[header] = addr | |
f8d1cf65 | 198 | |
f8d1cf65 CM |
199 | to_addr = '' |
200 | cc_addr = '' | |
201 | bcc_addr = '' | |
202 | ||
d884c4d8 CM |
203 | if config.has_option('stgit', 'autobcc'): |
204 | autobcc = config.get('stgit', 'autobcc') | |
205 | else: | |
206 | autobcc = '' | |
207 | ||
e83b3149 | 208 | if options.to: |
61eed152 | 209 | to_addr = ', '.join(options.to) |
e83b3149 | 210 | if options.cc: |
61eed152 | 211 | cc_addr = ', '.join(options.cc + extra_cc) |
f8d1cf65 | 212 | elif extra_cc: |
61eed152 | 213 | cc_addr = ', '.join(extra_cc) |
e83b3149 | 214 | if options.bcc: |
61eed152 | 215 | bcc_addr = ', '.join(options.bcc + [autobcc]) |
d884c4d8 CM |
216 | elif autobcc: |
217 | bcc_addr = autobcc | |
f8d1cf65 | 218 | |
61eed152 CM |
219 | __replace_header('To', to_addr) |
220 | __replace_header('Cc', cc_addr) | |
221 | __replace_header('Bcc', bcc_addr) | |
f8d1cf65 CM |
222 | |
223 | def __get_signers_list(msg): | |
224 | """Return the address list generated from signed-off-by and | |
225 | acked-by lines in the message. | |
226 | """ | |
227 | addr_list = [] | |
228 | ||
229 | r = re.compile('^(signed-off-by|acked-by):\s+(.+)$', re.I) | |
230 | for line in msg.split('\n'): | |
231 | m = r.match(line) | |
232 | if m: | |
233 | addr_list.append(m.expand('\g<2>')) | |
234 | ||
235 | return addr_list | |
e83b3149 | 236 | |
61eed152 CM |
237 | def __build_extra_headers(msg, msg_id, ref_id = None): |
238 | """Build extra email headers and encoding | |
19a56fa1 | 239 | """ |
61eed152 CM |
240 | del msg['Date'] |
241 | msg['Date'] = email.Utils.formatdate(localtime = True) | |
242 | msg['Message-ID'] = msg_id | |
243 | if ref_id: | |
244 | msg['In-Reply-To'] = ref_id | |
245 | msg['References'] = ref_id | |
246 | msg['User-Agent'] = 'StGIT/%s' % version.version | |
247 | ||
248 | def __encode_message(msg): | |
249 | # 7 or 8 bit encoding | |
250 | charset = email.Charset.Charset('utf-8') | |
251 | charset.body_encoding = None | |
252 | ||
253 | # encode headers | |
254 | for header, value in msg.items(): | |
255 | words = [] | |
256 | for word in value.split(' '): | |
257 | try: | |
258 | uword = unicode(word, 'utf-8') | |
259 | except UnicodeDecodeError: | |
260 | # maybe we should try a different encoding or report | |
261 | # the error. At the moment, we just ignore it | |
262 | pass | |
263 | words.append(email.Header.Header(uword).encode()) | |
264 | new_val = ' '.join(words) | |
265 | msg.replace_header(header, new_val) | |
266 | ||
267 | # encode the body and set the MIME and encoding headers | |
268 | msg.set_charset(charset) | |
19a56fa1 | 269 | |
58c61f10 | 270 | def __edit_message(msg): |
0ba13ee9 KH |
271 | fname = '.stgitmail.txt' |
272 | ||
273 | # create the initial file | |
274 | f = file(fname, 'w') | |
275 | f.write(msg) | |
276 | f.close() | |
277 | ||
278 | # the editor | |
279 | if config.has_option('stgit', 'editor'): | |
280 | editor = config.get('stgit', 'editor') | |
281 | elif 'EDITOR' in os.environ: | |
282 | editor = os.environ['EDITOR'] | |
283 | else: | |
284 | editor = 'vi' | |
285 | editor += ' %s' % fname | |
286 | ||
287 | print 'Invoking the editor: "%s"...' % editor, | |
288 | sys.stdout.flush() | |
289 | print 'done (exit code: %d)' % os.system(editor) | |
290 | ||
291 | # read the message back | |
292 | f = file(fname) | |
293 | msg = f.read() | |
294 | f.close() | |
295 | ||
296 | return msg | |
297 | ||
e3e05587 CM |
298 | def __build_cover(tmpl, total_nr, msg_id, options): |
299 | """Build the cover message (series description) to be sent via SMTP | |
b4bddc06 | 300 | """ |
901288c2 | 301 | sender = __get_sender() |
dae0f0be | 302 | |
d0d139a3 CM |
303 | if options.version: |
304 | version_str = ' %s' % options.version | |
ed5de0cc CM |
305 | else: |
306 | version_str = '' | |
d0d139a3 | 307 | |
d323b5da RR |
308 | if options.prefix: |
309 | prefix_str = options.prefix + ' ' | |
310 | else: | |
311 | prefix_str = '' | |
312 | ||
b4bddc06 | 313 | total_nr_str = str(total_nr) |
b8d258e5 CM |
314 | patch_nr_str = '0'.zfill(len(total_nr_str)) |
315 | if total_nr > 1: | |
316 | number_str = ' %s/%s' % (patch_nr_str, total_nr_str) | |
317 | else: | |
318 | number_str = '' | |
b4bddc06 | 319 | |
901288c2 CM |
320 | tmpl_dict = {'sender': sender, |
321 | # for backward template compatibility | |
322 | 'maintainer': sender, | |
61eed152 CM |
323 | # for backward template compatibility |
324 | 'endofheaders': '', | |
325 | # for backward template compatibility | |
326 | 'date': '', | |
d0d139a3 | 327 | 'version': version_str, |
d323b5da | 328 | 'prefix': prefix_str, |
b8d258e5 CM |
329 | 'patchnr': patch_nr_str, |
330 | 'totalnr': total_nr_str, | |
331 | 'number': number_str} | |
b4bddc06 CM |
332 | |
333 | try: | |
61eed152 | 334 | msg_string = tmpl % tmpl_dict |
b4bddc06 CM |
335 | except KeyError, err: |
336 | raise CmdException, 'Unknown patch template variable: %s' \ | |
337 | % err | |
338 | except TypeError: | |
339 | raise CmdException, 'Only "%(name)s" variables are ' \ | |
340 | 'supported in the patch template' | |
341 | ||
58c61f10 CM |
342 | if options.edit_cover: |
343 | msg_string = __edit_message(msg_string) | |
344 | ||
61eed152 CM |
345 | # The Python email message |
346 | try: | |
347 | msg = email.message_from_string(msg_string) | |
348 | except Exception, ex: | |
349 | raise CmdException, 'template parsing error: %s' % str(ex) | |
350 | ||
351 | __build_address_headers(msg, options) | |
352 | __build_extra_headers(msg, msg_id, options.refid) | |
353 | __encode_message(msg) | |
354 | ||
355 | msg_string = msg.as_string(options.mbox) | |
356 | ||
61eed152 | 357 | return msg_string.strip('\n') |
b4bddc06 | 358 | |
2bb96902 | 359 | def __build_message(tmpl, patch, patch_nr, total_nr, msg_id, ref_id, options): |
b4bddc06 CM |
360 | """Build the message to be sent via SMTP |
361 | """ | |
362 | p = crt_series.get_patch(patch) | |
363 | ||
364 | descr = p.get_description().strip() | |
365 | descr_lines = descr.split('\n') | |
366 | ||
367 | short_descr = descr_lines[0].rstrip() | |
61eed152 | 368 | long_descr = '\n'.join(descr_lines[1:]).lstrip() |
b4bddc06 | 369 | |
1d1485c3 CM |
370 | authname = p.get_authname(); |
371 | authemail = p.get_authemail(); | |
372 | commname = p.get_commname(); | |
373 | commemail = p.get_commemail(); | |
374 | ||
901288c2 | 375 | sender = __get_sender() |
1d1485c3 CM |
376 | |
377 | fromauth = '%s <%s>' % (authname, authemail) | |
901288c2 | 378 | if fromauth != sender: |
1d1485c3 CM |
379 | fromauth = 'From: %s\n\n' % fromauth |
380 | else: | |
381 | fromauth = '' | |
dae0f0be | 382 | |
d0d139a3 CM |
383 | if options.version: |
384 | version_str = ' %s' % options.version | |
ed5de0cc CM |
385 | else: |
386 | version_str = '' | |
d0d139a3 | 387 | |
d323b5da RR |
388 | if options.prefix: |
389 | prefix_str = options.prefix + ' ' | |
390 | else: | |
391 | prefix_str = '' | |
392 | ||
b4bddc06 CM |
393 | total_nr_str = str(total_nr) |
394 | patch_nr_str = str(patch_nr).zfill(len(total_nr_str)) | |
b8d258e5 CM |
395 | if total_nr > 1: |
396 | number_str = ' %s/%s' % (patch_nr_str, total_nr_str) | |
397 | else: | |
398 | number_str = '' | |
b4bddc06 CM |
399 | |
400 | tmpl_dict = {'patch': patch, | |
901288c2 CM |
401 | 'sender': sender, |
402 | # for backward template compatibility | |
403 | 'maintainer': sender, | |
b4bddc06 CM |
404 | 'shortdescr': short_descr, |
405 | 'longdescr': long_descr, | |
61eed152 CM |
406 | # for backward template compatibility |
407 | 'endofheaders': '', | |