5 # This software may be used and distributed according to the terms |
5 # This software may be used and distributed according to the terms |
6 # of the GNU General Public License, incorporated herein by reference. |
6 # of the GNU General Public License, incorporated herein by reference. |
7 |
7 |
8 from i18n import gettext as _ |
8 from i18n import gettext as _ |
9 from demandload import * |
9 from demandload import * |
10 demandload(globals(), "errno getpass os re smtplib socket sys tempfile") |
10 demandload(globals(), "errno getpass os re socket sys tempfile") |
11 demandload(globals(), "ConfigParser mdiff templater traceback util") |
11 demandload(globals(), "ConfigParser mdiff templater traceback util") |
12 |
12 |
13 class ui(object): |
13 class ui(object): |
14 def __init__(self, verbose=False, debug=False, quiet=False, |
14 def __init__(self, verbose=False, debug=False, quiet=False, |
15 interactive=True, traceback=False, parentui=None): |
15 interactive=True, traceback=False, parentui=None): |
166 def configrevlog(self): |
166 def configrevlog(self): |
167 result = {} |
167 result = {} |
168 for key, value in self.configitems("revlog"): |
168 for key, value in self.configitems("revlog"): |
169 result[key.lower()] = value |
169 result[key.lower()] = value |
170 return result |
170 return result |
171 |
|
172 def diffopts(self, opts={}): |
|
173 return mdiff.diffopts( |
|
174 text=opts.get('text'), |
|
175 showfunc=(opts.get('show_function') or |
|
176 self.configbool('diff', 'showfunc', None)), |
|
177 git=(opts.get('git') or |
|
178 self.configbool('diff', 'git', None)), |
|
179 ignorews=(opts.get('ignore_all_space') or |
|
180 self.configbool('diff', 'ignorews', None)), |
|
181 ignorewsamount=(opts.get('ignore_space_change') or |
|
182 self.configbool('diff', 'ignorewsamount', None)), |
|
183 ignoreblanklines=(opts.get('ignore_blank_lines') or |
|
184 self.configbool('diff', 'ignoreblanklines', None))) |
|
185 |
171 |
186 def username(self): |
172 def username(self): |
187 """Return default username to be used in commits. |
173 """Return default username to be used in commits. |
188 |
174 |
189 Searched in this order: $HGUSER, [ui] section of hgrcs, $EMAIL |
175 Searched in this order: $HGUSER, [ui] section of hgrcs, $EMAIL |
293 finally: |
279 finally: |
294 os.unlink(name) |
280 os.unlink(name) |
295 |
281 |
296 return t |
282 return t |
297 |
283 |
298 def sendmail(self): |
|
299 '''send mail message. object returned has one method, sendmail. |
|
300 call as sendmail(sender, list-of-recipients, msg).''' |
|
301 |
|
302 def smtp(): |
|
303 '''send mail using smtp.''' |
|
304 |
|
305 local_hostname = self.config('smtp', 'local_hostname') |
|
306 s = smtplib.SMTP(local_hostname=local_hostname) |
|
307 mailhost = self.config('smtp', 'host') |
|
308 if not mailhost: |
|
309 raise util.Abort(_('no [smtp]host in hgrc - cannot send mail')) |
|
310 mailport = int(self.config('smtp', 'port', 25)) |
|
311 self.note(_('sending mail: smtp host %s, port %s\n') % |
|
312 (mailhost, mailport)) |
|
313 s.connect(host=mailhost, port=mailport) |
|
314 if self.configbool('smtp', 'tls'): |
|
315 self.note(_('(using tls)\n')) |
|
316 s.ehlo() |
|
317 s.starttls() |
|
318 s.ehlo() |
|
319 username = self.config('smtp', 'username') |
|
320 password = self.config('smtp', 'password') |
|
321 if username and password: |
|
322 self.note(_('(authenticating to mail server as %s)\n') % |
|
323 (username)) |
|
324 s.login(username, password) |
|
325 return s |
|
326 |
|
327 class sendmail(object): |
|
328 '''send mail using sendmail.''' |
|
329 |
|
330 def __init__(self, ui, program): |
|
331 self.ui = ui |
|
332 self.program = program |
|
333 |
|
334 def sendmail(self, sender, recipients, msg): |
|
335 cmdline = '%s -f %s %s' % ( |
|
336 self.program, templater.email(sender), |
|
337 ' '.join(map(templater.email, recipients))) |
|
338 self.ui.note(_('sending mail: %s\n') % cmdline) |
|
339 fp = os.popen(cmdline, 'w') |
|
340 fp.write(msg) |
|
341 ret = fp.close() |
|
342 if ret: |
|
343 raise util.Abort('%s %s' % ( |
|
344 os.path.basename(self.program.split(None, 1)[0]), |
|
345 util.explain_exit(ret)[0])) |
|
346 |
|
347 method = self.config('email', 'method', 'smtp') |
|
348 if method == 'smtp': |
|
349 mail = smtp() |
|
350 else: |
|
351 mail = sendmail(self, method) |
|
352 return mail |
|
353 |
|
354 def print_exc(self): |
284 def print_exc(self): |
355 '''print exception traceback if traceback printing enabled. |
285 '''print exception traceback if traceback printing enabled. |
356 only to call in exception handler. returns true if traceback |
286 only to call in exception handler. returns true if traceback |
357 printed.''' |
287 printed.''' |
358 if self.traceback: |
288 if self.traceback: |