mercurial/mail.py
changeset 7948 de377b1a9a84
parent 7195 9fabcb1fe68d
child 8225 46293a0c7e9f
equal deleted inserted replaced
7947:a454eeb1b827 7948:de377b1a9a84
     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 _
     8 from i18n import _
     9 import os, smtplib, socket
     9 import os, smtplib, socket
    10 import email.Header, email.MIMEText, email.Utils
    10 import email.Header, email.MIMEText, email.Utils
    11 import util
    11 import util, encoding
    12 
    12 
    13 def _smtp(ui):
    13 def _smtp(ui):
    14     '''build an smtp connection and return a function to send mail'''
    14     '''build an smtp connection and return a function to send mail'''
    15     local_hostname = ui.config('smtp', 'local_hostname')
    15     local_hostname = ui.config('smtp', 'local_hostname')
    16     s = smtplib.SMTP(local_hostname=local_hostname)
    16     s = smtplib.SMTP(local_hostname=local_hostname)
    98     return email.MIMEText.MIMEText(s, subtype)
    98     return email.MIMEText.MIMEText(s, subtype)
    99 
    99 
   100 def _charsets(ui):
   100 def _charsets(ui):
   101     '''Obtains charsets to send mail parts not containing patches.'''
   101     '''Obtains charsets to send mail parts not containing patches.'''
   102     charsets = [cs.lower() for cs in ui.configlist('email', 'charsets')]
   102     charsets = [cs.lower() for cs in ui.configlist('email', 'charsets')]
   103     fallbacks = [util._fallbackencoding.lower(),
   103     fallbacks = [encoding.fallbackencoding.lower(),
   104                  util._encoding.lower(), 'utf-8']
   104                  encoding.encoding.lower(), 'utf-8']
   105     for cs in fallbacks: # util.unique does not keep order
   105     for cs in fallbacks: # util.unique does not keep order
   106         if cs not in charsets:
   106         if cs not in charsets:
   107             charsets.append(cs)
   107             charsets.append(cs)
   108     return [cs for cs in charsets if not cs.endswith('ascii')]
   108     return [cs for cs in charsets if not cs.endswith('ascii')]
   109 
   109 
   110 def _encode(ui, s, charsets):
   110 def _encode(ui, s, charsets):
   111     '''Returns (converted) string, charset tuple.
   111     '''Returns (converted) string, charset tuple.
   112     Finds out best charset by cycling through sendcharsets in descending
   112     Finds out best charset by cycling through sendcharsets in descending
   113     order. Tries both _encoding and _fallbackencoding for input. Only as
   113     order. Tries both encoding and fallbackencoding for input. Only as
   114     last resort send as is in fake ascii.
   114     last resort send as is in fake ascii.
   115     Caveat: Do not use for mail parts containing patches!'''
   115     Caveat: Do not use for mail parts containing patches!'''
   116     try:
   116     try:
   117         s.decode('ascii')
   117         s.decode('ascii')
   118     except UnicodeDecodeError:
   118     except UnicodeDecodeError:
   119         sendcharsets = charsets or _charsets(ui)
   119         sendcharsets = charsets or _charsets(ui)
   120         for ics in (util._encoding, util._fallbackencoding):
   120         for ics in (encoding.encoding, encoding.fallbackencoding):
   121             try:
   121             try:
   122                 u = s.decode(ics)
   122                 u = s.decode(ics)
   123             except UnicodeDecodeError:
   123             except UnicodeDecodeError:
   124                 continue
   124                 continue
   125             for ocs in sendcharsets:
   125             for ocs in sendcharsets: