def handle_command(line, mail_state): """Handle each SMTP command as it's sent to the server The paramater 'line' is the currently stream of data ending in '\\n'. 'mail_state' is an instance of 'blackhole.state.MailState'. """ close = False if mail_state.reading: resp = None # Not exactly nice but it's only way I could safely figure # out if it was the \n.\n if line[0] == "." and len(line) == 3 and ord(line[0]) == 46: mail_state.reading = False resp = response() elif line.lower().startswith("ehlo"): resp = [] for k, r in enumerate(EHLO_RESPONSES): r = r.format(options.message_size_limit) if k == 1 else r resp.append("%s\r\n" % r) elif any(line.lower().startswith(e) for e in ['helo', 'mail from', 'rcpt to', 'noop']): resp = response(250) elif line.lower().startswith("rset"): new_id = email_id() log.debug("[%s] RSET received, changing ID to [%s]" % (mail_state.email_id, new_id)) # Reset mail state mail_state.reading = False mail_state.email_id = new_id resp = response(250) elif line.lower().startswith("starttls"): if not ssl or not options.ssl: resp = response(500) else: resp = response(220) elif line.lower().startswith("vrfy"): resp = response(252) elif line.lower().startswith("quit"): resp = response(221) close = True elif line.lower().startswith("data"): resp = response(354) mail_state.reading = True else: resp = response(500) # this is a blocking action, sadly # async non blocking methods did not # work. =( if options.delay > 0: # import has to be called here for # some reason... import time time.sleep(options.delay) return resp, close
def connection_ready(sock, fd, events): """ Accepts the socket connections and passes them off to be handled. 'sock' is an instance of 'socket'. 'fd' is an open file descriptor for the current connection. 'events' is an integer of the number of events on the socket. """ while True: try: connection, address = sock.accept() except socket.error as e: if e.errno not in (errno.EWOULDBLOCK, errno.EAGAIN): raise return log.debug("Connection from '%s'" % address[0]) connection.setblocking(0) stream = connection_stream(connection) # No stream, bail out if not stream: return mail_state = MailState() mail_state.email_id = email_id() mail_state.stream = stream # Sadly there is nothing I can do about the handle and loop # fuctions. They have to exist within connection_ready def handle(line): """ Handle a line of socket data, figure out if it's a valid SMTP keyword and handle it accordingly. """ log.debug("[%s] RECV: %s" % (mail_state.email_id, line.rstrip())) resp, close = handle_command(line, mail_state) if resp: # Multiple responses, i.e. EHLO if isinstance(resp, list): for r in resp: write_response(mail_state, r) else: # Otherwise it's a single response write_response(mail_state, resp) # Switch to SSL connection if starttls is called # and we have an SSL library if line.lower().startswith("starttls") and ssl and options.ssl: fileno = mail_state.stream.socket.fileno() IOLoop.current().remove_handler(fileno) mail_state.stream = ssl_connection(connection) # Close connection if close is True: log.debug("Closing") mail_state.stream.close() del mail_state.stream return else: loop() def loop(): """ Loop over the socket data until we receive a newline character (\n) """ # Protection against stream already reading exceptions if not mail_state.stream.reading(): mail_state.stream.read_until("\n", handle) hm = "220 %s [%s]\r\n" % (get_mailname(), __fullname__) mail_state.stream.write(hm) loop()
def test_email_id_generator(self): self.assertTrue(re.match(r"^[A-F0-9]{10}$", email_id()))