Commits

Anonymous committed a5f0a54

basic message 'sending' is possible

Comments (0)

Files changed (6)

pymta/__init__.py

 # -*- coding: UTF-8 -*-
 
 from pymta.default_policy import *
+from pymta.model import *
 from pymta.mta import *
 from pymta.processor import *
 from pymta.smtp_session import *
+# -*- coding: UTF-8 -*-
+
+
+__all__ = ['Message', 'Peer']
+
+
+class Message(object):
+    def __init__(self, peer, smtp_from=None, smtp_to=None, msg_data=None):
+        self.peer = peer
+        self.smtp_from = smtp_from
+        self.smtp_to = smtp_to
+        self.msg_data = msg_data
+
+
+class Peer(object):
+    def __init__(self, remote_ip, remote_port):
+        self.remote_ip = remote_ip
+        self.remote_port = remote_port
+    
+    def __repr__(self):
+        return '%s(%s, %s)' % (self.__class__.__name__, self.remote_ip, 
+                               self.remote_port)
+
+
     primary_hostname = property(primary_hostname)
     
     
+    def new_message_received(self, msg):
+        """Called from the SMTPSession whenever a new message was accepted."""
+        print msg
+        raise NotImplementedError
+    
     # Do something with the gathered message
     # TODO: Rewrite!
     def process_message(self, peer, mailfrom, rcpttos, data):

pymta/processor.py

 
 from repoze.workflow.statemachine import StateMachine, StateMachineError
 
+from pymta.model import Message
+
 __all__ = ['SMTPProcessor']
 
 
         self._policy = policy
         
         self._command_arguments = None
+        self._message = None
+        
         self.remote_ip_string = None
         self.remote_port = None
         self._build_state_machine()
         primary_hostname = self._session.primary_hostname
         reply_text = '%s Hello %s' % (primary_hostname, self.remote_ip_string)
         self.reply(220, reply_text)
+        self._message = Message(None)
     
     def smtp_quit(self):
         primary_hostname = self._session.primary_hostname
         self.reply(250, primary_hostname)
     
     def smtp_mail_from(self):
-        raise NotImplementedError
-        self.reply(250, 'NI')
+        # TODO: Check for good email address!
+        # TODO: Check for single email address!
+        # TODO: Policy
+        self._message.smtp_from = self._command_arguments
+        self.reply(250, 'OK')
     
     def smtp_rcpt_to(self):
-        raise NotImplementedError
-        self.reply(250, 'NI')
+        # TODO: Check for good email address!
+        # TODO: Handle multiple arguments
+        # TODO: Policy
+        self._message.smtp_to = self._command_arguments
+        self.reply(250, 'OK')
     
     def smtp_data(self):
-        raise NotImplementedError
-        self.reply(250, 'NI')
-        
-        
+        msg_data = self._command_arguments
+        # TODO: Policy check
+        self._message.msg_data = msg_data
+        self._session.new_message_received(self._message)
+        self._message = None
+        self.reply(250, 'OK')
+        # Now we must not loose the message anymore!
 
 

pymta/smtp_session.py

     # Communication helper methods
     
     def push(self, code, msg=None):
-        "Send a message to the peer (using the correct SMTP line terminators."
+        """Send a message to the peer (using the correct SMTP line terminators
+        (usually only called from the SMTPProcessor)."""
         if msg == None:
             msg = code
         else:
             msg += self.LINE_TERMINATOR
         asynchat.async_chat.push(self, msg)
     
+    def new_message_received(self, msg):
+        """Called from the SMTPProcessor when a new message was received 
+        successfully."""
+        self._server.new_message_received(msg)
+        
+    
     # Implementation of base class abstract method
     # TODO: Rewrite!
     def collect_incoming_data(self, data):

tests/basic_message_sending_test.py

 class MockSession(object):
     primary_hostname = 'localhost'
     
-    def __init__(self):
+    def __init__(self, server):
+        self._server = server
         self.replies = []
         self.open = True
     
         assert self.open
         self.open = False
     
+    def new_message_received(self, msg):
+        self._server.new_message_received(msg)
+    
+
+
+class MockServer(object):
+    def __init__(self):
+        self.messages = []
+
+    
+    def new_message_received(self, msg):
+        self.messages.append(msg)
 
 
 class BasicMessageSendingTest(TestCase):
 
     def setUp(self):
-        self.session = MockSession()
+        self.server = MockServer()
+        self.session = MockSession(self.server)
         self.processor = SMTPProcessor(session=self.session)
         self.processor.new_connection('127.0.0.1', 4567)
     
 
     def test_send_simple_mail(self):
         self._send('HELO', 'foo.example.com')
-        self._send('MAIL FROM', 'foo.example.com')
+        self._send('MAIL FROM', 'foo@example.com')
         self._send('RCPT TO', 'bar@example.com')
-        msg = 'Subject: Test\r\n\r\nJust testing...\r\n'
-        self._send('DATA', msg)
+        rfc822_msg = 'Subject: Test\r\n\r\nJust testing...\r\n'
+        self._send('DATA', rfc822_msg)
         self._close_connection()
-        raise NotImplementedError('We have to check the email')
+        
+        self.assertEqual(1, len(self.server.messages))
+        msg = self.server.messages[0]
+        self.assertEqual('foo@example.com', msg.smtp_from)
+        self.assertEqual('bar@example.com', msg.smtp_to)
+        self.assertEqual(rfc822_msg, msg.msg_data)
 
+        
+        
+        
+        
 
 
+