Skeleton code for filters.
[citadel.git] / webcit-ng / server / room_functions.c
index 1b5d9335b2665f4d9e0dc806b9ac1ba04a5fad8d..09e7bac39f307cd41c3cde7e04028c55a0992648 100644 (file)
@@ -1,39 +1,32 @@
 // Room functions
 //
-// Copyright (c) 1996-2023 by the citadel.org team
+// Copyright (c) 1996-2024 by the citadel.org team
 //
-// This program is open source software.  Use, duplication, or
-// disclosure are subject to the GNU General Public License v3.
+// This program is open source software.  Use, duplication, or disclosure is subject to the GNU General Public License v3.
 
 #include "webcit.h"
 
 
-// Return a "zero-terminated" array of message numbers in the current room.
+// Return an array of message numbers in the current room.
 // Caller owns the memory and must free it.  Returns NULL if any problems.
-long *get_msglist(struct ctdlsession *c, char *which_msgs) {
+Array *get_msglist(struct ctdlsession *c, char *which_msgs) {
        char buf[1024];
-       long *msglist = NULL;
-       int num_msgs = 0;
-       int num_alloc = 0;
+       Array *msglist = NULL;
+
+       msglist = array_new(sizeof(long));
+       if (msglist == NULL) {
+               return(NULL);
+       }
 
        ctdl_printf(c, "MSGS %s", which_msgs);
        ctdl_readline(c, buf, sizeof(buf));
        if (buf[0] == '1') {
-               do {
-                       if (num_msgs >= num_alloc) {
-                               if (num_alloc == 0) {
-                                       num_alloc = 1024;
-                                       msglist = malloc(num_alloc * sizeof(long));
-                               }
-                               else {
-                                       num_alloc *= 2;
-                                       msglist = realloc(msglist, num_alloc * sizeof(long));
-                               }
-                       }
-                       ctdl_readline(c, buf, sizeof(buf));
-                       msglist[num_msgs++] = atol(buf);
-               } while (strcmp(buf, "000"));   // this makes the last element a "0" terminator
+               while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) { 
+                       long m = atol(buf);
+                       array_append(msglist, &m);
+               }
        }
+
        return msglist;
 }
 
@@ -111,18 +104,21 @@ void json_mailbox(struct http_transaction *h, struct ctdlsession *c) {
        ctdl_readline(c, buf, sizeof(buf));
        if (buf[0] == '1') {
                while (ctdl_readline(c, buf, sizeof(buf)), (strcmp(buf, "000"))) {
-                       utf8ify_rfc822_string(buf);
                        JsonValue *jmsg = NewJsonObject(HKEY("message"));
                        JsonObjectAppend(jmsg, NewJsonNumber(HKEY("msgnum"), extract_long(buf, 0)));
                        JsonObjectAppend(jmsg, NewJsonNumber(HKEY("time"), extract_long(buf, 1)));
                        extract_token(field, buf, 2, '|', sizeof field);
+                       utf8ify_rfc822_string(field);
                        JsonObjectAppend(jmsg, NewJsonPlainString(HKEY("author"), field, -1));
                        extract_token(field, buf, 4, '|', sizeof field);
+                       utf8ify_rfc822_string(field);
                        JsonObjectAppend(jmsg, NewJsonPlainString(HKEY("addr"), field, -1));
                        extract_token(field, buf, 5, '|', sizeof field);
+                       utf8ify_rfc822_string(field);
                        JsonObjectAppend(jmsg, NewJsonPlainString(HKEY("subject"), field, -1));
                        JsonObjectAppend(jmsg, NewJsonNumber(HKEY("msgidhash"), extract_long(buf, 6)));
                        extract_token(field, buf, 7, '|', sizeof field);
+                       utf8ify_rfc822_string(field);
                        JsonObjectAppend(jmsg, NewJsonPlainString(HKEY("references"), field, -1));
                        JsonArrayAppend(j, jmsg);               // add the message to the array
                }
@@ -143,14 +139,16 @@ void json_mailbox(struct http_transaction *h, struct ctdlsession *c) {
 // Client is requesting a message list
 void json_msglist(struct http_transaction *h, struct ctdlsession *c, char *which) {
        int i = 0;
-       long *msglist = get_msglist(c, which);
+       Array *msglist = get_msglist(c, which);
        JsonValue *j = NewJsonArray(HKEY("msgs"));
 
        if (msglist != NULL) {
-               for (i = 0; msglist[i] > 0; ++i) {
-                       JsonArrayAppend(j, NewJsonNumber(HKEY("m"), msglist[i]));
+               for (i = 0; i < array_len(msglist); ++i) {
+                       long m;
+                       memcpy(&m, array_get_element_at(msglist, i), sizeof(long));
+                       JsonArrayAppend(j, NewJsonNumber(HKEY("m"), m));
                }
-               free(msglist);
+               array_free(msglist);
        }
 
        StrBuf *sj = NewStrBuf();
@@ -415,17 +413,21 @@ void propfind_the_room_itself(struct http_transaction *h, struct ctdlsession *c)
        // If a depth greater than zero was specified, transmit the collection listing
        // BEGIN COLLECTION
        if (dav_depth > 0) {
-               long *msglist = get_msglist(c, "ALL");
+               Array *msglist = get_msglist(c, "ALL");
                if (msglist) {
                        int i;
-                       for (i = 0; (msglist[i] > 0); ++i) {
-                               if ((i % 10) == 0)
+                       for (i = 0; i < array_len(msglist); ++i) {
+                               if ((i % 10) == 0) {
                                        syslog(LOG_DEBUG, "PROPFIND enumerated %d messages", i);
-                               e = NULL;       // EUID gets stored here
+                               }
+                               e = NULL;               // EUID gets stored here
                                timestamp = 0;
 
+                               long m;
+                               memcpy(&m, array_get_element_at(msglist, i), sizeof(long));
+
                                char cbuf[1024];
-                               ctdl_printf(c, "MSG0 %ld|3", msglist[i]);
+                               ctdl_printf(c, "MSG0 %ld|3", m);
                                ctdl_readline(c, cbuf, sizeof(cbuf));
                                if (cbuf[0] == '1')
                                        while (ctdl_readline(c, cbuf, sizeof(cbuf)), strcmp(cbuf, "000")) {
@@ -441,7 +443,7 @@ void propfind_the_room_itself(struct http_transaction *h, struct ctdlsession *c)
                                        }
                                if (e == NULL) {
                                        e = malloc(20);
-                                       sprintf(e, "%ld", msglist[i]);
+                                       sprintf(e, "%ld", m);
                                }
                                StrBufAppendPrintf(Buf, "<D:response>");
 
@@ -480,13 +482,13 @@ void propfind_the_room_itself(struct http_transaction *h, struct ctdlsession *c)
                                                free(datestring);
                                        }
                                        if (enumerate_by_euid) {        // FIXME ajc 2017oct30 should this be inside the timestamp conditional?
-                                               StrBufAppendPrintf(Buf, "<D:getetag>\"%ld\"</D:getetag>", msglist[i]);
+                                               StrBufAppendPrintf(Buf, "<D:getetag>\"%ld\"</D:getetag>", m);
                                        }
                                }
                                StrBufAppendPrintf(Buf, "</D:prop></D:propstat></D:response>\n");
                                free(e);
                        }
-                       free(msglist);
+                       array_free(msglist);
                };
        }
        // END COLLECTION