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