mercurial/ui.py
changeset 2292 903ab41ac7eb
parent 2286 9f745d3675d4
child 2293 3dc6f2501dbc
equal deleted inserted replaced
2291:1cad19678b4c 2292:903ab41ac7eb
     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 import ConfigParser
     8 import ConfigParser
     9 from i18n import gettext as _
     9 from i18n import gettext as _
    10 from demandload import *
    10 from demandload import *
    11 demandload(globals(), "errno getpass os re smtplib socket sys tempfile util")
    11 demandload(globals(), "errno getpass os re smtplib socket sys tempfile")
       
    12 demandload(globals(), "templater util")
    12 
    13 
    13 class ui(object):
    14 class ui(object):
    14     def __init__(self, verbose=False, debug=False, quiet=False,
    15     def __init__(self, verbose=False, debug=False, quiet=False,
    15                  interactive=True, traceback=False, parentui=None):
    16                  interactive=True, traceback=False, parentui=None):
    16         self.overlay = {}
    17         self.overlay = {}
   268             os.unlink(name)
   269             os.unlink(name)
   269 
   270 
   270         return t
   271         return t
   271 
   272 
   272     def sendmail(self):
   273     def sendmail(self):
   273         s = smtplib.SMTP()
   274         '''send mail message. object returned has one method, sendmail.
   274         mailhost = self.config('smtp', 'host')
   275         call as sendmail(sender, list-of-recipients, msg).'''
   275         if not mailhost:
   276 
   276             raise util.Abort(_('no [smtp]host in hgrc - cannot send mail'))
   277         def smtp():
   277         s.connect(host=mailhost, port=int(self.config('smtp', 'port', 25)))
   278             '''send mail using smtp.'''
   278         if self.configbool('smtp', 'tls'):
   279 
   279             s.ehlo()
   280             s = smtplib.SMTP()
   280             s.starttls()
   281             mailhost = self.config('smtp', 'host')
   281             s.ehlo()
   282             if not mailhost:
   282         username = self.config('smtp', 'username')
   283                 raise util.Abort(_('no [smtp]host in hgrc - cannot send mail'))
   283         password = self.config('smtp', 'password')
   284             mailport = int(self.config('smtp', 'port', 25))
   284         if username and password:
   285             self.note(_('sending mail: smtp host %s, port %s\n') %
   285             s.login(username, password)
   286                       (mailhost, mailport))
   286         return s
   287             s.connect(host=mailhost, port=mailport)
       
   288             if self.configbool('smtp', 'tls'):
       
   289                 self.note(_('(using tls)\n'))
       
   290                 s.ehlo()
       
   291                 s.starttls()
       
   292                 s.ehlo()
       
   293             username = self.config('smtp', 'username')
       
   294             password = self.config('smtp', 'password')
       
   295             if username and password:
       
   296                 self.note(_('(authenticating to mail server as %s)\n') %
       
   297                           (username))
       
   298                 s.login(username, password)
       
   299             return s
       
   300 
       
   301         class sendmail(object):
       
   302             '''send mail using sendmail.'''
       
   303 
       
   304             def __init__(self, ui, program):
       
   305                 self.ui = ui
       
   306                 self.program = program
       
   307 
       
   308             def sendmail(self, sender, recipients, msg):
       
   309                 cmdline = '%s -f %s %s' % (
       
   310                     self.program, templater.email(sender),
       
   311                     ' '.join(map(templater.email, recipients)))
       
   312                 self.ui.note(_('sending mail: %s\n') % cmdline)
       
   313                 fp = os.popen(cmdline, 'w')
       
   314                 fp.write(msg)
       
   315                 ret = fp.close()
       
   316                 if ret:
       
   317                     raise util.Abort('%s %s' % (
       
   318                         os.path.basename(self.program.split(None, 1)[0]),
       
   319                         util.explain_exit(ret)[0]))
       
   320 
       
   321         method = self.config('email', 'method', 'smtp')
       
   322         if method == 'smtp':
       
   323             mail = smtp()
       
   324         else:
       
   325             mail = sendmail(self, method)
       
   326         return mail