Changed virus message structure.

see #187
This commit is contained in:
Antoine Nguyen
2011-11-09 09:24:19 +01:00
parent 2479ad23ab
commit 07f6f3e91e
2 changed files with 53 additions and 16 deletions
+5 -9
View File
@@ -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)
+48 -7
View File
@@ -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)