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