22575: Remove extra ;, From Dennis Davis.
[heimdal.git] / appl / popper / popper.c
blob2cbf16ba3d1f175546ed16736f262911dc1591cf
1 /*
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.
5 */
7 #include <popper.h>
8 RCSID("$Id$");
10 int hangup = FALSE ;
12 static RETSIGTYPE
13 catchSIGHUP(int sig)
15 hangup = TRUE ;
17 /* This should not be a problem on BSD systems */
18 signal(SIGHUP, catchSIGHUP);
19 signal(SIGPIPE, catchSIGHUP);
20 SIGRETURN(0);
23 int pop_timeout = POP_TIMEOUT;
25 jmp_buf env;
27 static RETSIGTYPE
28 ring(int sig)
30 longjmp(env,1);
34 * fgets, but with a timeout
36 static char *
37 tgets(char *str, int size, FILE *fp, int timeout)
39 char *ret;
41 signal(SIGALRM, ring);
42 alarm(timeout);
43 if (setjmp(env)) {
44 alarm(0);
45 signal(SIGALRM, SIG_DFL);
46 return NULL;
48 ret = fgets(str, size, fp);
49 alarm(0);
50 signal(SIGALRM, SIG_DFL);
51 return ret;
54 /*
55 * popper: Handle a Post Office Protocol version 3 session
57 int
58 main (int argc, char **argv)
60 POP p;
61 state_table * s;
62 char message[MAXLINELEN];
64 signal(SIGHUP, catchSIGHUP);
65 signal(SIGPIPE, catchSIGHUP);
67 /* Start things rolling */
68 pop_init(&p,argc,argv);
70 /* Tell the user that we are listenting */
71 pop_msg(&p,POP_SUCCESS, "POP3 server ready");
73 /* State loop. The POP server is always in a particular state in
74 which a specific suite of commands can be executed. The following
75 loop reads a line from the client, gets the command, and processes
76 it in the current context (if allowed) or rejects it. This continues
77 until the client quits or an error occurs. */
79 for (p.CurrentState=auth1;p.CurrentState!=halt&&p.CurrentState!=error;) {
80 if (hangup) {
81 pop_msg(&p, POP_FAILURE, "POP hangup: %s", p.myhost);
82 if (p.CurrentState > auth2 && !pop_updt(&p))
83 pop_msg(&p, POP_FAILURE,
84 "POP mailbox update failed: %s", p.myhost);
85 p.CurrentState = error;
86 } else if (tgets(message, MAXLINELEN, p.input, pop_timeout) == NULL) {
87 pop_msg(&p, POP_FAILURE, "POP timeout: %s", p.myhost);
88 if (p.CurrentState > auth2 && !pop_updt(&p))
89 pop_msg(&p,POP_FAILURE,
90 "POP mailbox update failed: %s", p.myhost);
91 p.CurrentState = error;
93 else {
94 /* Search for the command in the command/state table */
95 if ((s = pop_get_command(&p,message)) == NULL) continue;
97 /* Call the function associated with this command in
98 the current state */
99 if (s->function) p.CurrentState = s->result[(*s->function)(&p)];
101 /* Otherwise assume NOOP and send an OK message to the client */
102 else {
103 p.CurrentState = s->success_state;
104 pop_msg(&p,POP_SUCCESS,"time passes");
109 /* Say goodbye to the client */
110 pop_msg(&p,POP_SUCCESS,"Pop server at %s signing off.",p.myhost);
112 /* Log the end of activity */
113 pop_log(&p,POP_PRIORITY,
114 "(v%s) Ending request from \"%s\" at %s\n",VERSION,p.client,p.ipaddr);
116 /* Stop logging */
117 closelog();
119 return(0);