//--------------------------------------------------------------------------------------// void Http_getMailboxes(T R) { const char *mailbox = Request_getId(R); TRACE(TRACE_DEBUG,"mailbox [%s]", mailbox); char *endptr = NULL; struct evbuffer *buf; uint64_t id = 0; if (! mailbox) { Request_error(R, HTTP_SERVUNAVAIL, "Server error"); return; } if (! (id = strtoull(mailbox, &endptr, 10))) { Request_error(R, HTTP_NOTFOUND, "Not found"); return; } TRACE(TRACE_DEBUG,"mailbox id [%lu]", id); buf = evbuffer_new(); Request_setContentType(R,"application/json; charset=utf-8"); if (Request_getMethod(R) == NULL) { /* * retrieve mailbox meta-data * C < GET /mailboxes/876 * * or * * append a new message * C < POST /mailboxes/876 */ const char *msg; uint64_t msg_id = 0; MailboxState_T b = MailboxState_new(id); unsigned exists = MailboxState_getExists(b); if ((msg = evhttp_find_header(Request_getPOST(R),"message"))) { if (! db_append_msg(msg, MailboxState_getId(b), MailboxState_getOwner(b), NULL, &msg_id, TRUE)) exists++; } evbuffer_add_printf(buf, "{\"mailboxes\": {\n"); evbuffer_add_printf(buf, " \"%lu\":{\"name\":\"%s\",\"exists\":%d}", MailboxState_getId(b), MailboxState_getName(b), exists); evbuffer_add_printf(buf, "\n}}\n"); MailboxState_free(&b); } else if (MATCH(Request_getMethod(R),"messages")) { /* * list messages in mailbox * C < GET /mailboxes/876/messages */ MailboxState_T b = MailboxState_new(id); GTree *msns = MailboxState_getMsn(b); GList *ids = g_tree_keys(msns); GTree *msginfo = MailboxState_getMsginfo(b); evbuffer_add_printf(buf, "{\"messages\": {\n"); while (ids && ids->data) { uint64_t *msn = (uint64_t *)ids->data; uint64_t *uid = (uint64_t *)g_tree_lookup(msns, msn); MessageInfo *info = (MessageInfo *)g_tree_lookup(msginfo, uid); evbuffer_add_printf(buf, " \"%lu\":{\"size\":%lu}", *uid, info->rfcsize); if (! g_list_next(ids)) break; ids = g_list_next(ids); evbuffer_add_printf(buf,",\n"); } evbuffer_add_printf(buf, "\n}}\n"); if (ids) g_list_free(g_list_first(ids)); MailboxState_free(&b); } if (EVBUFFER_LENGTH(buf)) Request_send(R, HTTP_OK, "OK", buf); else Request_error(R, HTTP_SERVUNAVAIL, "Server error"); evbuffer_free(buf); }
GTree * MailboxState_get_set(MailboxState_T M, const char *set, gboolean uid) { GTree *inset, *a, *b; GList *sets = NULL; GString *t; uint64_t lo = 0, hi = 0; gboolean error = FALSE; if (uid) inset = MailboxState_getIds(M); else inset = MailboxState_getMsn(M); a = g_tree_new_full((GCompareDataFunc)ucmpdata,NULL, (GDestroyNotify)uint64_free, (GDestroyNotify)uint64_free); b = g_tree_new_full((GCompareDataFunc)ucmpdata,NULL, (GDestroyNotify)uint64_free, (GDestroyNotify)uint64_free); if (! uid) { lo = 1; hi = MailboxState_getExists(M); } else { GList *ids = g_tree_keys(inset); if (ids) { ids = g_list_last(ids); hi = *((uint64_t *)ids->data); ids = g_list_first(ids); lo = *((uint64_t *)ids->data); g_list_free(g_list_first(ids)); } } t = g_string_new(set); sets = g_string_split(t,","); g_string_free(t,TRUE); sets = g_list_first(sets); while(sets) { uint64_t l = 0, r = 0; char *rest = (char *)sets->data; if (strlen(rest) < 1) break; if (g_tree_nnodes(inset) == 0) { // empty box if (rest[0] == '*') { uint64_t *k = mempool_pop(small_pool, sizeof(uint64_t)); uint64_t *v = mempool_pop(small_pool, sizeof(uint64_t)); *k = 1; *v = MailboxState_getUidnext(M); g_tree_insert(b, k, v); } else { if (! (l = dm_strtoull(sets->data, &rest, 10))) { error = TRUE; break; } if (rest[0]) { if (rest[0] != ':') { error = TRUE; break; } rest++; if ((rest[0] != '*') && (! dm_strtoull(rest, NULL, 10))) { error = TRUE; break; } } uint64_t *k = mempool_pop(small_pool, sizeof(uint64_t)); uint64_t *v = mempool_pop(small_pool, sizeof(uint64_t)); *k = 1; *v = MailboxState_getUidnext(M); g_tree_insert(b, k, v); } } else { if (rest[0] == '*') { l = hi; r = l; if (strlen(rest) > 1) rest++; } else { if (! (l = dm_strtoull(sets->data,&rest,10))) { error = TRUE; break; } if (l == 0xffffffff) l = hi; // outlook l = max(l,lo); r = l; } if (rest[0]==':') { if (strlen(rest)>1) rest++; if (rest[0] == '*') r = hi; else { if (! (r = dm_strtoull(rest,NULL,10))) { error = TRUE; break; } if (r == 0xffffffff) r = hi; // outlook } if (!r) break; } if (! (l && r)) break; find_range(inset, min(l,r), max(l,r), a, uid); if (g_tree_merge(b,a,IST_SUBSEARCH_OR)) { error = TRUE; TRACE(TRACE_ERR, "cannot compare null trees"); break; } } if (! g_list_next(sets)) break; sets = g_list_next(sets); } g_list_destroy(sets); if (a) g_tree_destroy(a); if (error) { g_tree_destroy(b); b = NULL; TRACE(TRACE_DEBUG, "return NULL"); } return b; }