fetchmail.py
上传用户:gyjinxi
上传日期:2007-01-04
资源大小:159k
文件大小:4k
- from poplib import POP3
- from mailbox import UnixMailbox, _Subfile
- from string import split
- try: from cStringIO import StringIO
- except: from StringIO import StringIO
- from Message import Message
- import string, os
- from bobomailrc import *
- class Mailbox(UnixMailbox):
- def __init__(self, filename):
- self.fp = None
- self.seekp = 0
- self.filename = filename
- self.connected = 0
- def __del__(self):
- try: self.fp.close()
- except: pass
-
- def open(self, auth):
- self.fp = open(self.filename)
- self.auth = auth
- self.parse()
- self.connected = 1
- def parse(self):
- self.msgs = []
- while 1:
- m = self.next()
- if not m: break
- m.unique_num = len(self.msgs)
- self.msgs.append(m)
- def __len__(self):
- return len(self.msgs)
- def __getitem__(self, index):
- if type(index) == type(()): index, preview = index
- else: preview = false
- if not preview: self.msgs[index].parse()
- return self.msgs[index]
- def __delitem__(self, index):
- newfn = "%s.new" % self.filename
- new = MailboxList(newfn)
- for i in range(len(self)):
- if i <> index:
- new.append(self[i])
- os.remove(self.filename)
- os.rename(newfn, self.filename)
- self.msgs = new.msgs
- self.fp = new.fp
- def next(self):
- while 1:
- self.fp.seek(self.seekp)
- try:
- self._search_start()
- except EOFError:
- self.seekp = self.fp.tell()
- return None
- start = self.fp.tell()
- self._search_end()
- self.seekp = stop = self.fp.tell()
- if start <> stop: break
- return Message(_Subfile(self.fp, start, stop), preview=1)
- def append(self, msg):
- self.fp.seek(1,2)
- if self.fp.read(1) <> 'n':
- self.fp.write('n')
- self.fp.write(msg.unixfrom)
- self.fp.write(str(msg))
- def close(self): pass
- class MailSpool(Mailbox):
- def __init__(self):
- Mailbox.__init__(self, "")
- def open(self, auth):
- self.filename = "/var/spool/mail/%s" % auth.userid
- Mailbox.open(self, auth)
- class UserMailbox(Mailbox):
- def open(self, auth):
- self.filename = "%s/%s.%s" % (user_dir, auth.userid, self.filename)
- Mailbox.login(self, auth)
- class POP3Box:
- def __init__(self): self.connected = 0
- def open(self, auth):
- self.con = POP3(pop3_host)
- self.con.user(auth.userid)
- self.con.pass_(auth.password)
- self.connected = 1
- def close(self):
- if self.connected:
- self.con.quit()
- self.connected = 0
-
- def __del__(self):
- try: self.close()
- except: pass
-
- def __len__(self):
- return self.con.stat()[0]
-
- def __getitem__(self, index):
- if type(index) == type(()): index, preview = index
- else: preview = false
- index = index + 1
- if preview:
- m = Message(StringIO(string.join(self.con.top(index, 0)[1], "n")), preview)
- else: m = Message(StringIO(string.join(self.con.retr(index)[1], "n")), preview)
- m.unique_num = index - 1
- return m
- def __delitem__(self, index):
- self.con.dele(index + 1)
-
- class IMAP4Box:
- def __init__(self):
- self.connected = 0
- def open(self, auth):
- self.auth = auth
- self.con = IMAP4(imap4_host)
- self.con.login(auth.userid, auth.password)
- self.connected = 1
- def close(self):
- if self.connected:
- self.con.close()
- self.con.logout()
- self.connected = 0
-
- def __del__(self):
- try: self.close()
- except: pass
- def __len__(self):
- resp = self.mailbox.select()
- resp, data = self.mailbox.search(None, "ALL")
- return len(split(data[0]))
- def __delitem__(self, index):
- if type(index) == type(()): index, preview = index
- else: preview = false
- def __getitem__(self, index):
- if type(index) == type(()): index, preview = index
- else: preview = false
- resp = self.mailbox.select()
- resp, data = self.mailbox.search(None, "ALL")
- entries = split(data[0])
- index = entries[index-1]
- resp, data = self.mailbox.fetch(index, '(RFC822)')
- buf = data[0][1]
- return Message(StringIO(buf))
- #if __name__ == "__main__":
- #class Auth: userid = "guest"
- #from auth import POP3Authentication
- #a = POP3Authentication()
- #a.login(auth_host, "guest", "guest")
- #a.relogin(a.hash)
- #ml = POP3Box()
- #ml.login(a)
- #for x in ml: print x.headers