Skeleton code for filters.
[citadel.git] / webcit-ng / server / room_functions.c
1 // Room functions
2 //
3 // Copyright (c) 1996-2024 by the citadel.org team
4 //
5 // This program is open source software.  Use, duplication, or disclosure is subject to the GNU General Public License v3.
6
7 #include "webcit.h"
8
9
10 // Return an array of message numbers in the current room.
11 // Caller owns the memory and must free it.  Returns NULL if any problems.
12 Array *get_msglist(struct ctdlsession *c, char *which_msgs) {
13         char buf[1024];
14         Array *msglist = NULL;
15
16         msglist = array_new(sizeof(long));
17         if (msglist == NULL) {
18                 return(NULL);
19         }
20
21         ctdl_printf(c, "MSGS %s", which_msgs);
22         ctdl_readline(c, buf, sizeof(buf));
23         if (buf[0] == '1') {
24                 while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) { 
25                         long m = atol(buf);
26                         array_append(msglist, &m);
27                 }
28         }
29
30         return msglist;
31 }
32
33
34 // Supplied with a list of potential matches from an If-Match: or If-None-Match: header, and
35 // a message number (which we always use as the entity tag in Citadel), return nonzero if the
36 // message number matches any of the supplied tags in the string.
37 int match_etags(char *taglist, long msgnum) {
38         int num_tags = num_tokens(taglist, ',');
39         int i = 0;
40         char tag[1024];
41
42         if (msgnum <= 0) {                                      // no msgnum?  no match.
43                 return (0);
44         }
45
46         for (i = 0; i < num_tags; ++i) {
47                 extract_token(tag, taglist, i, ',', sizeof tag);
48                 string_trim(tag);
49                 char *lq = (strchr(tag, '"'));
50                 char *rq = (strrchr(tag, '"'));
51                 if (lq < rq) {                                  // has two double quotes
52                         strcpy(rq, "");
53                         strcpy(tag, ++lq);
54                 }
55                 string_trim(tag);
56                 if (!strcmp(tag, "*")) {                        // wildcard match
57                         return (1);
58                 }
59                 long tagmsgnum = atol(tag);
60                 if ((tagmsgnum > 0) && (tagmsgnum == msgnum)) { // match
61                         return (1);
62                 }
63         }
64
65         return (0);                                             // no match
66 }
67
68
69 // Client is requesting a STAT (name and modification time) of the current room
70 void json_stat(struct http_transaction *h, struct ctdlsession *c) {
71         char buf[1024];
72         char field[1024];
73
74         ctdl_printf(c, "STAT");
75         ctdl_readline(c, buf, sizeof(buf));
76         if (buf[0] == '2') {
77                 JsonValue *j = NewJsonObject(HKEY("stat"));
78                 extract_token(field, &buf[4], 0, '|', sizeof field);
79                 JsonObjectAppend(j, NewJsonPlainString(HKEY("name"), field, -1));
80                 JsonObjectAppend(j, NewJsonNumber(HKEY("room_mtime"), extract_long(&buf[4], 1)));
81
82                 StrBuf *sj = NewStrBuf();
83                 SerializeJson(sj, j, 1);                                // '1' == free the source array
84                 add_response_header(h, strdup("Content-type"), strdup("application/json"));
85                 h->response_code = 200;
86                 h->response_string = strdup("OK");
87                 h->response_body_length = StrLength(sj);
88                 h->response_body = SmashStrBuf(&sj);
89         }
90         else {
91                 do_404(h);
92         }
93         return;
94 }
95
96
97 // Client is requesting a mailbox summary of the current room
98 void json_mailbox(struct http_transaction *h, struct ctdlsession *c) {
99         char buf[1024];
100         char field[1024];
101         JsonValue *j = NewJsonArray(HKEY("msgs"));
102
103         ctdl_printf(c, "MSGS ALL|||9");                         // "9" as the fourth parameter delivers a mailbox summary
104         ctdl_readline(c, buf, sizeof(buf));
105         if (buf[0] == '1') {
106                 while (ctdl_readline(c, buf, sizeof(buf)), (strcmp(buf, "000"))) {
107                         JsonValue *jmsg = NewJsonObject(HKEY("message"));
108                         JsonObjectAppend(jmsg, NewJsonNumber(HKEY("msgnum"), extract_long(buf, 0)));
109                         JsonObjectAppend(jmsg, NewJsonNumber(HKEY("time"), extract_long(buf, 1)));
110                         extract_token(field, buf, 2, '|', sizeof field);
111                         utf8ify_rfc822_string(field);
112                         JsonObjectAppend(jmsg, NewJsonPlainString(HKEY("author"), field, -1));
113                         extract_token(field, buf, 4, '|', sizeof field);
114                         utf8ify_rfc822_string(field);
115                         JsonObjectAppend(jmsg, NewJsonPlainString(HKEY("addr"), field, -1));
116                         extract_token(field, buf, 5, '|', sizeof field);
117                         utf8ify_rfc822_string(field);
118                         JsonObjectAppend(jmsg, NewJsonPlainString(HKEY("subject"), field, -1));
119                         JsonObjectAppend(jmsg, NewJsonNumber(HKEY("msgidhash"), extract_long(buf, 6)));
120                         extract_token(field, buf, 7, '|', sizeof field);
121                         utf8ify_rfc822_string(field);
122                         JsonObjectAppend(jmsg, NewJsonPlainString(HKEY("references"), field, -1));
123                         JsonArrayAppend(j, jmsg);               // add the message to the array
124                 }
125         }
126
127         StrBuf *sj = NewStrBuf();
128         SerializeJson(sj, j, 1);                                // '1' == free the source array
129
130         add_response_header(h, strdup("Content-type"), strdup("application/json"));
131         h->response_code = 200;
132         h->response_string = strdup("OK");
133         h->response_body_length = StrLength(sj);
134         h->response_body = SmashStrBuf(&sj);
135         return;
136 }
137
138
139 // Client is requesting a message list
140 void json_msglist(struct http_transaction *h, struct ctdlsession *c, char *which) {
141         int i = 0;
142         Array *msglist = get_msglist(c, which);
143         JsonValue *j = NewJsonArray(HKEY("msgs"));
144
145         if (msglist != NULL) {
146                 for (i = 0; i < array_len(msglist); ++i) {
147                         long m;
148                         memcpy(&m, array_get_element_at(msglist, i), sizeof(long));
149                         JsonArrayAppend(j, NewJsonNumber(HKEY("m"), m));
150                 }
151                 array_free(msglist);
152         }
153
154         StrBuf *sj = NewStrBuf();
155         SerializeJson(sj, j, 1);                                // '1' == free the source array
156
157         add_response_header(h, strdup("Content-type"), strdup("application/json"));
158         h->response_code = 200;
159         h->response_string = strdup("OK");
160         h->response_body_length = StrLength(sj);
161         h->response_body = SmashStrBuf(&sj);
162         return;
163 }
164
165
166 // Client is requesting the room info banner
167 void read_room_info_banner(struct http_transaction *h, struct ctdlsession *c) {
168         char buf[1024];
169
170         ctdl_printf(c, "RINF");
171         ctdl_readline(c, buf, sizeof(buf));
172         if (buf[0] == '1') {
173                 StrBuf *info = NewStrBuf();
174                 while (ctdl_readline(c, buf, sizeof(buf)), (strcmp(buf, "000"))){
175                         StrBufAppendPrintf(info, "%s\n", buf);
176                 }
177                 add_response_header(h, strdup("Content-type"), strdup("text/plain"));
178                 h->response_code = 200;
179                 h->response_string = strdup("OK");
180                 h->response_body_length = StrLength(info);
181                 h->response_body = SmashStrBuf(&info);
182                 return;
183         }
184         else {
185                 do_404(h);
186         }
187 }
188
189
190 // Client is setting the "Last Read Pointer" (marking all messages as "seen" up to this message)
191 void set_last_read_pointer(struct http_transaction *h, struct ctdlsession *c) {
192         char cbuf[1024];
193         ctdl_printf(c, "SLRP %d", atoi(get_url_param(h, "last")));
194         ctdl_readline(c, cbuf, sizeof(cbuf));
195         if (cbuf[0] == '2') {
196                 do_204(h);
197         }
198         else {
199                 do_404(h);
200         }
201 }
202
203
204 // Client requested an object in a room.
205 void object_in_room(struct http_transaction *h, struct ctdlsession *c) {
206         char buf[1024];
207         long msgnum = (-1);
208         char unescaped_euid[1024];
209
210         extract_token(buf, h->url, 4, '/', sizeof buf);
211
212         if (!strcasecmp(buf, "mailbox")) {              // Client is requesting a mailbox summary
213                 json_mailbox(h, c);
214                 return;
215         }
216
217         if (!strcasecmp(buf, "stat")) {                 // Client is requesting a stat command (name and modification time)
218                 json_stat(h, c);
219                 return;
220         }
221
222         if (!strncasecmp(buf, "msgs.", 5)) {            // Client is requesting a list of message numbers
223                 unescape_input(&buf[5]);
224                 json_msglist(h, c, &buf[5]);
225                 return;
226         }
227
228         if (!strcasecmp(buf, "info.txt")) {             // Client is requesting the room info banner
229                 read_room_info_banner(h, c);
230                 return;
231         }
232
233         if (!strcasecmp(buf, "slrp")) {                 // Set the Last Read Pointer
234                 set_last_read_pointer(h, c);
235                 return;
236         }
237
238         // If we get to this point, the client is requesting a specific object *in* the room.
239
240         if ((c->room_default_view == VIEW_CALENDAR)     // room types where objects are referenced by EUID
241            || (c->room_default_view == VIEW_TASKS)
242            || (c->room_default_view == VIEW_ADDRESSBOOK)
243            ) {
244                 safestrncpy(unescaped_euid, buf, sizeof unescaped_euid);
245                 unescape_input(unescaped_euid);
246                 msgnum = locate_message_by_uid(c, unescaped_euid);
247         }
248         else {                                          // otherwise the object is being referenced by message number
249                 msgnum = atol(buf);
250         }
251
252         // All methods except PUT require the message to already exist
253         if ((msgnum <= 0) && (strcasecmp(h->method, "PUT"))) {
254                 do_404(h);
255         }
256
257         // If we get to this point we have a valid message number in an accessible room.
258         syslog(LOG_DEBUG, "msgnum is %ld, method is %s", msgnum, h->method);
259
260         // A sixth component in the URL can be one of two things:
261         // (1) a MIME part specifier, in which case the client wants to download that component within the message
262         // (2) a content-type, in which ase the client wants us to try to render it a certain way
263         if (num_tokens(h->url, '/') >= 6) {
264                 extract_token(buf, h->url, 5, '/', sizeof buf);
265                 if (!IsEmptyStr(buf)) {
266                         if (!strcasecmp(buf, "json")) {
267                                 json_render_one_message(h, c, msgnum);
268                         }
269                         else {
270                                 download_mime_component(h, c, msgnum, buf);
271                         }
272                         return;
273                 }
274         }
275
276         // Ok, we want a full message, but first let's check for the if[-none]-match headers.
277         char *if_match = header_val(h, "If-Match");
278         if ((if_match != NULL) && (!match_etags(if_match, msgnum))) {
279                 do_412(h);
280                 return;
281         }
282
283         char *if_none_match = header_val(h, "If-None-Match");
284         if ((if_none_match != NULL) && (match_etags(if_none_match, msgnum))) {
285                 do_412(h);
286                 return;
287         }
288
289         // Now perform the requested operation.
290         if (!strcasecmp(h->method, "DELETE")) {
291                 dav_delete_message(h, c, msgnum);
292         }
293         else if (!strcasecmp(h->method, "GET")) {
294                 dav_get_message(h, c, msgnum);
295         }
296         else if (!strcasecmp(h->method, "PUT")) {
297                 dav_put_message(h, c, unescaped_euid, msgnum);
298         }
299         else if (!strcasecmp(h->method, "MOVE")) {
300                 dav_move_or_copy_message(h, c, msgnum, DAV_MOVE);
301         }
302         else if (!strcasecmp(h->method, "COPY")) {
303                 dav_move_or_copy_message(h, c, msgnum, DAV_COPY);
304         }
305         else {
306                 do_404(h);              // We should never get here, but if we do, a 404 error is clean.
307         }
308 }
309
310
311 // Called by the_room_itself() when the HTTP method is REPORT
312 void report_the_room_itself(struct http_transaction *h, struct ctdlsession *c) {
313         if (c->room_default_view == VIEW_CALENDAR) {
314                 caldav_report(h, c);    // CalDAV REPORTs ... fmgwac
315                 return;
316         }
317
318         do_404(h);                      // future implementations like CardDAV will require code paths here
319 }
320
321
322 // Called by the_room_itself() when the HTTP method is OPTIONS
323 void options_the_room_itself(struct http_transaction *h, struct ctdlsession *c) {
324         h->response_code = 200;
325         h->response_string = strdup("OK");
326         if (c->room_default_view == VIEW_CALENDAR) {
327                 add_response_header(h, strdup("DAV"), strdup("1, calendar-access"));    // offer CalDAV
328         }
329         else if (c->room_default_view == VIEW_ADDRESSBOOK) {
330                 add_response_header(h, strdup("DAV"), strdup("1, addressbook"));        // offer CardDAV
331         }
332         else {
333                 add_response_header(h, strdup("DAV"), strdup("1"));     // ordinary WebDAV for all other room types
334         }
335         add_response_header(h, strdup("Allow"), strdup("OPTIONS, PROPFIND, GET, PUT, REPORT, DELETE, MOVE, COPY"));
336 }
337
338
339 // Called by the_room_itself() when the HTTP method is PROPFIND
340 void propfind_the_room_itself(struct http_transaction *h, struct ctdlsession *c) {
341         char *e;
342         long timestamp;
343         int dav_depth = (header_val(h, "Depth") ? atoi(header_val(h, "Depth")) : INT_MAX);
344         syslog(LOG_DEBUG, "Client PROPFIND requested depth: %d", dav_depth);
345         StrBuf *Buf = NewStrBuf();
346
347         StrBufAppendPrintf(Buf,
348                 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
349                 "<D:multistatus "
350                 "xmlns:D=\"DAV:\" "
351                 "xmlns:C=\"urn:ietf:params:xml:ns:caldav\""
352                 ">"
353         );
354
355         // Transmit the collection resource
356         StrBufAppendPrintf(Buf, "<D:response>");
357         StrBufAppendPrintf(Buf, "<D:href>");
358         StrBufXMLEscAppend(Buf, NULL, h->site_prefix, strlen(h->site_prefix), 0);
359         StrBufAppendPrintf(Buf, "/ctdl/r/");
360         StrBufXMLEscAppend(Buf, NULL, c->room, strlen(c->room), 0);
361         StrBufAppendPrintf(Buf, "</D:href>");
362
363         StrBufAppendPrintf(Buf, "<D:propstat>");
364         StrBufAppendPrintf(Buf, "<D:status>HTTP/1.1 200 OK</D:status>");
365         StrBufAppendPrintf(Buf, "<D:prop>");
366         StrBufAppendPrintf(Buf, "<D:displayname>");
367         StrBufXMLEscAppend(Buf, NULL, c->room, strlen(c->room), 0);
368         StrBufAppendPrintf(Buf, "</D:displayname>");
369
370         StrBufAppendPrintf(Buf, "<D:owner />"); // empty owner ought to be legal; see rfc3744 section 5.1
371
372         StrBufAppendPrintf(Buf, "<D:resourcetype><D:collection />");
373         switch (c->room_default_view) {
374         case VIEW_CALENDAR:
375                 StrBufAppendPrintf(Buf, "<C:calendar />");      // RFC4791 section 4.2
376                 break;
377         }
378         StrBufAppendPrintf(Buf, "</D:resourcetype>");
379
380         int enumerate_by_euid = 0;      // nonzero if messages will be retrieved by euid instead of msgnum
381         switch (c->room_default_view) {
382         case VIEW_CALENDAR:             // RFC4791 section 5.2
383                 StrBufAppendPrintf(Buf, "<C:supported-calendar-component-set><C:comp name=\"VEVENT\"/></C:supported-calendar-component-set>");
384                 StrBufAppendPrintf(Buf, "<C:supported-calendar-data>");
385                 StrBufAppendPrintf(Buf, "<C:calendar-data content-type=\"text/calendar\" version=\"2.0\"/>");
386                 StrBufAppendPrintf(Buf, "</C:supported-calendar-data>");
387                 enumerate_by_euid = 1;
388                 break;
389         case VIEW_TASKS:                // RFC4791 section 5.2
390                 StrBufAppendPrintf(Buf, "<C:supported-calendar-component-set><C:comp name=\"VTODO\"/></C:supported-calendar-component-set>");
391                 StrBufAppendPrintf(Buf, "<C:supported-calendar-data>");
392                 StrBufAppendPrintf(Buf, "<C:calendar-data content-type=\"text/calendar\" version=\"2.0\"/>");
393                 StrBufAppendPrintf(Buf, "</C:supported-calendar-data>");
394                 enumerate_by_euid = 1;
395                 break;
396         case VIEW_ADDRESSBOOK:          // FIXME put some sort of CardDAV crapola here when we implement it
397                 enumerate_by_euid = 1;
398                 break;
399         case VIEW_WIKI:                 // FIXME invent "WikiDAV" ?  The versioning stuff in DAV could be useful.
400                 enumerate_by_euid = 1;
401                 break;
402         }
403
404         // FIXME get the mtime
405         // StrBufAppendPrintf(Buf, "<D:getlastmodified>");
406         // escputs(datestring);
407         // StrBufAppendPrintf(Buf, "</D:getlastmodified>");
408
409         StrBufAppendPrintf(Buf, "</D:prop>");
410         StrBufAppendPrintf(Buf, "</D:propstat>");
411         StrBufAppendPrintf(Buf, "</D:response>\n");
412
413         // If a depth greater than zero was specified, transmit the collection listing
414         // BEGIN COLLECTION
415         if (dav_depth > 0) {
416                 Array *msglist = get_msglist(c, "ALL");
417                 if (msglist) {
418                         int i;
419                         for (i = 0; i < array_len(msglist); ++i) {
420                                 if ((i % 10) == 0) {
421                                         syslog(LOG_DEBUG, "PROPFIND enumerated %d messages", i);
422                                 }
423                                 e = NULL;               // EUID gets stored here
424                                 timestamp = 0;
425
426                                 long m;
427                                 memcpy(&m, array_get_element_at(msglist, i), sizeof(long));
428
429                                 char cbuf[1024];
430                                 ctdl_printf(c, "MSG0 %ld|3", m);
431                                 ctdl_readline(c, cbuf, sizeof(cbuf));
432                                 if (cbuf[0] == '1')
433                                         while (ctdl_readline(c, cbuf, sizeof(cbuf)), strcmp(cbuf, "000")) {
434                                                 if ((enumerate_by_euid) && (!strncasecmp(cbuf, "exti=", 5))) {
435                                                         // e = strdup(&cbuf[5]);
436                                                         int elen = (2 * strlen(&cbuf[5]));
437                                                         e = malloc(elen);
438                                                         urlesc(e, elen, &cbuf[5]);
439                                                 }
440                                                 if (!strncasecmp(cbuf, "time=", 5)) {
441                                                         timestamp = atol(&cbuf[5]);
442                                                 }
443                                         }
444                                 if (e == NULL) {
445                                         e = malloc(20);
446                                         sprintf(e, "%ld", m);
447                                 }
448                                 StrBufAppendPrintf(Buf, "<D:response>");
449
450                                 // Generate the 'href' tag for this message
451                                 StrBufAppendPrintf(Buf, "<D:href>");
452                                 StrBufXMLEscAppend(Buf, NULL, h->site_prefix, strlen(h->site_prefix), 0);
453                                 StrBufAppendPrintf(Buf, "/ctdl/r/");
454                                 StrBufXMLEscAppend(Buf, NULL, c->room, strlen(c->room), 0);
455                                 StrBufAppendPrintf(Buf, "/");
456                                 StrBufXMLEscAppend(Buf, NULL, e, strlen(e), 0);
457                                 StrBufAppendPrintf(Buf, "</D:href>");
458                                 StrBufAppendPrintf(Buf, "<D:propstat>");
459                                 StrBufAppendPrintf(Buf, "<D:status>HTTP/1.1 200 OK</D:status>");
460                                 StrBufAppendPrintf(Buf, "<D:prop>");
461
462                                 switch (c->room_default_view) {
463                                 case VIEW_CALENDAR:
464                                         StrBufAppendPrintf(Buf,
465                                                            "<D:getcontenttype>text/calendar; component=vevent</D:getcontenttype>");
466                                         break;
467                                 case VIEW_TASKS:
468                                         StrBufAppendPrintf(Buf,
469                                                            "<D:getcontenttype>text/calendar; component=vtodo</D:getcontenttype>");
470                                         break;
471                                 case VIEW_ADDRESSBOOK:
472                                         StrBufAppendPrintf(Buf, "<D:getcontenttype>text/x-vcard</D:getcontenttype>");
473                                         break;
474                                 }
475
476                                 if (timestamp > 0) {
477                                         char *datestring = http_datestring(timestamp);
478                                         if (datestring) {
479                                                 StrBufAppendPrintf(Buf, "<D:getlastmodified>");
480                                                 StrBufXMLEscAppend(Buf, NULL, datestring, strlen(datestring), 0);
481                                                 StrBufAppendPrintf(Buf, "</D:getlastmodified>");
482                                                 free(datestring);
483                                         }
484                                         if (enumerate_by_euid) {        // FIXME ajc 2017oct30 should this be inside the timestamp conditional?
485                                                 StrBufAppendPrintf(Buf, "<D:getetag>\"%ld\"</D:getetag>", m);
486                                         }
487                                 }
488                                 StrBufAppendPrintf(Buf, "</D:prop></D:propstat></D:response>\n");
489                                 free(e);
490                         }
491                         array_free(msglist);
492                 };
493         }
494         // END COLLECTION
495
496         StrBufAppendPrintf(Buf, "</D:multistatus>\n");
497
498         add_response_header(h, strdup("Content-type"), strdup("text/xml"));
499         h->response_code = 207;
500         h->response_string = strdup("Multi-Status");
501         h->response_body_length = StrLength(Buf);
502         h->response_body = SmashStrBuf(&Buf);
503 }
504
505 // some good examples here
506 // http://blogs.nologin.es/rickyepoderi/index.php?/archives/14-Introducing-CalDAV-Part-I.html
507
508
509 // Called by the_room_itself() when the HTTP method is PROPFIND
510 void get_the_room_itself(struct http_transaction *h, struct ctdlsession *c) {
511         JsonValue *j = NewJsonObject(HKEY("gotoroom"));
512
513         JsonObjectAppend(j, NewJsonPlainString(HKEY("name"), c->room, -1));
514         JsonObjectAppend(j, NewJsonNumber(HKEY("current_view"), c->room_current_view));
515         JsonObjectAppend(j, NewJsonNumber(HKEY("default_view"), c->room_default_view));
516         JsonObjectAppend(j, NewJsonNumber(HKEY("is_room_aide"), c->is_room_aide));
517         JsonObjectAppend(j, NewJsonNumber(HKEY("is_trash_folder"), c->is_trash_folder));
518         JsonObjectAppend(j, NewJsonNumber(HKEY("can_delete_messages"), c->can_delete_messages));
519         JsonObjectAppend(j, NewJsonNumber(HKEY("new_messages"), c->new_messages));
520         JsonObjectAppend(j, NewJsonNumber(HKEY("total_messages"), c->total_messages));
521         JsonObjectAppend(j, NewJsonNumber(HKEY("last_seen"), c->last_seen));
522         JsonObjectAppend(j, NewJsonNumber(HKEY("room_mtime"), c->room_mtime));
523
524         StrBuf *sj = NewStrBuf();
525         SerializeJson(sj, j, 1);        // '1' == free the source array
526
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);
532         return;
533 }
534
535
536 // Handle REST/DAV requests for the room itself (such as /ctdl/r/roomname
537 // or /ctdl/r/roomname/ but *not* specific objects within the room)
538 void the_room_itself(struct http_transaction *h, struct ctdlsession *c) {
539
540         // OPTIONS method on the room itself usually is a DAV client assessing what's here.
541         if (!strcasecmp(h->method, "OPTIONS")) {
542                 options_the_room_itself(h, c);
543                 return;
544         }
545
546         // PROPFIND method on the room itself could be looking for a directory
547         if (!strcasecmp(h->method, "PROPFIND")) {
548                 propfind_the_room_itself(h, c);
549                 return;
550         }
551
552         // REPORT method on the room itself is probably the dreaded CalDAV tower-of-crapola
553         if (!strcasecmp(h->method, "REPORT")) {
554                 report_the_room_itself(h, c);
555                 return;
556         }
557
558         // GET method on the room itself is an API call, possibly from our JavaScript front end
559         if (!strcasecmp(h->method, "get")) {
560                 get_the_room_itself(h, c);
561                 return;
562         }
563
564         // we probably want a "go to this room" for interactive access
565         do_404(h);
566 }
567
568
569 // Dispatcher for "/ctdl/r" and "/ctdl/r/" for the room list
570 void room_list(struct http_transaction *h, struct ctdlsession *c) {
571         char buf[1024];
572         char roomname[1024];
573
574         ctdl_printf(c, "LKRA");
575         ctdl_readline(c, buf, sizeof(buf));
576         if (buf[0] != '1') {
577                 do_502(h);
578                 return;
579         }
580
581         JsonValue *j = NewJsonArray(HKEY("lkra"));
582         while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) {
583
584                 // 0   |1      |2      |3      |4       |5 |6           |7           |8
585                 // name|QRflags|QRfloor|QRorder|QRflags2|ra|current_view|default_view|mtime
586                 JsonValue *jr = NewJsonObject(HKEY("room"));
587
588                 extract_token(roomname, buf, 0, '|', sizeof roomname);
589                 JsonObjectAppend(jr, NewJsonPlainString(HKEY("name"), roomname, -1));
590
591                 JsonObjectAppend(jr, NewJsonNumber(HKEY("floor"), extract_int(buf, 2)));
592                 JsonObjectAppend(jr, NewJsonNumber(HKEY("rorder"), extract_int(buf, 3)));
593
594                 int ra = extract_int(buf, 5);
595                 JsonObjectAppend(jr, NewJsonBool(HKEY("known"), (ra & UA_KNOWN)));
596                 JsonObjectAppend(jr, NewJsonBool(HKEY("hasnewmsgs"), (ra & UA_HASNEWMSGS)));
597
598                 JsonObjectAppend(jr, NewJsonNumber(HKEY("current_view"), extract_int(buf, 6)));
599                 JsonObjectAppend(jr, NewJsonNumber(HKEY("default_view"), extract_int(buf, 7)));
600                 JsonObjectAppend(jr, NewJsonNumber(HKEY("mtime"), extract_int(buf, 8)));
601
602                 JsonArrayAppend(j, jr); // add the room to the array
603         }
604
605         StrBuf *sj = NewStrBuf();
606         SerializeJson(sj, j, 1);        // '1' == free the source array
607
608         add_response_header(h, strdup("Content-type"), strdup("application/json"));
609         h->response_code = 200;
610         h->response_string = strdup("OK");
611         h->response_body_length = StrLength(sj);
612         h->response_body = SmashStrBuf(&sj);
613 }
614
615
616 // Dispatcher for paths starting with /ctdl/r/
617 void ctdl_r(struct http_transaction *h, struct ctdlsession *c) {
618         char requested_roomname[128];
619         char buf[1024];
620         long room_flags = 0;
621         long room_flags2 = 0;
622
623         // All room-related functions require being "in" the room specified.  Are we in that room already?
624         extract_token(requested_roomname, h->url, 3, '/', sizeof requested_roomname);
625         unescape_input(requested_roomname);
626
627         if (IsEmptyStr(requested_roomname)) {   //      /ctdl/r/
628                 room_list(h, c);
629                 return;
630         }
631         // If not, try to go there.
632         if (strcasecmp(requested_roomname, c->room)) {
633                 ctdl_printf(c, "GOTO %s", requested_roomname);
634                 ctdl_readline(c, buf, sizeof(buf));
635                 if (buf[0] == '2') {
636                         // buf[3] will indicate whether any instant messages are waiting
637                         extract_token(c->room, &buf[4], 0, '|', sizeof c->room);
638                         c->new_messages = extract_int(&buf[4], 1);
639                         c->total_messages = extract_int(&buf[4], 2);
640                         //      3       (int)info                       Info flag: set to nonzero if the user needs to read this room's info file
641                         room_flags = extract_long(&buf[4], 3);          // Various flags associated with this room.
642                         //      5       (long)CC->room.QRhighest        The highest message number present in this room
643                         c->last_seen = extract_long(&buf[4], 6);        // The highest message number the user has read in this room
644                         //      7       (int)rmailflag                  Boolean flag: 1 if this is a Mail> room, 0 otherwise.
645                         c->is_room_aide = extract_int(&buf[4], 8);
646                         //      9                                       This position is no longer used
647                         //      10      (int)CC->room.QRfloor           The floor number this room resides on
648                         c->room_current_view = extract_int(&buf[4], 11);
649                         c->room_default_view = extract_int(&buf[4], 12);
650                         c->is_trash_folder  = extract_int(&buf[4], 13); // Boolean flag: 1 if this is the user's Trash folder, 0 otherwise.
651                         room_flags2 = extract_long(&buf[4], 14);        // More flags associated with this room.
652                         c->room_mtime = extract_long(&buf[4], 15);      // Timestamp of the last write activity in this room
653
654                         // If any of these three conditions are met, let the client know it has permission to delete messages.
655                         if ((c->is_room_aide) || (room_flags & QR_MAILBOX) || (room_flags2 & QR2_COLLABDEL)) {
656                                 c->can_delete_messages = 1;
657                         }
658                         else {
659                                 c->can_delete_messages = 0;
660                         }
661                 }
662                 else {
663                         do_404(h);
664                         return;
665                 }
666         }
667         // At this point our Citadel client session is "in" the specified room.
668
669         if (num_tokens(h->url, '/') == 4) {     //      /ctdl/r/roomname
670                 the_room_itself(h, c);
671                 return;
672         }
673
674         extract_token(buf, h->url, 4, '/', sizeof buf);
675         if (num_tokens(h->url, '/') == 5) {
676                 if (IsEmptyStr(buf)) {
677                         the_room_itself(h, c);  //      /ctdl/r/roomname/       ( same as /ctdl/r/roomname )
678                 }
679                 else {
680                         object_in_room(h, c);   //      /ctdl/r/roomname/object
681                 }
682                 return;
683         }
684         if (num_tokens(h->url, '/') >= 6) {
685                 object_in_room(h, c);   //      /ctdl/r/roomname/object/ or possibly /ctdl/r/roomname/object/component
686                 return;
687         }
688         // If we get to this point, the client specified a valid room but requested an action we don't know how to perform.
689         do_404(h);
690 }