From 07f6f3e91e6b01c20b4b713d8c6498f2c6c741e2 Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Wed, 9 Nov 2011 09:24:19 +0100 Subject: [PATCH] Changed virus message structure. see #187 --- demo/views.py | 14 +++++------- lib/emailutils.py | 55 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/demo/views.py b/demo/views.py index 8d974f21e..e43f64fa8 100755 --- a/demo/views.py +++ b/demo/views.py @@ -1,20 +1,16 @@ # coding: utf-8 +from django.conf import settings from django.contrib.auth.decorators import login_required from modoboa.admin.lib import is_not_localadmin from modoboa.lib.webutils import ajax_response -from modoboa.lib.emailutils import sendmail_simple +from modoboa.lib.emailutils import sendmail_simple, sendmail_fromfile @login_required @is_not_localadmin() def send_virus(request): - status, error = sendmail_simple("virus@example.net", request.user.username, """ -This is the EICAR testvirus pattern. - -X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H* - -It cannot harm your computer, but virus scanners -will detect and treat it as if it were a virus. -""") + status, error = sendmail_fromfile("virus@example.net", request.user.username, + os.path.join(settings.MODOBOA_DIR, + "tmp/virus.msg")) if status: return ajax_response(request) return ajax_response(request, "ko", respmsg=error) diff --git a/lib/emailutils.py b/lib/emailutils.py index e913dd907..317c82d40 100644 --- a/lib/emailutils.py +++ b/lib/emailutils.py @@ -213,6 +213,28 @@ def decode(s, encodings=('utf8', 'latin1', 'windows-1252', 'ascii'), charset=Non pass return s.decode('ascii', 'ignore') +def __sendmail(sender, rcpt, msgstring, server='localhost', port=25): + """Message sending + + Return a tuple (True, None) on success, (False, error message) + otherwise. + + :param sender: sender address + :param rcpt: recipient address + :param msgstring: the message structure (must be a string) + :param server: the sending server's address + :param port: the listening port + :return: tuple + """ + try: + s = smtplib.SMTP(server, port) + s.connect() + s.sendmail(sender, [rcpt], msgstring) + s.quit() + except smtplib.SMTPException, e: + return False, "SMTP error: %s" % str(e) + return True, None + def sendmail_simple(sender, rcpt, content=""): """Simple way to send a text message @@ -233,11 +255,30 @@ def sendmail_simple(sender, rcpt, content=""): msg["Message-ID"] = make_msgid() msg["Date"] = formatdate(time.time(), True) + return __sendmail(sender, rcpt, msg.as_string()) + +def sendmail_fromfile(sender, rcpt, fname): + """Send a message contained within a file + + The given file name must represent a valid message structure. It + must not include the From: and To: headers, they are automatically + added by the function. + + :param sender: sender address + :param rcpt: recipient address + :param fname: the name of the file containing the message + :return: a tuple + """ try: - s = smtplib.SMTP() - s.connect() - s.sendmail(msg["From"], [rcpt], msg.as_string()) - s.quit() - except smtplib.SMTPException, e: - return False, "SMTP error: %s" % str(e) - return True, None + fp = open(fname) + except IOError, e: + return False, str(e) + + content = """From: %s +To: %s +""" % (sender, rcpt) + content += fp.read() + fp.close() + + return __sendmail(sender, rcpt, content) +