11 #include <sys/types.h>
19 #include "sysdep_decls.h"
20 #include "citserver.h"
23 #include "dynloader.h"
30 #include "internet_addressing.h"
31 #include "serv_pop3.h"
38 * This cleanup function blows away the temporary memory and files used by
41 void pop3_cleanup_function(void) {
44 /* Don't do this stuff if this is not a POP3 session! */
45 if (CC->h_command_function != pop3_command_loop) return;
47 lprintf(9, "Performing POP3 cleanup hook\n");
49 if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
50 fclose(POP3->msgs[i].temp);
52 if (POP3->msgs != NULL) phree(POP3->msgs);
54 lprintf(9, "Finished POP3 cleanup hook\n");
60 * Here's where our POP3 session begins its happy day.
62 void pop3_greeting(void) {
64 strcpy(CC->cs_clientname, "POP3 session");
66 CtdlAllocUserData(SYM_POP3, sizeof(struct citpop3));
70 cprintf("+OK Welcome to the Citadel/UX POP3 server at %s\r\n",
76 * Specify user name (implements POP3 "USER" command)
78 void pop3_user(char *argbuf) {
82 cprintf("-ERR You are already logged in.\r\n");
86 strcpy(username, argbuf);
89 lprintf(9, "Trying <%s>\n", username);
90 if (CtdlLoginExistingUser(username) == login_ok) {
91 cprintf("+OK Password required for %s\r\n", username);
94 cprintf("-ERR No such user.\r\n");
101 * Back end for pop3_grab_mailbox()
103 void pop3_add_message(long msgnum) {
105 lprintf(9, "in pop3_add_message()\n");
108 if (POP3->num_msgs < 2) POP3->msgs = mallok(sizeof(struct pop3msg));
109 else POP3->msgs = reallok(POP3->msgs,
110 (POP3->num_msgs * sizeof(struct pop3msg)) ) ;
111 POP3->msgs[POP3->num_msgs-1].msgnum = msgnum;
112 POP3->msgs[POP3->num_msgs-1].deleted = 0;
114 POP3->msgs[POP3->num_msgs-1].temp = fp;
115 CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, fp, 0);
116 POP3->msgs[POP3->num_msgs-1].rfc822_length = ftell(fp);
122 * Open the inbox and read its contents.
123 * (This should be called only once, by pop3_pass(), and returns the number
124 * of messages in the inbox, or -1 for error)
126 int pop3_grab_mailbox(void) {
127 if (getroom(&CC->quickroom, MAILROOM) != 0) return(-1);
128 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, pop3_add_message);
129 return(POP3->num_msgs);
133 * Authorize with password (implements POP3 "PASS" command)
135 void pop3_pass(char *argbuf) {
139 strcpy(password, argbuf);
142 lprintf(9, "Trying <%s>\n", password);
143 if (CtdlTryPassword(password) == pass_ok) {
144 msgs = pop3_grab_mailbox();
146 cprintf("+OK %s is logged in (%d messages)\r\n",
147 CC->usersupp.fullname, msgs);
148 lprintf(9, "POP3 password login successful\n");
151 cprintf("-ERR Can't open your mailbox\r\n");
155 cprintf("-ERR That is NOT the password! Go away!\r\n");
162 * list available msgs
164 void pop3_list(char *argbuf) {
165 cprintf("-ERR oops, not finished\r\n");
172 * Main command loop for POP3 sessions.
174 void pop3_command_loop(void) {
178 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
179 if (client_gets(cmdbuf) < 1) {
180 lprintf(3, "POP3 socket is broken. Ending session.\r\n");
184 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
185 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
187 if (!strncasecmp(cmdbuf, "NOOP", 4)) {
188 cprintf("+OK This command successfully did nothing.\r\n");
191 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
192 cprintf("+OK Goodbye...\r\n");
197 else if (!strncasecmp(cmdbuf, "USER", 4)) {
198 pop3_user(&cmdbuf[5]);
201 else if (!strncasecmp(cmdbuf, "PASS", 4)) {
202 pop3_pass(&cmdbuf[5]);
205 else if (!CC->logged_in) {
206 cprintf("-ERR Not logged in.\r\n");
209 else if (!strncasecmp(cmdbuf, "LIST", 4)) {
210 pop3_list(&cmdbuf[5]);
214 cprintf("500 I'm afraid I can't do that, Dave.\r\n");
221 char *Dynamic_Module_Init(void)
223 SYM_POP3 = CtdlGetDynamicSymbol();
224 CtdlRegisterServiceHook(POP3_PORT,
227 CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);