[Twisted-Python] IMAP4 Client extrange behavior
Hello all, I have this imap4 client http://www.pastebin.com/m4e387f1a most of the code is barrowed from a IRC friend, bob_f , it works fine but as soon as more that 1 email arrrives to the inbox, it only prints the first new messages but not the others, I wonder what am I doing wrong Thanks a lot Here is the code: #!/usr/bin/env python #coding=utf-8 """ Client de IMAP4 que descarga contenido del INBOX de una cuenta en especifico para luego extraer el numero de telefono que debe venir en los correos enviados """ import StringIO import sys from Config import retCredentials from twisted.internet.task import LoopingCall from twisted.internet import protocol from twisted.internet import defer from twisted.mail import imap4 from twisted.python import util from twisted.python import log from twisted.internet import reactor debug = 1 class IMAP4Client(imap4.IMAP4Client): def serverGreeting(self,caps): """ Metodo llamado cuando el servidor contesta """ if debug: print "On serverGreeting" self.serverCapabilities = caps if self.greetDeferred is not None: d, self.greetDeferred = self.greetDeferred, None d.addCallback(self.cbLogin) d.callback(self) def cbLogin(self, proto): """ Callback to IMAP login """ if debug: print "On Login" login = self.login(self.factory.username, self.factory.password) login.addCallback(self.startPolling) def startPolling(self, proto): """ Callback to poll every x seconds """ call = LoopingCall(self.selectMailbox,mailbox="INBOX") call.start(20, now=True) def selectMailbox(self, mailbox): """ Select the mailbox to examin """ if debug: print "On selectMailbox" mailbox = self.factory.mailbox return self.select(mailbox).addCallback(self.cbSelectSuccess) def cbSelectSuccess(self, selected): """ Examine the INBOX mailbox for new mails """ if debug: print "On cbSelectSuccess" self.messageCount = selected['EXISTS'] print "Messages: ", self.messageCount unseen = selected['EXISTS'] - selected['RECENT'] if selected['RECENT'] == 0: print "No new messages" return return self.fetchMessage("%s:*" % (unseen) ).addCallback(self.cbProcMessages) def cbProcMessages(self,messages): print messages class IMAP4ClientFactory(protocol.ClientFactory): protocol = IMAP4Client def __init__(self, username, password, onConn): self.username = username self.password = password self.mailbox = 'INBOX' self.onConn = onConn def buildProtocol(self,addr): if debug: print "On buildProtocol" p = self.protocol() p.factory = self p.greetDeferred = self.onConn auth = imap4.CramMD5ClientAuthenticator(self.username) p.registerAuthenticator(auth) return p def clientConectionFailed(self, connector, reason): d, self.onConn = self.onConn, None d.errback(reason) def ebConnection(reason): log.startLogging(sys.stdout) log.err(reason) reactor.stop() PORT = 143 RESULT = "INBOX" def main(): credentials = retCredentials() hostname = credentials['server'] username = credentials['mailbox'] password = util.getPassword('IMAP4 Password: ') onConn = defer.Deferred( ).addErrback(ebConnection ) factory = IMAP4ClientFactory(username, password, onConn) reactor.connectTCP(hostname, PORT, factory) reactor.run() if __name__ == "__main__": main() -- http://celord.blogspot.com/
Hello all I have solved this, here are my final functions, I thing to notice is that with twisted 8.2 the one from the debian/ubuntu repos this functions do not work properly, in the other hand, they work just fine with twisted 9 and 10 def selectMailbox(self, mailbox): """ Select the mailbox to examin """ if debug: print "On selectMailbox" mailbox = self.factory.mailbox return self.select(mailbox).addCallback(self.cbSelectSuccess) def cbSelectSuccess(self, selected): """ Examine the INBOX mailbox for new mails """ if debug: print "On cbSelectSuccess" self.messageCount = selected['EXISTS'] print "Messages: ", self.messageCount unseen = imap4.Query(unseen=True) return self.search(unseen ).addCallback(self.cbSearch) def cbSearch(self,messages): if debug: print "on cbSearch" messageSet = imap4.MessageSet() for message in messages: messageSet += message if messageSet: self.fetchMessage(messageSet, False ).addCallback(self.cbProcMessage) else: return def cbProcMessage(self, messages): if debug: print "on cbProcEnvelop" for message in messages.iteritems(): body = message[1]['RFC822'] print body 2010/2/22 César García <celord@gmail.com>:
Hello all, I have this imap4 client http://www.pastebin.com/m4e387f1a most of the code is barrowed from a IRC friend, bob_f , it works fine but as soon as more that 1 email arrrives to the inbox, it only prints the first new messages but not the others, I wonder what am I doing wrong
Thanks a lot
Here is the code:
#!/usr/bin/env python #coding=utf-8
""" Client de IMAP4 que descarga contenido del INBOX de una cuenta en especifico para luego extraer el numero de telefono que debe venir en los correos enviados """
import StringIO import sys from Config import retCredentials
from twisted.internet.task import LoopingCall from twisted.internet import protocol from twisted.internet import defer from twisted.mail import imap4 from twisted.python import util from twisted.python import log from twisted.internet import reactor debug = 1
class IMAP4Client(imap4.IMAP4Client):
def serverGreeting(self,caps): """ Metodo llamado cuando el servidor contesta """ if debug: print "On serverGreeting"
self.serverCapabilities = caps if self.greetDeferred is not None: d, self.greetDeferred = self.greetDeferred, None d.addCallback(self.cbLogin) d.callback(self)
def cbLogin(self, proto): """ Callback to IMAP login """ if debug: print "On Login"
login = self.login(self.factory.username, self.factory.password) login.addCallback(self.startPolling)
def startPolling(self, proto): """ Callback to poll every x seconds """ call = LoopingCall(self.selectMailbox,mailbox="INBOX") call.start(20, now=True)
def selectMailbox(self, mailbox): """ Select the mailbox to examin """ if debug: print "On selectMailbox"
mailbox = self.factory.mailbox return self.select(mailbox).addCallback(self.cbSelectSuccess)
def cbSelectSuccess(self, selected): """ Examine the INBOX mailbox for new mails
""" if debug: print "On cbSelectSuccess"
self.messageCount = selected['EXISTS'] print "Messages: ", self.messageCount
unseen = selected['EXISTS'] - selected['RECENT']
if selected['RECENT'] == 0: print "No new messages" return
return self.fetchMessage("%s:*" % (unseen) ).addCallback(self.cbProcMessages)
def cbProcMessages(self,messages):
print messages
class IMAP4ClientFactory(protocol.ClientFactory):
protocol = IMAP4Client
def __init__(self, username, password, onConn):
self.username = username self.password = password self.mailbox = 'INBOX' self.onConn = onConn
def buildProtocol(self,addr): if debug: print "On buildProtocol" p = self.protocol() p.factory = self p.greetDeferred = self.onConn auth = imap4.CramMD5ClientAuthenticator(self.username) p.registerAuthenticator(auth)
return p
def clientConectionFailed(self, connector, reason): d, self.onConn = self.onConn, None d.errback(reason)
def ebConnection(reason): log.startLogging(sys.stdout) log.err(reason) reactor.stop()
PORT = 143 RESULT = "INBOX"
def main(): credentials = retCredentials() hostname = credentials['server'] username = credentials['mailbox'] password = util.getPassword('IMAP4 Password: ')
onConn = defer.Deferred( ).addErrback(ebConnection )
factory = IMAP4ClientFactory(username, password, onConn)
reactor.connectTCP(hostname, PORT, factory) reactor.run()
if __name__ == "__main__": main()
participants (1)
-
César García