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 |
575bbdae | 20 | from stgit.argparse import opt |
b4bddc06 CM |
21 | from stgit.commands.common import * |
22 | from stgit.utils import * | |
5e888f30 | 23 | from stgit.out import * |
20a52e06 | 24 | from stgit import argparse, stack, git, version, templates |
b4bddc06 | 25 | from stgit.config import config |
a0fe60a2 | 26 | from stgit.run import Run |
ef954fe6 | 27 | from stgit.lib import git as gitlib |
b4bddc06 | 28 | |
575bbdae | 29 | help = 'Send a patch or series of patches by e-mail' |
33ff9cdd | 30 | kind = 'patch' |
575bbdae KH |
31 | usage = [' [options] [<patch1>] [<patch2>] [<patch3>..<patch4>]'] |
32 | description = r""" | |
cec913c4 KH |
33 | Send a patch or a range of patches by e-mail using the SMTP server |
34 | specified by the 'stgit.smtpserver' configuration option, or the | |
a0fe60a2 CM |
35 | '--smtp-server' command line option. This option can also be an |
36 | absolute path to 'sendmail' followed by command line arguments. | |
37 | ||
38 | The From address and the e-mail format are generated from the template | |
39 | file passed as argument to '--template' (defaulting to | |
40 | '.git/patchmail.tmpl' or '~/.stgit/templates/patchmail.tmpl' or | |
e5c32acf | 41 | '/usr/share/stgit/templates/patchmail.tmpl'). A patch can be sent as |
a0fe60a2 CM |
42 | attachment using the --attach option in which case the |
43 | 'mailattch.tmpl' template will be used instead of 'patchmail.tmpl'. | |
79df2f0d CM |
44 | |
45 | The To/Cc/Bcc addresses can either be added to the template file or | |
46 | passed via the corresponding command line options. They can be e-mail | |
47 | addresses or aliases which are automatically expanded to the values | |
48 | stored in the [mail "alias"] section of GIT configuration files. | |
2bb96902 | 49 | |
0ba13ee9 KH |
50 | A preamble e-mail can be sent using the '--cover' and/or |
51 | '--edit-cover' options. The first allows the user to specify a file to | |
52 | be used as a template. The latter option will invoke the editor on the | |
53 | specified file (defaulting to '.git/covermail.tmpl' or | |
94d18868 YD |
54 | '~/.stgit/templates/covermail.tmpl' or |
55 | '/usr/share/stgit/templates/covermail.tmpl'). | |
e3e05587 CM |
56 | |
57 | All the subsequent e-mails appear as replies to the first e-mail sent | |
58 | (either the preamble or the first patch). E-mails can be seen as | |
59 | replies to a different e-mail by using the '--refid' option. | |
26aab5b0 CM |
60 | |
61 | SMTP authentication is also possible with '--smtp-user' and | |
62 | '--smtp-password' options, also available as configuration settings: | |
fc44c2ca PR |
63 | 'smtpuser' and 'smtppassword'. TLS encryption can be enabled by |
64 | '--smtp-tls' option and 'smtptls' setting. | |
26aab5b0 | 65 | |
27827959 KH |
66 | The following variables are accepted by both the preamble and the |
67 | patch e-mail templates: | |
26aab5b0 | 68 | |
26aab5b0 | 69 | %(diffstat)s - diff statistics |
27827959 | 70 | %(number)s - empty if only one patch is sent or ' patchnr/totalnr' |
26aab5b0 | 71 | %(patchnr)s - patch number |
27827959 | 72 | %(sender)s - 'sender' or 'authname <authemail>' as per the config file |
26aab5b0 | 73 | %(totalnr)s - total number of patches to be sent |
27827959 KH |
74 | %(version)s - ' version' string passed on the command line (or empty) |
75 | ||
76 | In addition to the common variables, the preamble e-mail template | |
77 | accepts the following: | |
78 | ||
79 | %(shortlog)s - first line of each patch description, listed by author | |
80 | ||
81 | In addition to the common variables, the patch e-mail template accepts | |
82 | the following: | |
83 | ||
26aab5b0 | 84 | %(authdate)s - patch creation date |
27827959 KH |
85 | %(authemail)s - author's email |
86 | %(authname)s - author's name | |
26aab5b0 | 87 | %(commemail)s - committer's e-mail |
27827959 KH |
88 | %(commname)s - committer's name |
89 | %(diff)s - unified diff of the patch | |
77eeb7f4 | 90 | %(fromauth)s - 'From: author\n\n' if different from sender |
27827959 KH |
91 | %(longdescr)s - the rest of the patch description, after the first line |
92 | %(patch)s - patch name | |
93 | %(prefix)s - 'prefix ' string passed on the command line | |
94 | %(shortdescr)s - the first line of the patch description""" | |
b4bddc06 | 95 | |
6c8a90e1 KH |
96 | args = [argparse.patch_range(argparse.applied_patches, |
97 | argparse.unapplied_patches, | |
98 | argparse.hidden_patches)] | |
575bbdae KH |
99 | options = [ |
100 | opt('-a', '--all', action = 'store_true', | |
101 | short = 'E-mail all the applied patches'), | |
102 | opt('--to', action = 'append', | |
103 | short = 'Add TO to the To: list'), | |
104 | opt('--cc', action = 'append', | |
105 | short = 'Add CC to the Cc: list'), | |
106 | opt('--bcc', action = 'append', | |
107 | short = 'Add BCC to the Bcc: list'), | |
108 | opt('--auto', action = 'store_true', | |
109 | short = 'Automatically cc the patch signers'), | |
110 | opt('--noreply', action = 'store_true', | |
111 | short = 'Do not send subsequent messages as replies'), | |
112 | opt('--unrelated', action = 'store_true', | |
113 | short = 'Send patches without sequence numbering'), | |
114 | opt('--attach', action = 'store_true', | |
115 | short = 'Send a patch as attachment'), | |
116 | opt('-v', '--version', metavar = 'VERSION', | |
117 | short = 'Add VERSION to the [PATCH ...] prefix'), | |
118 | opt('--prefix', metavar = 'PREFIX', | |
119 | short = 'Add PREFIX to the [... PATCH ...] prefix'), | |
120 | opt('-t', '--template', metavar = 'FILE', | |
121 | short = 'Use FILE as the message template'), | |
122 | opt('-c', '--cover', metavar = 'FILE', | |
123 | short = 'Send FILE as the cover message'), | |
124 | opt('-e', '--edit-cover', action = 'store_true', | |
125 | short = 'Edit the cover message before sending'), | |
126 | opt('-E', '--edit-patches', action = 'store_true', | |
127 | short = 'Edit each patch before sending'), | |
128 | opt('-s', '--sleep', type = 'int', metavar = 'SECONDS', | |
129 | short = 'Sleep for SECONDS between e-mails sending'), | |
130 | opt('--refid', | |
131 | short = 'Use REFID as the reference id'), | |
132 | opt('--smtp-server', metavar = 'HOST[:PORT] or "/path/to/sendmail -t -i"', | |
133 | short = 'SMTP server or command to use for sending mail'), | |
134 | opt('-u', '--smtp-user', metavar = 'USER', | |
135 | short = 'Username for SMTP authentication'), | |
136 | opt('-p', '--smtp-password', metavar = 'PASSWORD', | |
137 | short = 'Password for SMTP authentication'), | |
138 | opt('-T', '--smtp-tls', action = 'store_true', | |
139 | short = 'Use SMTP with TLS encryption'), | |
6c8a90e1 | 140 | opt('-b', '--branch', args = [argparse.stg_branches], |
575bbdae KH |
141 | short = 'Use BRANCH instead of the default branch'), |
142 | opt('-m', '--mbox', action = 'store_true', | |
143 | short = 'Generate an mbox file instead of sending') | |
144 | ] + argparse.diff_opts_option() | |
145 | ||
117ed129 | 146 | directory = DirectoryHasRepository(log = False) |
b4bddc06 | 147 | |
901288c2 | 148 | def __get_sender(): |
dae0f0be CM |
149 | """Return the 'authname <authemail>' string as read from the |
150 | configuration file | |
151 | """ | |
c73e63b7 YD |
152 | sender=config.get('stgit.sender') |
153 | if not sender: | |
9e3f506f KH |
154 | try: |
155 | sender = str(git.user()) | |
156 | except git.GitException: | |
157 | sender = str(git.author()) | |
9e3f506f | 158 | if not sender: |
901288c2 | 159 | raise CmdException, 'unknown sender details' |
cd74a041 CM |
160 | sender = email.Utils.parseaddr(sender) |
161 | ||
162 | return email.Utils.formataddr(address_or_alias(sender)) | |
dae0f0be | 163 | |
cd74a041 CM |
164 | def __addr_list(msg, header): |
165 | return [addr for name, addr in | |
166 | email.Utils.getaddresses(msg.get_all(header, []))] | |
9e3f506f | 167 | |
d650d6ed | 168 | def __parse_addresses(msg): |
b4bddc06 CM |
169 | """Return a two elements tuple: (from, [to]) |
170 | """ | |
d650d6ed | 171 | from_addr_list = __addr_list(msg, 'From') |
24aadb3f | 172 | if len(from_addr_list) == 0: |
b4bddc06 | 173 | raise CmdException, 'No "From" address' |
d650d6ed CM |
174 | |
175 | to_addr_list = __addr_list(msg, 'To') + __addr_list(msg, 'Cc') \ | |
176 | + __addr_list(msg, 'Bcc') | |
b4bddc06 CM |
177 | if len(to_addr_list) == 0: |
178 | raise CmdException, 'No "To/Cc/Bcc" addresses' | |
179 | ||
cd74a041 | 180 | return (from_addr_list[0], set(to_addr_list)) |
b4bddc06 | 181 | |
a0fe60a2 CM |
182 | def __send_message_sendmail(sendmail, msg): |
183 | """Send the message using the sendmail command. | |
184 | """ | |
185 | cmd = sendmail.split() | |
186 | Run(*cmd).raw_input(msg).discard_output() | |
187 | ||
188 | def __send_message_smtp(smtpserver, from_addr, to_addr_list, msg, | |
189 | smtpuser, smtppassword, use_tls): | |
b4bddc06 CM |
190 | """Send the message using the given SMTP server |
191 | """ | |
192 | try: | |
193 | s = smtplib.SMTP(smtpserver) | |
194 | except Exception, err: | |
195 | raise CmdException, str(err) | |
196 | ||
197 | s.set_debuglevel(0) | |
198 | try: | |
eb026d93 B |
199 | if smtpuser and smtppassword: |
200 | s.ehlo() | |
fc44c2ca PR |
201 | if use_tls: |
202 | if not hasattr(socket, 'ssl'): | |
203 | raise CmdException, "cannot use TLS - no SSL support in Python" | |
204 | s.starttls() | |
205 | s.ehlo() | |
eb026d93 B |
206 | s.login(smtpuser, smtppassword) |
207 | ||
0bc1343c YD |
208 | result = s.sendmail(from_addr, to_addr_list, msg) |
209 | if len(result): | |
210 | print "mail server refused delivery for the following recipients: %s" % result | |
b4bddc06 CM |
211 | except Exception, err: |
212 | raise CmdException, str(err) | |
213 | ||
214 | s.quit() | |
215 | ||
a0fe60a2 CM |
216 | def __send_message(smtpserver, from_addr, to_addr_list, msg, |
217 | sleep, smtpuser, smtppassword, use_tls): | |
218 | """Message sending dispatcher. | |
219 | """ | |
220 | if smtpserver.startswith('/'): | |
221 | # Use the sendmail tool | |
222 | __send_message_sendmail(smtpserver, msg) | |
223 | else: | |
224 | # Use the SMTP server (we have host and port information) | |
225 | __send_message_smtp(smtpserver, from_addr, to_addr_list, msg, | |
226 | smtpuser, smtppassword, use_tls) | |
227 | # give recipients a chance of receiving patches in the correct order | |
228 | time.sleep(sleep) | |
229 | ||
61eed152 | 230 | def __build_address_headers(msg, options, extra_cc = []): |
f8d1cf65 CM |
231 | """Build the address headers and check existing headers in the |
232 | template. | |
233 | """ | |
cd74a041 CM |
234 | def __addr_pairs(msg, header, extra): |
235 | pairs = email.Utils.getaddresses(msg.get_all(header, []) + extra) | |
236 | # remove pairs without an address and resolve the aliases | |
237 | return [address_or_alias(p) for p in pairs if p[1]] | |
238 | ||
239 | def __update_header(header, addr = '', ignore = ()): | |
240 | addr_pairs = __addr_pairs(msg, header, [addr]) | |
241 | del msg[header] | |
242 | # remove the duplicates and filter the addresses | |
243 | addr_dict = dict((addr, email.Utils.formataddr((name, addr))) | |
244 | for name, addr in addr_pairs if addr not in ignore) | |
245 | if addr_dict: | |
246 | msg[header] = ', '.join(addr_dict.itervalues()) | |
247 | return set(addr_dict.iterkeys()) | |
f8d1cf65 | 248 | |
f8d1cf65 CM |
249 | to_addr = '' |
250 | cc_addr = '' | |
cd74a041 | 251 | extra_cc_addr = '' |
f8d1cf65 CM |
252 | bcc_addr = '' |
253 | ||
c73e63b7 | 254 | autobcc = config.get('stgit.autobcc') or '' |
d884c4d8 | 255 | |
e83b3149 | 256 | if options.to: |
61eed152 | 257 | to_addr = ', '.join(options.to) |
e83b3149 | 258 | if options.cc: |
cd74a041 CM |
259 | cc_addr = ', '.join(options.cc) |
260 | if extra_cc: | |
261 | extra_cc_addr = ', '.join(extra_cc) | |
e83b3149 | 262 | if options.bcc: |
61eed152 | 263 | bcc_addr = ', '.join(options.bcc + [autobcc]) |
d884c4d8 CM |
264 | elif autobcc: |
265 | bcc_addr = autobcc | |
f8d1cf65 | 266 | |
cd74a041 CM |
267 | # if an address is on a header, ignore it from the rest |
268 | to_set = __update_header('To', to_addr) | |
269 | cc_set = __update_header('Cc', cc_addr, to_set) | |
270 | bcc_set = __update_header('Bcc', bcc_addr, to_set.union(cc_set)) | |
271 | ||
272 | # --auto generated addresses, don't include the sender | |
273 | from_set = __update_header('From') | |
274 | __update_header('Cc', extra_cc_addr, to_set.union(bcc_set).union(from_set)) | |
275 | ||
276 | # update other address headers | |
277 | __update_header('Reply-To') | |
278 | __update_header('Mail-Reply-To') | |
279 | __update_header('Mail-Followup-To') | |
f8d1cf65 CM |
280 | |
281 | def __get_signers_list(msg): | |
282 | """Return the address list generated from signed-off-by and | |
283 | acked-by lines in the message. | |
284 | """ | |
285 | addr_list = [] | |
de41ecc8 DW |
286 | tags = '%s|%s|%s|%s|%s|%s|%s' % ( |
287 | 'signed-off-by', | |
288 | 'acked-by', | |
289 | 'cc', | |
290 | 'reviewed-by', | |
291 | 'reported-by', | |
292 | 'tested-by', | |
293 | 'reported-and-tested-by') | |
294 | regex = '^(%s):\s+(.+)$' % tags | |
295 | ||
296 | r = re.compile(regex, re.I) | |
f8d1cf65 CM |
297 | for line in msg.split('\n'): |
298 | m = r.match(line) | |
299 | if m: | |
300 | addr_list.append(m.expand('\g<2>')) | |
301 | ||
302 | return addr_list | |
e83b3149 | 303 | |
61eed152 CM |
304 | def __build_extra_headers(msg, msg_id, ref_id = None): |
305 | """Build extra email headers and encoding | |
19a56fa1 | 306 | """ |
61eed152 CM |
307 | del msg['Date'] |
308 | msg['Date'] = email.Utils.formatdate(localtime = True) | |
309 | msg['Message-ID'] = msg_id | |
310 | if ref_id: | |
00375337 CM |
311 | # make sure the ref id has the angle brackets |
312 | ref_id = '<%s>' % ref_id.strip(' \t\n<>') | |
61eed152 CM |
313 | msg['In-Reply-To'] = ref_id |
314 | msg['References'] = ref_id | |
d5214f56 | 315 | msg['User-Agent'] = 'StGit/%s' % version.version |
61eed152 CM |
316 | |
317 | def __encode_message(msg): | |
318 | # 7 or 8 bit encoding | |
319 | charset = email.Charset.Charset('utf-8') | |
320 | charset.body_encoding = None | |
321 | ||
322 | # encode headers | |
323 | for header, value in msg.items(): | |
324 | words = [] | |
325 | for word in value.split(' '): | |
326 | try: | |
327 | uword = unicode(word, 'utf-8') | |
328 | except UnicodeDecodeError: | |
329 | # maybe we should try a different encoding or report | |
330 | # the error. At the moment, we just ignore it | |
331 | pass | |
332 | words.append(email.Header.Header(uword).encode()) | |
333 | new_val = ' '.join(words) | |
334 | msg.replace_header(header, new_val) | |
335 | ||
336 | # encode the body and set the MIME and encoding headers | |
e5c32acf CM |
337 | if msg.is_multipart(): |
338 | for p in msg.get_payload(): | |
339 | p.set_charset(charset) | |
340 | else: | |
341 | msg.set_charset(charset) | |
19a56fa1 | 342 | |
58c61f10 | 343 | def __edit_message(msg): |
0ba13ee9 KH |
344 | fname = '.stgitmail.txt' |
345 | ||
346 | # create the initial file | |
347 | f = file(fname, 'w') | |
348 | f.write(msg) | |
349 | f.close() | |
350 | ||
83bb4e4c | 351 | call_editor(fname) |
0ba13ee9 KH |
352 | |
353 | # read the message back | |
354 | f = file(fname) | |
355 | msg = f.read() | |
356 | f.close() | |
357 | ||
358 | return msg | |
359 | ||
99c4a4c5 | 360 | def __build_cover(tmpl, patches, msg_id, options): |
e3e05587 | 361 | """Build the cover message (series description) to be sent via SMTP |
b4bddc06 | 362 | """ |
901288c2 | 363 | sender = __get_sender() |
dae0f0be | 364 | |
d0d139a3 CM |
365 | if options.version: |
366 | version_str = ' %s' % options.version | |
ed5de0cc CM |
367 | else: |
368 | version_str = '' | |
d0d139a3 | 369 | |
d323b5da RR |
370 | if options.prefix: |
371 | prefix_str = options.prefix + ' ' | |
372 | else: | |
a7e0d4ee YD |
373 | confprefix = config.get('stgit.mail.prefix') |
374 | if confprefix: | |
375 | prefix_str = confprefix + ' ' | |
376 | else: | |
377 | prefix_str = '' | |
d323b5da | 378 | |
99c4a4c5 | 379 | total_nr_str = str(len(patches)) |
b8d258e5 | 380 | patch_nr_str = '0'.zfill(len(total_nr_str)) |
99c4a4c5 | 381 | if len(patches) > 1: |
b8d258e5 CM |
382 | number_str = ' %s/%s' % (patch_nr_str, total_nr_str) |
383 | else: | |
384 | number_str = '' | |
b4bddc06 | 385 | |
901288c2 CM |
386 | tmpl_dict = {'sender': sender, |
387 | # for backward template compatibility | |
388 | 'maintainer': sender, | |
61eed152 CM |
389 | # for backward template compatibility |
390 | 'endofheaders': '', | |
391 | # for backward template compatibility | |
392 | 'date': '', | |
d0d139a3 | 393 | 'version': version_str, |
d323b5da | 394 | 'prefix': prefix_str, |
b8d258e5 CM |
395 | 'patchnr': patch_nr_str, |
396 | 'totalnr': total_nr_str, | |
99c4a4c5 | 397 | 'number': number_str, |
27827959 KH |
398 | 'shortlog': stack.shortlog(crt_series.get_patch(p) |
399 | for p in patches), | |
ef954fe6 | 400 | 'diffstat': gitlib.diffstat(git.diff( |
e4560d7e CM |
401 | rev1 = git_id(crt_series, '%s^' % patches[0]), |
402 | rev2 = git_id(crt_series, '%s' % patches[-1])))} | |
b4bddc06 CM |
403 | |
404 | try: | |
61eed152 | 405 | msg_string = tmpl % tmpl_dict |
b4bddc06 CM |
406 | except KeyError, err: |
407 | raise CmdException, 'Unknown patch template variable: %s' \ | |
408 | % err | |
409 | except TypeError: | |
410 | raise CmdException, 'Only "%(name)s" variables are ' \ | |
411 | 'supported in the patch template' | |
412 | ||
58c61f10 CM |
413 | if options.edit_cover: |
414 | msg_string = __edit_message(msg_string) | |
415 | ||
61eed152 CM |
416 | # The Python email message |
417 | try: | |
418 | msg = email.message_from_string(msg_string) | |
419 | except Exception, ex: | |
420 | raise CmdException, 'template parsing error: %s' % str(ex) | |
421 | ||
422 | __build_address_headers(msg, options) | |
423 | __build_extra_headers(msg, msg_id, options.refid) | |
424 | __encode_message(msg) | |
425 | ||
d650d6ed | 426 | return msg |
b4bddc06 | 427 | |
2bb96902 | 428 | def __build_message(tmpl, patch, patch_nr, total_nr, msg_id, ref_id, options): |
b4bddc06 CM |
429 | """Build the message to be sent via SMTP |
430 | """ | |
431 | p = crt_series.get_patch(patch) | |
432 | ||
c897c87c AS |
433 | if p.get_description(): |
434 | descr = p.get_description().strip() | |
435 | else: | |
436 | # provide a place holder and force the edit message option on | |
437 | descr = '<empty message>' | |
438 | options.edit_patches = True | |
b4bddc06 | 439 | |
c897c87c | 440 | descr_lines = descr.split('\n') |
42857cbe ST |
441 | short_descr = descr_lines[0].strip() |
442 | long_descr = '\n'.join(l.rstrip() for l in descr_lines[1:]).lstrip('\n') | |
b4bddc06 | 443 | |
1d1485c3 CM |
444 | authname = p.get_authname(); |
445 | authemail = p.get_authemail(); | |
446 | commname = p.get_commname(); | |
447 | commemail = p.get_commemail(); | |
448 | ||
901288c2 | 449 | sender = __get_sender() |
1d1485c3 CM |
450 | |
451 | fromauth = '%s <%s>' % (authname, authemail) | |
901288c2 | 452 | if fromauth != sender: |
1d1485c3 CM |
453 | fromauth = 'From: %s\n\n' % fromauth |
454 | else: | |
455 | fromauth = '' | |
dae0f0be | 456 | |
d0d139a3 CM |
457 | if options.version: |
458 | version_str = ' %s' % options.version | |
ed5de0cc CM |
459 | else: |
460 | version_str = '' | |
d0d139a3 | 461 | |
d323b5da RR |
462 | if options.prefix: |
463 | prefix_str = options.prefix + ' ' | |
464 | else: | |
a7e0d4ee YD |
465 | confprefix = config.get('stgit.mail.prefix') |
466 | if confprefix: | |
467 | prefix_str = confprefix + ' ' | |
468 | else: | |
469 | prefix_str = '' | |
0d219030 | 470 | |
b4bddc06 CM |
471 | total_nr_str = str(total_nr) |
472 | patch_nr_str = str(patch_nr).zfill(len(total_nr_str)) | |
c2a8af1d | 473 | if not options.unrelated and total_nr > 1: |
b8d258e5 CM |
474 | number_str = ' %s/%s' % (patch_nr_str, total_nr_str) |
475 | else: | |
476 | number_str = '' | |
b4bddc06 | 477 | |
e4560d7e CM |
478 | diff = git.diff(rev1 = git_id(crt_series, '%s^' % patch), |
479 | rev2 = git_id(crt_series, '%s' % patch), | |
a45cea15 | 480 | diff_flags = options.diff_flags) |
b4bddc06 | 481 | tmpl_dict = {'patch': patch, |
901288c2 CM |
482 | 'sender': sender, |
483 | # for backward template compatibility | |
484 | 'maintainer': sender, | |
b4bddc06 CM |
485 | 'shortdescr': short_descr, |
486 | 'longdescr': long_descr, | |
61eed152 CM |
487 | # for backward template compatibility |
488 | 'endofheaders': '', | |
a45cea15 | 489 | 'diff': diff, |
ef954fe6 | 490 | 'diffstat': gitlib.diffstat(diff), |
61eed152 CM |
491 | # for backward template compatibility |
492 | 'date': '', | |
d0d139a3 | 493 | 'version': version_str, |
d323b5da | 494 | 'prefix': prefix_str, |
b4bddc06 CM |
495 | 'patchnr': patch_nr_str, |
496 | 'totalnr': total_nr_str, | |
b8d258e5 | 497 | 'number': number_str, |
1d1485c3 CM |
498 | 'fromauth': fromauth, |
499 | 'authname': authname, | |
500 | 'authemail': authemail, | |
b4bddc06 | 501 | 'authdate': p.get_authdate(), |
1d1485c3 CM |
502 | 'commname': commname, |
503 | 'commemail': commemail} | |
61eed152 | 504 | # change None to '' |
b4bddc06 CM |
505 | for key in tmpl_dict: |
506 | if not tmpl_dict[key]: | |
507 | tmpl_dict[key] = '' | |
508 | ||
509 | try: | |
61eed152 | 510 | msg_string = tmpl % tmpl_dict |
b4bddc06 CM |
511 | except KeyError, err: |
512 | raise CmdException, 'Unknown patch template variable: %s' \ | |
513 | % err | |
514 | except TypeError: | |
515 | raise CmdException, 'Only "%(name)s" variables are ' \ | |
516 | 'supported in the patch template' | |
517 | ||
58c61f10 CM |
518 | if options.edit_patches: |
519 | msg_string = __edit_message(msg_string) | |
520 | ||
61eed152 CM |
521 | # The Python email message |
522 | try: | |
523 | msg = email.message_from_string(msg_string) | |
524 | except Exception, ex: | |
525 | raise CmdException, 'template parsing error: %s' % str(ex) | |
526 | ||
527 | if options.auto: | |
528 | extra_cc = __get_signers_list(descr) | |
529 | else: | |
530 | extra_cc = [] | |
531 | ||
532 | __build_address_headers(msg, options, extra_cc) | |
533 | __build_extra_headers(msg, msg_id, ref_id) | |
534 | __encode_message(msg) | |
535 | ||
d650d6ed | 536 | return msg |
b4bddc06 | 537 | |
b4bddc06 CM |
538 | def func(parser, options, args): |
539 | """Send the patches by e-mail using the patchmail.tmpl file as | |
540 | a template | |
541 | """ | |
cec913c4 | 542 | smtpserver = options.smtp_server or config.get('stgit.smtpserver') |
eb026d93 | 543 | |
b4bddc06 | 544 | applied = crt_series.get_applied() |
b4bddc06 | 545 | |
6b1e0111 CM |
546 | if options.all: |
547 | patches = applied | |
548 | elif len(args) >= 1: | |
b4f656f0 CM |
549 | unapplied = crt_series.get_unapplied() |
550 | patches = parse_patches(args, applied + unapplied, len(applied)) | |
b4bddc06 | 551 | else: |
9a316368 | 552 | raise CmdException, 'Incorrect options. Unknown patches to send' |
b4bddc06 | 553 | |
3c04f430 CM |
554 | out.start('Checking the validity of the patches') |
555 | for p in patches: | |
556 | if crt_series.empty_patch(p): | |
557 | raise CmdException, 'Cannot send empty patch "%s"' % p | |
558 | out.done() | |
559 | ||
c73e63b7 YD |
560 | smtppassword = options.smtp_password or config.get('stgit.smtppassword') |
561 | smtpuser = options.smtp_user or config.get('stgit.smtpuser') | |
fc44c2ca | 562 | smtpusetls = options.smtp_tls or config.get('stgit.smtptls') == 'yes' |
eb026d93 B |
563 | |
564 | if (smtppassword and not smtpuser): | |
565 | raise CmdException, 'SMTP password supplied, username needed' | |
fc44c2ca PR |
566 | if (smtpusetls and not smtpuser): |
567 | raise CmdException, 'SMTP over TLS requested, username needed' | |
6cf5ec9b PR |
568 | if (smtpuser and not smtppassword): |
569 | smtppassword = getpass.getpass("Please enter SMTP password: ") | |
eb026d93 | 570 | |
b4bddc06 | 571 | total_nr = len(patches) |
9a316368 CM |
572 | if total_nr == 0: |
573 | raise CmdException, 'No patches to send' | |
b4bddc06 | 574 | |
c2a8af1d CM |
575 | if options.refid: |
576 | if options.noreply or options.unrelated: | |
577 | raise CmdException, \ | |
578 | '--refid option not allowed with --noreply or --unrelated' | |
d1ed3a12 | 579 | ref_id = options.refid |
c2a8af1d CM |
580 | else: |
581 | ref_id = None | |
b4bddc06 | 582 | |
c73e63b7 | 583 | sleep = options.sleep or config.getint('stgit.smtpdelay') |
b4bddc06 | 584 | |
e3e05587 | 585 | # send the cover message (if any) |
0ba13ee9 | 586 | if options.cover or options.edit_cover: |
c2a8af1d CM |
587 | if options.unrelated: |
588 | raise CmdException, 'cover sending not allowed with --unrelated' | |
589 | ||
e3e05587 CM |
590 | # find the template file |
591 | if options.cover: | |
16fee874 | 592 | tmpl = file(options.cover).read() |
e3e05587 | 593 | else: |
1f3bb017 CM |
594 | tmpl = templates.get_template('covermail.tmpl') |
595 | if not tmpl: | |
596 | raise CmdException, 'No cover message template file found' | |
b4bddc06 CM |
597 | |
598 | msg_id = email.Utils.make_msgid('stgit') | |
99c4a4c5 | 599 | msg = __build_cover(tmpl, patches, msg_id, options) |
2bb96902 | 600 | from_addr, to_addr_list = __parse_addresses(msg) |
b4bddc06 | 601 | |
d650d6ed CM |
602 | msg_string = msg.as_string(options.mbox) |
603 | ||
b4bddc06 | 604 | # subsequent e-mails are seen as replies to the first one |
d1ed3a12 CM |
605 | if not options.noreply: |
606 | ref_id = msg_id | |
b4bddc06 | 607 | |
29f00589 | 608 | if options.mbox: |
27ac2b7e | 609 | out.stdout_raw(msg_string + '\n') |
29f00589 | 610 | else: |
27ac2b7e | 611 | out.start('Sending the cover message') |
d650d6ed | 612 | __send_message(smtpserver, from_addr, to_addr_list, msg_string, |
fc44c2ca | 613 | sleep, smtpuser, smtppassword, smtpusetls) |
27ac2b7e | 614 | out.done() |
b4bddc06 CM |
615 | |
616 | # send the patches | |
617 | if options.template: | |
1f3bb017 | 618 | tmpl = file(options.template).read() |
b4bddc06 | 619 | else: |
e5c32acf CM |
620 | if options.attach: |
621 | tmpl = templates.get_template('mailattch.tmpl') | |
622 | else: | |
623 | tmpl = templates.get_template('patchmail.tmpl') | |
1f3bb017 CM |
624 | if not tmpl: |
625 | raise CmdException, 'No e-mail template file found' | |
b4bddc06 CM |
626 | |
627 | for (p, patch_nr) in zip(patches, range(1, len(patches) + 1)): | |
628 | msg_id = email.Utils.make_msgid('stgit') | |
2bb96902 CM |
629 | msg = __build_message(tmpl, p, patch_nr, total_nr, msg_id, ref_id, |
630 | options) | |
631 | from_addr, to_addr_list = __parse_addresses(msg) | |
632 | ||
d650d6ed CM |
633 | msg_string = msg.as_string(options.mbox) |
634 | ||
b4bddc06 | 635 | # subsequent e-mails are seen as replies to the first one |
c2a8af1d | 636 | if not options.noreply and not options.unrelated and not ref_id: |
b4bddc06 CM |
637 | ref_id = msg_id |
638 | ||
29f00589 | 639 | if options.mbox: |
27ac2b7e | 640 | out.stdout_raw(msg_string + '\n') |
29f00589 | 641 | else: |
27ac2b7e | 642 | out.start('Sending patch "%s"' % p) |
d650d6ed | 643 | __send_message(smtpserver, from_addr, to_addr_list, msg_string, |
fc44c2ca | 644 | sleep, smtpuser, smtppassword, smtpusetls) |
27ac2b7e | 645 | out.done() |