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)
38 msglist = malloc(num_alloc * sizeof(long));
43 msglist = realloc(msglist, num_alloc * sizeof(long));
46 ctdl_readline(c, buf, sizeof(buf));
47 msglist[num_msgs++] = atol(buf);
48 } while (strcmp(buf, "000")); // this makes the last element a "0" terminator
54 * Supplied with a list of potential matches from an If-Match: or If-None-Match: header, and
55 * a message number (which we always use as the entity tag in Citadel), return nonzero if the
56 * message number matches any of the supplied tags in the string.
58 int match_etags(char *taglist, long msgnum)
60 int num_tags = num_tokens(taglist, ',');
64 if (msgnum <= 0) // no msgnum? no match.
69 for (i=0; i<num_tags; ++i)
71 extract_token(tag, taglist, i, ',', sizeof tag);
73 char *lq = (strchr(tag, '"'));
74 char *rq = (strrchr(tag, '"'));
75 if (lq < rq) // has two double quotes
81 if (!strcmp(tag, "*")) // wildcard match
85 long tagmsgnum = atol(tag);
86 if ( (tagmsgnum > 0) && (tagmsgnum == msgnum) ) // match
92 return(0); // no match
97 * Client is requesting a message list
99 void json_msglist(struct http_transaction *h, struct ctdlsession *c, char *which)
102 long *msglist = get_msglist(c, which);
103 JsonValue *j = NewJsonArray(HKEY("msgs"));
107 for (i=0; msglist[i]>0 ; ++i)
109 JsonArrayAppend(j, NewJsonNumber( HKEY("m"), msglist[i]));
114 StrBuf *sj = NewStrBuf();
115 SerializeJson(sj, j, 1); // '1' == free the source array
117 add_response_header(h, strdup("Content-type"), strdup("application/json"));
118 h->response_code = 200;
119 h->response_string = strdup("OK");
120 h->response_body_length = StrLength(sj);
121 h->response_body = SmashStrBuf(&sj);
127 * Client requested an object in a room.
129 void object_in_room(struct http_transaction *h, struct ctdlsession *c)
133 char unescaped_euid[1024];
135 extract_token(buf, h->uri, 4, '/', sizeof buf);
137 if (!strncasecmp(buf, "msgs.", 5)) // Client is requesting a list of message numbers
139 json_msglist(h, c, &buf[5]);
144 if (!strncasecmp(buf, "threads", 5)) // Client is requesting a threaded view (still kind of fuzzy here)
146 threaded_view(h, c, &buf[5]);
150 if (!strncasecmp(buf, "flat", 5)) // Client is requesting a flat view (still kind of fuzzy here)
152 flat_view(h, c, &buf[5]);
157 if ( (c->room_default_view == VIEW_CALENDAR) // room types where objects are referenced by EUID
158 || (c->room_default_view == VIEW_TASKS)
159 || (c->room_default_view == VIEW_ADDRESSBOOK)
161 safestrncpy(unescaped_euid, buf, sizeof unescaped_euid);
162 unescape_input(unescaped_euid);
163 msgnum = locate_message_by_uid(c, unescaped_euid);
171 * All methods except PUT require the message to already exist
173 if ( (msgnum <= 0) && (strcasecmp(h->method, "PUT")) )
179 * If we get to this point we have a valid message number in an accessible room.
181 syslog(LOG_DEBUG, "msgnum is %ld, method is %s", msgnum, h->method);
184 * A sixth component in the URL can be one of two things:
185 * (1) a MIME part specifier, in which case the client wants to download that component within the message
186 * (2) a content-type, in which ase the client wants us to try to render it a certain way
188 if (num_tokens(h->uri, '/') == 6)
190 extract_token(buf, h->uri, 5, '/', sizeof buf);
191 if (!IsEmptyStr(buf)) {
192 if (!strcasecmp(buf, "json"))
194 json_render_one_message(h, c, msgnum);
198 download_mime_component(h, c, msgnum, buf);
205 * Ok, we want a full message, but first let's check for the if[-none]-match headers.
207 char *if_match = header_val(h, "If-Match");
208 if ( (if_match != NULL) && (!match_etags(if_match, msgnum)) )
214 char *if_none_match = header_val(h, "If-None-Match");
215 if ( (if_none_match != NULL) && (match_etags(if_none_match, msgnum)) )
225 if (!strcasecmp(h->method, "DELETE"))
227 dav_delete_message(h, c, msgnum);
229 else if (!strcasecmp(h->method, "GET"))
231 dav_get_message(h, c, msgnum);
233 else if (!strcasecmp(h->method, "PUT"))
235 dav_put_message(h, c, unescaped_euid, msgnum);
239 do_404(h); // Got this far but the method made no sense? Bummer.
246 * Called by the_room_itself() when the HTTP method is REPORT
248 void report_the_room_itself(struct http_transaction *h, struct ctdlsession *c)
250 if (c->room_default_view == VIEW_CALENDAR)
252 caldav_report(h, c); // CalDAV REPORTs ... fmgwac
256 do_404(h); // future implementations like CardDAV will require code paths here
261 * Called by the_room_itself() when the HTTP method is OPTIONS
263 void options_the_room_itself(struct http_transaction *h, struct ctdlsession *c)
265 h->response_code = 200;
266 h->response_string = strdup("OK");
267 if (c->room_default_view == VIEW_CALENDAR)
269 add_response_header(h, strdup("DAV"), strdup("1, calendar-access")); // offer CalDAV
271 else if (c->room_default_view == VIEW_ADDRESSBOOK)
273 add_response_header(h, strdup("DAV"), strdup("1, addressbook")); // offer CardDAV
277 add_response_header(h, strdup("DAV"), strdup("1")); // ordinary WebDAV for all other room types
279 add_response_header(h, strdup("Allow"), strdup("OPTIONS, PROPFIND, GET, PUT, REPORT, DELETE"));
284 * Called by the_room_itself() when the HTTP method is PROPFIND
286 void propfind_the_room_itself(struct http_transaction *h, struct ctdlsession *c)
290 int dav_depth = (header_val(h, "Depth") ? atoi(header_val(h, "Depth")) : INT_MAX);
291 syslog(LOG_DEBUG, "Client PROPFIND requested depth: %d", dav_depth);
292 StrBuf *Buf = NewStrBuf();
294 StrBufAppendPrintf(Buf, "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
297 "xmlns:C=\"urn:ietf:params:xml:ns:caldav\""
301 /* Transmit the collection resource */
302 StrBufAppendPrintf(Buf, "<D:response>");
303 StrBufAppendPrintf(Buf, "<D:href>");
304 StrBufXMLEscAppend(Buf, NULL, h->site_prefix, strlen(h->site_prefix), 0);
305 StrBufAppendPrintf(Buf, "/ctdl/r/");
306 StrBufXMLEscAppend(Buf, NULL, c->room, strlen(c->room), 0);
307 StrBufAppendPrintf(Buf, "</D:href>");
309 StrBufAppendPrintf(Buf, "<D:propstat>");
310 StrBufAppendPrintf(Buf, "<D:status>HTTP/1.1 200 OK</D:status>");
311 StrBufAppendPrintf(Buf, "<D:prop>");
312 StrBufAppendPrintf(Buf, "<D:displayname>");
313 StrBufXMLEscAppend(Buf, NULL, c->room, strlen(c->room), 0);
314 StrBufAppendPrintf(Buf, "</D:displayname>");
316 StrBufAppendPrintf(Buf, "<D:owner />"); // empty owner ought to be legal; see rfc3744 section 5.1
318 StrBufAppendPrintf(Buf, "<D:resourcetype><D:collection />");
319 switch(c->room_default_view)
322 StrBufAppendPrintf(Buf, "<C:calendar />"); // RFC4791 section 4.2
325 StrBufAppendPrintf(Buf, "</D:resourcetype>");
327 int enumerate_by_euid = 0; // nonzero if messages will be retrieved by euid instead of msgnum
328 switch(c->room_default_view)
330 case VIEW_CALENDAR: // RFC4791 section 5.2
331 StrBufAppendPrintf(Buf, "<C:supported-calendar-component-set><C:comp name=\"VEVENT\"/></C:supported-calendar-component-set>");
332 StrBufAppendPrintf(Buf, "<C:supported-calendar-data>");
333 StrBufAppendPrintf(Buf, "<C:calendar-data content-type=\"text/calendar\" version=\"2.0\"/>");
334 StrBufAppendPrintf(Buf, "</C:supported-calendar-data>");
335 enumerate_by_euid = 1;
337 case VIEW_TASKS: // RFC4791 section 5.2
338 StrBufAppendPrintf(Buf, "<C:supported-calendar-component-set><C:comp name=\"VTODO\"/></C:supported-calendar-component-set>");
339 StrBufAppendPrintf(Buf, "<C:supported-calendar-data>");
340 StrBufAppendPrintf(Buf, "<C:calendar-data content-type=\"text/calendar\" version=\"2.0\"/>");
341 StrBufAppendPrintf(Buf, "</C:supported-calendar-data>");
342 enumerate_by_euid = 1;
344 case VIEW_ADDRESSBOOK: // FIXME put some sort of CardDAV crapola here when we implement it
345 enumerate_by_euid = 1;
347 case VIEW_WIKI: // FIXME invent "WikiDAV" ?
348 enumerate_by_euid = 1;
353 /* FIXME get the mtime
354 StrBufAppendPrintf(Buf, "<D:getlastmodified>");
356 StrBufAppendPrintf(Buf, "</D:getlastmodified>");
359 StrBufAppendPrintf(Buf, "</D:prop>");
360 StrBufAppendPrintf(Buf, "</D:propstat>");
361 StrBufAppendPrintf(Buf, "</D:response>\n");
363 // If a depth greater than zero was specified, transmit the collection listing
367 long *msglist = get_msglist(c, "ALL");
371 for (i=0; (msglist[i] > 0); ++i)
373 if ((i%10) == 0) syslog(LOG_DEBUG, "PROPFIND enumerated %d messages", i);
374 e = NULL; // EUID gets stored here
378 ctdl_printf(c, "MSG0 %ld|3", msglist[i]);
379 ctdl_readline(c, cbuf, sizeof(cbuf));
380 if (cbuf[0] == '1') while (ctdl_readline(c, cbuf, sizeof(cbuf)), strcmp(cbuf, "000"))
382 if ( (enumerate_by_euid) && (!strncasecmp(cbuf, "exti=", 5)) )
384 // e = strdup(&cbuf[5]);
385 int elen = (2 * strlen(&cbuf[5]));
387 urlesc(e, elen, &cbuf[5]);
389 if (!strncasecmp(cbuf, "time=", 5))
391 timestamp = atol(&cbuf[5]);
397 sprintf(e, "%ld", msglist[i]);
399 StrBufAppendPrintf(Buf, "<D:response>");
401 // Generate the 'href' tag for this message
402 StrBufAppendPrintf(Buf, "<D:href>");
403 StrBufXMLEscAppend(Buf, NULL, h->site_prefix, strlen(h->site_prefix), 0);
404 StrBufAppendPrintf(Buf, "/ctdl/r/");
405 StrBufXMLEscAppend(Buf, NULL, c->room, strlen(c->room), 0);
406 StrBufAppendPrintf(Buf, "/");
407 StrBufXMLEscAppend(Buf, NULL, e, strlen(e), 0);
408 StrBufAppendPrintf(Buf, "</D:href>");
409 StrBufAppendPrintf(Buf, "<D:propstat>");
410 StrBufAppendPrintf(Buf, "<D:status>HTTP/1.1 200 OK</D:status>");
411 StrBufAppendPrintf(Buf, "<D:prop>");
413 switch(c->room_default_view)
416 StrBufAppendPrintf(Buf, "<D:getcontenttype>text/calendar; component=vevent</D:getcontenttype>");
419 StrBufAppendPrintf(Buf, "<D:getcontenttype>text/calendar; component=vtodo</D:getcontenttype>");
421 case VIEW_ADDRESSBOOK:
422 StrBufAppendPrintf(Buf, "<D:getcontenttype>text/x-vcard</D:getcontenttype>");
428 char *datestring = http_datestring(timestamp);
431 StrBufAppendPrintf(Buf, "<D:getlastmodified>");
432 StrBufXMLEscAppend(Buf, NULL, datestring, strlen(datestring), 0);
433 StrBufAppendPrintf(Buf, "</D:getlastmodified>");
436 if (enumerate_by_euid) // FIXME ajc 2017oct30 should this be inside the timestamp conditional?
438 StrBufAppendPrintf(Buf, "<D:getetag>\"%ld\"</D:getetag>", msglist[i]);
441 StrBufAppendPrintf(Buf, "</D:prop></D:propstat></D:response>\n");
449 StrBufAppendPrintf(Buf, "</D:multistatus>\n");
451 add_response_header(h, strdup("Content-type"), strdup("text/xml"));
452 h->response_code = 207;
453 h->response_string = strdup("Multi-Status");
454 h->response_body_length = StrLength(Buf);
455 h->response_body = SmashStrBuf(&Buf);
458 // some good examples here
459 // http://blogs.nologin.es/rickyepoderi/index.php?/archives/14-Introducing-CalDAV-Part-I.html
463 * Called by the_room_itself() when the HTTP method is PROPFIND
465 void get_the_room_itself(struct http_transaction *h, struct ctdlsession *c)
467 JsonValue *j = NewJsonObject(HKEY("gotoroom"));
469 JsonObjectAppend(j, NewJsonPlainString( HKEY("name"), c->room, -1));
470 JsonObjectAppend(j, NewJsonNumber( HKEY("current_view"), c->room_current_view ));
471 JsonObjectAppend(j, NewJsonNumber( HKEY("default_view"), c->room_default_view ));
472 JsonObjectAppend(j, NewJsonNumber( HKEY("new_messages"), c->new_messages ));
473 JsonObjectAppend(j, NewJsonNumber( HKEY("total_messages"), c->total_messages ));
474 JsonObjectAppend(j, NewJsonNumber( HKEY("last_seen"), c->last_seen ));
476 StrBuf *sj = NewStrBuf();
477 SerializeJson(sj, j, 1); // '1' == free the source array
479 add_response_header(h, strdup("Content-type"), strdup("application/json"));
480 h->response_code = 200;
481 h->response_string = strdup("OK");
482 h->response_body_length = StrLength(sj);
483 h->response_body = SmashStrBuf(&sj);
489 * Handle REST/DAV requests for the room itself (such as /ctdl/r/roomname
490 * or /ctdl/r/roomname/ but *not* specific objects within the room)
492 void the_room_itself(struct http_transaction *h, struct ctdlsession *c)
494 // OPTIONS method on the room itself usually is a DAV client assessing what's here.
496 if (!strcasecmp(h->method, "OPTIONS"))
498 options_the_room_itself(h, c);
502 // PROPFIND method on the room itself could be looking for a directory
504 if (!strcasecmp(h->method, "PROPFIND"))
506 propfind_the_room_itself(h, c);
510 // REPORT method on the room itself is probably the dreaded CalDAV tower-of-crapola
512 if (!strcasecmp(h->method, "REPORT"))
514 report_the_room_itself(h, c);
518 // GET method on the room itself is an API call, possibly from our JavaScript front end
520 if (!strcasecmp(h->method, "get"))
522 get_the_room_itself(h, c);
526 // we probably want a "go to this room" for interactive access
532 * Dispatcher for "/ctdl/r" and "/ctdl/r/" for the room list
534 void room_list(struct http_transaction *h, struct ctdlsession *c)
539 ctdl_printf(c, "LKRA");
540 ctdl_readline(c, buf, sizeof(buf));
547 JsonValue *j = NewJsonArray(HKEY("lkra"));
548 while (ctdl_readline(c, buf, sizeof(buf)) , strcmp(buf, "000"))
551 // name|QRflags|QRfloor|QRorder|QRflags2|ra|current_view|default_view|mtime
552 JsonValue *jr = NewJsonObject(HKEY("room"));
554 extract_token(roomname, buf, 0, '|', sizeof roomname);
555 JsonObjectAppend(jr, NewJsonPlainString( HKEY("name"), roomname, -1));
557 int ra = extract_int(buf, 5);
558 JsonObjectAppend(jr, NewJsonBool( HKEY("known"), (ra && UA_KNOWN)));
559 JsonObjectAppend(jr, NewJsonBool( HKEY("hasnewmsgs"), (ra && UA_HASNEWMSGS)));
561 int floor = extract_int(buf, 2);
562 JsonObjectAppend(jr, NewJsonNumber( HKEY("floor"), floor));
564 int rorder = extract_int(buf, 3);
565 JsonObjectAppend(jr, NewJsonNumber( HKEY("rorder"), rorder));
567 JsonArrayAppend(j, jr); // add the room to the array
570 StrBuf *sj = NewStrBuf();
571 SerializeJson(sj, j, 1); // '1' == free the source array
573 add_response_header(h, strdup("Content-type"), strdup("application/json"));
574 h->response_code = 200;
575 h->response_string = strdup("OK");
576 h->response_body_length = StrLength(sj);
577 h->response_body = SmashStrBuf(&sj);
582 * Dispatcher for paths starting with /ctdl/r/
584 void ctdl_r(struct http_transaction *h, struct ctdlsession *c)
586 char requested_roomname[128];
589 // All room-related functions require being "in" the room specified. Are we in that room already?
590 extract_token(requested_roomname, h->uri, 3, '/', sizeof requested_roomname);
591 unescape_input(requested_roomname);
593 if (IsEmptyStr(requested_roomname)) // /ctdl/r/
599 // If not, try to go there.
600 if (strcasecmp(requested_roomname, c->room))
602 ctdl_printf(c, "GOTO %s", requested_roomname);
603 ctdl_readline(c, buf, sizeof(buf));
606 // buf[3] will indicate whether any instant messages are waiting
607 extract_token(c->room, &buf[4], 0, '|', sizeof c->room);
608 c->new_messages = extract_int(&buf[4], 1);
609 c->total_messages = extract_int(&buf[4], 2);
610 // 3 (int)info Info flag: set to nonzero if the user needs to read this room's info file
611 // 4 (int)CCC->room.QRflags Various flags associated with this room.
612 // 5 (long)CCC->room.QRhighest The highest message number present in this room
613 c->last_seen = extract_long(&buf[4], 6); // The highest message number the user has read in this room
614 // 7 (int)rmailflag Boolean flag: 1 if this is a Mail> room, 0 otherwise.
615 // 8 (int)raideflag Nonzero if user is either Aide or a Room Aide in this room
616 // 9 (int)newmailcount The number of new Mail messages the user has
617 // 10 (int)CCC->room.QRfloor The floor number this room resides on
618 c->room_current_view = extract_int(&buf[4], 11);
619 c->room_default_view = extract_int(&buf[4], 12);
620 // 13 (int)is_trash Boolean flag: 1 if this is the user's Trash folder, 0 otherwise.
621 // 14 (int)CCC->room.QRflags2 More flags associated with this room
622 // 15 (long)CCC->room.QRmtime Timestamp of the last write activity in this room
631 // At this point our Citadel client session is "in" the specified room.
633 if (num_tokens(h->uri, '/') == 4) // /ctdl/r/roomname
635 the_room_itself(h, c);
639 extract_token(buf, h->uri, 4, '/', sizeof buf);
640 if (num_tokens(h->uri, '/') == 5)
644 the_room_itself(h, c); // /ctdl/r/roomname/ ( same as /ctdl/r/roomname )
648 object_in_room(h, c); // /ctdl/r/roomname/object
652 if (num_tokens(h->uri, '/') == 6)
654 object_in_room(h, c); // /ctdl/r/roomname/object/ or possibly /ctdl/r/roomname/object/component
658 // If we get to this point, the client specified a valid room but requested an action we don't know how to perform.