/* $Id$
*
- * An implementation of Post Office Protocol version 3 (RFC 1939).
- *
+ * POP3 server for the Citadel/UX system
* Copyright (C) 1998-2000 by Art Cancro and others.
* This code is released under the terms of the GNU General Public License.
+ *
+ * Current status of standards conformance:
+ *
+ * -> All required POP3 commands described in RFC1939 are implemented.
+ *
+ * -> Nearly all of the optional commands in RFC1939 are also implemented.
+ * The only one missing is APOP, because it implements a "shared secret"
+ * method of authentication which would require some major changes to the
+ * Citadel server core.
+ *
+ * -> The deprecated "LAST" command is included in this implementation, because
+ * there exist mail clients which insist on using it (such as Bynari
+ * TradeMail, and certain versions of Eudora).
+ *
*/
#include "sysdep.h"
POP3->msgs[POP3->num_msgs-1].deleted = 0;
fp = tmpfile();
POP3->msgs[POP3->num_msgs-1].temp = fp;
- CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, fp, 0);
+
+ CtdlRedirectOutput(fp, -1);
+ CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, 1);
+ CtdlRedirectOutput(NULL, -1);
+
POP3->msgs[POP3->num_msgs-1].rfc822_length = ftell(fp);
}
* of messages in the inbox, or -1 for error)
*/
int pop3_grab_mailbox(void) {
+ struct visit vbuf;
+ int i;
+
if (getroom(&CC->quickroom, MAILROOM) != 0) return(-1);
+
+ /* Load up the messages */
CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, pop3_add_message);
+
+ /* Figure out which are old and which are new */
+ CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
+ POP3->lastseen = (-1);
+ if (POP3->num_msgs) for (i=0; i<POP3->num_msgs; ++i) {
+ if ((POP3->msgs[POP3->num_msgs-1].msgnum) <= vbuf.v_lastseen) {
+ POP3->lastseen = i;
+ }
+ }
+
return(POP3->num_msgs);
}
+/*
+ * RETR command (fetch a message)
+ */
+void pop3_retr(char *argbuf) {
+ int which_one;
+ int ch = 0;
+ size_t bytes_remaining;
+
+ which_one = atoi(argbuf);
+ if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
+ cprintf("-ERR No such message.\r\n");
+ return;
+ }
+
+ if (POP3->msgs[which_one - 1].deleted) {
+ cprintf("-ERR Sorry, you deleted that message.\r\n");
+ return;
+ }
+
+ cprintf("+OK Whoop, there it is:\r\n");
+ bytes_remaining = POP3->msgs[which_one -1].rfc822_length;
+ rewind(POP3->msgs[which_one - 1].temp);
+ while (bytes_remaining-- > 0) {
+ ch = getc(POP3->msgs[which_one - 1].temp);
+ cprintf("%c", ch);
+ }
+ if (ch != 10) {
+ lprintf(5, "Problem: message ends with 0x%2x, not 0x0a\n", ch);
+ }
+ cprintf(".\r\n");
+}
+
+
+/*
+ * TOP command (dumb way of fetching a partial message or headers-only)
+ */
+void pop3_top(char *argbuf) {
+ int which_one;
+ int lines_requested = 0;
+ int lines_dumped = 0;
+ char buf[1024];
+ char *ptr;
+ int in_body = 0;
+ int done = 0;
+
+ sscanf(argbuf, "%d %d", &which_one, &lines_requested);
+ if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
+ cprintf("-ERR No such message.\r\n");
+ return;
+ }
+
+ if (POP3->msgs[which_one - 1].deleted) {
+ cprintf("-ERR Sorry, you deleted that message.\r\n");
+ return;
+ }
+
+ cprintf("+OK Whoop, there it is:\r\n");
+ rewind(POP3->msgs[which_one - 1].temp);
+ while (ptr = fgets(buf, sizeof buf, POP3->msgs[which_one - 1].temp),
+ ( (ptr!=NULL) && (done == 0))) {
+ if (in_body == 1)
+ if (lines_dumped >= lines_requested) done = 1;
+ if ((in_body == 0) || (done == 0))
+ client_write(buf, strlen(buf));
+ if (in_body) ++lines_dumped;
+ if ((buf[0]==13)||(buf[0]==10)) in_body = 1;
+ }
+ if (buf[strlen(buf)-1] != 10) cprintf("\n");
+ cprintf(".\r\n");
+}
+
+
+/*
+ * DELE (delete message from mailbox)
+ */
+void pop3_dele(char *argbuf) {
+ int which_one;
+
+ which_one = atoi(argbuf);
+ if ( (which_one < 1) || (which_one > POP3->num_msgs) ) {
+ cprintf("-ERR No such message.\r\n");
+ return;
+ }
+
+ if (POP3->msgs[which_one - 1].deleted) {
+ cprintf("-ERR You already deleted that message.\r\n");
+ return;
+ }
+
+ /* Flag the message as deleted. Will expunge during QUIT command. */
+ POP3->msgs[which_one - 1].deleted = 1;
+ cprintf("+OK Message %d disappears in a cloud of orange smoke.\r\n",
+ which_one);
+}
+
+
+/* Perform "UPDATE state" stuff
+ */
+void pop3_update(void) {
+ int i;
+ struct visit vbuf;
+
+ /* Remove messages marked for deletion */
+ if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
+ if (POP3->msgs[i].deleted) {
+ CtdlDeleteMessages(MAILROOM,
+ POP3->msgs[i].msgnum, NULL);
+ }
+ }
+
+ /* Set last read pointer */
+ if (POP3->num_msgs > 0) {
+ lgetuser(&CC->usersupp, CC->curr_user);
+
+ CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
+ vbuf.v_lastseen = POP3->msgs[POP3->num_msgs-1].msgnum;
+ CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
+
+ lputuser(&CC->usersupp);
+ }
+
+}
+
+
+/*
+ * RSET (reset, i.e. undelete any deleted messages) command
+ */
+void pop3_rset(char *argbuf) {
+ int i;
+
+ if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
+ if (POP3->msgs[i].deleted) {
+ POP3->msgs[i].deleted = 0;
+ }
+ }
+ cprintf("+OK all that has come to pass, has now gone away.\r\n");
+}
+
+
+
+/*
+ * LAST (Determine which message is the last unread message)
+ */
+void pop3_last(char *argbuf) {
+ cprintf("+OK %d\r\n", POP3->lastseen + 1);
+}
+
+
+
+/*
+ * UIDL (Universal IDentifier Listing) is easy. Our 'unique' message
+ * identifiers are simply the Citadel message numbers in the database.
+ */
+void pop3_uidl(char *argbuf) {
+ int i;
+ int which_one;
+
+ which_one = atoi(argbuf);
+
+ /* "list one" mode */
+ if (which_one > 0) {
+ if (which_one > POP3->num_msgs) {
+ cprintf("-ERR no such message, only %d are here\r\n",
+ POP3->num_msgs);
+ return;
+ }
+ else if (POP3->msgs[which_one-1].deleted) {
+ cprintf("-ERR Sorry, you deleted that message.\r\n");
+ return;
+ }
+ else {
+ cprintf("+OK %d %ld\n",
+ which_one,
+ POP3->msgs[which_one-1].msgnum
+ );
+ return;
+ }
+ }
+
+ /* "list all" (scan listing) mode */
+ else {
+ cprintf("+OK Here's your mail:\r\n");
+ if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
+ if (! POP3->msgs[i].deleted) {
+ cprintf("%d %ld\r\n",
+ i+1,
+ POP3->msgs[i].msgnum);
+ }
+ }
+ cprintf(".\r\n");
+ }
+}
+
+
+
+
/*
* Main command loop for POP3 sessions.
*/
else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
cprintf("+OK Goodbye...\r\n");
+ pop3_update();
CC->kill_me = 1;
return;
}
pop3_stat(&cmdbuf[5]);
}
+ else if (!strncasecmp(cmdbuf, "RETR", 4)) {
+ pop3_retr(&cmdbuf[5]);
+ }
+
+ else if (!strncasecmp(cmdbuf, "DELE", 4)) {
+ pop3_dele(&cmdbuf[5]);
+ }
+
+ else if (!strncasecmp(cmdbuf, "RSET", 4)) {
+ pop3_rset(&cmdbuf[5]);
+ }
+
+ else if (!strncasecmp(cmdbuf, "UIDL", 4)) {
+ pop3_uidl(&cmdbuf[5]);
+ }
+
+ else if (!strncasecmp(cmdbuf, "TOP", 3)) {
+ pop3_top(&cmdbuf[4]);
+ }
+
+ else if (!strncasecmp(cmdbuf, "LAST", 4)) {
+ pop3_last(&cmdbuf[4]);
+ }
+
else {
cprintf("500 I'm afraid I can't do that, Dave.\r\n");
}
char *Dynamic_Module_Init(void)
{
SYM_POP3 = CtdlGetDynamicSymbol();
- CtdlRegisterServiceHook(POP3_PORT,
+ CtdlRegisterServiceHook(config.c_pop3_port,
+ NULL,
pop3_greeting,
pop3_command_loop);
CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);