You are looking at historical revision 15903 of this page. It may differ significantly from its current revision.

smtp

Description

smtp is a collection of parser combinators and state machine primitives for the grammar defined in RFC 5321 (Simple Mail Transfer Protocol).

Data Types for SMTP Commands

Represenation of SMTP commands:

 
(define-datatype cmd cmd?
 (Helo (s string?))
 (Ehlo (s string?))
 (MailFrom (m mailbox?) (parameters list?))
 (RcptTo   (m mailbox?) (parameters list?))
 (Data)
 (Rset)
 (Send  (m mailbox?))
 (Soml  (m mailbox?))
 (Saml  (m mailbox?))
 (Vrfy  (s string?))
 (Expn  (s string?))
 (Help  (s string?))
 (Noop)
 (Quit)
 (Turn)
 (WrongArg (cmd string?)  (message string?)))

The record printer defined for this datatype prints values of this type in a format conforming to the RFC. For example:

 csi> (print (Helo "myhost.org"))
 HELO myhost.org

The mailbox datatype has the following definition:

(define-datatype mailbox mailbox?
  (Mailbox (local-part string?) 
           (domain string?)))

Data Types for SMTP Replies

Representation of SMTP replies:

 (define-datatype reply reply?
  (Reply (code code?) (msg list?)))

An SMTP reply is a three-digit return code plus comments. This is what the list of strings is for; one string per line in the reply. the record printer will append an CRLF end-of-line marker to each entry in that list, so that the resulting string is ready to be sent back to the peer.

For example:

> (print (Reply (Code (Success) (MailSystem) 0)
                    (list "worked" "like" "a charm")))
250-worked
250-like
250 a charm

The code datatype consists of success code, category and supplemental code:

(define-datatype code code?
  (Code (suc success-code?) (cat category?) (num integer?)))

In addition, the success-code and category datatypes can be used to map symbolic identifiers to integers and vice versa:

(define-enumerated-type 
  success-code success-code? success-vector 
  success-code-inject success-code-project 
  (Unused)
  (PreliminarySuccess)
  (Success)
  (IntermediateSuccess)
  (TransientFailure)
  (PermanentFailure))
(define-enumerated-type 
  category category? category-vector
  category-inject category-project
  (Syntax)
  (Information)
  (Connection)
  (Unspecified3)
  (Unspecified4)
  (MailSystem))

define-enumerated-type defines a new record type, with as many instances as there are instance names. name-vector is bound to a vector containing the instances of the type in the same order as the instance-name list. name-inject and name-project are procedures that map integers to an instance and vice versa.

ESMTP State Machine

[procedure] (start-session)

Procedure start-session returns an ESMTP state machine object (a procedure), which takes in a stream containing an SMTP command and returns an appropriate session-fsm value:

 (define-datatype session-fsm session-fsm?
   (Event (ev event?))
   (Trans (ev event?) (fsm procedure?)))

A stream in this case is defined as the representation used by the abnf library.

The Event variant signals an event that must be processed by the calling library, while Trans signals an event and a state machine transition. The following events can be returned by this state machine:

(define-datatype event event?
  (SayHelo       (s string?))
  (SayHeloAgain  (s string?))
  (SayEhlo       (s string?))
  (SayEhloAgain  (s string?))
  (SetMailFrom   (m mailbox?) (parameters? list))
  (AddRcptTo     (m mailbox?) (parameters? list))
  (StartData)
  (NeedHeloFirst)
  (NeedMailFromFirst)
  (NeedRcptToFirst)
  (NotImplemented) ;; Turn, Send, Soml, Saml, Vrfy, Expn.
  (ResetState)
  (SayOK)
  (SeeksHelp    (s string?))
  (Shutdown)
  (SyntaxErrorIn (s string?))
  (Unrecognized  (s string?)))

Requires

Examples


;; An example MTA implementation

(use datatype smtp)

(define domain    "example.net")
(define host      "chicken-mta")
(define mailfrom  (make-parameter #f))
(define rcpto     (make-parameter '()))
(define data      (make-parameter #f))

(define (handle-event ev)
  (cases event ev
	 (SayHelo (s)
	  (Reply (Code (Success) (MailSystem) 0)
		 (list host " " "Hello " s)))
	 
	 (SayHeloAgain (s)
	  (Reply (Code (Success) (MailSystem) 0)
		 (list host " " "Hello " s)))

	 (SayEhlo (s)
	  (Reply (Code (Success) (MailSystem) 0)
		 (list host " " "Hello " s)))
	 
	 (SayEhloAgain (s)
	  (Reply (Code (Success) (MailSystem) 0)
		 (list host " " "Hello " s)))
	 
	 (SetMailFrom (m)
	   (mailfrom m)
	   (Reply (Code (Success) (MailSystem) 0) 
		  (list "OK")))

	 (AddRcptTo (m)
	    (if (not (mailfrom))
	       (Reply (Code (PermanentFailure) (Syntax) 3)
	   	      (list "command out of sequence"))
	       (begin
		 (rcpto (cons m (rcpto)))
		 (Reply (Code (Success) (MailSystem) 0) 
			(list "Accepted")))))

	 (StartData ()
	    (if (not (rcpto))
	       (Reply (Code (PermanentFailure) (MailSystem) 4)
	   	      (list "no valid recipients"))
	       (begin
		 (data (list))
		 (Reply (Code (IntermediateSuccess) (MailSystem) 4)
			(list "Ready")))))

	 (NeedHeloFirst ()
	   (Reply (Code (PermanentFailure) (Syntax) 3)
	   	      (list "command out of sequence: "
			    "need HELO first")
		      ))

	 (NeedMailFromFirst ()
	   (Reply (Code (PermanentFailure) (Syntax) 3)
	   	      (list "command out of sequence: "
			    "need MAIL first")
		      ))

	 (NeedMailRcptToFirst ()
	   (Reply (Code (PermanentFailure) (Syntax) 3)
	   	      (list "command out of sequence: "
			    "need RCPT first")
		      ))

	 (NotImplemented ()
	   (Reply (Code (PermanentFailure) (Syntax) 2)
		  (list "command not implemented")))


	 (ResetState ()
	     (mailfrom #f)
	     (rcpto    #f)
	     (data     #f)
	     (Reply (Code (Success) (MailSystem) 0) 
		    (list "Reset OK")))

	 (SayOK ()
	     (Reply (Code (Success) (MailSystem) 0) 
		    (list "OK")))

	 (SeeksHelp (s)
	     (Reply (Code (Success) (Information) 4) 
		    (list "Commands supported:"
			  "HELO EHLO MAIL RCPT DATA QUIT RSET NOOP HELP")))

	 (Shutdown ()
	    (Reply (Code (Success) (MailSystem) 1)
		   (list host " closing connection")))

	 (SyntaxErrorIn (s)
	    (Reply (Code (PermanentFailure) (Syntax) 1)
		   (list "syntax error in " s)))

	 (Unrecognized (s)
	    (Reply (Code (PermanentFailure) (Syntax) 0)
		   (list "Unrecognized " s)))
	 ))

;; from SSAX lib
(define (peek-next-char port)
  (read-char port) 
  (peek-char port))

(define (read-smtp-line port)
  (let loop ((cs (list)))
    (let ((c (peek-char port)))
    (if (eof-object? c) (reverse cs)
	(let ((n (peek-next-char port)))
	  (cond ((and (eq? n #\newline) (eq? c #\return))
		 (begin
		   (read-char port)
		   (reverse (cons* n c cs)))
		 )
		(else (loop (cons c cs)))))))))

(define data-end (list #\. #\return #\newline))
      
(define (handle-data in out cont)
  (let loop ((tempdata (list)))
    (let ((line (read-smtp-line in)))
      (if (equal? line data-end)
	  (begin (data (reverse tempdata))
		 (fprintf out "~A" 
			  (Reply (Code (Success) (MailSystem) 0) (list "OK")))
		 (cont)) 
	  (loop (cons (list->string line) tempdata))))))

(define (main in out)  
  (let loop ((fsm (start-session)))
    (let ((line     (read-smtp-line in)))
      (if (null? line) (loop fsm)
	  (let ((instream (list `(() ,line))))
	    (let-values
	     (((reply ev fsm)
	       (cases session-fsm (fsm instream)
		      (Event (ev)
			     (let ((reply (handle-event ev)))
			       (values reply ev fsm)))
		      (Trans (ev fsm)
			     (let ((reply (handle-event ev)))
			       (values reply ev fsm))))))
	     (fprintf out "~A" reply)
	     (cases event ev
		    (StartData ()
			       (handle-data in out (lambda () (loop fsm))))
		    (Shutdown ()
			      (begin))
		    (else (loop fsm)))))))))
		     

Version History

License

Based on the Haskell Rfc2821 module by Peter Simons.

 Copyright 2009 Ivan Raikov.
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are
 met:
 
 Redistributions of source code must retain the above copyright
 notice, this list of conditions and the following disclaimer.
 
 Redistributions in binary form must reproduce the above copyright
 notice, this list of conditions and the following disclaimer in the
 documentation and/or other materials provided with the distribution.
 
 Neither the name of the author nor the names of its contributors may
 be used to endorse or promote products derived from this software
 without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.