main.py
上传用户:gyjinxi
上传日期:2007-01-04
资源大小:159k
文件大小:7k
- # Distributed under GNU GPL. See COPYING
- # Copyright (c) 1998-2000 Henning Schr鰀er
- from string import replace
- from bobomailrc import *
- from bobomailrc import __version__, __author__
- from mimeviewer import *
- from Message import *
- from logging import *
- import fintl
- gettext = fintl.gettext
- fintl.bindtextdomain("bobomail", locale_dir)
- fintl.textdomain("bobomail")
- _ = gettext
- def HTMLTemplate(template, extra={}, **exchange):
- page = open(template_template).read()
- extra["%(body)s"] = open(template).read()
- for old, new in extra.items():
- page = replace(page, old, new)
- exchange["imagedir"] = image_dir
- html = HTML(page)
- return apply(html, (), exchange)
- class BoboMail:
- "BoboMail main class"
- def __init__(self, authtype, folders):
- self.auth = authtype
- self.folders = folders
- def Debug(self, REQUEST):
- "Debug session"
- return REQUEST
- def getFolder(self, name):
- mbox = self.folders[name]
- if not mbox.connected:
- mbox.open(self.auth)
- return mbox
- def index_html(self, REQUEST, RESPONSE):
- "Show start page"
- return HTMLTemplate(login_template, title=_("Login"))
-
- def Login(self, userid, password, REQUEST, RESPONSE):
- "Log into BoboMail"
- if self.auth.login(auth_host, userid, password):
- log("OK: %s logged in" % userid)
- RESPONSE.redirect(
- "%s/List?sid=%s" % (REQUEST["SCRIPT_NAME"], self.auth.hash))
- #return self.List(self.auth.hash)
- else:
- return self.authError()
- def List(self, sid, by="Date"):
- "Show mails"
- if self.auth.relogin(sid):
- inbox = self.getFolder("inbox")
- msgs = []
- for i in range(len(inbox)):
- msgs.append(inbox[i, 1])
- inbox.close()
- t = _("List inbox for %s")
- return HTMLTemplate(
- list_template, {"%(by)s": by}, mails=msgs, sid=sid,
- title=t % self.auth.userid)
- else:
- return self.authError()
-
- def Read(self, sid, index):
- "Read single message"
- if self.auth.relogin(sid):
- inbox = self.getFolder("inbox")
- msg = inbox[index]
- inbox.close()
- return HTMLTemplate(mail_template,
- title=_("Read message"), msg=msg, sid=sid, index=index, message=MessageViewer(msg, sid))
- else:
- return self.authError()
- def GetPart(self, sid, index, num, RESPONSE):
- "Get decodes message part"
- if self.auth.relogin(sid):
- inbox = self.getFolder("inbox")
- msg = inbox[index]
- inbox.close()
- part = getMIMEPart(msg, num)
- RESPONSE.setHeader("Content-Type", part.gettype())
- if part.filename:
- RESPONSE.setHeader("Content-Disposition", 'filename="%s"' % part.filename)
- return part.decode()
- else:
- return self.authError()
- def Compose(self, sid, To="", Subject=""):
- "Compose new message"
- if self.auth.relogin(sid):
- return HTMLTemplate(compose_template,
- sid=sid, to=To, subject=Subject, title=_("Compose new message"))
- else:
- return self.authError()
-
- def Reply(self, sid, index):
- "Reply to message"
- if self.auth.relogin(sid):
- inbox = self.getFolder("inbox")
- msg = inbox[index]
- inbox.close()
- return HTMLTemplate(compose_template,
- title=_("Reply to message"), sid=sid, to=msg.FromAddr,
- subject=_("Re: %s") % msg.Subject, imagedir=image_dir,
- body=quotedtext(repr(MessageViewer(msg, sid))))
- else:
- return self.authError()
- def Forward(self, sid, index):
- "Forward a message"
- if self.auth.relogin(sid):
- inbox = self.getFolder("inbox")
- msg = inbox[index]
- inbox.close()
- return HTMLTemplate(compose_template,
- title=_("Forward message"), sid=sid,
- subject=_("Fwd: %s - %s") % (msg.FromAddr, msg.Subject),
- body=quotedtext(repr(MessageViewer(msg, sid))))
- else:
- return self.authError()
- def Delete(self, sid, REQUEST, RESPONSE, index=None):
- "Delete messages"
- if self.auth.relogin(sid):
- inbox = self.getFolder("inbox")
- if index:
- del inbox[index]
- else:
- import cgi, re, string
- numpat = re.compile("^[0123456789*]")
- form = cgi.FieldStorage()
- delcount = 0
- log("%s" % form.keys())
- for i in form.keys():
- log("%s : %s" % (i, numpat.match(i)))
- if numpat.match(i):
- del inbox[int(i) - delcount]
- delcount = delcount + 1
- inbox.close()
- RESPONSE.redirect("%s/List?sid=%s" % (REQUEST["SCRIPT_NAME"], self.auth.hash))
- else: return self.authError()
- def Bounce(self, sid, To, REQUEST, index=None):
- "Bounce messages"
- return "bounce coming soon"
- def Send(self, sid, to, cc="", bcc="", subject="", body="", attach=None):
- "Send mail"
- if self.auth.relogin(sid):
- from sendmail import *
- if attach is not None: filename = attach.filename
- else: filename = ""
- errors = sendmail(
- "%s@%s" % (self.auth.userid, domain), to, cc, bcc, subject, body, attach, filename)
- return HTMLTemplate(sent_template, title=_("Sent mail"), sid=sid, failed=errors)
- else: return self.authError()
-
- def About(self, sid=None):
- "About bobomail"
- return HTMLTemplate(about_template, title=_("About"), version=__version__, sid=sid)
- def Help(self, sid=None):
- "Help for bobomail"
- return _("Only loosers and communists need help (Weird Al Yankovic)")
- def Prefs(self, sid):
- "Preferenes"
- return "Currently nothing to configure here. Look into bobomailrc.py"
- def AddrBook(self, sid):
- "Address book"
- return "Address book coming soon"
- def AddrAdd(self, name, email):
- "Add to address book"
- return "Adding adresses coming soon"
- def Goto(self, REQUEST, RESPONSE):
- "Goto URL (necessary to hide REFERRER information"
- url = urlunquote(REQUEST.get("QUERY_STRING", ""))
- RESPONSE.redirect(url)
- def GetFile(self, REQUEST, RESPONSE):
- "Get file (if you use BoboMailHTTPServer)"
- import os
- fn = urlunquote(REQUEST.get("QUERY_STRING", ""))
- if fn[0] not in [".", os.sep]:
- import mimetypes
- mimetypes.init(["/etc/mime.types", bm_mimetypes])
- ext = os.path.splitext(fn)[1]
- ctype = mimetypes.guess_type(ext)[0] or "application/octet-stream"
- RESPONSE.setHeader("Content-Type", ctype)
- return open(os.path.join(app_dir, fn)).read()
- def Logout(self, REQUEST, RESPONSE, sid=None):
- "Log out"
- if self.auth.relogin(sid):
- self.auth.logout()
- RESPONSE.redirect("%s/index_html" % REQUEST["SCRIPT_NAME"])
- def error(self, msg, reason=None):
- return HTMLTemplate(error_template,
- title=_("Error"), description=msg, reason=reason)
-
- def authError(self):
- log("FAILURE: authenticating %s" % getattr(self.auth, "userid", ""))
- return self.error(
- _("Authentication failed. Please check your userid and password" +
- " and try to login again."), reason=_("Or maybe you session has expired"))
- # cgi must run as root
- #from fetchmail import MailSpool, UserMailbox
- #from auth import SimpleAuthentication
- #SimpleAuthentication.atable = {"guest": "guest", "testuser": "secret"}
- #bobomail = BoboMail(
- # SimpleAuthentication(),
- # {"inbox": MailSpool(), "sent": UserMailbox("sent")})
- from fetchmail import POP3Box
- from auth import POP3Authentication
- bobomail = BoboMail(POP3Authentication(), {"inbox": POP3Box()})
- bobo_application = bobomail # only publish bobomail instance
- # Test stuff
- if __name__ == "__main__":
- class DUMMY:
- def __getattr__(*args): pass
- def __setattr__(*args): pass
- def __getitem__(*args): pass
- def __setitem__(*args): pass
- def redirect(*args): pass
- def setHeader(*args): pass
- #print bobomail.GetFile({"QUERY_STRING": "images/bobomail.css"}, DUMMY())
- #print bobomail.index_html(DUMMY(), DUMMY())
- bobomail.Login("guest", "guest", DUMMY(), DUMMY())
- #bobomail.Delete(bobomail.auth.hash, DUMMY(), DUMMY(), index=1)
- print bobomail.List(bobomail.auth.hash)
- #print bobomail.Read(bobomail.auth.hash, 1)
- #bobomail.GetPart(bobomail.auth.hash, 1, [1], DUMMY())