3 // Copyright (c) 1996-2022 by the citadel.org team
5 // This program is open source software. Use, duplication, or
6 // disclosure are subject to the GNU General Public License v3.
11 // Return a "zero-terminated" array of message numbers in the current room.
12 // Caller owns the memory and must free it. Returns NULL if any problems.
13 long *get_msglist(struct ctdlsession *c, char *which_msgs) {
19 ctdl_printf(c, "MSGS %s", which_msgs);
20 ctdl_readline(c, buf, sizeof(buf));
23 if (num_msgs >= num_alloc) {
26 msglist = malloc(num_alloc * sizeof(long));
30 msglist = realloc(msglist, num_alloc * sizeof(long));
33 ctdl_readline(c, buf, sizeof(buf));
34 msglist[num_msgs++] = atol(buf);
35 } while (strcmp(buf, "000")); // this makes the last element a "0" terminator
41 // Supplied with a list of potential matches from an If-Match: or If-None-Match: header, and
42 // a message number (which we always use as the entity tag in Citadel), return nonzero if the
43 // message number matches any of the supplied tags in the string.
44 int match_etags(char *taglist, long msgnum) {
45 int num_tags = num_tokens(taglist, ',');
49 if (msgnum <= 0) { // no msgnum? no match.
53 for (i = 0; i < num_tags; ++i) {
54 extract_token(tag, taglist, i, ',', sizeof tag);
56 char *lq = (strchr(tag, '"'));
57 char *rq = (strrchr(tag, '"'));
58 if (lq < rq) { // has two double quotes
63 if (!strcmp(tag, "*")) { // wildcard match
66 long tagmsgnum = atol(tag);
67 if ((tagmsgnum > 0) && (tagmsgnum == msgnum)) { // match
72 return (0); // no match
76 // Client is requesting a message list
77 void json_msglist(struct http_transaction *h, struct ctdlsession *c, char *which) {
79 long *msglist = get_msglist(c, which);
80 JsonValue *j = NewJsonArray(HKEY("msgs"));
82 if (msglist != NULL) {
83 for (i = 0; msglist[i] > 0; ++i) {
84 JsonArrayAppend(j, NewJsonNumber(HKEY("m"), msglist[i]));
89 StrBuf *sj = NewStrBuf();
90 SerializeJson(sj, j, 1); // '1' == free the source array
92 add_response_header(h, strdup("Content-type"), strdup("application/json"));
93 h->response_code = 200;
94 h->response_string = strdup("OK");
95 h->response_body_length = StrLength(sj);
96 h->response_body = SmashStrBuf(&sj);
101 // Client is requesting the room info banner
102 void read_room_info_banner(struct http_transaction *h, struct ctdlsession *c) {
105 ctdl_printf(c, "RINF");
106 ctdl_readline(c, buf, sizeof(buf));
108 StrBuf *info = NewStrBuf();
109 while (ctdl_readline(c, buf, sizeof(buf)), (strcmp(buf, "000"))){
110 StrBufAppendPrintf(info, "%s\n", buf);
112 add_response_header(h, strdup("Content-type"), strdup("text/plain"));
113 h->response_code = 200;
114 h->response_string = strdup("OK");
115 h->response_body_length = StrLength(info);
116 h->response_body = SmashStrBuf(&info);
125 // Client is setting the "Last Read Pointer" (marking all messages as "seen" up to this message)
126 void set_last_read_pointer(struct http_transaction *h, struct ctdlsession *c) {
128 ctdl_printf(c, "SLRP %d", atoi(get_url_param(h, "last")));
129 ctdl_readline(c, cbuf, sizeof(cbuf));
130 if (cbuf[0] == '2') {
139 // Client requested an object in a room.
140 void object_in_room(struct http_transaction *h, struct ctdlsession *c) {
143 char unescaped_euid[1024];
145 extract_token(buf, h->url, 4, '/', sizeof buf);
147 if (!strncasecmp(buf, "msgs.", 5)) { // Client is requesting a list of message numbers
148 unescape_input(&buf[5]);
149 json_msglist(h, c, &buf[5]);
153 if (!strcasecmp(buf, "info.txt")) { // Client is requesting the room info banner
154 read_room_info_banner(h, c);
158 if (!strcasecmp(buf, "slrp")) { // Set the Last Read Pointer
159 set_last_read_pointer(h, c);
163 // If we get to this point, the client is requesting a specific object *in* the room.
165 if ((c->room_default_view == VIEW_CALENDAR) // room types where objects are referenced by EUID
166 || (c->room_default_view == VIEW_TASKS)
167 || (c->room_default_view == VIEW_ADDRESSBOOK)
169 safestrncpy(unescaped_euid, buf, sizeof unescaped_euid);
170 unescape_input(unescaped_euid);
171 msgnum = locate_message_by_uid(c, unescaped_euid);
173 else { // otherwise the object is being referenced by message number
177 // All methods except PUT require the message to already exist
178 if ((msgnum <= 0) && (strcasecmp(h->method, "PUT"))) {
182 // If we get to this point we have a valid message number in an accessible room.
183 syslog(LOG_DEBUG, "msgnum is %ld, method is %s", msgnum, h->method);
185 // A sixth component in the URL can be one of two things:
186 // (1) a MIME part specifier, in which case the client wants to download that component within the message
187 // (2) a content-type, in which ase the client wants us to try to render it a certain way
188 if (num_tokens(h->url, '/') == 6) {
189 extract_token(buf, h->url, 5, '/', sizeof buf);
190 if (!IsEmptyStr(buf)) {
191 if (!strcasecmp(buf, "json")) {
192 json_render_one_message(h, c, msgnum);
195 download_mime_component(h, c, msgnum, buf);
201 // Ok, we want a full message, but first let's check for the if[-none]-match headers.
202 char *if_match = header_val(h, "If-Match");
203 if ((if_match != NULL) && (!match_etags(if_match, msgnum))) {
208 char *if_none_match = header_val(h, "If-None-Match");
209 if ((if_none_match != NULL) && (match_etags(if_none_match, msgnum))) {
216 if (!strcasecmp(h->method, "DELETE")) {
217 dav_delete_message(h, c, msgnum);
219 else if (!strcasecmp(h->method, "GET")) {
220 dav_get_message(h, c, msgnum);
222 else if (!strcasecmp(h->method, "PUT")) {
223 dav_put_message(h, c, unescaped_euid, msgnum);
226 do_404(h); // Got this far but the method made no sense? Bummer.
232 // Called by the_room_itself() when the HTTP method is REPORT
233 void report_the_room_itself(struct http_transaction *h, struct ctdlsession *c) {
234 if (c->room_default_view == VIEW_CALENDAR) {
235 caldav_report(h, c); // CalDAV REPORTs ... fmgwac
239 do_404(h); // future implementations like CardDAV will require code paths here
243 // Called by the_room_itself() when the HTTP method is OPTIONS
244 void options_the_room_itself(struct http_transaction *h, struct ctdlsession *c) {
245 h->response_code = 200;
246 h->response_string = strdup("OK");
247 if (c->room_default_view == VIEW_CALENDAR) {
248 add_response_header(h, strdup("DAV"), strdup("1, calendar-access")); // offer CalDAV
250 else if (c->room_default_view == VIEW_ADDRESSBOOK) {
251 add_response_header(h, strdup("DAV"), strdup("1, addressbook")); // offer CardDAV
254 add_response_header(h, strdup("DAV"), strdup("1")); // ordinary WebDAV for all other room types
256 add_response_header(h, strdup("Allow"), strdup("OPTIONS, PROPFIND, GET, PUT, REPORT, DELETE"));
260 // Called by the_room_itself() when the HTTP method is PROPFIND
261 void propfind_the_room_itself(struct http_transaction *h, struct ctdlsession *c) {
264 int dav_depth = (header_val(h, "Depth") ? atoi(header_val(h, "Depth")) : INT_MAX);
265 syslog(LOG_DEBUG, "Client PROPFIND requested depth: %d", dav_depth);
266 StrBuf *Buf = NewStrBuf();
268 StrBufAppendPrintf(Buf, "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
269 "<D:multistatus " "xmlns:D=\"DAV:\" " "xmlns:C=\"urn:ietf:params:xml:ns:caldav\"" ">");
271 // Transmit the collection resource
272 StrBufAppendPrintf(Buf, "<D:response>");
273 StrBufAppendPrintf(Buf, "<D:href>");
274 StrBufXMLEscAppend(Buf, NULL, h->site_prefix, strlen(h->site_prefix), 0);
275 StrBufAppendPrintf(Buf, "/ctdl/r/");
276 StrBufXMLEscAppend(Buf, NULL, c->room, strlen(c->room), 0);
277 StrBufAppendPrintf(Buf, "</D:href>");
279 StrBufAppendPrintf(Buf, "<D:propstat>");
280 StrBufAppendPrintf(Buf, "<D:status>HTTP/1.1 200 OK</D:status>");
281 StrBufAppendPrintf(Buf, "<D:prop>");
282 StrBufAppendPrintf(Buf, "<D:displayname>");
283 StrBufXMLEscAppend(Buf, NULL, c->room, strlen(c->room), 0);
284 StrBufAppendPrintf(Buf, "</D:displayname>");
286 StrBufAppendPrintf(Buf, "<D:owner />"); // empty owner ought to be legal; see rfc3744 section 5.1
288 StrBufAppendPrintf(Buf, "<D:resourcetype><D:collection />");
289 switch (c->room_default_view) {
291 StrBufAppendPrintf(Buf, "<C:calendar />"); // RFC4791 section 4.2
294 StrBufAppendPrintf(Buf, "</D:resourcetype>");
296 int enumerate_by_euid = 0; // nonzero if messages will be retrieved by euid instead of msgnum
297 switch (c->room_default_view) {
298 case VIEW_CALENDAR: // RFC4791 section 5.2
299 StrBufAppendPrintf(Buf, "<C:supported-calendar-component-set><C:comp name=\"VEVENT\"/></C:supported-calendar-component-set>");
300 StrBufAppendPrintf(Buf, "<C:supported-calendar-data>");
301 StrBufAppendPrintf(Buf, "<C:calendar-data content-type=\"text/calendar\" version=\"2.0\"/>");
302 StrBufAppendPrintf(Buf, "</C:supported-calendar-data>");
303 enumerate_by_euid = 1;
305 case VIEW_TASKS: // RFC4791 section 5.2
306 StrBufAppendPrintf(Buf, "<C:supported-calendar-component-set><C:comp name=\"VTODO\"/></C:supported-calendar-component-set>");
307 StrBufAppendPrintf(Buf, "<C:supported-calendar-data>");
308 StrBufAppendPrintf(Buf, "<C:calendar-data content-type=\"text/calendar\" version=\"2.0\"/>");
309 StrBufAppendPrintf(Buf, "</C:supported-calendar-data>");
310 enumerate_by_euid = 1;
312 case VIEW_ADDRESSBOOK: // FIXME put some sort of CardDAV crapola here when we implement it
313 enumerate_by_euid = 1;
315 case VIEW_WIKI: // FIXME invent "WikiDAV" ?
316 enumerate_by_euid = 1;
320 // FIXME get the mtime
321 // StrBufAppendPrintf(Buf, "<D:getlastmodified>");
322 // escputs(datestring);
323 // StrBufAppendPrintf(Buf, "</D:getlastmodified>");
325 StrBufAppendPrintf(Buf, "</D:prop>");
326 StrBufAppendPrintf(Buf, "</D:propstat>");
327 StrBufAppendPrintf(Buf, "</D:response>\n");
329 // If a depth greater than zero was specified, transmit the collection listing
332 long *msglist = get_msglist(c, "ALL");
335 for (i = 0; (msglist[i] > 0); ++i) {
337 syslog(LOG_DEBUG, "PROPFIND enumerated %d messages", i);
338 e = NULL; // EUID gets stored here
342 ctdl_printf(c, "MSG0 %ld|3", msglist[i]);
343 ctdl_readline(c, cbuf, sizeof(cbuf));
345 while (ctdl_readline(c, cbuf, sizeof(cbuf)), strcmp(cbuf, "000")) {
346 if ((enumerate_by_euid) && (!strncasecmp(cbuf, "exti=", 5))) {
347 // e = strdup(&cbuf[5]);
348 int elen = (2 * strlen(&cbuf[5]));
350 urlesc(e, elen, &cbuf[5]);
352 if (!strncasecmp(cbuf, "time=", 5)) {
353 timestamp = atol(&cbuf[5]);
358 sprintf(e, "%ld", msglist[i]);
360 StrBufAppendPrintf(Buf, "<D:response>");
362 // Generate the 'href' tag for this message
363 StrBufAppendPrintf(Buf, "<D:href>");
364 StrBufXMLEscAppend(Buf, NULL, h->site_prefix, strlen(h->site_prefix), 0);
365 StrBufAppendPrintf(Buf, "/ctdl/r/");
366 StrBufXMLEscAppend(Buf, NULL, c->room, strlen(c->room), 0);
367 StrBufAppendPrintf(Buf, "/");
368 StrBufXMLEscAppend(Buf, NULL, e, strlen(e), 0);
369 StrBufAppendPrintf(Buf, "</D:href>");
370 StrBufAppendPrintf(Buf, "<D:propstat>");
371 StrBufAppendPrintf(Buf, "<D:status>HTTP/1.1 200 OK</D:status>");
372 StrBufAppendPrintf(Buf, "<D:prop>");
374 switch (c->room_default_view) {
376 StrBufAppendPrintf(Buf,
377 "<D:getcontenttype>text/calendar; component=vevent</D:getcontenttype>");
380 StrBufAppendPrintf(Buf,
381 "<D:getcontenttype>text/calendar; component=vtodo</D:getcontenttype>");
383 case VIEW_ADDRESSBOOK:
384 StrBufAppendPrintf(Buf, "<D:getcontenttype>text/x-vcard</D:getcontenttype>");
389 char *datestring = http_datestring(timestamp);
391 StrBufAppendPrintf(Buf, "<D:getlastmodified>");
392 StrBufXMLEscAppend(Buf, NULL, datestring, strlen(datestring), 0);
393 StrBufAppendPrintf(Buf, "</D:getlastmodified>");
396 if (enumerate_by_euid) { // FIXME ajc 2017oct30 should this be inside the timestamp conditional?
397 StrBufAppendPrintf(Buf, "<D:getetag>\"%ld\"</D:getetag>", msglist[i]);
400 StrBufAppendPrintf(Buf, "</D:prop></D:propstat></D:response>\n");
408 StrBufAppendPrintf(Buf, "</D:multistatus>\n");
410 add_response_header(h, strdup("Content-type"), strdup("text/xml"));
411 h->response_code = 207;
412 h->response_string = strdup("Multi-Status");
413 h->response_body_length = StrLength(Buf);
414 h->response_body = SmashStrBuf(&Buf);
417 // some good examples here
418 // http://blogs.nologin.es/rickyepoderi/index.php?/archives/14-Introducing-CalDAV-Part-I.html
421 // Called by the_room_itself() when the HTTP method is PROPFIND
422 void get_the_room_itself(struct http_transaction *h, struct ctdlsession *c) {
423 JsonValue *j = NewJsonObject(HKEY("gotoroom"));
425 JsonObjectAppend(j, NewJsonPlainString(HKEY("name"), c->room, -1));
426 JsonObjectAppend(j, NewJsonNumber(HKEY("current_view"), c->room_current_view));
427 JsonObjectAppend(j, NewJsonNumber(HKEY("default_view"), c->room_default_view));
428 JsonObjectAppend(j, NewJsonNumber(HKEY("new_messages"), c->new_messages));
429 JsonObjectAppend(j, NewJsonNumber(HKEY("total_messages"), c->total_messages));
430 JsonObjectAppend(j, NewJsonNumber(HKEY("last_seen"), c->last_seen));
432 StrBuf *sj = NewStrBuf();
433 SerializeJson(sj, j, 1); // '1' == free the source array
435 add_response_header(h, strdup("Content-type"), strdup("application/json"));
436 h->response_code = 200;
437 h->response_string = strdup("OK");
438 h->response_body_length = StrLength(sj);
439 h->response_body = SmashStrBuf(&sj);
444 // Handle REST/DAV requests for the room itself (such as /ctdl/r/roomname
445 // or /ctdl/r/roomname/ but *not* specific objects within the room)
446 void the_room_itself(struct http_transaction *h, struct ctdlsession *c) {
448 // OPTIONS method on the room itself usually is a DAV client assessing what's here.
449 if (!strcasecmp(h->method, "OPTIONS")) {
450 options_the_room_itself(h, c);
454 // PROPFIND method on the room itself could be looking for a directory
455 if (!strcasecmp(h->method, "PROPFIND")) {
456 propfind_the_room_itself(h, c);
460 // REPORT method on the room itself is probably the dreaded CalDAV tower-of-crapola
461 if (!strcasecmp(h->method, "REPORT")) {
462 report_the_room_itself(h, c);
466 // GET method on the room itself is an API call, possibly from our JavaScript front end
467 if (!strcasecmp(h->method, "get")) {
468 get_the_room_itself(h, c);
472 // we probably want a "go to this room" for interactive access
477 // Dispatcher for "/ctdl/r" and "/ctdl/r/" for the room list
478 void room_list(struct http_transaction *h, struct ctdlsession *c) {
482 ctdl_printf(c, "LKRA");
483 ctdl_readline(c, buf, sizeof(buf));
489 JsonValue *j = NewJsonArray(HKEY("lkra"));
490 while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) {
492 // 0 |1 |2 |3 |4 |5 |6 |7 |8
493 // name|QRflags|QRfloor|QRorder|QRflags2|ra|current_view|default_view|mtime
494 JsonValue *jr = NewJsonObject(HKEY("room"));
496 extract_token(roomname, buf, 0, '|', sizeof roomname);
497 JsonObjectAppend(jr, NewJsonPlainString(HKEY("name"), roomname, -1));
499 JsonObjectAppend(jr, NewJsonNumber(HKEY("floor"), extract_int(buf, 2)));
500 JsonObjectAppend(jr, NewJsonNumber(HKEY("rorder"), extract_int(buf, 3)));
502 int ra = extract_int(buf, 5);
503 JsonObjectAppend(jr, NewJsonBool(HKEY("known"), (ra & UA_KNOWN)));
504 JsonObjectAppend(jr, NewJsonBool(HKEY("hasnewmsgs"), (ra & UA_HASNEWMSGS)));
506 JsonObjectAppend(jr, NewJsonNumber(HKEY("current_view"), extract_int(buf, 6)));
507 JsonObjectAppend(jr, NewJsonNumber(HKEY("default_view"), extract_int(buf, 7)));
508 JsonObjectAppend(jr, NewJsonNumber(HKEY("mtime"), extract_int(buf, 8)));
510 JsonArrayAppend(j, jr); // add the room to the array
513 StrBuf *sj = NewStrBuf();
514 SerializeJson(sj, j, 1); // '1' == free the source array
516 add_response_header(h, strdup("Content-type"), strdup("application/json"));
517 h->response_code = 200;
518 h->response_string = strdup("OK");
519 h->response_body_length = StrLength(sj);
520 h->response_body = SmashStrBuf(&sj);
524 // Dispatcher for paths starting with /ctdl/r/
525 void ctdl_r(struct http_transaction *h, struct ctdlsession *c) {
526 char requested_roomname[128];
529 // All room-related functions require being "in" the room specified. Are we in that room already?
530 extract_token(requested_roomname, h->url, 3, '/', sizeof requested_roomname);
531 unescape_input(requested_roomname);
533 if (IsEmptyStr(requested_roomname)) { // /ctdl/r/
537 // If not, try to go there.
538 if (strcasecmp(requested_roomname, c->room)) {
539 ctdl_printf(c, "GOTO %s", requested_roomname);
540 ctdl_readline(c, buf, sizeof(buf));
542 // buf[3] will indicate whether any instant messages are waiting
543 extract_token(c->room, &buf[4], 0, '|', sizeof c->room);
544 c->new_messages = extract_int(&buf[4], 1);
545 c->total_messages = extract_int(&buf[4], 2);
546 // 3 (int)info Info flag: set to nonzero if the user needs to read this room's info file
547 // 4 (int)CC->room.QRflags Various flags associated with this room.
548 // 5 (long)CC->room.QRhighest The highest message number present in this room
549 c->last_seen = extract_long(&buf[4], 6); // The highest message number the user has read in this room
550 // 7 (int)rmailflag Boolean flag: 1 if this is a Mail> room, 0 otherwise.
551 c->is_room_aide = extract_int(&buf[4], 8);
552 // 9 (int)newmailcount The number of new Mail messages the user has
553 // 10 (int)CC->room.QRfloor The floor number this room resides on
554 c->room_current_view = extract_int(&buf[4], 11);
555 c->room_default_view = extract_int(&buf[4], 12);
556 // 13 (int)is_trash Boolean flag: 1 if this is the user's Trash folder, 0 otherwise.
557 // 14 (int)CC->room.QRflags2 More flags associated with this room
558 // 15 (long)CC->room.QRmtime Timestamp of the last write activity in this room
565 // At this point our Citadel client session is "in" the specified room.
567 if (num_tokens(h->url, '/') == 4) { // /ctdl/r/roomname
568 the_room_itself(h, c);
572 extract_token(buf, h->url, 4, '/', sizeof buf);
573 if (num_tokens(h->url, '/') == 5) {
574 if (IsEmptyStr(buf)) {
575 the_room_itself(h, c); // /ctdl/r/roomname/ ( same as /ctdl/r/roomname )
578 object_in_room(h, c); // /ctdl/r/roomname/object
582 if (num_tokens(h->url, '/') == 6) {
583 object_in_room(h, c); // /ctdl/r/roomname/object/ or possibly /ctdl/r/roomname/object/component
586 // If we get to this point, the client specified a valid room but requested an action we don't know how to perform.