/* routing setup */ void Request_handle(T R) { if (R->controller) { if (MATCH(R->controller,"users")) R->cb = Http_getUsers; else if (MATCH(R->controller,"mailboxes")) R->cb = Http_getMailboxes; else if (MATCH(R->controller,"messages")) R->cb = Http_getMessages; } if (R->cb) { if (! Request_basic_auth(R)) return; Request_setContentType(R,"text/html; charset=utf-8"); R->cb(R); } else { const char *host = evhttp_find_header(R->req->input_headers, "Host"); char *url = g_strdup_printf("http://%s%s", host?host:"","/users/"); Request_header(R, "Location", url); g_free(url); Request_error(R, HTTP_MOVEPERM, "Not found"); } }
//--------------------------------------------------------------------------------------// void Http_getUsers(T R) { struct evbuffer *buf; char *username = NULL; uint64_t id = 0; if (Request_getId(R)) { /* * id can be specified both by name and number * * C < /users/testuser1 * C < /users/123 * */ if ((id = strtoull(Request_getId(R), NULL, 10))) username = auth_get_userid(id); else if (auth_user_exists(Request_getId(R), &id)) username = g_strdup(Request_getId(R)); if (! (username && id)) Request_error(R, HTTP_NOTFOUND, "User not found"); } buf = evbuffer_new(); if (Request_getMethod(R) == NULL) { GList *users = NULL; if (username) { MailboxState_T M; const char *mailbox; uint64_t mboxid; /* * retrieve user meta-data * C < /users/testuser1 * create/delete mailbox for user * POST C < /users/testuser1 */ if ((mailbox = evhttp_find_header(Request_getPOST(R),"create"))) { const char *message; if (db_mailbox_create_with_parents(mailbox, BOX_COMMANDLINE, id, &mboxid, &message)) { Request_error(R, HTTP_BADREQUEST, message); evbuffer_free(buf); return; } } if ((mailbox = evhttp_find_header(Request_getPOST(R),"delete"))) { int access; /* check if there is an attempt to delete inbox */ if (MATCH(mailbox, "INBOX")) { Request_error(R, HTTP_BADREQUEST, "NO cannot delete special mailbox INBOX"); evbuffer_free(buf); return; } if (! (db_findmailbox(mailbox, id, &mboxid)) ) { Request_error(R, HTTP_NOTFOUND, "NO mailbox doesn't exists"); evbuffer_free(buf); return; } /* Check if the user has ACL delete rights to this mailbox */ M = MailboxState_new(mboxid); access = acl_has_right(M, id, ACL_RIGHT_DELETE); if (access != 1) { Request_error(R, HTTP_BADREQUEST, "NO permission denied"); evbuffer_free(buf); return; } /* ok remove mailbox */ if (db_delete_mailbox(mboxid, 0, 1)) { Request_error(R, HTTP_SERVUNAVAIL, "NO delete failed"); evbuffer_free(buf); return; } } users = g_list_append_printf(users, "%s", username); } else { /* * list all users * C < /users/ * * create,edit,delete user * POST C < /users/ */ const char *user = NULL; if ((user = evhttp_find_header(Request_getPOST(R),"create"))) { const char *password, *encoding, *quota; password = evhttp_find_header(Request_getPOST(R), "password"); encoding = evhttp_find_header(Request_getPOST(R), "encoding"); quota = evhttp_find_header(Request_getPOST(R), "quota"); TRACE(TRACE_DEBUG, "create user: [%s] password: [%s] encoding [%s] quota [%s]", user, password, encoding, quota); } else if ((user = evhttp_find_header(Request_getPOST(R),"edit"))) { TRACE(TRACE_DEBUG, "edit user: [%s]", user); } else if ((user = evhttp_find_header(Request_getPOST(R),"delete"))) { TRACE(TRACE_DEBUG, "delete user: [%s]", user); } users = auth_get_known_users(); } Request_setContentType(R,"application/json; charset=utf-8"); evbuffer_add_printf(buf, "{\"users\": {\n"); while(users->data) { uint64_t id; if (auth_user_exists((char *)users->data, &id)) evbuffer_add_printf(buf, " \"%lu\":{\"name\":\"%s\"}", id, (char *)users->data); if (! g_list_next(users)) break; users = g_list_next(users); evbuffer_add_printf(buf,",\n"); } evbuffer_add_printf(buf, "\n}}\n"); g_list_destroy(users); } else if (MATCH(Request_getMethod(R),"mailboxes")) { GList *mailboxes = NULL; if (! username) { Request_error(R, HTTP_NOTFOUND, "User not found"); evbuffer_free(buf); return; } /* * list mailboxes for user * GET C < /users/testuser1/mailboxes * */ db_findmailbox_by_regex(id, "*", &mailboxes, FALSE); Request_setContentType(R,"application/json; charset=utf-8"); evbuffer_add_printf(buf, "{\"mailboxes\": {\n"); while (mailboxes->data) { MailboxState_T b = MailboxState_new(*((uint64_t *)mailboxes->data)); MailboxState_setOwner(b, id); //if (MailboxState_reload(b) == DM_SUCCESS) evbuffer_add_printf(buf, " \"%lu\":{\"name\":\"%s\",\"exists\":%u}", MailboxState_getId(b), MailboxState_getName(b), MailboxState_getExists(b)); MailboxState_free(&b); if (! g_list_next(mailboxes)) break; mailboxes = g_list_next(mailboxes); evbuffer_add_printf(buf,",\n"); } evbuffer_add_printf(buf, "\n}}\n"); } if (EVBUFFER_LENGTH(buf)) Request_send(R, HTTP_OK, "OK", buf); else Request_error(R, HTTP_SERVUNAVAIL, "Server error"); if (username) g_free(username); evbuffer_free(buf); }
void Http_getMessages(T R) { DbmailMessage *m = dbmail_message_new(); struct evbuffer *buf; uint64_t pid; uint64_t id = 0; if (! Request_getId(R)) return; if (! (id = strtoull(Request_getId(R), NULL, 10))) { Request_error(R, HTTP_NOTFOUND, "Not found"); return; } db_get_physmessage_id(id, &pid); if (! pid) { Request_error(R, HTTP_NOTFOUND, "Not found"); return; } buf = evbuffer_new(); m = dbmail_message_retrieve(m, pid); if (Request_getMethod(R) == NULL) { /* * retrieve message meta-data * C < GET /messages/1245911 */ uint64_t size = dbmail_message_get_size(m, TRUE); Request_setContentType(R,"application/json; charset=utf-8"); evbuffer_add_printf(buf, "{\"messages\": {\n"); evbuffer_add_printf(buf, " \"%lu\":{\"size\":%lu}", id, size); evbuffer_add_printf(buf, "\n}}\n"); } else if (MATCH(Request_getMethod(R), "view")) { /* * retrieve message by message_idnr * C < GET /messages/1245911/view */ char *s = dbmail_message_to_string(m); Request_setContentType(R, "message/rfc822; charset=utf-8"); evbuffer_add_printf(buf, "%s", s); g_free(s); } else if (MATCH(Request_getMethod(R),"headers")) { Request_setContentType(R, "text/plain; charset=utf-8"); if (Request_getArg(R) && strlen(Request_getArg(R))) { /* * retrieve selected message headers * C < GET /messages/1245911/headers/subject,from,to */ int i = 0; char **headerlist = g_strsplit(Request_getArg(R),",",0); while (headerlist[i]) { char *hname = headerlist[i]; hname[0] = g_ascii_toupper(hname[0]); TRACE(TRACE_DEBUG,"header: [%s]", headerlist[i]); GList * headers = dbmail_message_get_header_repeated(m, headerlist[i]); while(headers) { evbuffer_add_printf(buf, "%s: %s\n", hname, (char *)headers->data); if (! g_list_next(headers)) break; headers = g_list_next(headers); } i++; } } else { /* * retrieve all message headers * C < GET /messages/1245911/headers */ char *s = dbmail_message_hdrs_to_string(m); Request_setContentType(R, "text/plain; charset=utf-8"); evbuffer_add_printf(buf, "%s", s); g_free(s); } } if (EVBUFFER_LENGTH(buf)) Request_send(R, HTTP_OK, "OK", buf); else Request_error(R, HTTP_SERVUNAVAIL, "Server error"); evbuffer_free(buf); dbmail_message_free(m); }
//--------------------------------------------------------------------------------------// 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); }