#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;
}
// 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();
// 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")) {
}
if (e == NULL) {
e = malloc(20);
- sprintf(e, "%ld", msglist[i]);
+ sprintf(e, "%ld", m);
}
StrBufAppendPrintf(Buf, "<D:response>");
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