2 * Copyright (c) 1989 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
8 RCSID("$Id: popper.c,v 1.16 2002/07/04 14:09:25 joda Exp $");
17 /* This should not be a problem on BSD systems */
18 signal(SIGHUP, catchSIGHUP);
19 signal(SIGPIPE, catchSIGHUP);
23 int pop_timeout = POP_TIMEOUT;
34 * fgets, but with a timeout
37 tgets(char *str, int size, FILE *fp, int timeout)
39 signal(SIGALRM, ring);
44 str = fgets(str,size,fp);
46 signal(SIGALRM,SIG_DFL);
51 * popper: Handle a Post Office Protocol version 3 session
54 main (int argc, char **argv)
58 char message[MAXLINELEN];
60 signal(SIGHUP, catchSIGHUP);
61 signal(SIGPIPE, catchSIGHUP);
63 /* Start things rolling */
64 pop_init(&p,argc,argv);
66 /* Tell the user that we are listenting */
67 pop_msg(&p,POP_SUCCESS, "POP3 server ready");
69 /* State loop. The POP server is always in a particular state in
70 which a specific suite of commands can be executed. The following
71 loop reads a line from the client, gets the command, and processes
72 it in the current context (if allowed) or rejects it. This continues
73 until the client quits or an error occurs. */
75 for (p.CurrentState=auth1;p.CurrentState!=halt&&p.CurrentState!=error;) {
77 pop_msg(&p, POP_FAILURE, "POP hangup: %s", p.myhost);
78 if (p.CurrentState > auth2 && !pop_updt(&p))
79 pop_msg(&p, POP_FAILURE,
80 "POP mailbox update failed: %s", p.myhost);
81 p.CurrentState = error;
82 } else if (tgets(message, MAXLINELEN, p.input, pop_timeout) == NULL) {
83 pop_msg(&p, POP_FAILURE, "POP timeout: %s", p.myhost);
84 if (p.CurrentState > auth2 && !pop_updt(&p))
85 pop_msg(&p,POP_FAILURE,
86 "POP mailbox update failed: %s", p.myhost);
87 p.CurrentState = error;
90 /* Search for the command in the command/state table */
91 if ((s = pop_get_command(&p,message)) == NULL) continue;
93 /* Call the function associated with this command in
95 if (s->function) p.CurrentState = s->result[(*s->function)(&p)];
97 /* Otherwise assume NOOP and send an OK message to the client */
99 p.CurrentState = s->success_state;
100 pop_msg(&p,POP_SUCCESS,NULL);
105 /* Say goodbye to the client */
106 pop_msg(&p,POP_SUCCESS,"Pop server at %s signing off.",p.myhost);
108 /* Log the end of activity */
109 pop_log(&p,POP_PRIORITY,
110 "(v%s) Ending request from \"%s\" at %s\n",VERSION,p.client,p.ipaddr);