Guest User

Untitled

a guest
Jun 18th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. """ Python IMAP with TLS/SSL support """
  2. ##
  3. ## Author: Alexander Brill <alex@brill.no>
  4. ## Copyright (C) 2004 Alexander Brill
  5. ##
  6. ## This program is free software; you can redistribute it and/or
  7. ## modify it under the terms of the GNU General Public License
  8. ## as published by the Free Software Foundation; either version 2
  9. ## of the License, or (at your option) any later version.
  10. ##
  11. ## This program is distributed in the hope that it will be useful,
  12. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ## GNU General Public License for more details.
  15. ##
  16. ## You should have received a copy of the GNU General Public License
  17. ## along with this program; if not, write to the Free Software
  18. ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. ##
  20.  
  21. """USAGE:
  22. import imaptls
  23. imap = imaptls.IMAP4('hostname')
  24. imap.starttls(keyfile=None, certfile=None)
  25.  
  26. # create a simple function for our PLAIN login
  27. def sendAuth(response):
  28. return "user\0auth\0password"
  29.  
  30. typ, data = imap.authenticate("PLAIN", sendAuth)
  31. """
  32.  
  33.  
  34. import imaplib, socket
  35. from smtplib import SSLFakeSocket, SSLFakeFile
  36.  
  37. Commands = {
  38. 'STARTTLS': ('NONAUTH')
  39. }
  40. imaplib.Commands.update(Commands)
  41.  
  42.  
  43. class IMAP4(imaplib.IMAP4):
  44.  
  45. def starttls(self, keyfile = None, certfile = None):
  46. name = "STARTTLS"
  47. typ, dat = self._simple_command(name)
  48. if typ != 'OK':
  49. raise self.error(dat[-1])
  50. sslobj = socket.ssl(self.sock, keyfile, certfile)
  51. self.sock = SSLFakeSocket(self.sock, sslobj)
  52. self.file = SSLFakeFile(sslobj)
  53.  
  54. cap = 'CAPABILITY'
  55. self._simple_command(cap)
  56. if not cap in self.untagged_responses:
  57. raise self.error('no CAPABILITY response from server')
  58. self.capabilities = tuple(self.untagged_responses[cap][-1].upper().split())
Add Comment
Please, Sign In to add comment