"""
mailfeedlib
Utilities for generating feeds from mail messages.
"""
import sys, time, shelve, md5, re
from urllib import quote
from urlparse import urlparse
import email, email.Utils
from xml.sax.saxutils import escape
from scraperlib import FeedEntryDict, Scraper
import poplib
class POP3Client:
"""
Generic interface to fetch messages from a POP3 mailbox.
"""
def __init__(self, host='localhost', port=110, user=None, passwd=None):
"""Initialize POP3 connection details."""
self.host = host
self.port = port
self.user = user
self.passwd = passwd
def fetch_messages(self, max_messages=15):
"""
Fetch messages up to the maximum, return email.Message objects.
"""
# Connect to the POP3 mailbox
mailbox = poplib.POP3(self.host, self.port)
mailbox.user(self.user)
mailbox.pass_(self.passwd)
# Look up messages, establish a window for fetching
nums = range(len(mailbox.list()[1]))
end_pos = len(nums)
start_pos = max(0, end_pos - max_messages)
# Fetch and accumulate Messages
msgs = []
for i in nums[start_pos:end_pos]:
try:
msg_txt = "\n".join(mailbox.retr(i+1)[1])
msg = email.message_from_string(msg_txt)
msgs.append(msg)
except KeyboardInterrupt:
raise
except:
pass
# Log out of mailbox
mailbox.quit()
return msgs
import imaplib
class IMAP4Client:
"""
Generic interface to fetch messages from a IMAP4 mailbox.
"""
def __init__(self, host='localhost', port=110, user=None, passwd=None):
"""Initialize IMAP4 connection details."""
self.host = host
self.port = port
self.user = user
self.passwd = passwd
def fetch_messages(self, max_messages=15):
"""
Fetch messages up to the maximum, return email.Message objects.
"""
# Connect to the IMAP4 mailbox
mailbox = imaplib.IMAP4(self.host, int(self.port))
mailbox.login(self.user, self.passwd)
mailbox.select()
# Look up undeleted messages, establish a window for fetching
nums = mailbox.search(None, "UNDELETED")[1][0].split()
end_pos = len(nums)
start_pos = max(0, end_pos - max_messages)
# Fetch and accumulate Messages
msgs = []
for i in nums[start_pos:end_pos]:
try:
msg_txt = mailbox.fetch(str(i), "RFC822")[1][0][1]
msg = email.message_from_string(msg_txt)
msgs.append(msg)
except KeyboardInterrupt:
raise
except:
pass
# Log out of mailbox
mailbox.close()
mailbox.logout()
return msgs
class MailScraper(Scraper):
"""
Use an email client to download messages on which to base a feed.
"""
TAG_DOMAIN = "mail.example.com"
STATE_FN = "mail_scraper_state"
ATOM_ENTRY_TMPL = """
tags and escaped
if content_type == 'text/plain':
body_segs.append(u"\n%s
" % escape(body))
return "\n
\n".join(body_segs)