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