stable now but there are GIANT PIECES MISSING
[citadel.git] / citadel / modules / imap / imap_acl.c
index 7ef289db5cc069f450b5a3570b8958c660311cab..05426de46c008417244222b63d2daf2bad300943 100644 (file)
@@ -1,12 +1,9 @@
 /*
- * $Id$
- *
  * Functions which implement RFC2086 (and maybe RFC4314) (IMAP ACL extension)
  *
+ * Copyright (c) 2007-2017 by the citadel.org team
  *
- * Copyright (c) 2007-2009 by the citadel.org team
- *
- *  This program is free software; you can redistribute it and/or modify
+ *  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.
@@ -21,7 +18,6 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-
 #include "sysdep.h"
 #include <stdlib.h>
 #include <unistd.h>
 #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>
@@ -55,7 +40,6 @@
 #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_fetch.h"
 #include "imap_misc.h"
 #include "genstamp.h"
-
-
 #include "ctdl_module.h"
 
+
 /*
  * Implements the SETACL command.
  */
-void imap_setacl(int num_parms, char *parms[]) {
+void imap_setacl(int num_parms, ConstStr *Params) {
 
-       cprintf("%s BAD not yet implemented FIXME\r\n", parms[0]);
+       IReply("BAD not yet implemented FIXME");
        return;
 }
 
@@ -81,9 +64,9 @@ void imap_setacl(int num_parms, char *parms[]) {
 /*
  * Implements the DELETEACL command.
  */
-void imap_deleteacl(int num_parms, char *parms[]) {
+void imap_deleteacl(int num_parms, ConstStr *Params) {
 
-       cprintf("%s BAD not yet implemented FIXME\r\n", parms[0]);
+       IReply("BAD not yet implemented FIXME");
        return;
 }
 
@@ -92,9 +75,9 @@ void imap_deleteacl(int num_parms, char *parms[]) {
  * Given the bits returned by CtdlRoomAccess(), populate a string buffer
  * with IMAP ACL format flags.   This code is common to GETACL and MYRIGHTS.
  */
-void imap_acl_flags(char *rights, int ra)
+void imap_acl_flags(StrBuf *rights, int ra)
 {
-       strcpy(rights, "");
+       FlushStrBuf(rights);
 
        /* l - lookup (mailbox is visible to LIST/LSUB commands)
         * r - read (SELECT the mailbox, perform STATUS et al)
@@ -102,10 +85,10 @@ void imap_acl_flags(char *rights, int ra)
         */
        if (    (ra & UA_KNOWN)                                 /* known rooms */
           ||   ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))     /* zapped rooms */
-          ) {
-               strcat(rights, "l");
-               strcat(rights, "r");
-               strcat(rights, "s");
+       ) {
+               StrBufAppendBufPlain(rights, HKEY("l"), 0);
+               StrBufAppendBufPlain(rights, HKEY("r"), 0);
+               StrBufAppendBufPlain(rights, HKEY("s"), 0);
 
                /* Only output the remaining flags if the room is known */
 
@@ -115,14 +98,14 @@ void imap_acl_flags(char *rights, int ra)
                /* p - post (send mail to submission address for mailbox - not enforced) */
                /* c - create (CREATE new sub-mailboxes) */
                if (ra & UA_POSTALLOWED) {
-                       strcat(rights, "i");
-                       strcat(rights, "p");
-                       strcat(rights, "c");
+                       StrBufAppendBufPlain(rights, HKEY("i"), 0);
+                       StrBufAppendBufPlain(rights, HKEY("p"), 0);
+                       StrBufAppendBufPlain(rights, HKEY("c"), 0);
                }
 
                /* d - delete messages (STORE DELETED flag, perform EXPUNGE) */
                if (ra & UA_DELETEALLOWED) {
-                       strcat(rights, "d");
+                       StrBufAppendBufPlain(rights, HKEY("d"), 0);
                }
 
                /* a - administer (perform SETACL/DELETEACL/GETACL/LISTRIGHTS) */
@@ -134,8 +117,9 @@ void imap_acl_flags(char *rights, int ra)
                         * commands are not yet implemented, omitting the flag should
                         * theoretically prevent compliant clients from attempting to
                         * perform them.
+                        *
+                        * StrBufAppendBufPlain(rights, HKEY("a"), 0);
                         */
-                       /* strcat(rights, "a"); * commented out */
                }
        }
 }
@@ -144,7 +128,7 @@ void imap_acl_flags(char *rights, int ra)
 /*
  * Implements the GETACL command.
  */
-void imap_getacl(int num_parms, char *parms[]) {
+void imap_getacl(int num_parms, ConstStr *Params) {
        char roomname[ROOMNAMELEN];
        char savedroom[ROOMNAMELEN];
        int msgs, new;
@@ -152,20 +136,19 @@ void imap_getacl(int num_parms, char *parms[]) {
        struct ctdluser temp;
        struct cdbdata *cdbus;
        int ra;
-       char rights[32];
+       StrBuf *rights;
 
        if (num_parms != 3) {
-               cprintf("%s BAD usage error\r\n", parms[0]);
+               IReply("BAD usage error");
                return;
        }
 
        /*
         * Search for the specified room or folder
         */
-       ret = imap_grabroom(roomname, parms[2], 1);
+       ret = imap_grabroom(roomname, Params[2].Key, 1);
        if (ret != 0) {
-               cprintf("%s NO Invalid mailbox name or access denied\r\n",
-                       parms[0]);
+               IReply("NO Invalid mailbox name or access denied");
                return;
        }
 
@@ -176,17 +159,18 @@ void imap_getacl(int num_parms, char *parms[]) {
        if (IMAP->selected) {
                strcpy(savedroom, CC->room.QRname);
        }
-       CtdlUserGoto(roomname, 0, 0, &msgs, &new);
+       CtdlUserGoto(roomname, 0, 0, &msgs, &new, NULL, NULL);
 
-       cprintf("* ACL");
-       cprintf(" ");
-       imap_strout(parms[2]);
+       IAPuts("* ACL ");
+       IPutCParamStr(2);
 
        /*
         * Traverse the userlist
         */
+       rights = NewStrBuf();
        cdb_rewind(CDB_USERS);
-       while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
+       while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) 
+       {
                memset(&temp, 0, sizeof temp);
                memcpy(&temp, cdbus->ptr, sizeof temp);
                cdb_free(cdbus);
@@ -194,51 +178,51 @@ void imap_getacl(int num_parms, char *parms[]) {
                CtdlRoomAccess(&CC->room, &temp, &ra, NULL);
                if (!IsEmptyStr(temp.fullname)) {
                        imap_acl_flags(rights, ra);
-                       if (!IsEmptyStr(rights)) {
-                               cprintf(" ");
-                               imap_strout(temp.fullname);
-                               cprintf(" %s", rights);
+                       if (StrLength(rights) > 0) {
+                               IAPuts(" ");
+                               IPutStr(temp.fullname, strlen(temp.fullname));
+                               IAPuts(" ");
+                               iaputs(SKEY( rights));
                        }
                }
        }
-
-       cprintf("\r\n");
+       FreeStrBuf(&rights);
+       IAPuts("\r\n");
 
        /*
         * If another folder is selected, go back to that room so we can resume
         * our happy day without violent explosions.
         */
        if (IMAP->selected) {
-               CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
+               CtdlUserGoto(savedroom, 0, 0, &msgs, &new, NULL, NULL);
        }
 
-       cprintf("%s OK GETACL completed\r\n", parms[0]);
+       IReply("OK GETACL completed");
 }
 
 
 /*
  * Implements the LISTRIGHTS command.
  */
-void imap_listrights(int num_parms, char *parms[]) {
+void imap_listrights(int num_parms, ConstStr *Params) {
        char roomname[ROOMNAMELEN];
        char savedroom[ROOMNAMELEN];
        int msgs, new;
        int ret;
-       struct recptypes *valid;
+       recptypes *valid;
        struct ctdluser temp;
 
        if (num_parms != 4) {
-               cprintf("%s BAD usage error\r\n", parms[0]);
+               IReply("BAD usage error");
                return;
        }
 
        /*
         * Search for the specified room/folder
         */
-       ret = imap_grabroom(roomname, parms[2], 1);
+       ret = imap_grabroom(roomname, Params[2].Key, 1);
        if (ret != 0) {
-               cprintf("%s NO Invalid mailbox name or access denied\r\n",
-                       parms[0]);
+               IReply("NO Invalid mailbox name or access denied");
                return;
        }
 
@@ -246,16 +230,15 @@ void imap_listrights(int num_parms, char *parms[]) {
         * Search for the specified user
         */
        ret = (-1);
-       valid = validate_recipients(parms[3], NULL, 0);
+       valid = validate_recipients(Params[3].Key, NULL, 0);
        if (valid != NULL) {
                if (valid->num_local == 1) {
-                       ret = getuser(&temp, valid->recp_local);
+                       ret = CtdlGetUser(&temp, valid->recp_local);
                }
                free_recipients(valid);
        }
        if (ret != 0) {
-               cprintf("%s NO Invalid user name or access denied\r\n",
-                       parms[0]);
+               IReply("NO Invalid user name or access denied");
                return;
        }
 
@@ -266,30 +249,28 @@ void imap_listrights(int num_parms, char *parms[]) {
        if (IMAP->selected) {
                strcpy(savedroom, CC->room.QRname);
        }
-       CtdlUserGoto(roomname, 0, 0, &msgs, &new);
-
+       CtdlUserGoto(roomname, 0, 0, &msgs, &new, NULL, NULL);
 
        /*
         * Now output the list of rights
         */
-       cprintf("* LISTRIGHTS ");
-       imap_strout(parms[2]);
-       cprintf(" ");
-       imap_strout(parms[3]);
-       cprintf(" ");
-       imap_strout("");                /* FIXME ... do something here */
-       cprintf("\r\n");
-
+       IAPuts("* LISTRIGHTS ");
+       IPutCParamStr(2);
+       IAPuts(" ");
+       IPutCParamStr(3);
+       IAPuts(" ");
+       IPutStr(HKEY(""));              /* FIXME ... do something here */
+       IAPuts("\r\n");
 
        /*
         * If another folder is selected, go back to that room so we can resume
         * our happy day without violent explosions.
         */
        if (IMAP->selected) {
-               CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
+               CtdlUserGoto(savedroom, 0, 0, &msgs, &new, NULL, NULL);
        }
 
-       cprintf("%s OK LISTRIGHTS completed\r\n", parms[0]);
+       IReply("OK LISTRIGHTS completed");
        return;
 }
 
@@ -297,23 +278,22 @@ void imap_listrights(int num_parms, char *parms[]) {
 /*
  * Implements the MYRIGHTS command.
  */
-void imap_myrights(int num_parms, char *parms[]) {
+void imap_myrights(int num_parms, ConstStr *Params) {
        char roomname[ROOMNAMELEN];
        char savedroom[ROOMNAMELEN];
        int msgs, new;
        int ret;
        int ra;
-       char rights[32];
+       StrBuf *rights;
 
        if (num_parms != 3) {
-               cprintf("%s BAD usage error\r\n", parms[0]);
+               IReply("BAD usage error");
                return;
        }
 
-       ret = imap_grabroom(roomname, parms[2], 1);
+       ret = imap_grabroom(roomname, Params[2].Key, 1);
        if (ret != 0) {
-               cprintf("%s NO Invalid mailbox name or access denied\r\n",
-                       parms[0]);
+               IReply("NO Invalid mailbox name or access denied");
                return;
        }
 
@@ -324,24 +304,27 @@ void imap_myrights(int num_parms, char *parms[]) {
        if (IMAP->selected) {
                strcpy(savedroom, CC->room.QRname);
        }
-       CtdlUserGoto(roomname, 0, 0, &msgs, &new);
+       CtdlUserGoto(roomname, 0, 0, &msgs, &new, NULL, NULL);
 
        CtdlRoomAccess(&CC->room, &CC->user, &ra, NULL);
+       rights = NewStrBuf();
        imap_acl_flags(rights, ra);
 
-       cprintf("* MYRIGHTS ");
-       imap_strout(parms[2]);
-       cprintf(" %s\r\n", rights);
+       IAPuts("* MYRIGHTS ");
+       IPutCParamStr(2);
+       IAPuts(" ");
+       IPutStr(SKEY(rights));
+       IAPuts("\r\n");
+
+       FreeStrBuf(&rights);
 
        /*
         * 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 MYRIGHTS completed\r\n", parms[0]);
+       IReply("OK MYRIGHTS completed");
        return;
 }
-
-