]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Synced sphinx branch with current trunk
authorMenno Smits <menno@freshfoo.com>
Sun, 5 Jun 2011 16:24:20 +0000 (17:24 +0100)
committerMenno Smits <menno@freshfoo.com>
Sun, 5 Jun 2011 16:24:20 +0000 (17:24 +0100)
1  2 
imapclient/imapclient.py

index 7319b40d641da0c4d67b5f6d4204efeb613e1614,ab5fd3da36dfdcfc647058d2e91b3b379653cca8..98f6240c9b302efe93e44412832e974ea20c1da0
@@@ -62,32 -96,62 +75,56 @@@ class IMAPClient(object)
                             r'\((?P<status_items>.*)\)$')
  
      def __init__(self, host, port=None, use_uid=True, ssl=False):
-         if ssl:
-             ImapClass = imaplib.IMAP4_SSL
-             default_port = 993
-         else:
-             ImapClass = imaplib.IMAP4
-             default_port = 143
 -        """Initialise object instance and connect to the remote IMAP server.
--
 -        @param host: The IMAP server address/hostname to connect to.
 -        @param port: The port number to use (default is 143, 993 for SSL).
 -        @param use_uid: Should message UIDs be used (default is True).
 -        @param ssl: Make an SSL connection (default is False)
 -        """
          if port is None:
-             port = default_port
+             port = ssl and 993 or 143
  
-         self._imap = ImapClass(host, port)
+         self.host = host
+         self.port = port
+         self.ssl = ssl
          self.use_uid = use_uid
          self.folder_encode = True
+         self.log_file = sys.stderr
+ 
+         self._imap = self._create_IMAP4()
+         self._imap._mesg = self._log    # patch in custom debug log method
+         self._idle_tag = None
+ 
+     def _create_IMAP4(self):
+         # Create the IMAP instance in a separate method to make unit tests easier
+         ImapClass = self.ssl and imaplib.IMAP4_SSL or imaplib.IMAP4
+         return ImapClass(self.host, self.port)
  
-    
      def login(self, username, password):
 -        """Perform a simple login
 +        """Login using *username* and *password*, returning the
 +        server response.
          """
          typ, data = self._imap.login(username, password)
          self._checkok('login', typ, data)
          return data[0]
  
+     def oauth_login(self, url, oauth_token, oauth_token_secret,
+                     consumer_key='anonymous', consumer_secret='anonymous'):
+         """Authenticate using oauth.
+ 
+         @param url: The OAuth request URL.
+         @param oauth_token: An OAuth key.
+         @param oauth_token_secret: An OAuth secret.
+         @param consumer_key: An OAuth consumer key (defaults to 'anonymous').
+         @param consumer_secret: An OAuth consumer secret (defaults to 'anonymous').
+         """
+         if oauth2:
+             token = oauth2.Token(oauth_token, oauth_token_secret)
+             consumer = oauth2.Consumer(consumer_key, consumer_secret)
+             xoauth_callable = lambda x: oauth2.build_xoauth_string(url, consumer, token)
+             
+             typ, data = self._imap.authenticate('XOAUTH', xoauth_callable)
+             self._checkok('authenticate', typ, data)
+             return data[0]
+         else:
+             raise self.Error('The optional oauth2 dependency is needed for oauth authentication')
  
      def logout(self):
 -        """Perform a logout
 +        """Logout, returning the server response.
          """
          typ, data = self._imap.logout()
          self._checkbye('logout', typ, data)
              out[key] = value
          return out
  
+     def idle(self):
+         """Put server into IDLE mode.
+ 
+         In this mode the server will return unsolicited responses
+         about changes to the selected mailbox.
+ 
+         This method returns immediately. Use idle_check() to check for
+         IDLE responses and idle_done() to stop IDLE mode.
+ 
+         Note: Any other commmands issued while the server is in IDLE
+         mode will fail.
+ 
+         See RFC 2177 for more information about the IDLE extension.
+         http://tools.ietf.org/html/rfc2177
+         """
+         self._idle_tag = self._imap._command('IDLE')
+         resp = self._imap._get_response()
+         if resp is not None:
+             raise self.Error('Unexpected IDLE response: %s' % resp)
+ 
+     def idle_check(self, timeout=None):
+         """Check for any IDLE responses sent by the server.
+ 
+         This method should only be called if the server is in IDLE
+         mode (see idle()).
+ 
+         By default, this method will block until an IDLE response is
+         received. If timeout is provided, the call will block for at
+         most this number of seconds while waiting for an IDLE response.
+ 
+         The return value is a list of received IDLE responses. These
+         will be parsed with values converted to appropriate types. For
+         example:
+ 
+         [('OK', 'Still here'),
+          (1, 'EXISTS'),
+          (1, 'FETCH', ('FLAGS', ('\\NotJunk',)))]
+         """
+         # make the socket non-blocking so the timeout can be
+         # implemented for this call
+         if self.ssl:
+             sock = self._imap.sslobj
+         else:
+             sock = self._imap.sock
+         sock.setblocking(0)
+         try:
+             resps = []
+             rs, _, _ = select.select([sock], [], [], timeout)
+             if rs:
+                 while True:
+                     try:
+                         line = self._imap._get_line()
+                     except (socket.timeout, socket.error):
+                         break
+                     else:
+                         resps.append(_parse_idle_response(line))
+             return resps
+         finally:
+             sock.setblocking(1)
+ 
+     def idle_done(self):
+         """Take the server out of IDLE mode.
+ 
+         This method should only be called if the server is in IDLE mode.
+ 
+         The return value is (command_text, idle_responses) where
+         command_text is the text sent by the server when the IDLE
+         command finished (eg. 'Idle terminated') and idle_responses is
+         a list of parsed idle responses received since the last call
+         to idle_check() (if any). These are returned in parsed form as
+         per idle_check().
+         """
+         self._imap.send('DONE\r\n')
+         # Slurp up any remaining IDLE responses until the IDLE is done
+         tag = self._idle_tag
+         tagged_commands = self._imap.tagged_commands
+         resps = []
+         while True:
+             line = self._imap._get_response()
+             if tagged_commands[tag]:
+                 break
+             resps.append(_parse_idle_response(line))
+         self._idle_tag = None
+         typ, data = tagged_commands.pop(tag)
+         self._checkok('idle', typ, data)
+         return data[0], resps
  
      def folder_status(self, folder, what=None):
 -        """Requests the status from folder.
 +        """Return the status of *folder*.
  
 -        @param folder: The folder name.
 -        @param what: A sequence of status items to query. Defaults to
 -            ('MESSAGES', 'RECENT', 'UIDNEXT', 'UIDVALIDITY', 'UNSEEN').
 -        @return: Dictionary of the status items for the folder. The keys match
 -            the items specified in the what parameter.
 -        @rtype: dict
 +        *what* should be a sequence of status items to query. This
 +        defaults to ``('MESSAGES', 'RECENT', 'UIDNEXT', 'UIDVALIDITY',
 +        'UNSEEN')``.
 +
 +        Returns a dictionary of the status items for the folder with
 +        keys matching *what*.
          """
          if what is None:
              what = ('MESSAGES', 'RECENT', 'UIDNEXT', 'UIDVALIDITY', 'UNSEEN')
          typ, data = self._imap.getacl(folder)
          self._checkok('getacl', typ, data)
  
-         parts = list(response_lexer.Lexer([data[0]]))
+         parts = list(response_lexer.TokenSource(data))
          parts = parts[1:]       # First item is folder name
- 
-         out = []
-         for i in xrange(0, len(parts), 2):
-             out.append((parts[i], parts[i+1]))
-         return out
+         return [(parts[i], parts[i+1]) for i in xrange(0, len(parts), 2)]
  
 -
      def setacl(self, folder, who, what):
 -        """Set an ACL for a folder
 +        """Set an ACL (*what*) for user (*who*) for a folder.
  
 -        @param folder: Folder name to set an ACL for.
 -        @param who: User or group ID for the ACL.
 -        @param what: A string describing the ACL. Set to '' to remove an ACL.
 -        @return: Server response string.
 +        Set *what* to an empty string to remove an ACL. Returns the
 +        server response string.
          """
          typ, data = self._imap.setacl(folder, who, what)
          self._checkok('setacl', typ, data)
          # quoting. A hack but it works.
          return _quote_arg(name)
  
+     def __debug_get(self):
+         return self._imap.debug
+ 
+     def __debug_set(self, level):
+         if level is True:
+             level = 4
+         elif level is False:
+             level = 0
+         self._imap.debug = level
+ 
+     debug = property(__debug_get, __debug_set,
+                      doc="Set debug level. Integers from 0 to 5 or, True and " \
+                          "False can be used. True specifies debug level 4. " \
+                          "Debug output goes to stderr.")
+ 
+     def _log(self, text):
+         self.log_file.write('%s %s\n' % (datetime.now().strftime('%M:%S.%f'), text))
+         self.log_file.flush()
+         
  
  def messages_to_str(messages):
 -    """Convert a sequence of messages ids or a single message id into an
 -    message ID list for use with IMAP commands.
 -
 -    @param messages: A sequence of messages IDs or a single message ID.
 -        (eg. [1,4,5,7,8])
 -    @return: Message list string (eg. "1,4,5,6,8")
 +    """Convert a sequence of messages ids or a single integer message id
 +    into an id list string for use with IMAP commands
      """
      if isinstance(messages, (str, int, long)):
          messages = (messages,)