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