4 * Copyright (c) 1996-2018 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
19 * Return a "zero-terminated" array of message numbers in the current room.
20 * Caller owns the memory and must free it. Returns NULL if any problems.
22 long *get_msglist(struct ctdlsession *c, char *which_msgs)
29 ctdl_printf(c, "MSGS %s", which_msgs);
30 ctdl_readline(c, buf, sizeof(buf));
33 if (num_msgs >= num_alloc) {
36 msglist = malloc(num_alloc * sizeof(long));
40 msglist = realloc(msglist, num_alloc * sizeof(long));
43 ctdl_readline(c, buf, sizeof(buf));
44 msglist[num_msgs++] = atol(buf);
45 } while (strcmp(buf, "000")); // this makes the last element a "0" terminator
51 * Supplied with a list of potential matches from an If-Match: or If-None-Match: header, and
52 * a message number (which we always use as the entity tag in Citadel), return nonzero if the
53 * message number matches any of the supplied tags in the string.
55 int match_etags(char *taglist, long msgnum)
57 int num_tags = num_tokens(taglist, ',');
61 if (msgnum <= 0) { // no msgnum? no match.
65 for (i=0; i<num_tags; ++i) {
66 extract_token(tag, taglist, i, ',', sizeof tag);
68 char *lq = (strchr(tag, '"'));
69 char *rq = (strrchr(tag, '"'));
70 if (lq < rq) { // has two double quotes
75 if (!strcmp(tag, "*")) {
76 return(1); // wildcard match
78 long tagmsgnum = atol(tag);
79 if ( (tagmsgnum > 0) && (tagmsgnum == msgnum) ) { // match
84 return(0); // no match
89 * Client is requesting a message list
91 void json_msglist(struct http_transaction *h, struct ctdlsession *c, char *which)
94 long *msglist = get_msglist(c, which);
95 JsonValue *j = NewJsonArray(HKEY("msgs"));
97 if (msglist != NULL) {
98 for (i=0; msglist[i]>0 ; ++i) {
99 JsonArrayAppend(j, NewJsonNumber( HKEY("m"), msglist[i]));
104 StrBuf *sj = NewStrBuf();
105 SerializeJson(sj, j, 1); // '1' == free the source array
107 add_response_header(h, strdup("Content-type"), strdup("application/json"));
108 h->response_code = 200;
109 h->response_string = strdup("OK");
110 h->response_body_length = StrLength(sj);
111 h->response_body = SmashStrBuf(&sj);
119 * Client requested an object in a room.
121 void object_in_room(struct http_transaction *h, struct ctdlsession *c)
125 char unescaped_euid[1024];
127 extract_token(buf, h->uri, 4, '/', sizeof buf);
129 if (!strncasecmp(buf, "msgs.", 5)) { // Client is requesting a list of message numbers
130 json_msglist(h, c, &buf[5]);
135 if (!strncasecmp(buf, "threads", 5)) { // Client is requesting a threaded view (still kind of fuzzy here)
136 threaded_view(h, c, &buf[5]);
140 if (!strncasecmp(buf, "flat", 5)) { // Client is requesting a flat view (still kind of fuzzy here)
141 flat_view(h, c, &buf[5]);
146 if ( (c->room_default_view == VIEW_CALENDAR) // room types where objects are referenced by EUID
147 || (c->room_default_view == VIEW_TASKS)
148 || (c->room_default_view == VIEW_ADDRESSBOOK)
150 safestrncpy(unescaped_euid, buf, sizeof unescaped_euid);
151 unescape_input(unescaped_euid);
152 msgnum = locate_message_by_uid(c, unescaped_euid);
159 * All methods except PUT require the message to already exist
161 if ( (msgnum <= 0) && (strcasecmp(h->method, "PUT")) ) {
166 * If we get to this point we have a valid message number in an accessible room.
168 syslog(LOG_DEBUG, "msgnum is %ld, method is %s", msgnum, h->method);
172 * Does the client want us to render the message for them?
174 // FIXME put that logic here
177 * Was the client actually requesting a specific component within the message?
179 if (num_tokens(h->uri, '/') == 6) {
180 extract_token(buf, h->uri, 5, '/', sizeof buf);
181 if (!IsEmptyStr(buf)) {
182 download_mime_component(h, c, msgnum, buf);
188 * Ok, we want a full message, but first let's check for the if[-none]-match headers.
190 char *if_match = header_val(h, "If-Match");
191 if ( (if_match != NULL) && (!match_etags(if_match, msgnum)) ) {
196 char *if_none_match = header_val(h, "If-None-Match");
197 if ( (if_none_match != NULL) && (match_etags(if_none_match, msgnum)) ) {
206 if (!strcasecmp(h->method, "DELETE")) {
207 dav_delete_message(h, c, msgnum);
209 else if (!strcasecmp(h->method, "GET")) {
210 dav_get_message(h, c, msgnum);
212 else if (!strcasecmp(h->method, "PUT")) {
213 dav_put_message(h, c, unescaped_euid, msgnum);
216 do_404(h); // Got this far but the method made no sense? Bummer.
223 * Called by the_room_itself() when the HTTP method is REPORT
225 void report_the_room_itself(struct http_transaction *h, struct ctdlsession *c)
227 if (c->room_default_view == VIEW_CALENDAR) {
228 caldav_report(h, c); // CalDAV REPORTs ... fmgwac
232 do_404(h); // future implementations like CardDAV will require code paths here
237 * Called by the_room_itself() when the HTTP method is OPTIONS
239 void options_the_room_itself(struct http_transaction *h, struct ctdlsession *c)
241 h->response_code = 200;
242 h->response_string = strdup("OK");
243 if (c->room_default_view == VIEW_CALENDAR) {
244 add_response_header(h, strdup("DAV"), strdup("1, calendar-access")); // offer CalDAV
246 else if (c->room_default_view == VIEW_ADDRESSBOOK) {
247 add_response_header(h, strdup("DAV"), strdup("1, addressbook")); // offer CardDAV
250 add_response_header(h, strdup("DAV"), strdup("1")); // ordinary WebDAV for all other room types
252 add_response_header(h, strdup("Allow"), strdup("OPTIONS, PROPFIND, GET, PUT, REPORT, DELETE"));
257 * Called by the_room_itself() when the HTTP method is PROPFIND
259 void propfind_the_room_itself(struct http_transaction *h, struct ctdlsession *c)
263 int dav_depth = (header_val(h, "Depth") ? atoi(header_val(h, "Depth")) : INT_MAX);
264 syslog(LOG_DEBUG, "Client PROPFIND requested depth: %d", dav_depth);
265 StrBuf *Buf = NewStrBuf();
267 StrBufAppendPrintf(Buf, "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
270 "xmlns:C=\"urn:ietf:params:xml:ns:caldav\""
274 /* Transmit the collection resource */
275 StrBufAppendPrintf(Buf, "<D:response>");
276 StrBufAppendPrintf(Buf, "<D:href>");
277 StrBufXMLEscAppend(Buf, NULL, h->site_prefix, strlen(h->site_prefix), 0);
278 StrBufAppendPrintf(Buf, "/ctdl/r/");
279 StrBufXMLEscAppend(Buf, NULL, c->room, strlen(c->room), 0);
280 StrBufAppendPrintf(Buf, "</D:href>");
282 StrBufAppendPrintf(Buf, "<D:propstat>");
283 StrBufAppendPrintf(Buf, "<D:status>HTTP/1.1 200 OK</D:status>");
284 StrBufAppendPrintf(Buf, "<D:prop>");
285 StrBufAppendPrintf(Buf, "<D:displayname>");
286 StrBufXMLEscAppend(Buf, NULL, c->room, strlen(c->room), 0);
287 StrBufAppendPrintf(Buf, "</D:displayname>");
289 StrBufAppendPrintf(Buf, "<D:owner />"); // empty owner ought to be legal; see rfc3744 section 5.1
291 StrBufAppendPrintf(Buf, "<D:resourcetype><D:collection />");
292 switch(c->room_default_view) {
294 StrBufAppendPrintf(Buf, "<C:calendar />"); // RFC4791 section 4.2
297 StrBufAppendPrintf(Buf, "</D:resourcetype>");
299 int enumerate_by_euid = 0; // nonzero if messages will be retrieved by euid instead of msgnum
300 switch(c->room_default_view) {
301 case VIEW_CALENDAR: // RFC4791 section 5.2
302 StrBufAppendPrintf(Buf, "<C:supported-calendar-component-set><C:comp name=\"VEVENT\"/></C:supported-calendar-component-set>");
303 StrBufAppendPrintf(Buf, "<C:supported-calendar-data>");
304 StrBufAppendPrintf(Buf, "<C:calendar-data content-type=\"text/calendar\" version=\"2.0\"/>");
305 StrBufAppendPrintf(Buf, "</C:supported-calendar-data>");
306 enumerate_by_euid = 1;
308 case VIEW_TASKS: // RFC4791 section 5.2
309 StrBufAppendPrintf(Buf, "<C:supported-calendar-component-set><C:comp name=\"VTODO\"/></C:supported-calendar-component-set>");
310 StrBufAppendPrintf(Buf, "<C:supported-calendar-data>");
311 StrBufAppendPrintf(Buf, "<C:calendar-data content-type=\"text/calendar\" version=\"2.0\"/>");
312 StrBufAppendPrintf(Buf, "</C:supported-calendar-data>");
313 enumerate_by_euid = 1;
315 case VIEW_ADDRESSBOOK: // FIXME put some sort of CardDAV crapola here when we implement it
316 enumerate_by_euid = 1;
318 case VIEW_WIKI: // FIXME invent "WikiDAV" ?
319 enumerate_by_euid = 1;
324 /* FIXME get the mtime
325 StrBufAppendPrintf(Buf, "<D:getlastmodified>");
327 StrBufAppendPrintf(Buf, "</D:getlastmodified>");
330 StrBufAppendPrintf(Buf, "</D:prop>");
331 StrBufAppendPrintf(Buf, "</D:propstat>");
332 StrBufAppendPrintf(Buf, "</D:response>\n");
334 // If a depth greater than zero was specified, transmit the collection listing
337 long *msglist = get_msglist(c, "ALL");
340 for (i=0; (msglist[i] > 0); ++i) {
341 if ((i%10) == 0) syslog(LOG_DEBUG, "PROPFIND enumerated %d messages", i);
342 e = NULL; // EUID gets stored here
346 ctdl_printf(c, "MSG0 %ld|3", msglist[i]);
347 ctdl_readline(c, cbuf, sizeof(cbuf));
348 if (cbuf[0] == '1') while (ctdl_readline(c, cbuf, sizeof(cbuf)), strcmp(cbuf, "000")) {
349 if ( (enumerate_by_euid) && (!strncasecmp(cbuf, "exti=", 5)) ) {
350 // e = strdup(&cbuf[5]);
351 int elen = (2 * strlen(&cbuf[5]));
353 urlesc(e, elen, &cbuf[5]);
355 if (!strncasecmp(cbuf, "time=", 5)) {
356 timestamp = atol(&cbuf[5]);
361 sprintf(e, "%ld", msglist[i]);
363 StrBufAppendPrintf(Buf, "<D:response>");
365 // Generate the 'href' tag for this message
366 StrBufAppendPrintf(Buf, "<D:href>");
367 StrBufXMLEscAppend(Buf, NULL, h->site_prefix, strlen(h->site_prefix), 0);
368 StrBufAppendPrintf(Buf, "/ctdl/r/");
369 StrBufXMLEscAppend(Buf, NULL, c->room, strlen(c->room), 0);
370 StrBufAppendPrintf(Buf, "/");
371 StrBufXMLEscAppend(Buf, NULL, e, strlen(e), 0);
372 StrBufAppendPrintf(Buf, "</D:href>");
373 StrBufAppendPrintf(Buf, "<D:propstat>");
374 StrBufAppendPrintf(Buf, "<D:status>HTTP/1.1 200 OK</D:status>");
375 StrBufAppendPrintf(Buf, "<D:prop>");
377 switch(c->room_default_view) {
379 StrBufAppendPrintf(Buf, "<D:getcontenttype>text/calendar; component=vevent</D:getcontenttype>");
382 StrBufAppendPrintf(Buf, "<D:getcontenttype>text/calendar; component=vtodo</D:getcontenttype>");
384 case VIEW_ADDRESSBOOK:
385 StrBufAppendPrintf(Buf, "<D:getcontenttype>text/x-vcard</D:getcontenttype>");
390 char *datestring = http_datestring(timestamp);
392 StrBufAppendPrintf(Buf, "<D:getlastmodified>");
393 StrBufXMLEscAppend(Buf, NULL, datestring, strlen(datestring), 0);
394 StrBufAppendPrintf(Buf, "</D:getlastmodified>");
397 if (enumerate_by_euid) { // FIXME ajc 2017oct30 should this be inside the timestamp conditional?
398 StrBufAppendPrintf(Buf, "<D:getetag>\"%ld\"</D:getetag>", msglist[i]);
401 StrBufAppendPrintf(Buf, "</D:prop></D:propstat></D:response>\n");
409 StrBufAppendPrintf(Buf, "</D:multistatus>\n");
411 add_response_header(h, strdup("Content-type"), strdup("text/xml"));
412 h->response_code = 207;
413 h->response_string = strdup("Multi-Status");
414 h->response_body_length = StrLength(Buf);
415 h->response_body = SmashStrBuf(&Buf);
418 // some good examples here
419 // http://blogs.nologin.es/rickyepoderi/index.php?/archives/14-Introducing-CalDAV-Part-I.html
423 * Called by the_room_itself() when the HTTP method is PROPFIND
425 void get_the_room_itself(struct http_transaction *h, struct ctdlsession *c)
427 JsonValue *j = NewJsonObject(HKEY("gotoroom"));
429 JsonObjectAppend(j, NewJsonPlainString( HKEY("name"), c->room, -1));
430 JsonObjectAppend(j, NewJsonNumber( HKEY("current_view"), c->room_current_view ));
431 JsonObjectAppend(j, NewJsonNumber( HKEY("default_view"), c->room_default_view ));
432 JsonObjectAppend(j, NewJsonNumber( HKEY("new_messages"), c->new_messages ));
433 JsonObjectAppend(j, NewJsonNumber( HKEY("total_messages"), c->total_messages ));
434 JsonObjectAppend(j, NewJsonNumber( HKEY("last_seen"), c->last_seen ));
436 StrBuf *sj = NewStrBuf();
437 SerializeJson(sj, j, 1); // '1' == free the source array
439 add_response_header(h, strdup("Content-type"), strdup("application/json"));
440 h->response_code = 200;
441 h->response_string = strdup("OK");
442 h->response_body_length = StrLength(sj);
443 h->response_body = SmashStrBuf(&sj);
449 * Handle REST/DAV requests for the room itself (such as /ctdl/r/roomname
450 * or /ctdl/r/roomname/ but *not* specific objects within the room)
452 void the_room_itself(struct http_transaction *h, struct ctdlsession *c)
454 // OPTIONS method on the room itself usually is a DAV client assessing what's here.
456 if (!strcasecmp(h->method, "OPTIONS")) {
457 options_the_room_itself(h, c);
461 // PROPFIND method on the room itself could be looking for a directory
463 if (!strcasecmp(h->method, "PROPFIND")) {
464 propfind_the_room_itself(h, c);
468 // REPORT method on the room itself is probably the dreaded CalDAV tower-of-crapola
470 if (!strcasecmp(h->method, "REPORT")) {
471 report_the_room_itself(h, c);
475 // GET method on the room itself is an API call, possibly from our JavaScript front end
477 if (!strcasecmp(h->method, "get")) {
478 get_the_room_itself(h, c);
482 // we probably want a "go to this room" for interactive access
488 * Dispatcher for "/ctdl/r" and "/ctdl/r/" for the room list
490 void room_list(struct http_transaction *h, struct ctdlsession *c)
495 ctdl_printf(c, "LKRA");
496 ctdl_readline(c, buf, sizeof(buf));
502 JsonValue *j = NewJsonArray(HKEY("lkra"));
503 while (ctdl_readline(c, buf, sizeof(buf)) , strcmp(buf, "000")) {
505 // name|QRflags|QRfloor|QRorder|QRflags2|ra|current_view|default_view|mtime
506 JsonValue *jr = NewJsonObject(HKEY("room"));
508 extract_token(roomname, buf, 0, '|', sizeof roomname);
509 JsonObjectAppend(jr, NewJsonPlainString( HKEY("name"), roomname, -1));
511 int ra = extract_int(buf, 5);
512 JsonObjectAppend(jr, NewJsonBool( HKEY("known"), (ra && UA_KNOWN)));
513 JsonObjectAppend(jr, NewJsonBool( HKEY("hasnewmsgs"), (ra && UA_HASNEWMSGS)));
515 int floor = extract_int(buf, 2);
516 JsonObjectAppend(jr, NewJsonNumber( HKEY("floor"), floor));
518 int rorder = extract_int(buf, 3);
519 JsonObjectAppend(jr, NewJsonNumber( HKEY("rorder"), rorder));
521 JsonArrayAppend(j, jr); // add the room to the array
524 StrBuf *sj = NewStrBuf();
525 SerializeJson(sj, j, 1); // '1' == free the source array
527 add_response_header(h, strdup("Content-type"), strdup("application/json"));
528 h->response_code = 200;
529 h->response_string = strdup("OK");
530 h->response_body_length = StrLength(sj);
531 h->response_body = SmashStrBuf(&sj);
536 * Dispatcher for paths starting with /ctdl/r/
538 void ctdl_r(struct http_transaction *h, struct ctdlsession *c)
540 char requested_roomname[128];
543 // All room-related functions require being "in" the room specified. Are we in that room already?
544 extract_token(requested_roomname, h->uri, 3, '/', sizeof requested_roomname);
545 unescape_input(requested_roomname);
547 if (IsEmptyStr(requested_roomname)) { // /ctdl/r/
552 // If not, try to go there.
553 if (strcasecmp(requested_roomname, c->room)) {
554 ctdl_printf(c, "GOTO %s", requested_roomname);
555 ctdl_readline(c, buf, sizeof(buf));
557 // buf[3] will indicate whether any instant messages are waiting
558 extract_token(c->room, &buf[4], 0, '|', sizeof c->room);
559 c->new_messages = extract_int(&buf[4], 1);
560 c->total_messages = extract_int(&buf[4], 2);
561 // 3 (int)info Info flag: set to nonzero if the user needs to read this room's info file
562 // 4 (int)CCC->room.QRflags Various flags associated with this room.
563 // 5 (long)CCC->room.QRhighest The highest message number present in this room
564 c->last_seen = extract_long(&buf[4], 6); // The highest message number the user has read in this room
565 // 7 (int)rmailflag Boolean flag: 1 if this is a Mail> room, 0 otherwise.
566 // 8 (int)raideflag Nonzero if user is either Aide or a Room Aide in this room
567 // 9 (int)newmailcount The number of new Mail messages the user has
568 // 10 (int)CCC->room.QRfloor The floor number this room resides on
569 c->room_current_view = extract_int(&buf[4], 11);
570 c->room_default_view = extract_int(&buf[4], 12);
571 // 13 (int)is_trash Boolean flag: 1 if this is the user's Trash folder, 0 otherwise.
572 // 14 (int)CCC->room.QRflags2 More flags associated with this room
573 // 15 (long)CCC->room.QRmtime Timestamp of the last write activity in this room
581 // At this point our Citadel client session is "in" the specified room.
583 if (num_tokens(h->uri, '/') == 4) { // /ctdl/r/roomname
584 the_room_itself(h, c);
588 extract_token(buf, h->uri, 4, '/', sizeof buf);
589 if (num_tokens(h->uri, '/') == 5) {
590 if (IsEmptyStr(buf)) {
591 the_room_itself(h, c); // /ctdl/r/roomname/ ( same as /ctdl/r/roomname )
594 object_in_room(h, c); // /ctdl/r/roomname/object
598 if (num_tokens(h->uri, '/') == 6) {
599 object_in_room(h, c); // /ctdl/r/roomname/object/ or possibly /ctdl/r/roomname/object/component
603 // If we get to this point, the client specified a valid room but requested an action we don't know how to perform.