X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=lambdamail.scm;h=f1d8c461614fe9ffebe13efff0d5d7c7610fda52;hb=c834cd9a6b7e6c0e8737fecd1f5ccac8b3cc401d;hp=c47c14506509692b8a6e86242a5c677a2491f065;hpb=d95c6338e3c2bc2d3916022834e1e320fa8e7e93;p=lambdamail.git diff --git a/lambdamail.scm b/lambdamail.scm index c47c145..f1d8c46 100644 --- a/lambdamail.scm +++ b/lambdamail.scm @@ -8,6 +8,7 @@ (chicken string) (chicken pathname) (chicken file) + (chicken time) (chicken time posix) (chicken process-context) (chicken condition) @@ -78,33 +79,29 @@ (line-orig (smtp 'get-line))) (if (string? line-orig) (let ((line (string-downcase line-orig))) + (print "got " line) (cond ((string-prefix? "helo" line) (message-helo-set! msg (string-drop line (string-length "helo"))) - (print "got " line) (smtp 'ok) (loop msg (smtp 'get-line))) ((string-prefix? "mail from:" line) - (print "got " line) (message-from-set! msg (string-drop line (string-length "mail from:"))) (smtp 'ok) (loop msg (smtp 'get-line))) ((string-prefix? "rcpt to:" line) - (print "got " line) (message-to-set! msg (string-drop line (string-length "rcpt to:"))) (smtp 'ok) (loop msg (smtp 'get-line))) ((string-prefix? "data" line) - (print "got " line) (smtp 'intermediate) (let text-loop ((text-line (smtp 'get-line)) (text "")) - (print "Received '" text-line "'") (if (string=? "." text-line) (message-text-set! msg text) (text-loop (smtp 'get-line) - (conc text "\n" text-line)))) - (deliver-message msg config) + (conc text text-line "\n")))) + (process-message msg config) (smtp 'ok) (loop (make-empty-message) (smtp 'get-line))) @@ -115,7 +112,6 @@ (loop msg (smtp 'get-line))) (else (smtp 'not-implemented) - (print "got " line) (loop msg (smtp 'get-line))))) 'done))) @@ -123,11 +119,43 @@ ;;; Message delivery ;; -(define (deliver-message msg config) - (print "Message delivered:") - (print " * From: " (message-from msg)) - (print " * To: " (message-to msg)) - (print " * Text: " (message-text msg))) +(define (get-to-addresses config) + (map (lambda (p) (cons + (conc "<" (car p) "@" (config-host config) ">") + (cdr p))) + (map (lambda (file) (cons (pathname-file file) file)) + (glob (conc (config-spool-dir config) "/*"))))) + +(define (remove-angle-brackets addr) + (let ((left-idx (substring-index "<" addr)) + (right-idx (substring-index ">" addr))) + (substring addr (+ left-idx 1) right-idx))) + +(define (deliver-message-mbox msg dest-file) + (print "Delivering to mbox " dest-file) + (with-output-to-file dest-file + (lambda () + (print "\nFrom " (remove-angle-brackets (message-from msg))) + (print (message-text msg))) + #:append)) + +(define (deliver-message-maildir msg dest-dir) + (print "Delivering to maildir " dest-dir) + (with-output-to-file (conc dest-dir "/" (current-seconds)) + (lambda () + (print (message-text msg))))) + +(define (process-message msg config) + (let ((dest (assoc (message-to msg) (get-to-addresses config)))) + (if dest + (let ((dest-file (cdr dest))) + (if (directory-exists? dest-file) + (deliver-message-maildir msg dest-file) + (deliver-message-mbox msg dest-file)) + (print "Message DELIVERED:")) + (print "Message REJECTED:")) + (print " * From: " (message-from msg)) + (print " * To: " (message-to msg)))) ;;; Command line argument parsing