Changeset 209:80f530da50d7
- Timestamp:
- 03/23/11 15:22:37 (14 months ago)
- Author:
- Johannes Heckel <stone.pixel@…>
- Branch:
- idle+oauth
- Children:
- 210:2c3edbf7da22, 211:6e64dc583fa7
- Parents:
- 208:3eeca2dda55d (diff), 207:835686a87307 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
- Message:
-
Merge idle and oauth features.
- Files:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r207
|
r209
|
|
| 4 | 4 | |
| 5 | 5 | import re |
| 6 | | import imaplib |
| 7 | 6 | import response_lexer |
| 8 | 7 | from operator import itemgetter |
| 9 | 8 | import warnings |
| | 9 | |
| | 10 | try: |
| | 11 | import imaplib2 as imaplib |
| | 12 | imaplib2 = True |
| | 13 | except ImportError: |
| | 14 | imaplib2 = False |
| | 15 | import imaplib |
| | 16 | |
| 10 | 17 | #imaplib.Debug = 5 |
| 11 | | |
| | 18 | |
| 12 | 19 | try: |
| 13 | 20 | import oauth2 |
| … |
… |
|
| 288 | 295 | typ, data = self._imap.select(self._encode_folder_name(folder), readonly) |
| 289 | 296 | self._checkok('select', typ, data) |
| 290 | | return self._process_select_response(self._imap.untagged_responses) |
| 291 | | |
| | 297 | if imaplib2: |
| | 298 | untagged_responses = self._response_to_dict(self._imap.untagged_responses) |
| | 299 | else: |
| | 300 | untagged_responses = self._imap.untagged_responses |
| | 301 | return self._process_select_response(untagged_responses) |
| | 302 | |
| | 303 | def _response_to_dict(self, resp): |
| | 304 | dictresp = {} |
| | 305 | |
| | 306 | if len(resp) > 0: |
| | 307 | for item in resp: |
| | 308 | dictresp[item[0]] = item[1] |
| | 309 | |
| | 310 | return dictresp |
| 292 | 311 | |
| 293 | 312 | def _process_select_response(self, resp): |
| … |
… |
|
| 307 | 326 | return out |
| 308 | 327 | |
| 309 | | |
| | 328 | def idle(self, timeout=None, **kwargs): |
| | 329 | """Put server into IDLE mode. |
| | 330 | |
| | 331 | IDLE mode will end when the server notifies of a change, the sever |
| | 332 | reaches the timeout, or another IMAP4 command is scheduled. |
| | 333 | |
| | 334 | @param timeout: timeout for the IDLE command in seconds (default: 29 mins) |
| | 335 | @param callback: Function to be called when IDLE mode ends. If a callback |
| | 336 | is supplied the idle function will be asynchronous, returning immediately. |
| | 337 | @param cb_arg: An optional argument that will be passed to the callback function. |
| | 338 | @return: Server response. (None if callback is specified). |
| | 339 | """ |
| | 340 | if imaplib2: |
| | 341 | if 'callback' in kwargs: |
| | 342 | callback = kwargs.pop('callback') |
| | 343 | def _wrapped_callback(resp): |
| | 344 | typ, data = resp[0] |
| | 345 | self._checkok('idle', typ, data) |
| | 346 | # if there's a cb_arg, pass it along |
| | 347 | cb_arg = resp[1] |
| | 348 | if cb_arg: |
| | 349 | callback(data[0], cb_arg) |
| | 350 | else: |
| | 351 | callback(data[0]) |
| | 352 | self._imap.idle(timeout, callback=_wrapped_callback, **kwargs) |
| | 353 | else: |
| | 354 | typ, data = self._imap.idle(timeout) |
| | 355 | self._checkok('idle', typ, data) |
| | 356 | return data[0] |
| | 357 | else: |
| | 358 | raise self.Error('The imaplib2 module is required to use IDLE') |
| | 359 | |
| 310 | 360 | def folder_status(self, folder, what=None): |
| 311 | 361 | """Requests the status from folder. |
| … |
… |
|
| 535 | 585 | else: |
| 536 | 586 | tag = self._imap._command('FETCH', msg_list, parts_list, modifiers_list) |
| 537 | | typ, data = self._imap._command_complete('FETCH', tag) |
| | 587 | if imaplib2: |
| | 588 | typ, data = self._imap._command_complete(tag, 'FETCH') |
| | 589 | else: |
| | 590 | typ, data = self._imap._command_complete('FETCH', tag) |
| 538 | 591 | self._checkok('fetch', typ, data) |
| 539 | 592 | typ, data = self._imap._untagged_response(typ, data, 'FETCH') |
-
|
r208
|
r209
|
|
| 17 | 17 | #imaplib.Debug = 5 |
| 18 | 18 | |
| | 19 | try: |
| | 20 | import oauth2 |
| | 21 | except ImportError: |
| | 22 | oauth2 = None |
| | 23 | |
| 19 | 24 | import imap_utf7 |
| 20 | 25 | from fixed_offset import FixedOffset |
| … |
… |
|
| 120 | 125 | return data[0] |
| 121 | 126 | |
| | 127 | def oauth_login(self, url, oauth_token, oauth_token_secret, |
| | 128 | consumer_key='anonymous', consumer_secret='anonymous'): |
| | 129 | """Authenticate using oauth. |
| | 130 | |
| | 131 | @param url: The OAuth request URL. |
| | 132 | @param oauth_token: An OAuth key. |
| | 133 | @param oauth_token_secret: An OAuth secret. |
| | 134 | @param consumer_key: An OAuth consumer key (defaults to 'anonymous'). |
| | 135 | @param consumer_secret: An OAuth consumer secret (defaults to 'anonymous'). |
| | 136 | """ |
| | 137 | if oauth2: |
| | 138 | token = oauth2.Token(oauth_token, oauth_token_secret) |
| | 139 | consumer = oauth2.Consumer(consumer_key, consumer_secret) |
| | 140 | xoauth_callable = lambda x: oauth2.build_xoauth_string(url, consumer, token) |
| | 141 | |
| | 142 | typ, data = self._imap.authenticate('XOAUTH', xoauth_callable) |
| | 143 | self._checkok('authenticate', typ, data) |
| | 144 | return data[0] |
| | 145 | else: |
| | 146 | raise self.Error('The optional oauth2 dependency is needed for oauth authentication') |
| 122 | 147 | |
| 123 | 148 | def logout(self): |