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);
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 * Main command loop for POP3 sessions.
234 void pop3_command_loop(void) {
238 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
239 if (client_gets(cmdbuf) < 1) {
240 lprintf(3, "POP3 socket is broken. Ending session.\r\n");
244 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
245 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
247 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
248 cprintf("+OK This command successfully did nothing.\r\n");
251 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
252 cprintf("+OK Goodbye...\r\n");
257 else if (!strncasecmp(cmdbuf, "USER", 4)) {
258 pop3_user(&cmdbuf[5]);
261 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
262 pop3_pass(&cmdbuf[5]);
265 else if (!CC->logged_in) {
266 cprintf("-ERR Not logged in.\r\n");
269 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
270 pop3_list(&cmdbuf[5]);
273 else if (!strncasecmp(cmdbuf, "STAT", 4)) {
274 pop3_stat(&cmdbuf[5]);
278 cprintf("500 I'm afraid I can't do that, Dave.\r\n");
285 char *Dynamic_Module_Init(void)
287 SYM_POP3 = CtdlGetDynamicSymbol();
288 CtdlRegisterServiceHook(POP3_PORT,
291 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);