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' |
54239abf | 31 | usage = [' [options] [--] [<patch1>] [<patch2>] [<patch3>..<patch4>]'] |
575bbdae | 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', | |
97ccbbbc AC |
143 | short = 'Generate an mbox file instead of sending'), |
144 | opt('--git', action = 'store_true', | |
145 | short = 'Use git send-email (EXPERIMENTAL)') | |
575bbdae KH |
146 | ] + argparse.diff_opts_option() |
147 | ||
117ed129 | 148 | directory = DirectoryHasRepository(log = False) |
b4bddc06 | 149 | |
901288c2 | 150 | def __get_sender(): |
dae0f0be CM |
151 | """Return the 'authname <authemail>' string as read from the |
152 | configuration file | |
153 | """ | |
c73e63b7 YD |
154 | sender=config.get('stgit.sender') |
155 | if not sender: | |
9e3f506f KH |
156 | try: |
157 | sender = str(git.user()) | |
158 | except git.GitException: | |
ea09f8ce GH |
159 | try: |
160 | sender = str(git.author()) | |
161 | except git.GitException: | |
162 | pass | |
9e3f506f | 163 | if not sender: |
ea09f8ce GH |
164 | raise CmdException, ('Unknown sender name and e-mail; you should' |
165 | ' for example set git config user.name and' | |
166 | ' user.email') | |
cd74a041 CM |
167 | sender = email.Utils.parseaddr(sender) |
168 | ||
169 | return email.Utils.formataddr(address_or_alias(sender)) | |
dae0f0be | 170 | |
cd74a041 CM |
171 | def __addr_list(msg, header): |
172 | return [addr for name, addr in | |
173 | email.Utils.getaddresses(msg.get_all(header, []))] | |
9e3f506f | 174 | |
d650d6ed | 175 | def __parse_addresses(msg): |
b4bddc06 CM |
176 | """Return a two elements tuple: (from, [to]) |
177 | """ | |
d650d6ed | 178 | from_addr_list = __addr_list(msg, 'From') |
24aadb3f | 179 | if len(from_addr_list) == 0: |
b4bddc06 | 180 | raise CmdException, 'No "From" address' |
d650d6ed CM |
181 | |
182 | to_addr_list = __addr_list(msg, 'To') + __addr_list(msg, 'Cc') \ | |
183 | + __addr_list(msg, 'Bcc') | |
b4bddc06 CM |
184 | if len(to_addr_list) == 0: |
185 | raise CmdException, 'No "To/Cc/Bcc" addresses' | |
186 | ||
cd74a041 | 187 | return (from_addr_list[0], set(to_addr_list)) |
b4bddc06 | 188 | |
a0fe60a2 CM |
189 | def __send_message_sendmail(sendmail, msg): |
190 | """Send the message using the sendmail command. | |
191 | """ | |
192 | cmd = sendmail.split() | |
193 | Run(*cmd).raw_input(msg).discard_output() | |
194 | ||
89d7ec43 | 195 | def __send_message_smtp(smtpserver, from_addr, to_addr_list, msg, options): |
b4bddc06 CM |
196 | """Send the message using the given SMTP server |
197 | """ | |
89d7ec43 AC |
198 | smtppassword = options.smtp_password or config.get('stgit.smtppassword') |
199 | smtpuser = options.smtp_user or config.get('stgit.smtpuser') | |
200 | smtpusetls = options.smtp_tls or config.get('stgit.smtptls') == 'yes' | |
201 | ||
202 | if (smtppassword and not smtpuser): | |
203 | raise CmdException('SMTP password supplied, username needed') | |
204 | if (smtpusetls and not smtpuser): | |
205 | raise CmdException('SMTP over TLS requested, username needed') | |
206 | if (smtpuser and not smtppassword): | |
207 | smtppassword = getpass.getpass("Please enter SMTP password: ") | |
208 | ||
b4bddc06 CM |
209 | try: |
210 | s = smtplib.SMTP(smtpserver) | |
211 | except Exception, err: | |
212 | raise CmdException, str(err) | |
213 | ||
214 | s.set_debuglevel(0) | |
215 | try: | |
eb026d93 B |
216 | if smtpuser and smtppassword: |
217 | s.ehlo() | |
89d7ec43 | 218 | if smtpusetls: |
fc44c2ca PR |
219 | if not hasattr(socket, 'ssl'): |
220 | raise CmdException, "cannot use TLS - no SSL support in Python" | |
221 | s.starttls() | |
222 | s.ehlo() | |
eb026d93 B |
223 | s.login(smtpuser, smtppassword) |
224 | ||
0bc1343c YD |
225 | result = s.sendmail(from_addr, to_addr_list, msg) |
226 | if len(result): | |
227 | print "mail server refused delivery for the following recipients: %s" % result | |
b4bddc06 CM |
228 | except Exception, err: |
229 | raise CmdException, str(err) | |
230 | ||
231 | s.quit() | |
232 | ||
97ccbbbc AC |
233 | def __send_message_git(msg, options): |
234 | """Send the message using git send-email | |
235 | """ | |
236 | from subprocess import call | |
237 | from tempfile import mkstemp | |
238 | ||
239 | cmd = ["git", "send-email", "--from=%s" % msg['From']] | |
240 | cmd.append("--quiet") | |
241 | cmd.append("--suppress-cc=self") | |
242 | if not options.auto: | |
243 | cmd.append("--suppress-cc=body") | |
244 | ||
245 | # We only support To/Cc/Bcc in git send-email for now. | |
246 | for x in ['to', 'cc', 'bcc']: | |
247 | if getattr(options, x): | |
248 | cmd.extend('--%s=%s' % (x, a) for a in getattr(options, x)) | |
249 | ||
250 | # XXX: hack for now so that we don't duplicate To/Cc/Bcc headers | |
251 | # in the mail, as git send-email inserts those for us. | |
252 | del msg['To'] | |
253 | del msg['Cc'] | |
254 | del msg['Bcc'] | |
255 | ||
256 | (fd, path) = mkstemp() | |
257 | os.write(fd, msg.as_string(options.mbox)) | |
258 | os.close(fd) | |
259 | ||
260 | try: | |
261 | try: | |
262 | cmd.append(path) | |
263 | call(cmd) | |
264 | except Exception, err: | |
265 | raise CmdException, str(err) | |
266 | finally: | |
267 | os.unlink(path) | |
268 | ||
9cb369d4 | 269 | def __send_message(type, tmpl, options, *args): |
a0fe60a2 CM |
270 | """Message sending dispatcher. |
271 | """ | |
9cb369d4 AC |
272 | (build, outstr) = {'cover': (__build_cover, 'the cover message'), |
273 | 'patch': (__build_message, 'patch "%s"' % args[0])}[type] | |
274 | if type == 'patch': | |
275 | (patch_nr, total_nr) = (args[1], args[2]) | |
276 | ||
277 | msg_id = email.Utils.make_msgid('stgit') | |
278 | msg = build(tmpl, msg_id, options, *args) | |
279 | ||
280 | from_addr, to_addrs = __parse_addresses(msg) | |
281 | msg_str = msg.as_string(options.mbox) | |
282 | if options.mbox: | |
283 | out.stdout_raw(msg_str + '\n') | |
284 | return msg_id | |
285 | ||
97ccbbbc AC |
286 | if not options.git: |
287 | out.start('Sending ' + outstr) | |
89d7ec43 | 288 | |
9cb369d4 | 289 | smtpserver = options.smtp_server or config.get('stgit.smtpserver') |
97ccbbbc AC |
290 | if options.git: |
291 | __send_message_git(msg, options) | |
292 | elif smtpserver.startswith('/'): | |
a0fe60a2 | 293 | # Use the sendmail tool |
9cb369d4 | 294 | __send_message_sendmail(smtpserver, msg_str) |
a0fe60a2 CM |
295 | else: |
296 | # Use the SMTP server (we have host and port information) | |
9cb369d4 AC |
297 | __send_message_smtp(smtpserver, from_addr, to_addrs, msg_str, options) |
298 | ||
299 | # give recipients a chance of receiving related patches in correct order | |
300 | if type == 'cover' or (type == 'patch' and patch_nr < total_nr): | |
301 | sleep = options.sleep or config.getint('stgit.smtpdelay') | |
302 | time.sleep(sleep) | |
97ccbbbc AC |
303 | if not options.git: |
304 | out.done() | |
9cb369d4 | 305 | return msg_id |
a0fe60a2 | 306 | |
c28b8841 | 307 | def __update_header(msg, header, addr = '', ignore = ()): |
cd74a041 CM |
308 | def __addr_pairs(msg, header, extra): |
309 | pairs = email.Utils.getaddresses(msg.get_all(header, []) + extra) | |
310 | # remove pairs without an address and resolve the aliases | |
311 | return [address_or_alias(p) for p in pairs if p[1]] | |
312 | ||
c28b8841 AC |
313 | addr_pairs = __addr_pairs(msg, header, [addr]) |
314 | del msg[header] | |
315 | # remove the duplicates and filter the addresses | |
316 | addr_dict = dict((addr, email.Utils.formataddr((name, addr))) | |
317 | for name, addr in addr_pairs if addr not in ignore) | |
318 | if addr_dict: | |
319 | msg[header] = ', '.join(addr_dict.itervalues()) | |
320 | return set(addr_dict.iterkeys()) | |
f8d1cf65 | 321 | |
c28b8841 AC |
322 | def __build_address_headers(msg, options, extra_cc = []): |
323 | """Build the address headers and check existing headers in the | |
324 | template. | |
325 | """ | |
f8d1cf65 CM |
326 | to_addr = '' |
327 | cc_addr = '' | |
cd74a041 | 328 | extra_cc_addr = '' |
f8d1cf65 CM |
329 | bcc_addr = '' |
330 | ||
c73e63b7 | 331 | autobcc = config.get('stgit.autobcc') or '' |
d884c4d8 | 332 | |
e83b3149 | 333 | if options.to: |
61eed152 | 334 | to_addr = ', '.join(options.to) |
e83b3149 | 335 | if options.cc: |
cd74a041 CM |
336 | cc_addr = ', '.join(options.cc) |
337 | if extra_cc: | |
338 | extra_cc_addr = ', '.join(extra_cc) | |
e83b3149 | 339 | if options.bcc: |
61eed152 | 340 | bcc_addr = ', '.join(options.bcc + [autobcc]) |
d884c4d8 CM |
341 | elif autobcc: |
342 | bcc_addr = autobcc | |
f8d1cf65 | 343 | |
cd74a041 | 344 | # if an address is on a header, ignore it from the rest |
c28b8841 AC |
345 | to_set = __update_header(msg, 'To', to_addr) |
346 | cc_set = __update_header(msg, 'Cc', cc_addr, to_set) | |
347 | bcc_set = __update_header(msg, 'Bcc', bcc_addr, to_set.union(cc_set)) | |
cd74a041 CM |
348 | |
349 | # --auto generated addresses, don't include the sender | |
c28b8841 AC |
350 | from_set = __update_header(msg, 'From') |
351 | __update_header(msg, 'Cc', extra_cc_addr, | |
352 | to_set.union(bcc_set).union(from_set)) | |
f8d1cf65 CM |
353 | |
354 | def __get_signers_list(msg): | |
355 | """Return the address list generated from signed-off-by and | |
356 | acked-by lines in the message. | |
357 | """ | |
358 | addr_list = [] | |
de41ecc8 DW |
359 | tags = '%s|%s|%s|%s|%s|%s|%s' % ( |
360 | 'signed-off-by', | |
361 | 'acked-by', | |
362 | 'cc', | |
363 | 'reviewed-by', | |
364 | 'reported-by', | |
365 | 'tested-by', | |
366 | 'reported-and-tested-by') | |
367 | regex = '^(%s):\s+(.+)$' % tags | |
368 | ||
369 | r = re.compile(regex, re.I) | |
f8d1cf65 CM |
370 | for line in msg.split('\n'): |
371 | m = r.match(line) | |
372 | if m: | |
373 | addr_list.append(m.expand('\g<2>')) | |
374 | ||
375 | return addr_list | |
e83b3149 | 376 | |
61eed152 CM |
377 | def __build_extra_headers(msg, msg_id, ref_id = None): |
378 | """Build extra email headers and encoding | |
19a56fa1 | 379 | """ |
61eed152 CM |
380 | del msg['Date'] |
381 | msg['Date'] = email.Utils.formatdate(localtime = True) | |
382 | msg['Message-ID'] = msg_id | |
383 | if ref_id: | |
00375337 CM |
384 | # make sure the ref id has the angle brackets |
385 | ref_id = '<%s>' % ref_id.strip(' \t\n<>') | |
61eed152 CM |
386 | msg['In-Reply-To'] = ref_id |
387 | msg['References'] = ref_id | |
d5214f56 | 388 | msg['User-Agent'] = 'StGit/%s' % version.version |
61eed152 | 389 | |
c28b8841 AC |
390 | # update other address headers |
391 | __update_header(msg, 'Reply-To') | |
392 | __update_header(msg, 'Mail-Reply-To') | |
393 | __update_header(msg, 'Mail-Followup-To') | |
394 | ||
395 | ||
61eed152 CM |
396 | def __encode_message(msg): |
397 | # 7 or 8 bit encoding | |
398 | charset = email.Charset.Charset('utf-8') | |
399 | charset.body_encoding = None | |
400 | ||
401 | # encode headers | |
402 | for header, value in msg.items(): | |
403 | words = [] | |
404 | for word in value.split(' '): | |
405 | try: | |
406 | uword = unicode(word, 'utf-8') | |
407 | except UnicodeDecodeError: | |
408 | # maybe we should try a different encoding or report | |
409 | # the error. At the moment, we just ignore it | |
410 | pass | |
411 | words.append(email.Header.Header(uword).encode()) | |
412 | new_val = ' '.join(words) | |
413 | msg.replace_header(header, new_val) | |
414 | ||
415 | # encode the body and set the MIME and encoding headers | |
e5c32acf CM |
416 | if msg.is_multipart(): |
417 | for p in msg.get_payload(): | |
418 | p.set_charset(charset) | |
419 | else: | |
420 | msg.set_charset(charset) | |
19a56fa1 | 421 | |
58c61f10 | 422 | def __edit_message(msg): |
0ba13ee9 KH |
423 | fname = '.stgitmail.txt' |
424 | ||
425 | # create the initial file | |
426 | f = file(fname, 'w') | |
427 | f.write(msg) | |
428 | f.close() | |
429 | ||
83bb4e4c | 430 | call_editor(fname) |
0ba13ee9 KH |
431 | |
432 | # read the message back | |
433 | f = file(fname) | |
434 | msg = f.read() | |
435 | f.close() | |
436 | ||
437 | return msg | |
438 | ||
c18d3a36 | 439 | def __build_cover(tmpl, msg_id, options, patches): |
e3e05587 | 440 | """Build the cover message (series description) to be sent via SMTP |
b4bddc06 | 441 | """ |
901288c2 | 442 | sender = __get_sender() |
dae0f0be | 443 | |
d0d139a3 CM |
444 | if options.version: |
445 | version_str = ' %s' % options.version | |
ed5de0cc CM |
446 | else: |
447 | version_str = '' | |
d0d139a3 | 448 | |
d323b5da RR |
449 | if options.prefix: |
450 | prefix_str = options.prefix + ' ' | |
451 | else: | |
a7e0d4ee YD |
452 | confprefix = config.get('stgit.mail.prefix') |
453 | if confprefix: | |
454 | prefix_str = confprefix + ' ' | |
455 | else: | |
456 | prefix_str = '' | |
d323b5da | 457 | |
99c4a4c5 | 458 | total_nr_str = str(len(patches)) |
b8d258e5 | 459 | patch_nr_str = '0'.zfill(len(total_nr_str)) |
99c4a4c5 | 460 | if len(patches) > 1: |
b8d258e5 CM |
461 | number_str = ' %s/%s' % (patch_nr_str, total_nr_str) |
462 | else: | |
463 | number_str = '' | |
b4bddc06 | 464 | |
901288c2 CM |
465 | tmpl_dict = {'sender': sender, |
466 | # for backward template compatibility | |
467 | 'maintainer': sender, | |
61eed152 CM |
468 | # for backward template compatibility |
469 | 'endofheaders': '', | |
470 | # for backward template compatibility | |
471 | 'date': '', | |
d0d139a3 | 472 | 'version': version_str, |
d323b5da | 473 | 'prefix': prefix_str, |
b8d258e5 CM |
474 | 'patchnr': patch_nr_str, |
475 | 'totalnr': total_nr_str, | |
99c4a4c5 | 476 | 'number': number_str, |
27827959 | 477 | 'shortlog': stack.shortlog(crt_series.get_patch(p) |
c9379a15 | 478 | for p in reversed(patches)), |
ef954fe6 | 479 | 'diffstat': gitlib.diffstat(git.diff( |
e4560d7e | 480 | rev1 = git_id(crt_series, '%s^' % patches[0]), |
baf8241d CM |
481 | rev2 = git_id(crt_series, '%s' % patches[-1]), |
482 | diff_flags = options.diff_flags))} | |
b4bddc06 CM |
483 | |
484 | try: | |
61eed152 | 485 | msg_string = tmpl % tmpl_dict |
b4bddc06 CM |
486 | except KeyError, err: |
487 | raise CmdException, 'Unknown patch template variable: %s' \ | |
488 | % err | |
489 | except TypeError: | |
490 | raise CmdException, 'Only "%(name)s" variables are ' \ | |
491 | 'supported in the patch template' | |
492 | ||
58c61f10 CM |
493 | if options.edit_cover: |
494 | msg_string = __edit_message(msg_string) | |
495 | ||
61eed152 CM |
496 | # The Python email message |
497 | try: | |
498 | msg = email.message_from_string(msg_string) | |
499 | except Exception, ex: | |
500 | raise CmdException, 'template parsing error: %s' % str(ex) | |
501 | ||
502 | __build_address_headers(msg, options) | |
503 | __build_extra_headers(msg, msg_id, options.refid) | |
504 | __encode_message(msg) | |
505 | ||
d650d6ed | 506 | return msg |
b4bddc06 | 507 | |
c18d3a36 | 508 | def __build_message(tmpl, msg_id, options, patch, patch_nr, total_nr, ref_id): |
b4bddc06 CM |
509 | """Build the message to be sent via SMTP |
510 | """ | |
511 | p = crt_series.get_patch(patch) | |
512 | ||
c897c87c AS |
513 | if p.get_description(): |
514 | descr = p.get_description().strip() | |
515 | else: | |
516 | # provide a place holder and force the edit message option on | |
517 | descr = '<empty message>' | |
518 | options.edit_patches = True | |
b4bddc06 | 519 | |
c897c87c | 520 | descr_lines = descr.split('\n') |
42857cbe ST |
521 | short_descr = descr_lines[0].strip() |
522 | long_descr = '\n'.join(l.rstrip() for l in descr_lines[1:]).lstrip('\n') | |
b4bddc06 | 523 | |
1d1485c3 CM |
524 | authname = p.get_authname(); |
525 | authemail = p.get_authemail(); | |
526 | commname = p.get_commname(); | |
527 | commemail = p.get_commemail(); | |
528 | ||
901288c2 | 529 | sender = __get_sender() |
1d1485c3 CM |
530 | |
531 | fromauth = '%s <%s>' % (authname, authemail) | |
901288c2 | 532 | if fromauth != sender: |
1d1485c3 CM |
533 | fromauth = 'From: %s\n\n' % fromauth |
534 | else: | |
535 | fromauth = '' | |
dae0f0be | 536 | |
d0d139a3 CM |
537 | if options.version: |
538 | version_str = ' %s' % options.version | |
ed5de0cc CM |
539 | else: |
540 | version_str = '' | |
d0d139a3 | 541 | |
d323b5da RR |
542 | if options.prefix: |
543 | prefix_str = options.prefix + ' ' | |
544 | else: | |
a7e0d4ee YD |
545 | confprefix = config.get('stgit.mail.prefix') |
546 | if confprefix: | |
547 | prefix_str = confprefix + ' ' | |
548 | else: | |
549 | prefix_str = '' | |
0d219030 | 550 | |
b4bddc06 CM |
551 | total_nr_str = str(total_nr) |
552 | patch_nr_str = str(patch_nr).zfill(len(total_nr_str)) | |
c2a8af1d | 553 | if not options.unrelated and total_nr > 1: |
b8d258e5 CM |
554 | number_str = ' %s/%s' % (patch_nr_str, total_nr_str) |
555 | else: | |
556 | number_str = '' | |
b4bddc06 | 557 | |
e4560d7e CM |
558 | diff = git.diff(rev1 = git_id(crt_series, '%s^' % patch), |
559 | rev2 = git_id(crt_series, '%s' % patch), | |
a45cea15 | 560 | diff_flags = options.diff_flags) |
b4bddc06 | 561 | tmpl_dict = {'patch': patch, |
901288c2 CM |
562 | 'sender': sender, |
563 | # for backward template compatibility | |
564 | 'maintainer': sender, | |
b4bddc06 CM |
565 | 'shortdescr': short_descr, |
566 | 'longdescr': long_descr, | |
61eed152 CM |
567 | # for backward template compatibility |
568 | 'endofheaders': '', | |
a45cea15 | 569 | 'diff': diff, |
ef954fe6 | 570 | 'diffstat': gitlib.diffstat(diff), |
61eed152 CM |
571 | # for backward template compatibility |
572 | 'date': '', | |
d0d139a3 | 573 | 'version': version_str, |
d323b5da | 574 | 'prefix': prefix_str, |
b4bddc06 CM |
575 | 'patchnr': patch_nr_str, |
576 | 'totalnr': total_nr_str, | |
b8d258e5 | 577 | 'number': number_str, |
1d1485c3 CM |
578 | 'fromauth': fromauth, |
579 | 'authname': authname, | |
580 | 'authemail': authemail, | |
b4bddc06 | 581 | 'authdate': p.get_authdate(), |
1d1485c3 CM |
582 | 'commname': commname, |
583 | 'commemail': commemail} | |
61eed152 | 584 | # change None to '' |
b4bddc06 CM |
585 | for key in tmpl_dict: |
586 | if not tmpl_dict[key]: | |
587 | tmpl_dict[key] = '' | |
588 | ||
589 | try: | |
61eed152 | 590 | msg_string = tmpl % tmpl_dict |
b4bddc06 CM |
591 | except KeyError, err: |
592 | raise CmdException, 'Unknown patch template variable: %s' \ | |
593 | % err | |
594 | except TypeError: | |
595 | raise CmdException, 'Only "%(name)s" variables are ' \ | |
596 | 'supported in the patch template' | |
597 | ||
58c61f10 CM |
598 | if options.edit_patches: |
599 | msg_string = __edit_message(msg_string) | |
600 | ||
61eed152 CM |
601 | # The Python email message |
602 | try: | |
603 | msg = email.message_from_string(msg_string) | |
604 | except Exception, ex: | |
605 | raise CmdException, 'template parsing error: %s' % str(ex) | |
606 | ||
607 | if options.auto: | |
608 | extra_cc = __get_signers_list(descr) | |
609 | else: | |
610 | extra_cc = [] | |
611 | ||
612 | __build_address_headers(msg, options, extra_cc) | |
613 | __build_extra_headers(msg, msg_id, ref_id) | |
614 | __encode_message(msg) | |
615 | ||
d650d6ed | 616 | return msg |
b4bddc06 | 617 | |
b4bddc06 CM |
618 | def func(parser, options, args): |
619 | """Send the patches by e-mail using the patchmail.tmpl file as | |
620 | a template | |
621 | """ | |
b4bddc06 | 622 | applied = crt_series.get_applied() |
b4bddc06 | 623 | |
6b1e0111 CM |
624 | if options.all: |
625 | patches = applied | |
626 | elif len(args) >= 1: | |
b4f656f0 CM |
627 | unapplied = crt_series.get_unapplied() |
628 | patches = parse_patches(args, applied + unapplied, len(applied)) | |
b4bddc06 | 629 | else: |
9a316368 | 630 | raise CmdException, 'Incorrect options. Unknown patches to send' |
b4bddc06 | 631 | |
ea09f8ce GH |
632 | # early test for sender identity |
633 | __get_sender() | |
634 | ||
3c04f430 CM |
635 | out.start('Checking the validity of the patches') |
636 | for p in patches: | |
637 | if crt_series.empty_patch(p): | |
638 | raise CmdException, 'Cannot send empty patch "%s"' % p | |
639 | out.done() | |
640 | ||
b4bddc06 | 641 | total_nr = len(patches) |
9a316368 CM |
642 | if total_nr == 0: |
643 | raise CmdException, 'No patches to send' | |
b4bddc06 | 644 | |
c2a8af1d CM |
645 | if options.refid: |
646 | if options.noreply or options.unrelated: | |
647 | raise CmdException, \ | |
648 | '--refid option not allowed with --noreply or --unrelated' | |
d1ed3a12 | 649 | ref_id = options.refid |
c2a8af1d CM |
650 | else: |
651 | ref_id = None | |
b4bddc06 | 652 | |
b4bddc06 | 653 | |
e3e05587 | 654 | # send the cover message (if any) |
0ba13ee9 | 655 | if options.cover or options.edit_cover: |
c2a8af1d CM |
656 | if options.unrelated: |
657 | raise CmdException, 'cover sending not allowed with --unrelated' | |
658 | ||
e3e05587 CM |
659 | # find the template file |
660 | if options.cover: | |
16fee874 | 661 | tmpl = file(options.cover).read() |
e3e05587 | 662 | else: |
1f3bb017 CM |
663 | tmpl = templates.get_template('covermail.tmpl') |
664 | if not tmpl: | |
665 | raise CmdException, 'No cover message template file found' | |
b4bddc06 | 666 | |
9cb369d4 | 667 | msg_id = __send_message('cover', tmpl, options, patches) |
d650d6ed | 668 | |
b4bddc06 | 669 | # subsequent e-mails are seen as replies to the first one |
d1ed3a12 CM |
670 | if not options.noreply: |
671 | ref_id = msg_id | |
b4bddc06 | 672 | |
b4bddc06 CM |
673 | # send the patches |
674 | if options.template: | |
1f3bb017 | 675 | tmpl = file(options.template).read() |
b4bddc06 | 676 | else: |
e5c32acf CM |
677 | if options.attach: |
678 | tmpl = templates.get_template('mailattch.tmpl') | |
679 | else: | |
680 | tmpl = templates.get_template('patchmail.tmpl') | |
1f3bb017 CM |
681 | if not tmpl: |
682 | raise CmdException, 'No e-mail template file found' | |
b4bddc06 | 683 | |
9cb369d4 AC |
684 | for (p, n) in zip(patches, range(1, total_nr + 1)): |
685 | msg_id = __send_message('patch', tmpl, options, p, n, total_nr, ref_id) | |
d650d6ed | 686 | |
b4bddc06 | 687 | # subsequent e-mails are seen as replies to the first one |
c2a8af1d | 688 | if not options.noreply and not options.unrelated and not ref_id: |
b4bddc06 | 689 | ref_id = msg_id |