Advertisement
Guest User

Untitled

a guest
May 11th, 2017
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.62 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #coding=utf-8
  3.  
  4.  
  5. """
  6. Client de IMAP4 que descarga contenido del INBOX de una cuenta en especifico
  7. para luego extraer el numero de telefono que debe venir en los correos enviados
  8. """
  9.  
  10. import StringIO
  11. import sys
  12. from Config import retCredentials
  13. import messageParser
  14.  
  15. from twisted.internet.task import LoopingCall
  16. from twisted.internet import protocol
  17. from twisted.internet import defer
  18. from twisted.mail import imap4
  19. from twisted.python import util
  20. from twisted.python import log
  21. from twisted.internet import reactor
  22. debug = 0
  23.  
  24. class IMAP4Client(imap4.IMAP4Client):
  25.  
  26.     def serverGreeting(self,caps):
  27.         """
  28.        Metodo llamado cuando el servidor contesta
  29.        """
  30.         if debug: print "On serverGreeting"
  31.  
  32.         self.serverCapabilities = caps
  33.         if self.greetDeferred is not None:
  34.             d, self.greetDeferred = self.greetDeferred, None
  35.             d.addCallback(self.cbLogin)
  36.             d.callback(self)
  37.  
  38.  
  39.     def cbLogin(self, proto):
  40.         """
  41.        Callback to IMAP login
  42.        """
  43.         if debug: print "On Login"
  44.  
  45.         login =  self.login(self.factory.username, self.factory.password)
  46.         login.addCallback(self.startPolling)
  47.  
  48.  
  49.     def startPolling(self, proto):
  50.         """
  51.        Callback to poll every x seconds
  52.        """
  53.         call = LoopingCall(self.selectMailbox,mailbox="INBOX")
  54.         call.start(20, now=True)
  55.        
  56.        
  57.     def selectMailbox(self, mailbox):
  58.         """
  59.        Select the mailbox to examin
  60.        """
  61.         if debug: print "On selectMailbox"
  62.  
  63.         mailbox = self.factory.mailbox
  64.         return self.select(mailbox).addCallback(self.cbSelectSuccess)
  65.        
  66.  
  67.     def cbSelectSuccess(self, selected):
  68.         """
  69.        Examine the INBOX mailbox for new mails
  70.  
  71.        """
  72.         if debug: print "On cbSelectSuccess"
  73.  
  74.  
  75.         self.messageCount = selected['EXISTS']
  76.         print "Messages: ", self.messageCount
  77.  
  78.         unseen = selected['EXISTS'] - selected['RECENT']
  79.  
  80.         if selected['RECENT'] == 0:
  81.             print "No new messages"
  82.             return
  83.  
  84.         return self.fetchBody("%s:*" % (unseen,)
  85.                 ).addCallback(self.cbProcMessages)
  86.  
  87.     def cbProcMessages(self,messages):
  88.  
  89.  
  90.         for message in messages.iteritems():
  91.             print message[1]['RFC822.TEXT']
  92.            
  93.  
  94. class IMAP4ClientFactory(protocol.ClientFactory):
  95.  
  96.     protocol = IMAP4Client
  97.  
  98.     def __init__(self, username, password,  onConn):
  99.  
  100.         self.username = username
  101.         self.password = password
  102.         self.mailbox = 'INBOX'
  103.         self.onConn = onConn
  104.  
  105.     def buildProtocol(self,addr):
  106.         if debug: print "On buildProtocol"
  107.         p = self.protocol()
  108.         p.factory = self
  109.         p.greetDeferred = self.onConn
  110.         auth = imap4.CramMD5ClientAuthenticator(self.username)
  111.         p.registerAuthenticator(auth)
  112.  
  113.         return p
  114.  
  115.     def clientConectionFailed(self, connector, reason):
  116.         d, self.onConn = self.onConn, None
  117.         d.errback(reason)
  118.  
  119.  
  120.  
  121. def ebConnection(reason):
  122.     log.startLogging(sys.stdout)
  123.     log.err(reason)
  124.     reactor.stop()
  125.  
  126.  
  127.  
  128. PORT = 143
  129. RESULT = "INBOX"
  130.  
  131.  
  132. def main():
  133.     credentials = retCredentials()
  134.     hostname = credentials['server']
  135.     username = credentials['mailbox']
  136.     password = util.getPassword('IMAP4 Password: ')
  137.  
  138.     onConn = defer.Deferred(
  139.             ).addErrback(ebConnection
  140.             )
  141.  
  142.     factory = IMAP4ClientFactory(username, password, onConn)
  143.  
  144.    
  145.     reactor.connectTCP(hostname, PORT, factory)
  146.     reactor.run()
  147.  
  148. if  __name__ == "__main__":
  149.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement