3 * An implementation of Post Office Protocol version 3 (RFC 1939).
5 * Copyright (C) 1998-2000 by Art Cancro and others.
6 * This code is released under the terms of the GNU General Public License.
17 #include <sys/types.h>
25 #include "sysdep_decls.h"
26 #include "citserver.h"
29 #include "dynloader.h"
36 #include "internet_addressing.h"
37 #include "serv_pop3.h"
44 * This cleanup function blows away the temporary memory and files used by
47 void pop3_cleanup_function(void) {
50 /* Don't do this stuff if this is not a POP3 session! */
51 if (CC->h_command_function != pop3_command_loop) return;
53 lprintf(9, "Performing POP3 cleanup hook\n");
55 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
56 fclose(POP3->msgs[i].temp);
58 if (POP3->msgs != NULL) phree(POP3->msgs);
60 lprintf(9, "Finished POP3 cleanup hook\n");
66 * Here's where our POP3 session begins its happy day.
68 void pop3_greeting(void) {
70 strcpy(CC->cs_clientname, "POP3 session");
72 CtdlAllocUserData(SYM_POP3, sizeof(struct citpop3));
76 cprintf("+OK Welcome to the Citadel/UX POP3 server at %s\r\n",
82 * Specify user name (implements POP3 "USER" command)
84 void pop3_user(char *argbuf) {
88 cprintf("-ERR You are already logged in.\r\n");
92 strcpy(username, argbuf);
95 lprintf(9, "Trying <%s>\n", username);
96 if (CtdlLoginExistingUser(username) == login_ok) {
97 cprintf("+OK Password required for %s\r\n", username);
100 cprintf("-ERR No such user.\r\n");
107 * Back end for pop3_grab_mailbox()
109 void pop3_add_message(long msgnum) {
111 lprintf(9, "in pop3_add_message()\n");
114 if (POP3->num_msgs < 2) POP3->msgs = mallok(sizeof(struct pop3msg));
115 else POP3->msgs = reallok(POP3->msgs,
116 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
117 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
118 POP3->msgs[POP3->num_msgs-1].deleted = 0;
120 POP3->msgs[POP3->num_msgs-1].temp = fp;
121 CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, fp, 0, 1);
122 POP3->msgs[POP3->num_msgs-1].rfc822_length = ftell(fp);
128 * Open the inbox and read its contents.
129 * (This should be called only once, by pop3_pass(), and returns the number
130 * of messages in the inbox, or -1 for error)
132 int pop3_grab_mailbox(void) {
133 if (getroom(&CC->quickroom, MAILROOM) != 0) return(-1);
134 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, pop3_add_message);
135 return(POP3->num_msgs);
139 * Authorize with password (implements POP3 "PASS" command)
141 void pop3_pass(char *argbuf) {
145 strcpy(password, argbuf);
148 lprintf(9, "Trying <%s>\n", password);
149 if (CtdlTryPassword(password) == pass_ok) {
150 msgs = pop3_grab_mailbox();
152 cprintf("+OK %s is logged in (%d messages)\r\n",
153 CC->usersupp.fullname, msgs);
154 lprintf(9, "POP3 password login successful\n");
157 cprintf("-ERR Can't open your mailbox\r\n");
161 cprintf("-ERR That is NOT the password! Go away!\r\n");
168 * list available msgs
170 void pop3_list(char *argbuf) {
174 which_one = atoi(argbuf);
176 /* "list one" mode */
178 if (which_one > POP3->num_msgs) {
179 cprintf("-ERR no such message, only %d are here\r\n",
183 else if (POP3->msgs[which_one-1].deleted) {
184 cprintf("-ERR Sorry, you deleted that message.\r\n");
188 cprintf("+OK %d %d\n",
190 POP3->msgs[which_one-1].rfc822_length
196 /* "list all" (scan listing) mode */
198 cprintf("+OK Here's your mail:\r\n");
199 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
200 if (! POP3->msgs[i].deleted) {
203 POP3->msgs[i].rfc822_length);
212 * STAT (tally up the total message count and byte count) command
214 void pop3_stat(char *argbuf) {
216 size_t total_octets = 0;
219 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
220 if (! POP3->msgs[i].deleted) {
222 total_octets += POP3->msgs[i].rfc822_length;
226 cprintf("+OK %d %d\n", total_msgs, total_octets);
232 * RETR command (fetch a message)
234 void pop3_retr(char *argbuf) {
237 size_t bytes_remaining;
239 which_one = atoi(argbuf);
240 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
241 cprintf("-ERR No such message.\r\n");
245 if (POP3->msgs[which_one - 1].deleted) {
246 cprintf("-ERR Sorry, you deleted that message.\r\n");
250 cprintf("+OK Whoop, there it is:\r\n");
251 bytes_remaining = POP3->msgs[which_one -1].rfc822_length;
252 rewind(POP3->msgs[which_one - 1].temp);
253 while (bytes_remaining-- > 0) {
254 ch = getc(POP3->msgs[which_one - 1].temp);
262 * TOP command (dumb way of fetching a partial message or headers-only)
264 void pop3_top(char *argbuf) {
266 int lines_requested = 0;
267 int lines_dumped = 0;
273 sscanf(argbuf, "%d %d", &which_one, &lines_requested);
274 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
275 cprintf("-ERR No such message.\r\n");
279 if (POP3->msgs[which_one - 1].deleted) {
280 cprintf("-ERR Sorry, you deleted that message.\r\n");
284 cprintf("+OK Whoop, there it is:\r\n");
285 rewind(POP3->msgs[which_one - 1].temp);
286 while (ptr = fgets(buf, sizeof buf, POP3->msgs[which_one - 1].temp),
287 ( (ptr!=NULL) && (done == 0))) {
289 if (lines_dumped >= lines_requested) done = 1;
290 if ((in_body == 0) || (done == 0))
291 client_write(buf, strlen(buf));
292 if (in_body) ++lines_dumped;
293 if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
295 if (buf[strlen(buf)-1] != 10) cprintf("\n");
301 * DELE (delete message from mailbox)
303 void pop3_dele(char *argbuf) {
306 which_one = atoi(argbuf);
307 if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
308 cprintf("-ERR No such message.\r\n");
312 if (POP3->msgs[which_one - 1].deleted) {
313 cprintf("-ERR You already deleted that message.\r\n");
317 /* Flag the message as deleted. Will expunge during QUIT command. */
318 POP3->msgs[which_one - 1].deleted = 1;
319 cprintf("+OK Message %d disappears in a cloud of orange smoke.\r\n",
324 /* Perform "UPDATE state" stuff (remove messages marked for deletion)
326 void pop3_update(void) {
329 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
330 if (POP3->msgs[i].deleted) {
331 CtdlDeleteMessages(MAILROOM,
332 POP3->msgs[i].msgnum, NULL);
339 * RSET (reset, i.e. undelete any deleted messages) command
341 void pop3_rset(char *argbuf) {
344 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
345 if (POP3->msgs[i].deleted) {
346 POP3->msgs[i].deleted = 0;
349 cprintf("+OK all that has come to pass, has now gone away.\r\n");
356 * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
357 * identifiers are simply the Citadel message numbers in the database.
359 void pop3_uidl(char *argbuf) {
363 which_one = atoi(argbuf);
365 /* "list one" mode */
367 if (which_one > POP3->num_msgs) {
368 cprintf("-ERR no such message, only %d are here\r\n",
372 else if (POP3->msgs[which_one-1].deleted) {
373 cprintf("-ERR Sorry, you deleted that message.\r\n");
377 cprintf("+OK %d %ld\n",
379 POP3->msgs[which_one-1].msgnum
385 /* "list all" (scan listing) mode */
387 cprintf("+OK Here's your mail:\r\n");
388 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
389 if (! POP3->msgs[i].deleted) {
390 cprintf("%d %ld\r\n",
392 POP3->msgs[i].msgnum);
403 * Main command loop for POP3 sessions.
405 void pop3_command_loop(void) {
409 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
410 if (client_gets(cmdbuf) < 1) {
411 lprintf(3, "POP3 socket is broken. Ending session.\r\n");
415 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
416 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
418 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
419 cprintf("+OK This command successfully did nothing.\r\n");
422 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
423 cprintf("+OK Goodbye...\r\n");
429 else if (!strncasecmp(cmdbuf, "USER", 4)) {
430 pop3_user(&cmdbuf[5]);
433 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
434 pop3_pass(&cmdbuf[5]);
437 else if (!CC->logged_in) {
438 cprintf("-ERR Not logged in.\r\n");
441 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
442 pop3_list(&cmdbuf[5]);
445 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
446 pop3_stat(&cmdbuf[5]);
449 else if (!strncasecmp(cmdbuf, "RETR", 4)) {
450 pop3_retr(&cmdbuf[5]);
453 else if (!strncasecmp(cmdbuf, "DELE", 4)) {
454 pop3_dele(&cmdbuf[5]);
457 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
458 pop3_rset(&cmdbuf[5]);
461 else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
462 pop3_uidl(&cmdbuf[5]);
465 else if (!strncasecmp(cmdbuf, "TOP", 3)) {
466 pop3_top(&cmdbuf[4]);
470 cprintf("500 I'm afraid I can't do that, Dave.\r\n");
477 char *Dynamic_Module_Init(void)
479 SYM_POP3 = CtdlGetDynamicSymbol();
480 CtdlRegisterServiceHook(POP3_PORT,
483 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);