Esempio n. 1
0
class IMAPLogic:
	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.examine('INBOX'))
		d.addCallback(lambda x: self.proto.fetchSpecific('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 gotmessages(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, failure):
		reactor.stop()

	def errorhappened(self, failure):
		print "An error occurred:", failure.getErrorMessage()
		d = self.logout()
		d.addBoth(self.stopreactor)
		return failure
Esempio n. 2
0
 def _foundMail(_self, result):
     self.output("imap: found %d messages" %(len(result)))
     if result:
         messages = MessageSet()
         for uid in result:
             messages.add(uid)
         d = _self.fetchMessage(messages, uid=True)
         d = d.addCallback(_self._gotMail)
     else:
         d = _self.logout()
         d = d.addCallback(_self._done)
     return d.addErrback(_self.catchErrors)
Esempio n. 3
0
 def _foundMail(_self, result):
     self.output("imap: found %d messages" % (len(result)))
     if result:
         messages = MessageSet()
         for uid in result:
             messages.add(uid)
         d = _self.fetchMessage(messages, uid=True)
         d = d.addCallback(_self._gotMail)
     else:
         d = _self.logout()
         d = d.addCallback(_self._done)
     return d.addErrback(_self.catchErrors)
Esempio n. 4
0
            def _gotMail(_self, result):
                self.output("imap: got mail")
                messages = MessageSet()
                for data in result.itervalues():
                    message = message_from_string(data['RFC822'])
                    toRepoId = message.get('X-chandler-p2p-to')
                    if toRepoId is None or UUID(toRepoId) == self._repoId:
                        args = (data['UID'], message, peerId)
                        self.worker.queueRequest(('receive', args))
                        messages.add(data['UID'])
#                d = _self.logout()
#                d = d.addCallback(_self._done)
                d = _self.addFlags(messages, ('\\Deleted',), uid=True)
                d = d.addCallback(_self._flagsAdded)
                return d.addErrback(_self.catchErrors)
Esempio n. 5
0
    def test_fetch(self):
        t1 = Tag(name=u'foo')

        m1 = Message(body=u'm1', length=0, tags=[t1])
        m2 = Message(body=u'm2', length=0, tags=[t1])

        meta.Session.add_all([t1, m1, m2])
        meta.Session.commit()

        m = MessageSet(1, 2)
        n.eq_(list(t1.fetch(m, False)), [m1, m2])

        m = MessageSet()
        m.add(t1.getUID(1))
        m.add(t1.getUID(2))
        n.eq_(list(t1.fetch(m, True)), [m1, m2])
Esempio n. 6
0
            def _gotMail(_self, result):
                self.output("imap: got mail")
                messages = MessageSet()
                for data in result.itervalues():
                    message = message_from_string(data['RFC822'])
                    toRepoId = message.get('X-chandler-p2p-to')
                    if toRepoId is None or UUID(toRepoId) == self._repoId:
                        args = (data['UID'], message, peerId)
                        self.worker.queueRequest(('receive', args))
                        messages.add(data['UID'])


#                d = _self.logout()
#                d = d.addCallback(_self._done)
                d = _self.addFlags(messages, ('\\Deleted', ), uid=True)
                d = d.addCallback(_self._flagsAdded)
                return d.addErrback(_self.catchErrors)
Esempio n. 7
0
def cbFetch(result, proto):
    keys = result.keys()
    #keys.sort()
    #for k in keys:
        #print '%s %s' % (k, result[k][0][2])
#        print 'k = ', k
#        print 'Result[k] = ',  result[k]
#        for m in result[k]:
#            print 'm = ',  m
#            for n in m:
#                print 'n = ', n
#            
    messages = MessageSet()
    for uid in result:
        messages.add(uid)
    #print "messages = ",  messages
    
    d = proto.fetchMessage(messages, uid=False)
    d = d.addCallback(gotMail,  proto)
Esempio n. 8
0
	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
Esempio n. 9
0
    def test_Twisted_16_6_bug(self):
        """
        Make sure L{IMAP4DownloadProtocol.cbGotSearch} works.

        There is a bug in Twisted 16.6 where the IMAP4Client._fetch method does not
        accept a MessageSet argument - though it previously did.

        We also need to double check that IMAP4Client._store works with a MessageSet.
        """

        imap4 = IMAP4DownloadProtocol()
        imap4.sendCommand = lambda cmd: succeed(cmd)
        imap4._cbFetch = lambda result, requestedParts, structured: {
            123: {
                "UID": "456"
            }
        }
        imap4.cbGotMessage = lambda results, messageList: succeed(True)
        result = yield imap4.cbGotSearch((123, ))
        self.assertTrue(result is None)

        imap4 = IMAP4DownloadProtocol()
        imap4.sendCommand = lambda cmd: succeed(cmd)
        imap4._cbFetch = lambda result, requestedParts, structured: {
            "123": {
                "UID": "456"
            }
        }
        imap4.cbMessageUnseen = lambda results, messageList: succeed(True)
        ms = MessageSet()
        ms.add(123)
        result = yield imap4.cbFlagUnseen(ms)
        self.assertTrue(result is None)

        imap4 = IMAP4DownloadProtocol()
        imap4.sendCommand = lambda cmd: succeed(cmd)
        imap4._cbFetch = lambda result, requestedParts, structured: {
            "123": {
                "UID": "456"
            }
        }
        imap4.cbMessageDeleted = lambda results, messageList: succeed(True)
        ms = MessageSet()
        ms.add(123)
        result = yield imap4.cbFlagDeleted(ms)
        self.assertTrue(result is None)
Esempio n. 10
0
	def printqueryresult(self, result):
		print "The following %d messages matched:" % len(result)
		m = MessageSet()
		for item in result:
			m.add(item)
		print str(m)