1 ! Copyright (C) 2007 Elie CHAFTARI
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: combinators kernel prettyprint io io.timeouts io.server
4 sequences namespaces io.sockets continuations calendar io.encodings.ascii ;
7 ! Mock SMTP server for testing purposes.
9 ! Usage: 4321 mock-smtp-server
10 ! $ telnet 127.0.0.1 4321
12 ! Connected to localhost.
13 ! Escape character is '^]'.
17 ! MAIL FROM: <here@mail.com>
19 ! RCPT TO: <there@mail.com>
24 ! 354 Enter message, ending with "." on a line by itself
25 ! Hello I am still waiting for your call
31 ! Connection closed by foreign host.
37 { [ [ dup "HELO" head? ] keep "EHLO" head? or ] [
38 "220 and..?\r\n" write flush t
41 "bye\r\n" write flush f
43 { [ dup "MAIL FROM:" head? ] [
44 "220 OK\r\n" write flush t
46 { [ dup "RCPT TO:" head? ] [
47 "220 OK\r\n" write flush t
51 "354 Enter message, ending with \".\" on a line by itself\r\n"
54 { [ dup "." = data-mode get and ] [
56 "220 OK\r\n" write flush t
58 { [ data-mode get ] [ dup global [ print ] bind t ] }
60 "500 ERROR\r\n" write flush t
62 } cond nip [ process ] when ;
64 : mock-smtp-server ( port -- )
65 "Starting SMTP server on port " write dup . flush
66 "127.0.0.1" swap <inet4> ascii <server> [
68 1 minutes stdio get set-timeout
69 "220 hello\r\n" write flush