]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_imap.c
* Removed the built-in memory leak checker. It wasn't threadsafe and
[citadel.git] / citadel / serv_imap.c
index b2d8293dd5093798950e19d43d43c3e5462f213d..bee2dedb1077b1b2333523dc3ed58e1f820f712f 100644 (file)
@@ -81,12 +81,12 @@ struct irlparms {
 void imap_free_msgids(void)
 {
        if (IMAP->msgids != NULL) {
-               phree(IMAP->msgids);
+               free(IMAP->msgids);
                IMAP->msgids = NULL;
                IMAP->num_msgs = 0;
        }
        if (IMAP->flags != NULL) {
-               phree(IMAP->flags);
+               free(IMAP->flags);
                IMAP->flags = NULL;
        }
 }
@@ -98,7 +98,7 @@ void imap_free_msgids(void)
 void imap_free_transmitted_message(void)
 {
        if (IMAP->transmitted_message != NULL) {
-               phree(IMAP->transmitted_message);
+               free(IMAP->transmitted_message);
                IMAP->transmitted_message = NULL;
                IMAP->transmitted_length = 0;
        }
@@ -142,18 +142,18 @@ void imap_add_single_msgid(long msgnum, void *userdata)
 
        IMAP->num_msgs = IMAP->num_msgs + 1;
        if (IMAP->msgids == NULL) {
-               IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long)
+               IMAP->msgids = malloc(IMAP->num_msgs * sizeof(long)
                                      * REALLOC_INCREMENT);
        } else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
-               IMAP->msgids = reallok(IMAP->msgids,
+               IMAP->msgids = realloc(IMAP->msgids,
                                       (IMAP->num_msgs +
                                        REALLOC_INCREMENT) * sizeof(long));
        }
        if (IMAP->flags == NULL) {
-               IMAP->flags = mallok(IMAP->num_msgs * sizeof(long)
+               IMAP->flags = malloc(IMAP->num_msgs * sizeof(long)
                                     * REALLOC_INCREMENT);
        } else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
-               IMAP->flags = reallok(IMAP->flags,
+               IMAP->flags = realloc(IMAP->flags,
                                      (IMAP->num_msgs +
                                       REALLOC_INCREMENT) * sizeof(long));
        }
@@ -170,7 +170,7 @@ void imap_load_msgids(void)
 {
 
        if (IMAP->selected == 0) {
-               lprintf(5,
+               lprintf(CTDL_ERR,
                        "imap_load_msgids() can't run; no room selected\n");
                return;
        }
@@ -181,7 +181,7 @@ void imap_load_msgids(void)
                           imap_add_single_msgid, NULL);
 
        imap_set_seen_flags();
-       lprintf(9, "imap_load_msgids() mapped %d messages\n",
+       lprintf(CTDL_DEBUG, "imap_load_msgids() mapped %d messages\n",
                IMAP->num_msgs);
 }
 
@@ -202,7 +202,7 @@ void imap_rescan_msgids(void)
 
 
        if (IMAP->selected == 0) {
-               lprintf(5,
+               lprintf(CTDL_ERR,
                        "imap_load_msgids() can't run; no room selected\n");
                return;
        }
@@ -212,7 +212,7 @@ void imap_rescan_msgids(void)
         */
        cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
        if (cdbfr != NULL) {
-               msglist = mallok(cdbfr->len);
+               msglist = malloc(cdbfr->len);
                memcpy(msglist, cdbfr->ptr, cdbfr->len);
                num_msgs = cdbfr->len / sizeof(long);
                cdb_free(cdbfr);
@@ -285,7 +285,7 @@ void imap_rescan_msgids(void)
        }
 
        if (num_msgs != 0)
-               phree(msglist);
+               free(msglist);
 }
 
 
@@ -305,10 +305,23 @@ void imap_cleanup_function(void)
        if (CC->h_command_function != imap_command_loop)
                return;
 
-       lprintf(9, "Performing IMAP cleanup hook\n");
+       lprintf(CTDL_DEBUG, "Performing IMAP cleanup hook\n");
        imap_free_msgids();
        imap_free_transmitted_message();
-       lprintf(9, "Finished IMAP cleanup hook\n");
+
+       if (IMAP->cached_fetch != NULL) {
+               fclose(IMAP->cached_fetch);
+               IMAP->cached_fetch = NULL;
+               IMAP->cached_msgnum = (-1);
+       }
+
+       if (IMAP->cached_body != NULL) {
+               fclose(IMAP->cached_body);
+               IMAP->cached_body = NULL;
+               IMAP->cached_bodymsgnum = (-1);
+       }
+
+       lprintf(CTDL_DEBUG, "Finished IMAP cleanup hook\n");
 }
 
 
@@ -322,6 +335,8 @@ void imap_greeting(void)
        strcpy(CC->cs_clientname, "IMAP session");
        CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
        IMAP->authstate = imap_as_normal;
+       IMAP->cached_fetch = NULL;
+       IMAP->cached_msgnum = (-1);
 
        cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
                config.c_fqdn);
@@ -409,7 +424,7 @@ void imap_auth_login_pass(char *cmd)
  */
 void imap_capability(int num_parms, char *parms[])
 {
-       cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN");
+       cprintf("* CAPABILITY IMAP4 IMAP4REV1 NAMESPACE AUTH=LOGIN");
 
 #ifdef HAVE_OPENSSL
        cprintf(" STARTTLS");
@@ -548,7 +563,7 @@ int imap_do_expunge(void)
        int i;
        int num_expunged = 0;
 
-       lprintf(9, "imap_do_expunge() called\n");
+       lprintf(CTDL_DEBUG, "imap_do_expunge() called\n");
        if (IMAP->selected == 0)
                return (0);
 
@@ -558,10 +573,10 @@ int imap_do_expunge(void)
                                CtdlDeleteMessages(CC->room.QRname,
                                                   IMAP->msgids[i], "");
                                ++num_expunged;
-                               lprintf(9, "%ld ... deleted\n",
+                               lprintf(CTDL_DEBUG, "%ld ... deleted\n",
                                        IMAP->msgids[i]);
                        } else {
-                               lprintf(9, "%ld ... not deleted\n",
+                               lprintf(CTDL_DEBUG, "%ld ... not deleted\n",
                                        IMAP->msgids[i]);
                        }
                }
@@ -602,6 +617,44 @@ void imap_close(int num_parms, char *parms[])
 }
 
 
+/*
+ * Implements the NAMESPACE command.
+ */
+void imap_namespace(int num_parms, char *parms[])
+{
+       int i;
+       struct floor *fl;
+       int floors = 0;
+       char buf[SIZ];
+
+       cprintf("* NAMESPACE ");
+
+       /* All personal folders are subordinate to INBOX. */
+       cprintf("((\"INBOX|\" \"|\")) ");
+
+       /* Other users' folders ... coming soon! FIXME */
+       cprintf("NIL ");
+
+       /* Show all floors as shared namespaces.  Neato! */
+       cprintf("(");
+       for (i = 0; i < MAXFLOORS; ++i) {
+               fl = cgetfloor(i);
+               if (fl->f_flags & F_INUSE) {
+                       if (floors > 0) cprintf(" ");
+                       cprintf("(");
+                       sprintf(buf, "%s|", fl->f_name);
+                       imap_strout(buf);
+                       cprintf(" \"|\")");
+                       ++floors;
+               }
+       }
+       cprintf(")");
+
+       /* Wind it up with a newline and a completion message. */
+       cprintf("\r\n");
+       cprintf("%s OK NAMESPACE completed\r\n", parms[0]);
+}
+
 
 
 /*
@@ -746,6 +799,7 @@ void imap_create(int num_parms, char *parms[])
        if (strchr(parms[2], '\\') != NULL) {
                cprintf("%s NO Invalid character in folder name\r\n",
                        parms[0]);
+               lprintf(CTDL_DEBUG, "invalid character in folder name\n");
                return;
        }
 
@@ -753,6 +807,7 @@ void imap_create(int num_parms, char *parms[])
        if (ret < 0) {
                cprintf("%s NO Invalid mailbox name or location\r\n",
                        parms[0]);
+               lprintf(CTDL_DEBUG, "invalid mailbox name or location\n");
                return;
        }
        floornum = (ret & 0x00ff);      /* lower 8 bits = floor number */
@@ -761,6 +816,7 @@ void imap_create(int num_parms, char *parms[])
        if (flags & IR_MAILBOX) {
                if (strncasecmp(parms[2], "INBOX|", 6)) {
                        cprintf("%s NO Personal folders must be created under INBOX\r\n", parms[0]);
+                       lprintf(CTDL_DEBUG, "not subordinate to inbox\n");
                        return;
                }
        }
@@ -771,7 +827,7 @@ void imap_create(int num_parms, char *parms[])
                newroomtype = 0;        /* public folder */
        }
 
-       lprintf(7, "Create new room <%s> on floor <%d> with type <%d>\n",
+       lprintf(CTDL_INFO, "Create new room <%s> on floor <%d> with type <%d>\n",
                roomname, floornum, newroomtype);
 
        ret = create_room(roomname, newroomtype, "", floornum, 1, 0);
@@ -782,6 +838,7 @@ void imap_create(int num_parms, char *parms[])
        } else {
                cprintf("%s OK CREATE completed\r\n", parms[0]);
        }
+       lprintf(CTDL_DEBUG, "imap_create() completed\n");
 }
 
 
@@ -1064,7 +1121,7 @@ void imap_rename_backend(struct ctdlroom *qrbuf, void *data)
                                         sizeof newroomname,
                                         newfoldername) & 0xFF;
 
-               irlp = (struct irl *) mallok(sizeof(struct irl));
+               irlp = (struct irl *) malloc(sizeof(struct irl));
                strcpy(irlp->irl_newroom, newroomname);
                strcpy(irlp->irl_oldroom, qrbuf->QRname);
                irlp->irl_newfloor = newfloor;
@@ -1152,12 +1209,12 @@ void imap_rename(int num_parms, char *parms[])
                                           irl->irl_newfloor);
                        if (r != crr_ok) {
                                /* FIXME handle error returns better */
-                               lprintf(5, "CtdlRenameRoom() error %d\n",
+                               lprintf(CTDL_ERR, "CtdlRenameRoom() error %d\n",
                                        r);
                        }
                        irlp = irl;
                        irl = irl->next;
-                       phree(irlp);
+                       free(irlp);
                }
        }
 
@@ -1179,12 +1236,12 @@ void imap_command_loop(void)
        time(&CC->lastcmd);
        memset(cmdbuf, 0, sizeof cmdbuf);       /* Clear it, just in case */
        if (client_gets(cmdbuf) < 1) {
-               lprintf(3, "IMAP socket is broken.  Ending session.\r\n");
+               lprintf(CTDL_ERR, "IMAP socket is broken.  Ending session.\r\n");
                CC->kill_me = 1;
                return;
        }
 
-       lprintf(5, "IMAP: %s\r\n", cmdbuf);
+       lprintf(CTDL_INFO, "IMAP: %s\r\n", cmdbuf);
        while (strlen(cmdbuf) < 5)
                strcat(cmdbuf, " ");
 
@@ -1313,6 +1370,10 @@ void imap_command_loop(void)
                imap_append(num_parms, parms);
        }
 
+       else if (!strcasecmp(parms[1], "NAMESPACE")) {
+               imap_namespace(num_parms, parms);
+       }
+
        else if (IMAP->selected == 0) {
                cprintf("%s BAD no folder selected\r\n", parms[0]);
        }