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