Skip to content

Commit

Permalink
Added mailname and removed SMTP codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kura committed Jul 23, 2013
1 parent 4ad0925 commit dbf885f
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 18 deletions.
9 changes: 6 additions & 3 deletions blackhole/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
from tornado import iostream
from tornado.options import options

from blackhole import __fullname__
from blackhole.state import MailState
from blackhole.data import response, EHLO_RESPONSES
from blackhole.opts import ports
from blackhole.ssl_utils import sslkwargs
from blackhole.log import log
from blackhole.utils import email_id
from blackhole.utils import email_id, get_mailname


def sockets():
Expand Down Expand Up @@ -102,7 +103,7 @@ def handle_command(line, mail_state):
import time
IOLoop.instance().add_timeout(time.time() + 5, time.time())
elif line.lower().startswith("ehlo"):
resp = ["%s\n" % x for x in EHLO_RESPONSES]
resp = ["%s\r\n" % x for x in EHLO_RESPONSES]
elif any(line.lower().startswith(e) for e in ['helo', 'mail from',
'rcpt to', 'noop']):
resp = response(250)
Expand Down Expand Up @@ -194,5 +195,7 @@ def loop():
"""
stream.read_until("\n", handle)

stream.write(response(220))
hm = "220 %s [%s]\r\n" % (get_mailname(),
__fullname__)
stream.write(hm)
loop()
9 changes: 4 additions & 5 deletions blackhole/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@


RESPONSES = {
'220': "OK, ready",
'220': "OK",
'221': "Thank you for speaking to me",
'250': "OK, done",
'250': "OK",
'251': "OK, user not local, will forward",
'252': "OK, cannot VRFY user but will attempt delivery",
'253': "OK, messages pending",
Expand Down Expand Up @@ -63,7 +63,7 @@
# Random responses
RANDOM_RESPONSES = ACCEPT_RESPONSES + BOUNCE_RESPONSES

EHLO_RESPONSES = ("250-2.5.0 OK, done", "250-SIZE 512000",
EHLO_RESPONSES = ("250-OK", "250-SIZE 512000",
"250-VRFY", "250-STARTTLS",
"250-ENHANCEDSTATUSCODES", "250-8BITMIME",
"250 DSN")
Expand Down Expand Up @@ -123,5 +123,4 @@ def response_message(response):
response = str(response)
message = RESPONSES[response]
smtp_code = response
esmtp_code = ".".join(list(response))
return "%s %s %s\n" % (smtp_code, esmtp_code, message)
return "%s %s\r\n" % (smtp_code, message)
16 changes: 8 additions & 8 deletions blackhole/tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@


class TestResponses(unittest.TestCase):
ok_done = ('250 2.5.0 OK, done\n', False)
quit = ('221 2.2.1 Thank you for speaking to me\n', True)
data = ('354 3.5.4 Start mail input; end with <CRLF>.<CRLF>\n', False)
unknown = ('500 5.0.0 Command not recognized\n', False)
ok = ('220 2.2.0 OK, ready\n', False)
vrfy = ('252 2.5.2 OK, cannot VRFY user but will attempt delivery\n', False)
ehlo = (['250-2.5.0 OK, done\n', '250-SIZE 512000\n', '250-VRFY\n', '250-STARTTLS\n',
'250-ENHANCEDSTATUSCODES\n', '250-8BITMIME\n', '250 DSN\n'], False)
ok_done = ('250 OK\r\n', False)
quit = ('221 Thank you for speaking to me\r\n', True)
data = ('354 Start mail input; end with <CRLF>.<CRLF>\r\n', False)
unknown = ('500 Command not recognized\r\n', False)
ok = ('220 OK\r\n', False)
vrfy = ('252 OK, cannot VRFY user but will attempt delivery\r\n', False)
ehlo = (['250-OK\r\n', '250-SIZE 512000\r\n', '250-VRFY\r\n', '250-STARTTLS\r\n',
'250-ENHANCEDSTATUSCODES\r\n', '250-8BITMIME\r\n', '250 DSN\r\n'], False)

def test_handle_command_helo_response(self):
m = MailState()
Expand Down
15 changes: 15 additions & 0 deletions blackhole/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,18 @@ def email_id():
"""
alpha = list("1234567890ABCDEF")
return ''.join(random.choice(alpha) for x in range(10))


def get_mailname():
"""
Return a mailname for HELO.
Reads /etc/mailname if present
and falls back to socket.getfqdn
"""
mailname_file = "/etc/mailname"
if os.path.exists(mailname_file):
mnc = file(mailname_file, 'r').read().strip()
if mnc != "":
return mnc
return socket.getfqdn()
7 changes: 7 additions & 0 deletions docs/source/api-utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,10 @@ blackhole.utils

Will be reused later down the line
due to the limited number of characters.

.. function:: get_mailename()

Return a mailname for HELO.

Reads /etc/mailname if present
and falls back to socket.getfqdn
8 changes: 8 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ Getting started
modes
response-codes

FQDN
----

The FQDN that Blackhole will print on a new connection is automatically generated.

It will use the contents of `/etc/mailname`, if that file does not exist it will
use a name returned by `socket.getfqdn()`.

Tests & Coverage
================

Expand Down
4 changes: 2 additions & 2 deletions docs/source/response-codes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ All
+------+------------------------------------------------------------+
| Code | message |
+======+============================================================+
| 220 | OK, ready |
| 220 | OK |
+------+------------------------------------------------------------+
| 221 | Thank you for speaking to me |
+------+------------------------------------------------------------+
| 250 | OK, done |
| 250 | OK |
+------+------------------------------------------------------------+
| 251 | OK, user not local, will forward |
+------+------------------------------------------------------------+
Expand Down

0 comments on commit dbf885f

Please sign in to comment.