| 
									
										
										
										
											2008-02-07 02:03:27 -05:00
										 |  |  | ! Copyright (C) 2007 Elie CHAFTARI | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-02-26 02:30:05 -05:00
										 |  |  | USING: combinators kernel prettyprint io io.timeouts io.server | 
					
						
							|  |  |  | sequences namespaces io.sockets continuations calendar io.encodings.ascii ;
 | 
					
						
							|  |  |  | IN: smtp.server | 
					
						
							| 
									
										
										
										
											2008-02-07 02:03:27 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Mock SMTP server for testing purposes. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-11 04:35:43 -04:00
										 |  |  | ! Usage: 4321 mock-smtp-server | 
					
						
							| 
									
										
										
										
											2008-02-07 02:03:27 -05:00
										 |  |  | ! $ telnet 127.0.0.1 4321 | 
					
						
							|  |  |  | ! Trying 127.0.0.1... | 
					
						
							|  |  |  | ! Connected to localhost. | 
					
						
							|  |  |  | ! Escape character is '^]'. | 
					
						
							|  |  |  | ! 220 hello | 
					
						
							|  |  |  | ! EHLO | 
					
						
							|  |  |  | ! 220 and..? | 
					
						
							|  |  |  | ! MAIL FROM: <here@mail.com> | 
					
						
							|  |  |  | ! 220 OK | 
					
						
							|  |  |  | ! RCPT TO: <there@mail.com> | 
					
						
							|  |  |  | ! 220 OK | 
					
						
							|  |  |  | ! Hi | 
					
						
							|  |  |  | ! 500 ERROR | 
					
						
							|  |  |  | ! DATA | 
					
						
							|  |  |  | ! 354 Enter message, ending with "." on a line by itself | 
					
						
							|  |  |  | ! Hello I am still waiting for your call | 
					
						
							|  |  |  | ! Thanks | 
					
						
							|  |  |  | ! . | 
					
						
							|  |  |  | ! 220 OK | 
					
						
							|  |  |  | ! QUIT | 
					
						
							|  |  |  | ! bye | 
					
						
							|  |  |  | ! Connection closed by foreign host. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SYMBOL: data-mode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : process ( -- )
 | 
					
						
							|  |  |  |     readln { | 
					
						
							|  |  |  |         { [ [ dup "HELO" head? ] keep "EHLO" head? or ] [  | 
					
						
							|  |  |  |             "220 and..?\r\n" write flush t
 | 
					
						
							|  |  |  |           ] } | 
					
						
							|  |  |  |         { [ dup "QUIT" = ] [  | 
					
						
							|  |  |  |             "bye\r\n" write flush f
 | 
					
						
							|  |  |  |           ] } | 
					
						
							|  |  |  |         { [ dup "MAIL FROM:" head? ] [  | 
					
						
							|  |  |  |             "220 OK\r\n" write flush t
 | 
					
						
							|  |  |  |           ] } | 
					
						
							|  |  |  |         { [ dup "RCPT TO:" head? ] [  | 
					
						
							|  |  |  |             "220 OK\r\n" write flush t
 | 
					
						
							|  |  |  |           ] } | 
					
						
							|  |  |  |         { [ dup "DATA" = ] [ | 
					
						
							|  |  |  |             data-mode on  | 
					
						
							|  |  |  |             "354 Enter message, ending with \".\" on a line by itself\r\n" | 
					
						
							|  |  |  |             write flush t
 | 
					
						
							|  |  |  |           ] } | 
					
						
							|  |  |  |         { [ dup "." = data-mode get and ] [ | 
					
						
							|  |  |  |             data-mode off
 | 
					
						
							|  |  |  |             "220 OK\r\n" write flush t
 | 
					
						
							|  |  |  |           ] } | 
					
						
							| 
									
										
										
										
											2008-02-18 17:20:18 -05:00
										 |  |  |         { [ data-mode get ] [ dup global [ print ] bind t ] } | 
					
						
							| 
									
										
										
										
											2008-02-07 02:03:27 -05:00
										 |  |  |         { [ t ] [  | 
					
						
							|  |  |  |             "500 ERROR\r\n" write flush t
 | 
					
						
							|  |  |  |           ] } | 
					
						
							|  |  |  |     } cond nip [ process ] when ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-11 04:35:43 -04:00
										 |  |  | : mock-smtp-server ( port -- )
 | 
					
						
							| 
									
										
										
										
											2008-02-07 02:03:27 -05:00
										 |  |  |     "Starting SMTP server on port " write dup . flush
 | 
					
						
							| 
									
										
										
										
											2008-02-26 02:30:05 -05:00
										 |  |  |     "127.0.0.1" swap <inet4> ascii <server> [ | 
					
						
							| 
									
										
										
										
											2008-02-07 02:03:27 -05:00
										 |  |  |         accept [ | 
					
						
							| 
									
										
										
										
											2008-02-21 20:12:55 -05:00
										 |  |  |             1 minutes stdio get set-timeout | 
					
						
							| 
									
										
										
										
											2008-02-07 02:03:27 -05:00
										 |  |  |             "220 hello\r\n" write flush
 | 
					
						
							|  |  |  |             process | 
					
						
							| 
									
										
										
										
											2008-02-14 03:20:32 -05:00
										 |  |  |             global [ flush ] bind | 
					
						
							| 
									
										
										
										
											2008-02-07 02:03:27 -05:00
										 |  |  |         ] with-stream | 
					
						
							|  |  |  |     ] with-disposal ;
 |