#include <popper.h>
RCSID("$Id$");
int hangup = FALSE ;
static RETSIGTYPE
catchSIGHUP(int sig)
{
hangup = TRUE ;
signal(SIGHUP, catchSIGHUP);
signal(SIGPIPE, catchSIGHUP);
SIGRETURN(0);
}
int pop_timeout = POP_TIMEOUT;
jmp_buf env;
static RETSIGTYPE
ring(int sig)
{
longjmp(env,1);
}
static char *
tgets(char *str, int size, FILE *fp, int timeout)
{
char *ret;
signal(SIGALRM, ring);
alarm(timeout);
if (setjmp(env)) {
alarm(0);
signal(SIGALRM, SIG_DFL);
return NULL;
}
ret = fgets(str, size, fp);
alarm(0);
signal(SIGALRM, SIG_DFL);
return ret;
}
int
main (int argc, char **argv)
{
POP p;
state_table * s;
char message[MAXLINELEN];
signal(SIGHUP, catchSIGHUP);
signal(SIGPIPE, catchSIGHUP);
pop_init(&p,argc,argv);
pop_msg(&p,POP_SUCCESS, "POP3 server ready");
for (p.CurrentState=auth1;p.CurrentState!=halt&&p.CurrentState!=error;) {
if (hangup) {
pop_msg(&p, POP_FAILURE, "POP hangup: %s", p.myhost);
if (p.CurrentState > auth2 && !pop_updt(&p))
pop_msg(&p, POP_FAILURE,
"POP mailbox update failed: %s", p.myhost);
p.CurrentState = error;
} else if (tgets(message, MAXLINELEN, p.input, pop_timeout) == NULL) {
pop_msg(&p, POP_FAILURE, "POP timeout: %s", p.myhost);
if (p.CurrentState > auth2 && !pop_updt(&p))
pop_msg(&p,POP_FAILURE,
"POP mailbox update failed: %s", p.myhost);
p.CurrentState = error;
}
else {
if ((s = pop_get_command(&p,message)) == NULL) continue;
if (s->function) p.CurrentState = s->result[(*s->function)(&p)];
else {
p.CurrentState = s->success_state;
pop_msg(&p,POP_SUCCESS,"time passes");
}
}
}
pop_msg(&p,POP_SUCCESS,"Pop server at %s signing off.",p.myhost);
pop_log(&p,POP_PRIORITY,
"(v%s) Ending request from \"%s\" at %s\n",VERSION,p.client,p.ipaddr);
closelog();
return(0);
}