1 ! Copyright (C) 2007 Elie CHAFTARI
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: combinators kernel prettyprint io io.timeouts
4 sequences namespaces io.sockets continuations calendar
5 io.encodings.ascii io.streams.duplex destructors ;
8 ! Mock SMTP server for testing purposes.
10 ! Usage: 4321 mock-smtp-server
11 ! $ telnet 127.0.0.1 4321
13 ! Connected to localhost.
14 ! Escape character is '^]'.
18 ! MAIL FROM: <here@mail.com>
20 ! RCPT TO: <there@mail.com>
25 ! 354 Enter message, ending with "." on a line by itself
26 ! Hello I am still waiting for your call
32 ! Connection closed by foreign host.
38 { [ [ dup "HELO" head? ] keep "EHLO" head? or ] [
39 "220 and..?\r\n" write flush t
42 "bye\r\n" write flush f
44 { [ dup "MAIL FROM:" head? ] [
45 "220 OK\r\n" write flush t
47 { [ dup "RCPT TO:" head? ] [
48 "220 OK\r\n" write flush t
52 "354 Enter message, ending with \".\" on a line by itself\r\n"
55 { [ dup "." = data-mode get and ] [
57 "220 OK\r\n" write flush t
59 { [ data-mode get ] [ dup global [ print ] bind t ] }
61 "500 ERROR\r\n" write flush t
63 } cond nip [ process ] when ;
65 : mock-smtp-server ( port -- )
66 "Starting SMTP server on port " write dup . flush
67 "127.0.0.1" swap <inet4> ascii <server> [
70 "220 hello\r\n" write flush