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 | ||
29f00589 | 18 | import sys, os, re, time, datetime, smtplib, email.Utils |
b4bddc06 | 19 | from optparse import OptionParser, make_option |
b4bddc06 CM |
20 | |
21 | from stgit.commands.common import * | |
22 | from stgit.utils import * | |
23 | from stgit import stack, git | |
24 | from stgit.config import config | |
25 | ||
26 | ||
27 | help = 'send a patch or series of patches by e-mail' | |
ddab48a5 | 28 | usage = """%prog [options] [<patch> [<patch2...]] |
26aab5b0 CM |
29 | |
30 | Send a patch or a range of patches (defaulting to the applied patches) | |
2bb96902 CM |
31 | by e-mail using the 'smtpserver' configuration option. The From |
32 | address and the e-mail format are generated from the template file | |
33 | passed as argument to '--template' (defaulting to .git/patchmail.tmpl | |
34 | or /usr/share/stgit/templates/patchmail.tmpl). The To/Cc/Bcc addresses | |
35 | can either be added to the template file or passed via the | |
36 | corresponding command line options. | |
37 | ||
e3e05587 CM |
38 | A preamble e-mail can be sent using the '--cover' and/or '--edit' |
39 | options. The first allows the user to specify a file to be used as a | |
40 | template. The latter option will invoke the editor on the specified | |
41 | file (defaulting to .git/covermail.tmpl or | |
42 | /usr/share/stgit/templates/covermail.tmpl). | |
43 | ||
44 | All the subsequent e-mails appear as replies to the first e-mail sent | |
45 | (either the preamble or the first patch). E-mails can be seen as | |
46 | replies to a different e-mail by using the '--refid' option. | |
26aab5b0 CM |
47 | |
48 | SMTP authentication is also possible with '--smtp-user' and | |
49 | '--smtp-password' options, also available as configuration settings: | |
50 | 'smtpuser' and 'smtppassword'. | |
51 | ||
52 | The template e-mail headers and body must be separated by | |
53 | '%(endofheaders)s' variable, which is replaced by StGIT with | |
54 | additional headers and a blank line. The patch e-mail template accepts | |
55 | the following variables: | |
56 | ||
57 | %(patch)s - patch name | |
dae0f0be | 58 | %(maintainer)s - 'authname <authemail>' as read from 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 | |
61 | %(endofheaders)s - delimiter between e-mail headers and body | |
62 | %(diff)s - unified diff of the patch | |
63 | %(diffstat)s - diff statistics | |
64 | %(date)s - current date/time | |
d0d139a3 | 65 | %(version)s - ' version' string passed on the command line (or empty) |
26aab5b0 CM |
66 | %(patchnr)s - patch number |
67 | %(totalnr)s - total number of patches to be sent | |
b8d258e5 | 68 | %(number)s - empty if only one patch is sent or ' patchnr/totalnr' |
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 | ||
dae0f0be | 75 | For the preamble e-mail template, only the %(maintainer)s, %(date)s, |
d0d139a3 CM |
76 | %(endofheaders)s, %(version)s, %(patchnr)s, %(totalnr)s and %(number)s |
77 | variables are supported.""" | |
b4bddc06 | 78 | |
9a316368 CM |
79 | options = [make_option('-a', '--all', |
80 | help = 'e-mail all the applied patches', | |
81 | action = 'store_true'), | |
b4bddc06 CM |
82 | make_option('-r', '--range', |
83 | metavar = '[PATCH1][:[PATCH2]]', | |
84 | help = 'e-mail patches between PATCH1 and PATCH2'), | |
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'), | |
d0d139a3 CM |
94 | make_option('-v', '--version', metavar = 'VERSION', |
95 | help = 'add VERSION to the [PATCH ...] prefix'), | |
9a316368 CM |
96 | make_option('-t', '--template', metavar = 'FILE', |
97 | help = 'use FILE as the message template'), | |
e3e05587 CM |
98 | make_option('-c', '--cover', metavar = 'FILE', |
99 | help = 'send FILE as the cover message'), | |
100 | make_option('-e', '--edit', | |
101 | help = 'edit the cover message before sending', | |
102 | action = 'store_true'), | |
b4bddc06 CM |
103 | make_option('-s', '--sleep', type = 'int', metavar = 'SECONDS', |
104 | help = 'sleep for SECONDS between e-mails sending'), | |
105 | make_option('--refid', | |
d0d139a3 | 106 | help = 'use REFID as the reference id'), |
eb026d93 B |
107 | make_option('-u', '--smtp-user', metavar = 'USER', |
108 | help = 'username for SMTP authentication'), | |
109 | make_option('-p', '--smtp-password', metavar = 'PASSWORD', | |
2f7c8b0b CM |
110 | help = 'username for SMTP authentication'), |
111 | make_option('-b', '--branch', | |
29f00589 CM |
112 | help = 'use BRANCH instead of the default one'), |
113 | make_option('-m', '--mbox', | |
114 | help = 'generate an mbox file instead of sending', | |
115 | action = 'store_true')] | |
b4bddc06 CM |
116 | |
117 | ||
dae0f0be CM |
118 | def __get_maintainer(): |
119 | """Return the 'authname <authemail>' string as read from the | |
120 | configuration file | |
121 | """ | |
122 | if config.has_option('stgit', 'authname') \ | |
123 | and config.has_option('stgit', 'authemail'): | |
124 | return '%s <%s>' % (config.get('stgit', 'authname'), | |
125 | config.get('stgit', 'authemail')) | |
126 | else: | |
127 | return None | |
128 | ||
7cc615f3 | 129 | def __parse_addresses(addresses): |
b4bddc06 CM |
130 | """Return a two elements tuple: (from, [to]) |
131 | """ | |
7cc615f3 CL |
132 | def __addr_list(addrs): |
133 | m = re.search('[^@\s<,]+@[^>\s,]+', addrs); | |
d60cd083 CM |
134 | if (m == None): |
135 | return [] | |
7cc615f3 | 136 | return [ m.group() ] + __addr_list(addrs[m.end():]) |
b4bddc06 CM |
137 | |
138 | from_addr_list = [] | |
139 | to_addr_list = [] | |
7cc615f3 | 140 | for line in addresses.split('\n'): |
b4bddc06 CM |
141 | if re.match('from:\s+', line, re.I): |
142 | from_addr_list += __addr_list(line) | |
143 | elif re.match('(to|cc|bcc):\s+', line, re.I): | |
144 | to_addr_list += __addr_list(line) | |
145 | ||
24aadb3f | 146 | if len(from_addr_list) == 0: |
b4bddc06 CM |
147 | raise CmdException, 'No "From" address' |
148 | if len(to_addr_list) == 0: | |
149 | raise CmdException, 'No "To/Cc/Bcc" addresses' | |
150 | ||
151 | return (from_addr_list[0], to_addr_list) | |
152 | ||
eb026d93 B |
153 | def __send_message(smtpserver, from_addr, to_addr_list, msg, sleep, |
154 | smtpuser, smtppassword): | |
b4bddc06 CM |
155 | """Send the message using the given SMTP server |
156 | """ | |
157 | try: | |
158 | s = smtplib.SMTP(smtpserver) | |
159 | except Exception, err: | |
160 | raise CmdException, str(err) | |
161 | ||
162 | s.set_debuglevel(0) | |
163 | try: | |
eb026d93 B |
164 | if smtpuser and smtppassword: |
165 | s.ehlo() | |
166 | s.login(smtpuser, smtppassword) | |
167 | ||
b4bddc06 CM |
168 | s.sendmail(from_addr, to_addr_list, msg) |
169 | # give recipients a chance of receiving patches in the correct order | |
170 | time.sleep(sleep) | |
171 | except Exception, err: | |
172 | raise CmdException, str(err) | |
173 | ||
174 | s.quit() | |
175 | ||
29f00589 CM |
176 | def __write_mbox(from_addr, msg): |
177 | """Write an mbox like file to the standard output | |
178 | """ | |
179 | r = re.compile('^From ', re.M) | |
180 | msg = r.sub('>\g<0>', msg) | |
181 | ||
182 | print 'From %s %s' % (from_addr, datetime.datetime.today().ctime()) | |
183 | print msg | |
184 | ||
185 | ||
e83b3149 PO |
186 | def __build_address_headers(options): |
187 | headers_end = '' | |
188 | if options.to: | |
d60cd083 CM |
189 | headers_end += 'To: ' |
190 | for to in options.to: | |
191 | headers_end += '%s, ' % to | |
192 | headers_end = headers_end[:-2] + '\n' | |
e83b3149 | 193 | if options.cc: |
d60cd083 CM |
194 | headers_end += 'Cc: ' |
195 | for cc in options.cc: | |
196 | headers_end += '%s, ' % cc | |
197 | headers_end = headers_end[:-2] + '\n' | |
e83b3149 | 198 | if options.bcc: |
d60cd083 CM |
199 | headers_end += 'Bcc: ' |
200 | for bcc in options.bcc: | |
201 | headers_end += '%s, ' % bcc | |
202 | headers_end = headers_end[:-2] + '\n' | |
e83b3149 PO |
203 | return headers_end |
204 | ||
e3e05587 CM |
205 | def __build_cover(tmpl, total_nr, msg_id, options): |
206 | """Build the cover message (series description) to be sent via SMTP | |
b4bddc06 | 207 | """ |
dae0f0be CM |
208 | maintainer = __get_maintainer() |
209 | if not maintainer: | |
210 | maintainer = '' | |
211 | ||
e83b3149 | 212 | headers_end = __build_address_headers(options) |
2bb96902 | 213 | headers_end += 'Message-Id: %s\n' % msg_id |