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