fetchmail.py
上传用户:gyjinxi
上传日期:2007-01-04
资源大小:159k
文件大小:4k
源码类别:

WEB邮件程序

开发平台:

Python

  1. from poplib import POP3
  2. from mailbox import UnixMailbox, _Subfile
  3. from string import split
  4. try: from cStringIO import StringIO
  5. except: from StringIO import StringIO
  6. from Message import Message
  7. import string, os
  8. from bobomailrc import *
  9. class Mailbox(UnixMailbox):
  10. def __init__(self, filename):
  11. self.fp = None
  12. self.seekp = 0
  13. self.filename = filename
  14. self.connected = 0
  15. def __del__(self):
  16. try: self.fp.close()
  17. except: pass
  18. def open(self, auth):
  19. self.fp = open(self.filename)
  20. self.auth = auth
  21. self.parse()
  22. self.connected = 1
  23. def parse(self):
  24. self.msgs = []
  25. while 1:
  26. m = self.next()
  27. if not m: break
  28. m.unique_num = len(self.msgs)
  29. self.msgs.append(m)
  30. def __len__(self):
  31. return len(self.msgs)
  32. def __getitem__(self, index):
  33. if type(index) == type(()): index, preview = index
  34. else: preview = false
  35. if not preview: self.msgs[index].parse()
  36. return self.msgs[index]
  37. def __delitem__(self, index):
  38. newfn = "%s.new" % self.filename
  39. new = MailboxList(newfn)
  40. for i in range(len(self)):
  41. if i <> index:
  42. new.append(self[i])
  43. os.remove(self.filename)
  44. os.rename(newfn, self.filename)
  45. self.msgs = new.msgs
  46. self.fp = new.fp
  47. def next(self):
  48. while 1:
  49. self.fp.seek(self.seekp)
  50. try:
  51. self._search_start()
  52. except EOFError:
  53. self.seekp = self.fp.tell()
  54. return None
  55. start = self.fp.tell()
  56. self._search_end()
  57. self.seekp = stop = self.fp.tell()
  58. if start <> stop: break
  59. return Message(_Subfile(self.fp, start, stop), preview=1)
  60. def append(self, msg):
  61. self.fp.seek(1,2)
  62. if self.fp.read(1) <> 'n':
  63. self.fp.write('n')
  64. self.fp.write(msg.unixfrom)
  65. self.fp.write(str(msg))
  66. def close(self): pass
  67. class MailSpool(Mailbox):
  68. def __init__(self):
  69. Mailbox.__init__(self, "")
  70. def open(self, auth):
  71. self.filename = "/var/spool/mail/%s" % auth.userid
  72. Mailbox.open(self, auth)
  73. class UserMailbox(Mailbox):
  74. def open(self, auth):
  75. self.filename = "%s/%s.%s" % (user_dir, auth.userid, self.filename)
  76. Mailbox.login(self, auth)
  77. class POP3Box:
  78. def __init__(self): self.connected = 0
  79. def open(self, auth):
  80. self.con = POP3(pop3_host)
  81. self.con.user(auth.userid)
  82. self.con.pass_(auth.password)
  83. self.connected = 1
  84. def close(self):
  85. if self.connected:
  86. self.con.quit()
  87. self.connected = 0
  88. def __del__(self):
  89. try: self.close()
  90. except: pass
  91. def __len__(self):
  92. return self.con.stat()[0]
  93. def __getitem__(self, index):
  94. if type(index) == type(()): index, preview = index
  95. else: preview = false
  96. index = index + 1
  97. if preview:
  98. m = Message(StringIO(string.join(self.con.top(index, 0)[1], "n")), preview)
  99. else: m = Message(StringIO(string.join(self.con.retr(index)[1], "n")), preview)
  100. m.unique_num = index - 1
  101. return m
  102. def __delitem__(self, index):
  103. self.con.dele(index + 1)
  104. class IMAP4Box:
  105. def __init__(self):
  106. self.connected = 0
  107. def open(self, auth):
  108. self.auth = auth
  109. self.con = IMAP4(imap4_host)
  110. self.con.login(auth.userid, auth.password)
  111. self.connected = 1
  112. def close(self):
  113. if self.connected:
  114. self.con.close()
  115. self.con.logout()
  116. self.connected = 0
  117. def __del__(self):
  118. try: self.close()
  119. except: pass
  120. def __len__(self):
  121.     resp = self.mailbox.select()
  122.     resp, data = self.mailbox.search(None, "ALL")
  123.     return len(split(data[0]))
  124. def __delitem__(self, index):
  125. if type(index) == type(()): index, preview = index
  126. else: preview = false
  127. def __getitem__(self, index):
  128. if type(index) == type(()): index, preview = index
  129. else: preview = false
  130. resp = self.mailbox.select()
  131. resp, data = self.mailbox.search(None, "ALL")
  132. entries = split(data[0])
  133. index = entries[index-1]
  134. resp, data = self.mailbox.fetch(index, '(RFC822)')
  135. buf = data[0][1]
  136. return Message(StringIO(buf))
  137. #if __name__ == "__main__":
  138. #class Auth: userid = "guest"
  139. #from auth import POP3Authentication
  140. #a = POP3Authentication()
  141. #a.login(auth_host, "guest", "guest")
  142. #a.relogin(a.hash)
  143. #ml = POP3Box()
  144. #ml.login(a)
  145. #for x in ml: print x.headers