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