class IMAPLogic: """This class implements the main logic for the program.""" def __init__(self, proto): self.proto = proto self.factory = proto.factory d = self.proto.login(self.factory.username, self.factory.password) d.addCallback(lambda x: self.proto.select('INBOX')) d.addCallback(lambda x: self.proto.fetchUID('1:*')) d.addCallback(self.handleuids) d.addCallback(self.deletemessages) d.addCallback(self.logout) d.addCallback(self.stopreactor) d.addErrback(self.errorhappened) def handleuids(self, uids): self.uidlist = MessageSet() dlist = [] destfd = open(sys.argv[3], "at") for num, data in uids.items(): uid = data['UID'] d = self.proto.fetchSpecific(uid, uid=1, peek=1) d.addCallback(self.gotmessage, destfd, uid) dlist.append(d) dl = defer.DeferredList(dlist) dl.addCallback(lambda x, fd: fd.close(), destfd) return dl def gotmessage(self, data, destfd, uid): print "Received message UID", uid for key, value in data.items(): print "Writing message", key i = value[0].index('BODY') + 2 msg = email.message_from_string(value[0][i]) destfd.write(msg.as_string(unixfrom=1)) destfd.write("\n") self.uidlist.add(int(uid)) def deletemessages(self, data=None): print "Deleting messages", str(self.uidlist) d = self.proto.addFlags(str(self.uidlist), ["\\Deleted"], uid=1) d.addCallback(lambda x: self.proto.expunge()) return d def logout(self, data=None): return self.proto.logout() def stopreactor(self, data=None): reactor.stop() def errorhappened(self, failure): print "An error occured:", failure.getErrorMessage() d = self.logout() d.addBoth(self.stopreactor) return failure
class IMAPLogic: """This class implements the main logic for the program.""" def __init__(self, proto): self.proto = proto self.factory = proto.factory d = self.proto.login(self.factory.username, self.factory.password) d.addCallback(lambda x: self.proto.select('INBOX')) d.addCallback(lambda x: self.proto.fetchUID('1:*')) d.addCallback(self.handleuids) d.addCallback(self.deletemessages) d.addCallback(self.logout) d.addCallback(self.stopreactor) d.addErrback(self.errorhappened) def handleuids(self, uids): self.uidlist = MessageSet() dlist = [] destfd = open(sys.argv[3], "at") for num, data in uids.items(): uid = data['UID'] d = self.proto.fetchSpecific(uid, uid = 1, peek = 1) d.addCallback(self.gotmessage, destfd, uid) dlist.append(d) dl = defer.DeferredList(dlist) dl.addCallback(lambda x, fd: fd.close(), destfd) return dl def gotmessage(self, data, destfd, uid): print "Received message UID", uid for key, value in data.items(): print "Writing message", key i = value[0].index('BODY') + 2 msg = email.message_from_string(value[0][i]) destfd.write(msg.as_string(unixfrom = 1)) destfd.write("\n") self.uidlist.add(int(uid)) def deletemessages(self, data = None): print "Deleting messages", str(self.uidlist) d = self.proto.addFlags(str(self.uidlist), ["\\Deleted"], uid = 1) d.addCallback(lambda x: self.proto.expunge()) return d def logout(self, data = None): return self.proto.logout() def stopreactor(self, data = None): reactor.stop() def errorhappened(self, failure): print "An error occured:", failure.getErrorMessage() d = self.logout() d.addBoth(self.stopreactor) return failure
def handleuids(self, uids): self.uidlist = MessageSet() dlist = [] destfd = open(sys.argv[3], "at") for num, data in uids.items(): uid = data['UID'] d = self.proto.fetchSpecific(uid, uid=1, peek=1) d.addCallback(self.gotmessage, destfd, uid) dlist.append(d) dl = defer.DeferredList(dlist) dl.addCallback(lambda x, fd: fd.close(), destfd) return dl
def handleuids(self, uids): self.uidlist = MessageSet() dlist = [] destfd = open(sys.argv[3], "at") for num, data in uids.items(): uid = data['UID'] d = self.proto.fetchSpecific(uid, uid = 1, peek = 1) d.addCallback(self.gotmessage, destfd, uid) dlist.append(d) dl = defer.DeferredList(dlist) dl.addCallback(lambda x, fd: fd.close(), destfd) return dl
def addMessage(self, content, flags=(), date=None): """Add the given message to this mailbox.""" msg = self.storage.makeMessage(self.getUIDNext(True), content.read()) msg.date = date self.storage.addMessage(msg) self.store(MessageSet(long(msg.id), long(msg.id)), flags, 1, True) msg.recent = True msg.store() self.recent_count += 1 self.unseen_count += 1 for listener in self.listeners: listener.newMessages(self.getMessageCount(), self.getRecentCount()) d = defer.Deferred() reactor.callLater(0, d.callback, self.storage.keys().index(msg.id)) return d
def search(self, query, uid): """Search for messages that meet the given query criteria. @type query: C{list} @param query: The search criteria @rtype: C{list} @return: A list of message sequence numbers or message UIDs which match the search criteria. """ if self.getMessageCount() == 0: return [] all_msgs = MessageSet(long(self.storage.keys()[0]), long(self.storage.keys()[-1])) matches = [] for id, msg in self._messagesIter(all_msgs, uid): for q in query: if msg.matches(q): matches.append(id) break return matches
def printqueryresult(self, result): print "The following %d messages matched:" % len(result) m = MessageSet() for item in result: m.add(item) print str(m)