]> code.citadel.org Git - citadel.git/blob - citadel/serv_pop3.c
*** empty log message ***
[citadel.git] / citadel / serv_pop3.c
1 /* $Id$ */
2
3 #include "sysdep.h"
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <stdio.h>
7 #include <fcntl.h>
8 #include <signal.h>
9 #include <pwd.h>
10 #include <errno.h>
11 #include <sys/types.h>
12 #include <sys/time.h>
13 #include <sys/wait.h>
14 #include <string.h>
15 #include <limits.h>
16 #include "citadel.h"
17 #include "server.h"
18 #include <time.h>
19 #include "sysdep_decls.h"
20 #include "citserver.h"
21 #include "support.h"
22 #include "config.h"
23 #include "dynloader.h"
24 #include "room_ops.h"
25 #include "user_ops.h"
26 #include "policy.h"
27 #include "database.h"
28 #include "msgbase.h"
29 #include "tools.h"
30 #include "internet_addressing.h"
31
32
33 struct pop3msg {
34         long msgnum;
35         size_t rfc822_length;
36         int deleted;
37         FILE *temp;
38 };
39
40 struct citpop3 {                /* Information about the current session */
41         struct pop3msg *msgs;
42         int num_msgs;
43 };
44
45 #define POP3 ((struct citpop3 *)CtdlGetUserData(SYM_POP3))
46
47 long SYM_POP3;
48
49
50 void pop3_cleanup_function(void) {
51         int i;
52
53         lprintf(9, "Performing POP3 cleanup hook\n");
54
55         if (POP3->num_msgs > 0) for (i=0; i<POP3->num_msgs; ++i) {
56                 fclose(POP3->msgs[i].temp);
57         }
58         if (POP3->msgs != NULL) phree(POP3->msgs);
59 }
60
61
62
63 /*
64  * Here's where our POP3 session begins its happy day.
65  */
66 void pop3_greeting(void) {
67
68         strcpy(CC->cs_clientname, "POP3 session");
69         CC->internal_pgm = 1;
70         CtdlAllocUserData(SYM_POP3, sizeof(struct citpop3));
71         POP3->msgs = NULL;
72         POP3->num_msgs = 0;
73
74         cprintf("+OK Welcome to the Citadel/UX POP3 server at %s\r\n",
75                 config.c_fqdn);
76 }
77
78
79 /*
80  * Specify user name (implements POP3 "USER" command)
81  */
82 void pop3_user(char *argbuf) {
83         char username[256];
84
85         if (CC->logged_in) {
86                 cprintf("-ERR You are already logged in.\r\n");
87                 return;
88         }
89
90         strcpy(username, argbuf);
91         striplt(username);
92
93         lprintf(9, "Trying <%s>\n", username);
94         if (CtdlLoginExistingUser(username) == login_ok) {
95                 cprintf("+OK Password required for %s\r\n", username);
96         }
97         else {
98                 cprintf("-ERR No such user.\r\n");
99         }
100 }
101
102
103 /*
104  * Authorize with password (implements POP3 "PASS" command)
105  */
106 void pop3_pass(char *argbuf) {
107         char password[256];
108
109         strcpy(password, argbuf);
110         striplt(password);
111
112         lprintf(9, "Trying <%s>\n", password);
113         if (CtdlTryPassword(password) == pass_ok) {
114                 if (getroom(&CC->quickroom, MAILROOM) == 0) {
115                         cprintf("+OK %s is logged in!\r\n",
116                                 CC->usersupp.fullname);
117                         lprintf(9, "POP3 password login successful\n");
118                 }
119                 else {
120                         cprintf("-ERR can't find your mailbox\r\n");
121                 }
122         }
123         else {
124                 cprintf("-ERR That is NOT the password!  Go away!\r\n");
125         }
126 }
127
128
129
130 /*
131  * list available msgs
132  */
133 void pop3_list(char *argbuf) {
134         cprintf("-ERR oops, not finished\r\n");
135 }
136
137
138
139
140 /* 
141  * Main command loop for POP3 sessions.
142  */
143 void pop3_command_loop(void) {
144         char cmdbuf[256];
145
146         time(&CC->lastcmd);
147         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
148         if (client_gets(cmdbuf) < 1) {
149                 lprintf(3, "POP3 socket is broken.  Ending session.\r\n");
150                 CC->kill_me = 1;
151                 return;
152         }
153         lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
154         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
155
156         if (!strncasecmp(cmdbuf, "NOOP", 4)) {
157                 cprintf("+OK This command successfully did nothing.\r\n");
158         }
159
160         else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
161                 cprintf("+OK Goodbye...\r\n");
162                 CC->kill_me = 1;
163                 return;
164         }
165
166         else if (!strncasecmp(cmdbuf, "USER", 4)) {
167                 pop3_user(&cmdbuf[5]);
168         }
169
170         else if (!strncasecmp(cmdbuf, "PASS", 4)) {
171                 pop3_pass(&cmdbuf[5]);
172         }
173
174         else if (!CC->logged_in) {
175                 cprintf("-ERR Not logged in.\r\n");
176         }
177
178         else if (!strncasecmp(cmdbuf, "LIST", 4)) {
179                 pop3_list(&cmdbuf[5]);
180         }
181
182         else {
183                 cprintf("500 I'm afraid I can't do that, Dave.\r\n");
184         }
185
186 }
187
188
189
190 char *Dynamic_Module_Init(void)
191 {
192         SYM_POP3 = CtdlGetDynamicSymbol();
193         CtdlRegisterServiceHook(POP3_PORT,
194                                 pop3_greeting,
195                                 pop3_command_loop);
196         CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);
197         return "$Id$";
198 }