struct CitContext no longer contains separate module-specific pointers for
authorArt Cancro <ajc@citadel.org>
Wed, 21 Nov 2007 04:54:19 +0000 (04:54 +0000)
committerArt Cancro <ajc@citadel.org>
Wed, 21 Nov 2007 04:54:19 +0000 (04:54 +0000)
things like IMAP state, SMTP state, POP3 state, managesieve state, etc.
These four modules now use a single 'service_specific_data' pointer, which
is cast to whatever data type the module is using.  This is possible because
only one is used at a time (a session cannot be POP3 and SMTP simultaneously,
for example).  As before, when a module allocates memory and places
a pointer to it in this location, the module's cleanup function
continues to be responsible for freeing it, and as before, it must not
free this pointer under any other circumstances.

Third-party modules which implement new server protocols now have a place
to store session data without modifying the server core.  Just follow one
of the existing server protocol modules as an example.

citadel/modules/imap/serv_imap.c
citadel/modules/imap/serv_imap.h
citadel/modules/managesieve/serv_managesieve.c
citadel/modules/pop3/serv_pop3.c
citadel/modules/pop3/serv_pop3.h
citadel/modules/smtp/serv_smtp.c
citadel/server.h

index c86518fb8f08b7a890939b05debd6af1805a52ad..cd138b86d355d6cde0910581815e9d38843f367d 100644 (file)
@@ -491,7 +491,7 @@ void imap_greeting(void)
 {
 
        strcpy(CC->cs_clientname, "IMAP session");
-       IMAP = malloc(sizeof (struct citimap));
+       CC->session_specific_data = malloc(sizeof(struct citimap));
        memset(IMAP, 0, sizeof(struct citimap));
        IMAP->authstate = imap_as_normal;
        IMAP->cached_rfc822_data = NULL;
index 59e30643e851576470a22876a607e89dc3cb21ec..e2b3f4334f0977378e6436539c4544e60b00898f 100644 (file)
@@ -67,7 +67,7 @@ enum {
 #define IMAP_RECENT            64      /* reportable but not setable */
 
 
-#define IMAP CC->IMAP
+#define IMAP ((struct citimap *)CC->session_specific_data)
 
 /*
  * When loading arrays of message ID's into memory, increase the buffer to
index 065702fa1ef8631d03533092f731e68e5e3862bd..e40007d3fa152f6ca747562c31d7d578bb12f041 100644 (file)
@@ -95,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;
@@ -144,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();
 }
@@ -565,6 +566,20 @@ void managesieve_command_loop(void) {
 
 }
 
+/*
+ * This cleanup function blows away the temporary memory and files used by
+ * the server.
+ */
+void managesieve_cleanup_function(void) {
+
+       /* 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";
@@ -572,13 +587,13 @@ CTDL_MODULE_INIT(managesieve)
 {
 
 #ifdef HAVE_LIBSIEVE
-
-       CtdlRegisterServiceHook(config.c_managesieve_port,      /* MGSVE */
+       CtdlRegisterServiceHook(config.c_managesieve_port,
                                NULL,
                                managesieve_greeting,
                                managesieve_command_loop,
                                NULL, 
                                CitadelServiceManageSieve);
+       CtdlRegisterSessionHook(managesieve_cleanup_function, EVT_STOP);
 
 #else  /* HAVE_LIBSIEVE */
 
index 6aaa628a4cdd236eb535787b327c3e3d8ddc124e..dcdbb8ec6d8b2c581ed164e883719ce0bfa35759 100644 (file)
@@ -85,7 +85,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",
index 8c73f689d0d2bd796c010aecb6e2e30694d2180d..c57520f5a6dbacd35835d3a95761eacbf992d6b1 100644 (file)
@@ -20,7 +20,7 @@ struct citpop3 {              /* Information about the current session */
                                 * incremented by one.
                                 */
 
-#define POP3 CC->POP3
+#define POP3 ((struct citpop3 *)CC->session_specific_data)
 
 void pop3_cleanup_function(void);
 void pop3_greeting(void);
index b444ab4fed46e89b6748fc7b12207c91fcddbb49..77b2618ea055df4b82f474e6db1205d138ba1540 100644 (file)
@@ -105,7 +105,7 @@ enum {                              /* Command states for login authentication */
        smtp_plain
 };
 
-#define SMTP           CC->SMTP
+#define SMTP           ((struct citsmtp *)CC->session_specific_data)
 
 
 int run_queue_now = 0; /* Set to 1 to ignore SMTP send retry times */
@@ -127,7 +127,7 @@ void smtp_greeting(int is_msa)
        strcpy(CC->cs_clientname, "SMTP session");
        CC->internal_pgm = 1;
        CC->cs_flags |= CS_STEALTH;
-       SMTP = malloc(sizeof(struct citsmtp));
+       CC->session_specific_data = malloc(sizeof(struct citsmtp));
        memset(SMTP, 0, sizeof(struct citsmtp));
        SMTP->is_msa = is_msa;
 
index 95c820ef8c34dc3fe74b2430510993c043b782c6..982aa37d82ad363d9f53c38f2cf650a49550f50a 100644 (file)
@@ -139,10 +139,7 @@ struct CitContext {
        int msg4_dont_decode;
 
        /* Dynamically allocated session data */
-       struct citimap *IMAP;
-       struct citpop3 *POP3;
-       struct citsmtp *SMTP;
-       struct citmgsve *MGSVE;                 /**< Managesieve Session struct */
+       char *session_specific_data;            /* Used by individual protocol modules */
        struct cit_ical *CIT_ICAL;              /* calendaring data */
        struct ma_info *ma;                     /* multipart/alternative data */
        const char* ServiceName;                /**< whats our actual purpose? */