1 /* $Cambridge: exim/test/src/mtpscript.c,v 1.1 2006/02/06 16:24:05 ph10 Exp $ */
3 /* A little hacked up program that allows a script to play the part of a remote
4 SMTP/LMTP server on stdin/stdout for testing purposes. Hacked from the more
5 complicated version that does it over a socket. */
8 /* ANSI C standard includes */
28 /*************************************************
29 * SIGALRM handler - crash out *
30 *************************************************/
33 sigalrm_handler(int sig)
35 sig = sig; /* Keep picky compilers happy */
36 fprintf(log, "Server timed out\n");
42 /*************************************************
44 *************************************************/
46 int main(int argc, char **argv)
51 unsigned char sbuffer[1024];
52 unsigned char ibuffer[1024];
56 fprintf(stdout, "500 Script and log file required\n");
60 /* Get the script and log open */
62 script = fopen(argv[1], "r");
65 fprintf(stdout, "500 Failed to open script %s: %s\r\n", argv[1],
71 if (logfile[0] == '+')
77 log = fopen(logfile, logmode);
80 fprintf(stdout, "500 Failed to open log %s: %s\r\n", logfile,
85 /* SIGALRM handler crashes out */
87 signal(SIGALRM, sigalrm_handler);
89 /* Read the script, and do what it says. */
91 while (fgets(sbuffer, sizeof(sbuffer), script) != NULL)
93 int n = (int)strlen(sbuffer);
94 while (n > 0 && isspace(sbuffer[n-1])) n--;
97 /* If the script line starts with a digit, it is a response line which
100 if (isdigit(sbuffer[0]))
102 fprintf(log, "%s\n", sbuffer);
104 fprintf(stdout, "%s\r\n", sbuffer);
108 /* If the script line starts with "*sleep" we just sleep for a while
109 before continuing. Do not write this to the log, as it may not get
110 written at the right place in a log that's being shared. */
112 else if (strncmp(sbuffer, "*sleep ", 7) == 0)
114 sleep(atoi(sbuffer+7));
117 /* Otherwise the script line is the start of an input line we are expecting
118 from the client, or "*eof" indicating we expect the client to close the
119 connection. Read command line or data lines; the latter are indicated
120 by the expected line being just ".". */
124 int data = strcmp(sbuffer, ".") == 0;
126 fprintf(log, "%s\n", sbuffer);
129 /* Loop for multiple data lines */
135 if (fgets(ibuffer, sizeof(ibuffer), stdin) == NULL)
137 fprintf(log, "%sxpected EOF read from client\n",
138 (strncmp(sbuffer, "*eof", 4) == 0)? "E" : "Une");
142 n = (int)strlen(ibuffer);
143 while (n > 0 && isspace(ibuffer[n-1])) n--;
145 fprintf(log, "<<< %s\n", ibuffer);
146 if (!data || strcmp(ibuffer, ".") == 0) break;
149 /* Check received what was expected */
151 if (strncmp(sbuffer, ibuffer, (int)strlen(sbuffer)) != 0)
153 fprintf(log, "Comparison failed - bailing out\n");
159 /* This could appear in the wrong place in a shared log, so forgo it. */
160 /* fprintf(log, "End of script\n"); */
169 /* End of mtpscript.c */