~muffinresearch/bzr-email/bzr-email-keychain

« back to all changes in this revision

Viewing changes to smtp_connection.py

  • Committer: Stuart Colville
  • Date: 2007-08-14 11:01:06 UTC
  • Revision ID: scol@yahoo-inc.com-20070814110106-p7j98tirrv0xyrp3
added keychain authentication for Mac OSX and updated documentation

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
    from email.mime.text import MIMEText
29
29
    from email.mime.multipart import MIMEMultipart
30
30
    from email.utils import parseaddr
31
 
import smtplib
 
31
import smtplib,sys
32
32
 
33
33
from bzrlib import (
34
34
    errors,
54
54
 
55
55
        self._smtp_username = config.get_user_option('smtp_username')
56
56
        self._smtp_password = config.get_user_option('smtp_password')
57
 
 
58
 
        self._connection = None
59
 
 
 
57
        
 
58
        if sys.platform == 'darwin':
 
59
          smtp_keychain = config.get_user_option('smtp_keychain')
 
60
          smtp_keychain_item = config.get_user_option('smtp_keychain_item')
 
61
          if smtp_keychain and smtp_keychain_item:
 
62
            keychaincredentials = self._getKeychainCredentials(smtp_keychain,smtp_keychain_item)
 
63
            self._smtp_username = keychaincredentials["account"]
 
64
            self._smtp_password = keychaincredentials["password"]
 
65
         
 
66
        self._connection = None 
 
67
        
60
68
    def _connect(self):
61
69
        """If we haven't connected, connect and authenticate."""
62
70
        if self._connection is not None:
100
108
               self._smtp_password = None
101
109
               self._authenticate()
102
110
        except smtplib.SMTPException, e:
103
 
            raise errors.BzrCommandError(str(e))
 
111
            raise errors.BzrCommandError(str(e))  
 
112
            
 
113
    def _getKeychainCredentials(self,keychain,item):
 
114
      import commands,re
 
115
      resp = commands.getoutput("security find-generic-password -g -s "+item+" "+keychain+".keychain") 
 
116
      if resp.find('The specified item could not be found') != -1:
 
117
        print 'The specified item could not be found'
 
118
      if resp:
 
119
        password = re.search('password: "(.*?)"', resp, re.DOTALL)
 
120
        account = re.search('"acct"<blob>="(.*?)"', resp, re.DOTALL)
 
121
        if password and account:
 
122
          return {"account":account.group(1),"password":password.group(1)}
 
123
        else:
 
124
          print 'Keychain item not found'        
104
125
 
105
126
    @staticmethod
106
127
    def _split_address(address):