* Replaced citmail.c with a new one that simply SMTP-forwards to Citadel
[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 citpop3 {                /* Information about the current session */
34         char FIX[3];
35 };
36
37 #define POP3 ((struct citpop3 *)CtdlGetUserData(SYM_POP3))
38
39 long SYM_POP3;
40
41
42 /*
43  * Here's where our POP3 session begins its happy day.
44  */
45 void pop3_greeting(void) {
46
47         strcpy(CC->cs_clientname, "POP3 session");
48         CC->internal_pgm = 1;
49         CtdlAllocUserData(SYM_POP3, sizeof(struct citpop3));
50
51         cprintf("+OK Welcome to the Citadel/UX POP3 server at %s\r\n",
52                 config.c_fqdn);
53 }
54
55
56 /*
57  * Specify user name (implements POP3 "USER" command)
58  */
59 void pop3_user(char *argbuf) {
60         char username[256];
61
62         if (CC->logged_in) {
63                 cprintf("-ERR You are already logged in.\r\n");
64                 return;
65         }
66
67         strcpy(username, argbuf);
68         striplt(username);
69
70         lprintf(9, "Trying <%s>\n", username);
71         if (CtdlLoginExistingUser(username) == login_ok) {
72                 cprintf("+OK Password required for %s\r\n", username);
73         }
74         else {
75                 cprintf("-ERR No such user.\r\n");
76         }
77 }
78
79
80 /*
81  * Authorize with password (implements POP3 "PASS" command)
82  */
83 void pop3_pass(char *argbuf) {
84         char password[256];
85
86         strcpy(password, argbuf);
87         striplt(password);
88
89         lprintf(9, "Trying <%s>\n", password);
90         if (CtdlTryPassword(password) == pass_ok) {
91                 cprintf("+OK %s is logged in!\r\n", CC->usersupp.fullname);
92                 lprintf(9, "POP3 password login successful\n");
93         }
94         else {
95                 cprintf("-ERR That is NOT the password!  Go away!\r\n");
96         }
97 }
98
99
100
101 /* 
102  * Main command loop for POP3 sessions.
103  */
104 void pop3_command_loop(void) {
105         char cmdbuf[256];
106
107         time(&CC->lastcmd);
108         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
109         if (client_gets(cmdbuf) < 1) {
110                 lprintf(3, "POP3 socket is broken.  Ending session.\r\n");
111                 CC->kill_me = 1;
112                 return;
113         }
114         lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
115         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
116
117         if (!strncasecmp(cmdbuf, "NOOP", 4)) {
118                 cprintf("+OK This command successfully did nothing.\r\n");
119         }
120
121         else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
122                 cprintf("+OK Goodbye...\r\n");
123                 CC->kill_me = 1;
124                 return;
125         }
126
127         else if (!strncasecmp(cmdbuf, "USER", 4)) {
128                 pop3_user(&cmdbuf[5]);
129         }
130
131         else if (!strncasecmp(cmdbuf, "PASS", 4)) {
132                 pop3_pass(&cmdbuf[5]);
133         }
134
135         else if (!CC->logged_in) {
136                 cprintf("-ERR Not logged in.\r\n");
137         }
138
139         else {
140                 cprintf("500 I'm afraid I can't do that, Dave.\r\n");
141         }
142
143 }
144
145
146
147 char *Dynamic_Module_Init(void)
148 {
149         SYM_POP3 = CtdlGetDynamicSymbol();
150         CtdlRegisterServiceHook(POP3_PORT,
151                                 pop3_greeting,
152                                 pop3_command_loop);
153         return "$Id$";
154 }