stable now but there are GIANT PIECES MISSING
[citadel.git] / citadel / modules / imap / imap_metadata.c
index b8a8979676476cbc3f631e2190800bca45c0a7a7..5d5e95f9c0225695f48621e62f3eb1ee5e4e0c90 100644 (file)
@@ -1,25 +1,23 @@
 /*
- * $Id$
- *
  * IMAP METADATA extension
  *
  * This is an implementation of the Bynari variant of the METADATA extension.
  *
- * Copyright (c) 2007-2009 by the citadel.org team
+ * Copyright (c) 2007-2017 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 open source 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.
+ * 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
+ * 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
  */
 
 
 #include <pwd.h>
 #include <errno.h>
 #include <sys/types.h>
-
-#if TIME_WITH_SYS_TIME
-# include <sys/time.h>
-# include <time.h>
-#else
-# if HAVE_SYS_TIME_H
-#  include <sys/time.h>
-# else
-#  include <time.h>
-# endif
-#endif
-
+#include <time.h>
 #include <sys/wait.h>
 #include <ctype.h>
 #include <string.h>
 #include "support.h"
 #include "config.h"
 #include "user_ops.h"
-#include "policy.h"
 #include "database.h"
 #include "msgbase.h"
 #include "internet_addressing.h"
-#include "imap_tools.h"
 #include "serv_imap.h"
+#include "imap_tools.h"
 #include "imap_fetch.h"
 #include "imap_misc.h"
 #include "genstamp.h"
@@ -84,10 +70,10 @@ void imap_setmetadata(int num_parms, ConstStr *Params) {
        int setting_user_value = 0;
        char set_value[32];
        int set_view = VIEW_BBS;
-       struct visit vbuf;
+       visit vbuf;
 
        if (num_parms != 6) {
-               cprintf("%s BAD usage error\r\n", Params[0].Key);
+               IReply("BAD usage error");
                return;
        }
 
@@ -95,7 +81,7 @@ void imap_setmetadata(int num_parms, ConstStr *Params) {
         * Don't allow other types of metadata to be set
         */
        if (strcasecmp(Params[3].Key, "/vendor/kolab/folder-type")) {
-               cprintf("%s NO [METADATA TOOMANY] SETMETADATA failed\r\n", Params[0].Key);
+               IReply("NO [METADATA TOOMANY] SETMETADATA failed");
                return;
        }
 
@@ -106,7 +92,7 @@ void imap_setmetadata(int num_parms, ConstStr *Params) {
                setting_user_value = 1;                         /* per-user view */
        }
        else {
-               cprintf("%s NO [METADATA TOOMANY] SETMETADATA failed\r\n", Params[0].Key);
+               IReply("NO [METADATA TOOMANY] SETMETADATA failed");
                return;
        }
 
@@ -139,8 +125,7 @@ void imap_setmetadata(int num_parms, ConstStr *Params) {
 
        ret = imap_grabroom(roomname, Params[2].Key, 1);
        if (ret != 0) {
-               cprintf("%s NO Invalid mailbox name or access denied\r\n",
-                       Params[0].Key);
+               IReply("NO Invalid mailbox name or access denied");
                return;
        }
 
@@ -151,7 +136,7 @@ void imap_setmetadata(int num_parms, ConstStr *Params) {
        if (IMAP->selected) {
                strcpy(savedroom, CC->room.QRname);
        }
-       CtdlUserGoto(roomname, 0, 0, &msgs, &new);
+       CtdlUserGoto(roomname, 0, 0, &msgs, &new, NULL, NULL);
 
        /*
         * Always set the per-user view to the requested one.
@@ -164,7 +149,7 @@ void imap_setmetadata(int num_parms, ConstStr *Params) {
 
        if (setting_user_value)
        {
-               cprintf("%s OK SETANNOTATION complete\r\n", Params[0].Key);
+               IReply("OK SETANNOTATION complete");
        }
 
        /* If this is a "value.shared" set operation, we are allowed to perform it
@@ -179,19 +164,19 @@ void imap_setmetadata(int num_parms, ConstStr *Params) {
                CtdlGetRoomLock(&CC->room, CC->room.QRname);
                CC->room.QRdefaultview = set_view;
                CtdlPutRoomLock(&CC->room);
-               cprintf("%s OK SETANNOTATION complete\r\n", Params[0].Key);
+               IReply("OK SETANNOTATION complete");
        }
 
        /* If we got to this point, we don't have permission to set the default view. */
        else {
-               cprintf("%s NO [METADATA TOOMANY] SETMETADATA failed\r\n", Params[0].Key);
+               IReply("NO [METADATA TOOMANY] SETMETADATA failed");
        }
 
        /*
         * If a different folder was previously selected, return there now.
         */
        if ( (IMAP->selected) && (strcasecmp(roomname, savedroom)) ) {
-               CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
+               CtdlUserGoto(savedroom, 0, 0, &msgs, &new, NULL, NULL);
        }
        return;
 }
@@ -208,16 +193,18 @@ void imap_getmetadata(int num_parms, ConstStr *Params) {
        char savedroom[ROOMNAMELEN];
        int msgs, new;
        int ret;
+       int found = 0;
 
+/* this doesn't work if you have rooms/floors with spaces. 
+   we need this for the bynari connector.
        if (num_parms > 5) {
-               cprintf("%s BAD usage error\r\n", Params[0].Key);
+               IReply("BAD usage error");
                return;
        }
-
+*/
        ret = imap_grabroom(roomname, Params[2].Key, 1);
        if (ret != 0) {
-               cprintf("%s NO Invalid mailbox name or access denied\r\n",
-                       Params[0].Key);
+               IReply("NO Invalid mailbox name or access denied");
                return;
        }
 
@@ -228,34 +215,44 @@ void imap_getmetadata(int num_parms, ConstStr *Params) {
        if (IMAP->selected) {
                strcpy(savedroom, CC->room.QRname);
        }
-       CtdlUserGoto(roomname, 0, 0, &msgs, &new);
+       CtdlUserGoto(roomname, 0, 0, &msgs, &new, NULL, NULL);
 
-       cprintf("* METADATA ");
-       imap_strout(&Params[2]);
-       cprintf(" \"/vendor/kolab/folder-type\" (\"value.shared\" \"");
+       IAPuts("* METADATA ");
+       IPutCParamStr(2);
+       IAPuts(" \"/vendor/kolab/folder-type\" (\"value.shared\" \"");
 
        /* If it's one of our hard-coded default rooms, we know what to do... */
 
-       if (!strcasecmp(&CC->room.QRname[11], MAILROOM)) {
-               cprintf("mail.inbox");
-       }
-       else if (!strcasecmp(&CC->room.QRname[11], SENTITEMS)) {
-               cprintf("mail.sentitems");
-       }
-       else if (!strcasecmp(&CC->room.QRname[11], USERDRAFTROOM)) {
-               cprintf("mail.drafts");
-       }
-       else if (!strcasecmp(&CC->room.QRname[11], USERCALENDARROOM)) {
-               cprintf("event.default");
-       }
-       else if (!strcasecmp(&CC->room.QRname[11], USERCONTACTSROOM)) {
-               cprintf("contact.default");
-       }
-       else if (!strcasecmp(&CC->room.QRname[11], USERNOTESROOM)) {
-               cprintf("note.default");
-       }
-       else if (!strcasecmp(&CC->room.QRname[11], USERTASKSROOM)) {
-               cprintf("task.default");
+       if (CC->room.QRname[10] == '.')
+       {
+               if (!strcasecmp(&CC->room.QRname[11], MAILROOM)) {
+                       found = 1;
+                       IAPuts("mail.inbox");
+               }
+               else if (!strcasecmp(&CC->room.QRname[11], SENTITEMS)) {
+                       found = 1;
+                       IAPuts("mail.sentitems");
+               }
+               else if (!strcasecmp(&CC->room.QRname[11], USERDRAFTROOM)) {
+                       found = 1;
+                       IAPuts("mail.drafts");
+               }
+               else if (!strcasecmp(&CC->room.QRname[11], USERCALENDARROOM)) {
+                       found = 1;
+                       IAPuts("event.default");
+               }
+               else if (!strcasecmp(&CC->room.QRname[11], USERCONTACTSROOM)) {
+                       found = 1;
+                       IAPuts("contact.default");
+               }
+               else if (!strcasecmp(&CC->room.QRname[11], USERNOTESROOM)) {
+                       found = 1;
+                       IAPuts("note.default");
+               }
+               else if (!strcasecmp(&CC->room.QRname[11], USERTASKSROOM)) {
+                       found = 1;
+                       IAPuts("task.default");
+               }
        }
 
        /* Otherwise, use the view for this room to determine the type of data.
@@ -264,40 +261,42 @@ void imap_getmetadata(int num_parms, ConstStr *Params) {
         * user's view might only make changes to presentation.  It also saves us
         * an extra database access because we don't need to load the visit record.
         */
-
-       else if (CC->room.QRdefaultview == VIEW_CALENDAR) {
-               cprintf("event");
-       }
-       else if (CC->room.QRdefaultview == VIEW_ADDRESSBOOK) {
-               cprintf("contact");
-       }
-       else if (CC->room.QRdefaultview == VIEW_TASKS) {
-               cprintf("task");
-       }
-       else if (CC->room.QRdefaultview == VIEW_NOTES) {
-               cprintf("note");
-       }
-       else if (CC->room.QRdefaultview == VIEW_JOURNAL) {
-               cprintf("journal");
+       if (!found)
+       {
+               if (CC->room.QRdefaultview == VIEW_CALENDAR) {
+                       IAPuts("event");
+               }
+               else if (CC->room.QRdefaultview == VIEW_ADDRESSBOOK) {
+                       IAPuts("contact");
+               }
+               else if (CC->room.QRdefaultview == VIEW_TASKS) {
+                       IAPuts("task");
+               }
+               else if (CC->room.QRdefaultview == VIEW_NOTES) {
+                       IAPuts("note");
+               }
+               else if (CC->room.QRdefaultview == VIEW_JOURNAL) {
+                       IAPuts("journal");
+               }
        }
-
        /* If none of the above conditions were met, consider it an ordinary mailbox. */
-       else {
-               cprintf("mail");
+
+       if (!found) {
+               IAPuts("mail");
        }
 
        /* "mail.outbox" and "junkemail" are not implemented. */
 
-       cprintf("\")\r\n");
+       IAPuts("\")\r\n");
 
        /*
         * If a different folder was previously selected, return there now.
         */
        if ( (IMAP->selected) && (strcasecmp(roomname, savedroom)) ) {
-               CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
+               CtdlUserGoto(savedroom, 0, 0, &msgs, &new, NULL, NULL);
        }
 
-       cprintf("%s OK GETMETADATA complete\r\n", Params[0].Key);
+       IReply("OK GETMETADATA complete");
        return;
 }