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.
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
)
41 signal(SIGALRM
, ring
);
45 signal(SIGALRM
, SIG_DFL
);
48 ret
= fgets(str
, size
, fp
);
50 signal(SIGALRM
, SIG_DFL
);
55 * popper: Handle a Post Office Protocol version 3 session
58 main (int argc
, char **argv
)
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
;) {
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
;
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
99 if (s
->function
) p
.CurrentState
= s
->result
[(*s
->function
)(&p
)];
101 /* Otherwise assume NOOP and send an OK message to the client */
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
);