X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=webcit-ng%2Fserver%2Froom_functions.c;h=9153319c64d51beac9542ec3f535368de8763f4c;hb=372fb8eddb5c00a42314dd32f65f57394b90a7d2;hp=8d8ca3bcc5113c670c5a48161a5b630a39d1a858;hpb=bd435f1cde6721e2173619c881e2af8637c85482;p=citadel.git diff --git a/webcit-ng/server/room_functions.c b/webcit-ng/server/room_functions.c index 8d8ca3bcc..9153319c6 100644 --- a/webcit-ng/server/room_functions.c +++ b/webcit-ng/server/room_functions.c @@ -8,32 +8,26 @@ #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; } @@ -146,14 +140,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(); @@ -418,18 +414,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) { + 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 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")) { @@ -445,7 +444,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, ""); @@ -484,13 +483,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, "\"%ld\"", msglist[i]); + StrBufAppendPrintf(Buf, "\"%ld\"", m); } } StrBufAppendPrintf(Buf, "\n"); free(e); } - free(msglist); + array_free(msglist); }; } // END COLLECTION