Permalink
Browse files

Receive marche

  • Loading branch information...
1 parent 99f0f4b commit a19063c82d2124f7e9132ef7614956ead8859306 @aallai committed Nov 28, 2011
Showing with 91 additions and 21 deletions.
  1. +2 −2 core.py
  2. +89 −19 mediums.py
View
@@ -72,9 +72,9 @@ def receive(medium_list, receiver_key) :
for mid, segments in d.items() :
try :
- messages[mid] += chunk_list
+ messages[mid] += segments
except KeyError :
- messages[mid] = chunk_list
+ messages[mid] = segments
# python guesses how to sort this correctly
return {mid : ''.join([seg[1] for seg in sorted(segments)]) for mid, segments in messages.items()}
View
@@ -1,10 +1,9 @@
+import email
import smtplib
import imaplib
import poplib
+import re
from config import config
-
-POP = 0
-IMAP = 1
# base class for transmission medium
# just a draft for now
@@ -71,7 +70,7 @@ def __init__(self, host, user, passwd, port=0, use_ssl=True) :
else :
port = 143
- super().__init__(host, port, user, passwd)
+ super(ImapServer, self).__init__(host, port, user, passwd)
self.use_ssl = use_ssl
@@ -80,43 +79,92 @@ def fetch_mail(self) :
try :
server = None
- if use_ssl :
+ if self.use_ssl :
server = imaplib.IMAP4_SSL(self.host, self.port)
else :
- server = imaplib.IMAP(self.host, self.port)
+ server = imaplib.IMAP4(self.host, self.port)
server.login(self.user, self.passwd)
# checks INBOX by default
server.select()
+ # poplib version returns string with \r\n scrubbed out, do the same here
ls = []
for index in server.search(None, 'ALL')[1][0].split() :
- ls.append(server.fetch(index, '(UID BODY[TEXT])')[1][0][1])
+ ls.append(re.sub('\r\n', '\n', server.fetch(index, '(UID BODY[TEXT])')[1][0][1]).strip('\n'))
+
+ server.logout()
- return ls
+ return ls
- except IMAP4.error e :
+ except imaplib.IMAP4.error, e :
raise EmailError(e.message)
+class PopServer(MailboxServer) :
+
+ def __init__(self, host, user, passwd, port=0, use_ssl=True) :
+
+ if port == 0 :
+ if use_ssl :
+ port = 995
+ else :
+ port = 110
+
+ super(PopServer, self).__init__(host, port, user, passwd)
+ self.use_ssl = use_ssl
+
+ def fetch_mail(self) :
+
+ try :
+
+ server = None
+
+ if self.use_ssl :
+ server = poplib.POP3_SSL(self.host, self.port)
+ else :
+ server = poplib.POP3(self.host, self.port)
+
+ messages = []
+
+ server.user(self.user)
+ server.pass_(self.passwd)
+
+ # a bit convoluted, poplib gets rid of the newline separating header and body,
+ # not sure how to tell where the body starts, email can do it
+ for i in xrange(len(server.list()[1])) :
+ messages.append( email.message_from_string( '\n'.join(server.retr(i+1)[1]) ).get_payload() )
+
+ server.quit()
+
+ return messages
+
+ except poplib.error_proto, e :
+ raise EmailError(e.message)
+
+
class EmailMedium(Medium) :
'''
Represents an email account to which messages can be sent (and possbly received if we have the password for it).
- '''
-
+ '''
def __init__(self, address, mailbox_server=None) :
- # MTU depends on smtp settings of server... maybe we should connect here and figure it out
+
+ # the default MTU is pretty arbitrary... should discuss it
self.address = address
- self.mtu = 2048 # temporary
+ self.mtu = 1024
self.mailbox_server = mailbox_server
def send(self, data, mid, seq, key) :
-
+
+ '''
+ Send off a segment in an email
+ '''
+
# not sure this works with every smtp server
server = smtplib.SMTP(config['smtp_server'])
@@ -132,14 +180,36 @@ def send(self, data, mid, seq, key) :
def receive(self, key) :
+ '''
+ Scan email account for messages
+ '''
+
if not (self.mailbox_server) :
- raise EmailError("Tried to receive email account for which no IMAP/POP server was provided.")
+ raise EmailError('Tried to receive email account for which no IMAP/POP server was provided.')
- '''
- Scan email account for messages
- '''
+ messages = self.mailbox_server.fetch_mail()
+
+ d = {}
- messages = mailbox_server.fetch_mail()
+ for m in messages :
+ # \r gets thrown in everywhere
+ lines = m.split('\n')
+ if lines[0] == key : # protocol message for us, otherwise regular email
+
+ try :
+ mid, seq = lines[1].split()
+ seq = int(seq)
+ data = '\n'.join(lines[2:])
+
+ try :
+ d[mid] += [(seq, data)]
+ except KeyError :
+ d[mid] = [(seq, data)]
+
+ except ValueErrror : # bogus header, ignore
+ pass
+
+ return d

0 comments on commit a19063c

Please sign in to comment.