* More license declarations
[citadel.git] / citadel / modules / pop3 / serv_pop3.c
index fadb9f50cc15b0096111c3f147f096b7905d3aff..baee5a2aafc6926e7687f76d7da763d6a188323f 100644 (file)
@@ -2,8 +2,22 @@
  * $Id$ 
  *
  * POP3 service for the Citadel system
- * Copyright (C) 1998-2001 by Art Cancro and others.
- * This code is released under the terms of the GNU General Public License.
+ *
+ * Copyright (c) 1998-2009 by the citadel.org team
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  * Current status of standards conformance:
  *
@@ -41,6 +55,7 @@
 #include <string.h>
 #include <limits.h>
 #include <ctype.h>
+#include <libcitadel.h>
 #include "citadel.h"
 #include "server.h"
 #include "citserver.h"
@@ -51,7 +66,6 @@
 #include "policy.h"
 #include "database.h"
 #include "msgbase.h"
-#include "tools.h"
 #include "internet_addressing.h"
 #include "serv_pop3.h"
 #include "md5.h"
@@ -71,7 +85,7 @@ void pop3_cleanup_function(void) {
        /* Don't do this stuff if this is not a POP3 session! */
        if (CC->h_command_function != pop3_command_loop) return;
 
-       lprintf(CTDL_DEBUG, "Performing POP3 cleanup hook\n");
+       CtdlLogPrintf(CTDL_DEBUG, "Performing POP3 cleanup hook\n");
        if (POP3->msgs != NULL) free(POP3->msgs);
 
        free(POP3);
@@ -85,7 +99,7 @@ void pop3_cleanup_function(void) {
 void pop3_greeting(void) {
        strcpy(CC->cs_clientname, "POP3 session");
        CC->internal_pgm = 1;
-       POP3 = malloc(sizeof(struct citpop3));
+       CC->session_specific_data = malloc(sizeof(struct citpop3));
        memset(POP3, 0, sizeof(struct citpop3));
 
        cprintf("+OK Citadel POP3 server %s\r\n",
@@ -98,7 +112,14 @@ void pop3_greeting(void) {
  */
 void pop3s_greeting(void) {
        CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
-       if (!CC->redirect_ssl) CC->kill_me = 1;         /* kill session if no crypto */
+
+/* kill session if no crypto */
+#ifdef HAVE_OPENSSL
+       if (!CC->redirect_ssl) CC->kill_me = 1;
+#else
+       CC->kill_me = 1;
+#endif
+
        pop3_greeting();
 }
 
@@ -118,7 +139,7 @@ void pop3_user(char *argbuf) {
        strcpy(username, argbuf);
        striplt(username);
 
-       /* lprintf(CTDL_DEBUG, "Trying <%s>\n", username); */
+       /* CtdlLogPrintf(CTDL_DEBUG, "Trying <%s>\n", username); */
        if (CtdlLoginExistingUser(NULL, username) == login_ok) {
                cprintf("+OK Password required for %s\r\n", username);
        }
@@ -152,7 +173,7 @@ void pop3_add_message(long msgnum, void *userdata) {
                CC->redirect_buffer = malloc(SIZ);
                CC->redirect_len = 0;
                CC->redirect_alloc = SIZ;
-               CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL);
+               CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL, SUPPRESS_ENV_TO);
                smi.meta_rfc822_length = CC->redirect_len;
                free(CC->redirect_buffer);
                CC->redirect_buffer = NULL;
@@ -201,7 +222,7 @@ void pop3_login(void)
        if (msgs >= 0) {
                cprintf("+OK %s is logged in (%d messages)\r\n",
                        CC->user.fullname, msgs);
-               lprintf(CTDL_NOTICE, "POP3 authenticated %s\n", CC->user.fullname);
+               CtdlLogPrintf(CTDL_NOTICE, "POP3 authenticated %s\n", CC->user.fullname);
        }
        else {
                cprintf("-ERR Can't open your mailbox\r\n");
@@ -273,7 +294,7 @@ void pop3_pass(char *argbuf) {
        strcpy(password, argbuf);
        striplt(password);
 
-       /* lprintf(CTDL_DEBUG, "Trying <%s>\n", password); */
+       /* CtdlLogPrintf(CTDL_DEBUG, "Trying <%s>\n", password); */
        if (CtdlTryPassword(password) == pass_ok) {
                pop3_login();
        }
@@ -366,7 +387,10 @@ void pop3_retr(char *argbuf) {
        }
 
        cprintf("+OK Message %d:\r\n", which_one);
-       CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL);
+       CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum,
+               MT_RFC822, HEADERS_ALL, 0, 1, NULL,
+               (ESC_DOT|SUPPRESS_ENV_TO)
+       );
        cprintf(".\r\n");
 }
 
@@ -398,8 +422,7 @@ void pop3_top(char *argbuf) {
        CC->redirect_buffer = malloc(SIZ);
        CC->redirect_len = 0;
        CC->redirect_alloc = SIZ;
-       CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum,
-                       MT_RFC822, HEADERS_ALL, 0, 1, NULL);
+       CtdlOutputMsg(POP3->msgs[which_one - 1].msgnum, MT_RFC822, HEADERS_ALL, 0, 1, NULL, SUPPRESS_ENV_TO);
        msgtext = CC->redirect_buffer;
        CC->redirect_buffer = NULL;
        CC->redirect_len = 0;
@@ -615,15 +638,15 @@ void pop3_command_loop(void) {
        time(&CC->lastcmd);
        memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
        if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
-               lprintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
+               CtdlLogPrintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
                CC->kill_me = 1;
                return;
        }
        if (!strncasecmp(cmdbuf, "PASS", 4)) {
-               lprintf(CTDL_INFO, "POP3: PASS...\r\n");
+               CtdlLogPrintf(CTDL_INFO, "POP3: PASS...\r\n");
        }
        else {
-               lprintf(CTDL_INFO, "POP3: %s\r\n", cmdbuf);
+               CtdlLogPrintf(CTDL_INFO, "POP3: %s\r\n", cmdbuf);
        }
        while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
 
@@ -664,6 +687,11 @@ void pop3_command_loop(void) {
        else if (!CC->logged_in) {
                cprintf("-ERR Not logged in.\r\n");
        }
+       
+       else if (CC->nologin) {
+               cprintf("-ERR System busy, try later.\r\n");
+               CC->kill_me = 1;
+       }
 
        else if (!strncasecmp(cmdbuf, "LIST", 4)) {
                pop3_list(&cmdbuf[5]);
@@ -709,22 +737,25 @@ const char *CitadelServicePop3S="POP3S";
 
 CTDL_MODULE_INIT(pop3)
 {
-       CtdlRegisterServiceHook(config.c_pop3_port,
-                               NULL,
-                               pop3_greeting,
-                               pop3_command_loop,
-                               NULL,
-                               CitadelServicePop3);
+       if(!threading)
+       {
+               CtdlRegisterServiceHook(config.c_pop3_port,
+                                       NULL,
+                                       pop3_greeting,
+                                       pop3_command_loop,
+                                       NULL,
+                                       CitadelServicePop3);
 #ifdef HAVE_OPENSSL
-       CtdlRegisterServiceHook(config.c_pop3s_port,
-                               NULL,
-                               pop3s_greeting,
-                               pop3_command_loop,
-                               NULL,
-                               CitadelServicePop3S);
+               CtdlRegisterServiceHook(config.c_pop3s_port,
+                                       NULL,
+                                       pop3s_greeting,
+                                       pop3_command_loop,
+                                       NULL,
+                                       CitadelServicePop3S);
 #endif
-       CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);
-
+               CtdlRegisterSessionHook(pop3_cleanup_function, EVT_STOP);
+       }
+       
        /* return our Subversion id for the Log */
        return "$Id$";
 }