mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-10-18 05:51:26 +02:00
@@ -6,14 +6,14 @@
|
||||
"""
|
||||
import re
|
||||
from django.utils.translation import ugettext as _
|
||||
from modoboa.lib.exceptions import ModoboaException
|
||||
from modoboa.lib.exceptions import ModoboaException, InternalError
|
||||
|
||||
|
||||
class WebmailError(ModoboaException):
|
||||
errorexpr = re.compile("\[([^\]]+)\]\s*([^\.]+)")
|
||||
class WebmailInternalError(InternalError):
|
||||
errorexpr = re.compile(r'\[([^\]]+)\]\s*([^\.]+)')
|
||||
|
||||
def __init__(self, reason, ajax=False):
|
||||
match = WebmailError.errorexpr.match(reason)
|
||||
match = WebmailInternalError.errorexpr.match(reason)
|
||||
if not match:
|
||||
self.reason = reason
|
||||
else:
|
||||
@@ -24,6 +24,16 @@ class WebmailError(ModoboaException):
|
||||
return self.reason
|
||||
|
||||
|
||||
class UnknownAction(ModoboaException):
|
||||
"""
|
||||
Use this exception when the webmail encounter an unknown action.
|
||||
"""
|
||||
http_code = 404
|
||||
|
||||
def __init__(self):
|
||||
super(UnknownAction, self).__init__(_("Unknown action"))
|
||||
|
||||
|
||||
class ImapError(ModoboaException):
|
||||
def __init__(self, reason):
|
||||
self.reason = reason
|
||||
|
||||
@@ -11,9 +11,10 @@ import time
|
||||
from functools import wraps
|
||||
from django.utils.translation import ugettext as _
|
||||
from modoboa.lib import parameters, imap_utf7
|
||||
from modoboa.lib.exceptions import InternalError
|
||||
from modoboa.lib.connections import ConnectionsManager
|
||||
from modoboa.lib.webutils import static_url
|
||||
from exceptions import ImapError, WebmailError
|
||||
from exceptions import ImapError, WebmailInternalError
|
||||
from fetch_parser import parse_fetch_response
|
||||
|
||||
#imaplib.Debug = 4
|
||||
@@ -238,7 +239,7 @@ class IMAPconnector(object):
|
||||
data = self._cmd("LIST", "", "")
|
||||
m = self.list_response_pattern.match(data[0])
|
||||
if m is None:
|
||||
raise WebmailError(_("Failed to retrieve hierarchy delimiter"))
|
||||
raise InternalError(_("Failed to retrieve hierarchy delimiter"))
|
||||
self.__hdelimiter = m.group('delimiter')
|
||||
return self.__hdelimiter
|
||||
|
||||
@@ -564,20 +565,20 @@ class IMAPconnector(object):
|
||||
name = "%s%s%s" % (parent, self.hdelimiter, name)
|
||||
typ, data = self.m.create(self._encode_mbox_name(name))
|
||||
if typ == "NO":
|
||||
raise WebmailError(data[0])
|
||||
raise WebmailInternalError(data[0])
|
||||
return True
|
||||
|
||||
def rename_folder(self, oldname, newname):
|
||||
typ, data = self.m.rename(self._encode_mbox_name(oldname),
|
||||
self._encode_mbox_name(newname))
|
||||
if typ == "NO":
|
||||
raise WebmailError(data[0], ajax=True)
|
||||
raise WebmailInternalError(data[0], ajax=True)
|
||||
return True
|
||||
|
||||
def delete_folder(self, name):
|
||||
typ, data = self.m.delete(self._encode_mbox_name(name))
|
||||
if typ == "NO":
|
||||
raise WebmailError(data[0])
|
||||
raise WebmailInternalError(data[0])
|
||||
return True
|
||||
|
||||
def getquota(self, mailbox):
|
||||
|
||||
@@ -10,12 +10,13 @@ from django.core.files.uploadhandler import FileUploadHandler, SkipFile
|
||||
from django.utils.translation import ugettext as _, ugettext_lazy
|
||||
from django.conf import settings
|
||||
from modoboa.lib import u2u_decode, tables, parameters
|
||||
from modoboa.lib.exceptions import InternalError
|
||||
from modoboa.lib.webutils import size2integer
|
||||
from modoboa.lib.email_listing import EmailListing
|
||||
from modoboa.lib.emailutils import (
|
||||
EmailAddress, Email, prepare_addresses, set_email_headers
|
||||
)
|
||||
from modoboa.extensions.webmail.exceptions import WebmailError
|
||||
from modoboa.extensions.webmail.exceptions import WebmailInternalError
|
||||
from modoboa.extensions.webmail.imaputils import (
|
||||
IMAPconnector, get_imapconnector, BodyStructure
|
||||
)
|
||||
@@ -562,8 +563,8 @@ def save_attachment(f):
|
||||
dstdir = os.path.join(settings.MEDIA_ROOT, "webmail")
|
||||
try:
|
||||
fp = NamedTemporaryFile(dir=dstdir, delete=False)
|
||||
except Exception, e:
|
||||
raise WebmailError(str(e))
|
||||
except Exception as e:
|
||||
raise InternalError(str(e))
|
||||
for chunk in f.chunks():
|
||||
fp.write(chunk)
|
||||
fp.close()
|
||||
@@ -706,19 +707,19 @@ def send_mail(request, posturl=None):
|
||||
int(parameters.get_admin("SMTP_PORT")))
|
||||
if secmode == "starttls":
|
||||
s.starttls()
|
||||
except Exception, text:
|
||||
raise WebmailError(str(text))
|
||||
except Exception as text:
|
||||
raise WebmailInternalError(str(text))
|
||||
|
||||
if parameters.get_admin("SMTP_AUTHENTICATION") == "yes":
|
||||
try:
|
||||
s.login(request.user.username, get_password(request))
|
||||
except smtplib.SMTPException, e:
|
||||
raise WebmailError(str(e))
|
||||
raise WebmailInternalError(str(e))
|
||||
try:
|
||||
s.sendmail(request.user.email, rcpts, msg.as_string())
|
||||
s.quit()
|
||||
except smtplib.SMTPException, e:
|
||||
raise WebmailError(str(e))
|
||||
raise WebmailInternalError(str(e))
|
||||
|
||||
sentfolder = parameters.get_user(request.user, "SENT_FOLDER")
|
||||
IMAPconnector(user=request.user.username,
|
||||
|
||||
@@ -11,11 +11,12 @@ from django.contrib.auth.decorators import login_required
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from django.views.decorators.gzip import gzip_page
|
||||
from modoboa.lib import parameters
|
||||
from modoboa.lib.exceptions import ModoboaException, BadRequest
|
||||
from modoboa.lib.webutils import (
|
||||
_render_to_string, ajax_response, render_to_json_response
|
||||
)
|
||||
from modoboa.extensions.admin.lib import needs_mailbox
|
||||
from .exceptions import WebmailError
|
||||
from .exceptions import UnknownAction
|
||||
from .forms import FolderForm, AttachmentForm, ComposeMailForm
|
||||
from .imaputils import get_imapconnector, IMAPconnector, separate_mailbox
|
||||
from .lib import (
|
||||
@@ -43,7 +44,7 @@ def getattachment(request):
|
||||
pnum = request.GET.get("partnumber", None)
|
||||
fname = request.GET.get("fname", None)
|
||||
if not mbox or not mailid or not pnum or not fname:
|
||||
raise WebmailError(_("Invalid request"))
|
||||
raise BadRequest(_("Invalid request"))
|
||||
|
||||
imapc = get_imapconnector(request)
|
||||
partdef, payload = imapc.fetchpart(mailid, mbox, pnum)
|
||||
@@ -61,7 +62,7 @@ def getattachment(request):
|
||||
def move(request):
|
||||
for arg in ["msgset", "to"]:
|
||||
if not arg in request.GET:
|
||||
raise WebmailError(_("Invalid request"))
|
||||
raise BadRequest(_("Invalid request"))
|
||||
mbc = get_imapconnector(request)
|
||||
mbc.move(request.GET["msgset"], request.session["mbox"], request.GET["to"])
|
||||
resp = listmailbox(request, request.session["mbox"], update_session=False)
|
||||
@@ -74,7 +75,7 @@ def delete(request):
|
||||
mbox = request.GET.get("mbox", None)
|
||||
selection = request.GET.getlist("selection[]", None)
|
||||
if mbox is None or selection is None:
|
||||
raise WebmailError(_("Invalid request"))
|
||||
raise BadRequest(_("Invalid request"))
|
||||
selection = [item for item in selection if item.isdigit()]
|
||||
mbc = get_imapconnector(request)
|
||||
mbc.move(",".join(selection), mbox,
|
||||
@@ -92,12 +93,12 @@ def mark(request, name):
|
||||
status = request.GET.get("status", None)
|
||||
ids = request.GET.get("ids", None)
|
||||
if status is None or ids is None:
|
||||
raise WebmailError(_("Invalid request"))
|
||||
raise BadRequest(_("Invalid request"))
|
||||
imapc = get_imapconnector(request)
|
||||
try:
|
||||
getattr(imapc, "mark_messages_%s" % status)(name, ids)
|
||||
except AttributeError:
|
||||
raise WebmailError(_("Unknown action"))
|
||||
raise UnknownAction
|
||||
|
||||
return render_to_json_response({
|
||||
'action': status, 'mbox': name,
|
||||
@@ -109,7 +110,7 @@ def mark(request, name):
|
||||
@needs_mailbox()
|
||||
def empty(request, name):
|
||||
if name != parameters.get_user(request.user, "TRASH_FOLDER"):
|
||||
raise WebmailError(_("Invalid request"))
|
||||
raise BadRequest(_("Invalid request"))
|
||||
get_imapconnector(request).empty(name)
|
||||
content = "<div class='alert alert-info'>%s</div>" % _("Empty mailbox")
|
||||
return render_to_json_response({
|
||||
@@ -196,7 +197,7 @@ def editfolder(request, tplname="webmail/folder.html"):
|
||||
|
||||
name = request.GET.get("name", None)
|
||||
if name is None:
|
||||
raise WebmailError(_("Invalid request"))
|
||||
raise BadRequest(_("Invalid request"))
|
||||
shortname, parent = separate_mailbox(name, sep=mbc.hdelimiter)
|
||||
ctx = {"title": _("Edit mailbox"),
|
||||
"formid": "mboxform",
|
||||
@@ -219,7 +220,7 @@ def editfolder(request, tplname="webmail/folder.html"):
|
||||
def delfolder(request):
|
||||
name = request.GET.get("name", None)
|
||||
if name is None:
|
||||
raise WebmailError(_("Bad request"))
|
||||
raise BadRequest(_("Invalid request"))
|
||||
mbc = IMAPconnector(user=request.user.username,
|
||||
password=request.session["password"])
|
||||
mbc.delete_folder(name)
|
||||
@@ -251,7 +252,7 @@ def attachments(request, tplname="webmail/attachments.html"):
|
||||
"status": "ok", "fname": request.FILES["attachment"],
|
||||
"tmpname": os.path.basename(tmpname)
|
||||
})
|
||||
except WebmailError, inst:
|
||||
except ModoboaException as inst:
|
||||
error = _("Failed to save attachment: ") + str(inst)
|
||||
|
||||
if csuploader.toobig:
|
||||
@@ -413,7 +414,7 @@ def compose_and_send(request, action, callback=None):
|
||||
mbox = request.GET.get("mbox", None)
|
||||
mailid = request.GET.get("mailid", None)
|
||||
if mbox is None or mailid is None:
|
||||
raise WebmailError(_("Bad request"))
|
||||
raise BadRequest(_("Invalid request"))
|
||||
url = "?action=%s&mbox=%s&mailid=%s" % (action, mbox, mailid)
|
||||
if request.method == "POST":
|
||||
status, resp = send_mail(request, url)
|
||||
@@ -447,7 +448,7 @@ def getmailcontent(request):
|
||||
mbox = request.GET.get("mbox", None)
|
||||
mailid = request.GET.get("mailid", None)
|
||||
if mbox is None or mailid is None:
|
||||
raise WebmailError(_("Invalid request"))
|
||||
raise BadRequest(_("Invalid request"))
|
||||
email = ImapEmail(mbox, mailid, request, links=int(request.GET["links"]))
|
||||
return render(request, "common/viewmail.html", {
|
||||
"headers": email.render_headers(folder=mbox, mail_id=mailid),
|
||||
@@ -460,7 +461,7 @@ def viewmail(request):
|
||||
mbox = request.GET.get("mbox", None)
|
||||
mailid = request.GET.get("mailid", None)
|
||||
if mbox is None or mailid is None:
|
||||
raise WebmailError(_("Invalid request"))
|
||||
raise BadRequest(_("Invalid request"))
|
||||
links = request.GET.get("links", None)
|
||||
if links is None:
|
||||
links = 1 if parameters.get_user(request.user, "ENABLE_LINKS") == "yes" else 0
|
||||
@@ -489,7 +490,7 @@ def submailboxes(request):
|
||||
def check_unseen_messages(request):
|
||||
mboxes = request.GET.get("mboxes", None)
|
||||
if not mboxes:
|
||||
raise WebmailError(_("Invalid request"))
|
||||
raise BadRequest(_("Invalid request"))
|
||||
mboxes = mboxes.split(",")
|
||||
counters = dict()
|
||||
imapc = get_imapconnector(request)
|
||||
@@ -526,11 +527,11 @@ def index(request):
|
||||
|
||||
if action is not None:
|
||||
if not action in globals():
|
||||
raise WebmailError(_("Undefined action"))
|
||||
raise UnknownAction
|
||||
response = globals()[action](request)
|
||||
else:
|
||||
if request.is_ajax():
|
||||
raise WebmailError(_("Bad request"))
|
||||
raise BadRequest(_("Invalid request"))
|
||||
response = dict(selection="webmail")
|
||||
|
||||
curmbox = request.session.get("mbox", "INBOX")
|
||||
|
||||
Reference in New Issue
Block a user