Here it is, the new thread interface.
[citadel.git] / citadel / modules / managesieve / serv_managesieve.c
index c43d8aa192ceac085610b4da2eba4f147f98ac22..f05966c82b63f363a4aef3631eb90280f0f4c13a 100644 (file)
@@ -38,6 +38,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#include <libcitadel.h>
 #include "citadel.h"
 #include "server.h"
 #include "citserver.h"
 #include "policy.h"
 #include "database.h"
 #include "msgbase.h"
-#include "tools.h"
 #include "internet_addressing.h"
-#include "imap_tools.h"
+#include "imap_tools.h"        /* Needed for imap_parameterize */
 #include "genstamp.h"
 #include "domain.h"
 #include "clientsocket.h"
 #include "locate_host.h"
 #include "citadel_dirs.h"
 
-#ifdef HAVE_OPENSSL
-#include "serv_crypto.h"
-#endif
-
 #ifndef HAVE_SNPRINTF
 #include "snprintf.h"
 #endif
@@ -99,12 +95,13 @@ enum {      /** Command states for login authentication */
        mgsve_plain
 };
 
-#define MGSVE          CC->MGSVE
+#define MGSVE          ((struct citmgsve *)CC->session_specific_data)
 
 /*****************************************************************************/
 /*                      MANAGESIEVE Server                                   */
 /*****************************************************************************/
 
+
 void sieve_outbuf_append(char *str)
 {
         size_t newlen = strlen(str)+1;
@@ -148,7 +145,7 @@ void managesieve_greeting(void) {
 
        CC->internal_pgm = 1;
        CC->cs_flags |= CS_STEALTH;
-       MGSVE = malloc(sizeof(struct citmgsve));
+       CC->session_specific_data = malloc(sizeof(struct citmgsve));
        memset(MGSVE, 0, sizeof(struct citmgsve));
        cmd_mgsve_caps();
 }
@@ -159,12 +156,12 @@ long GetSizeToken(char * token)
        char *cursor = token;
        char *number;
 
-       while ((*cursor != '\0') && 
+       while (!IsEmptyStr(cursor) && 
               (*cursor != '{'))
        {
                cursor++;
        }
-       if (*cursor == '\0'
+       if (IsEmptyStr(cursor)
                return -1;
        number = cursor + 1;
        while ((*cursor != '\0') && 
@@ -176,7 +173,7 @@ long GetSizeToken(char * token)
        if (cursor[-1] == '+')
                cursor--;
 
-       if (*cursor == '\0'
+       if (IsEmptyStr(cursor)
                return -1;
        
        return atol(number);
@@ -246,17 +243,15 @@ void cmd_mgsve_auth(int num_parms, char **parms, struct sdm_userdata *u)
 }
 
 
-#ifdef HAVE_OPENSSL
 /**
  * STARTTLS command chapter 2.2 
  */
 void cmd_mgsve_starttls(void)
 { /** answer with OK, and fire off tls session. */
        cprintf("OK\r\n");
-       CtdlStartTLS(NULL, NULL, NULL);
+       CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
        cmd_mgsve_caps();
 }
-#endif
 
 
 
@@ -450,7 +445,7 @@ void mgsve_auth(char *argbuf) {
                if (strlen(argbuf) >= 7) {
                }
                else {
-                       CtdlEncodeBase64(username_prompt, "Username:", 9);
+                       CtdlEncodeBase64(username_prompt, "Username:", 9, 0);
                        cprintf("334 %s\r\n", username_prompt);
                }
                return;
@@ -477,7 +472,6 @@ void mgsve_auth(char *argbuf) {
 /*
  * implements the STARTTLS command (Citadel API version)
  */
-#ifdef HAVE_OPENSSL
 void _mgsve_starttls(void)
 {
        char ok_response[SIZ];
@@ -490,9 +484,8 @@ void _mgsve_starttls(void)
                "554 5.7.3 TLS not supported here\r\n");
        sprintf(error_response,
                "554 5.7.3 Internal error\r\n");
-       CtdlStartTLS(ok_response, nosup_response, error_response);
+       CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
 }
-#endif
 
 
 /* 
@@ -573,26 +566,43 @@ void managesieve_command_loop(void) {
 
 }
 
+/*
+ * This cleanup function blows away the temporary memory and files used by
+ * the server.
+ */
+void managesieve_cleanup_function(void) {
 
-#endif /* HAVE_LIBSIEVE */
+       /* Don't do this stuff if this is not a managesieve session! */
+       if (CC->h_command_function != managesieve_command_loop) return;
 
+       lprintf(CTDL_DEBUG, "Performing managesieve cleanup hook\n");
+       free(MGSVE);
+}
+
+
+
+#endif /* HAVE_LIBSIEVE */
+const char* CitadelServiceManageSieve = "ManageSieve";
 CTDL_MODULE_INIT(managesieve)
 {
-
+       if (!threading)
+       {
 #ifdef HAVE_LIBSIEVE
-
-       CtdlRegisterServiceHook(config.c_managesieve_port,      /* MGSVE */
-                               NULL,
-                               managesieve_greeting,
-                               managesieve_command_loop,
-                               NULL);
+               CtdlRegisterServiceHook(config.c_managesieve_port,
+                                       NULL,
+                                       managesieve_greeting,
+                                       managesieve_command_loop,
+                                       NULL, 
+                                       CitadelServiceManageSieve);
+               CtdlRegisterSessionHook(managesieve_cleanup_function, EVT_STOP);
 
 #else  /* HAVE_LIBSIEVE */
 
-       lprintf(CTDL_INFO, "This server is missing libsieve.  Managesieve protocol is disabled..\n");
+               lprintf(CTDL_INFO, "This server is missing libsieve.  Managesieve protocol is disabled..\n");
 
 #endif /* HAVE_LIBSIEVE */
-
+       }
+       
        /* return our Subversion id for the Log */
        return "$Id$";
 }