1 ;; Super-basic bell-and-whistle-free SMTP server.
3 ;; Intended for a single-user system
13 (chicken process-context)
15 srfi-1 srfi-13 matchable)
17 (define lambdamail-version "0.0.1")
22 (define-record message to from text helo)
23 (define (make-empty-message) (make-message "" "" "" ""))
29 (define ((make-smtp in-port out-port config) type)
30 (if (eq? type 'get-line)
34 ((greeting) (conc "220 " (config-host config)
35 " LambdaMail v" lambdamail-version))
37 ((intermediate) "354 intermediate")
38 ((close) "221 closing transmission channel")
39 ((not-implemented) "502 command not implemented"))
43 ;;; Server initialization
46 (define (run-server config)
47 (set-buffering-mode! (current-output-port) #:line)
48 (let ((listener (tcp-listen (config-port config) 10 "::")))
49 (print "LambdaMail v" lambdamail-version
50 " listening on port " (config-port config) " ...")
51 (print "(Host name: " (config-host config)
52 ", Spool dir: " (config-spool-dir config) ")")
53 (server-loop listener config)))
59 (define (server-loop listener config)
60 (let-values (((in-port out-port) (tcp-accept listener)))
61 (let-values (((local-ip remote-ip) (tcp-addresses in-port)))
62 (print "Accepted connection from " remote-ip " on " (seconds->string)))
64 (let ((smtp (make-smtp in-port out-port config)))
66 (process-smtp smtp config))
68 (print-error-message o)))
69 (print "Terminating connection.")
70 (close-input-port in-port)
71 (close-output-port out-port)
72 (server-loop listener config)))
74 ;;; SMTP processing loop
77 (define (process-smtp smtp config)
78 (let loop ((msg (make-empty-message))
79 (line-orig (smtp 'get-line)))
80 (if (string? line-orig)
81 (let ((line (string-downcase line-orig)))
84 ((string-prefix? "helo" line)
85 (message-helo-set! msg (string-drop line (string-length "helo")))
87 (loop msg (smtp 'get-line)))
88 ((string-prefix? "mail from:" line)
89 (message-from-set! msg (string-drop line (string-length "mail from:")))
91 (loop msg (smtp 'get-line)))
92 ((string-prefix? "rcpt to:" line)
93 (message-to-set! msg (string-drop line (string-length "rcpt to:")))
95 (loop msg (smtp 'get-line)))
96 ((string-prefix? "data" line)
98 (let text-loop ((text-line (smtp 'get-line))
100 (if (string=? "." text-line)
101 (message-text-set! msg text)
102 (text-loop (smtp 'get-line)
103 (conc text text-line "\n"))))
104 (process-message msg config)
106 (loop (make-empty-message)
108 ((string-prefix? "quit" line)
112 (loop msg (smtp 'get-line)))
114 (smtp 'not-implemented)
115 (loop msg (smtp 'get-line)))))
122 (define (get-to-addresses config)
123 (map (lambda (p) (cons
124 (conc "<" (car p) "@" (config-host config) ">")
126 (map (lambda (file) (cons (pathname-file file) file))
127 (glob (conc (config-spool-dir config) "/*")))))
129 (define (remove-angle-brackets addr)
130 (let ((left-idx (substring-index "<" addr))
131 (right-idx (substring-index ">" addr)))
132 (substring addr (+ left-idx 1) right-idx)))
134 (define (deliver-message-mbox msg dest-file)
135 (print "Delivering to mbox " dest-file)
136 (with-output-to-file dest-file
138 (print "\nFrom " (remove-angle-brackets (message-from msg)))
139 (print (message-text msg)))
142 (define (deliver-message-maildir msg dest-dir)
143 (print "Delivering to maildir " dest-dir)
144 (with-output-to-file (conc dest-dir "/" (current-seconds))
146 (print (message-text msg)))))
148 (define (process-message msg config)
149 (let ((dest (assoc (message-to msg) (get-to-addresses config))))
151 (let ((dest-file (cdr dest)))
152 (if (directory-exists? dest-file)
153 (deliver-message-maildir msg dest-file)
154 (deliver-message-mbox msg dest-file))
155 (print "Message DELIVERED:"))
156 (print "Message REJECTED:"))
157 (print " * From: " (message-from msg))
158 (print " * To: " (message-to msg))))
161 ;;; Command line argument parsing
164 (define (print-usage progname)
165 (print "Usage: " progname " hostname [port [spooldir]]"))
168 (let ((progname (pathname-file (car (argv))))
170 (config (make-config "" 25 "/var/spool/mail")))
172 (print-usage progname)
174 (config-host-set! config (car args))
175 (unless (null? (cdr args))
176 (config-port-set! config (string->number (cadr args)))
177 (unless (null? (cddr args))
178 (config-spool-dir-set! (caddr args))))
179 (run-server config)))))
183 ;; (run-server (make-config "thelambdalab.xyz" 2525 "/var/spool/mail"))