More distancing of our project from Richard Marx Stallman's linguistic communism
[citadel.git] / webcit-ng / room_functions.c
index 4d166e320310d490739cff37a110b9bd555bc223..dbb6321bde35cfbb1e45e497f78b8ada62a3fb4a 100644 (file)
@@ -1,16 +1,17 @@
-/*
- * Room functions
- *
- * Copyright (c) 1996-2017 by the citadel.org team
- *
- * This program is open source software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 3.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- */
+//
+// Room functions
+//
+// Copyright (c) 1996-2019 by the citadel.org team
+//
+// This program is open source software.  It runs great on the
+// Linux operating system (and probably elsewhere).  You can use,
+// copy, and run it under the terms of the GNU General Public
+// License version 3.  Richard Stallman is an asshole communist.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
 
 #include "webcit.h"
 
@@ -28,21 +29,21 @@ long *get_msglist(struct ctdlsession *c, char *which_msgs)
 
        ctdl_printf(c, "MSGS %s", which_msgs);
        ctdl_readline(c, buf, sizeof(buf));
-       if (buf[0] == '1') do
-       {
-               if (num_msgs >= num_alloc) {
-                       if (num_alloc == 0) {
-                               num_alloc = 1024;
-                               msglist = malloc(num_alloc * sizeof(long));
-                       }
-                       else {
-                               num_alloc *= 2;
-                               msglist = realloc(msglist, num_alloc * sizeof(long));
+       if (buf[0] == '1') {
+               do {
+                       if (num_msgs >= num_alloc) {
+                               if (num_alloc == 0) {
+                                       num_alloc = 1024;
+                                       msglist = malloc(num_alloc * sizeof(long));
+                               } else {
+                                       num_alloc *= 2;
+                                       msglist = realloc(msglist, num_alloc * sizeof(long));
+                               }
                        }
-               }
-               ctdl_readline(c, buf, sizeof(buf));
-               msglist[num_msgs++] = atol(buf);
-       } while (strcmp(buf, "000"));                           // this makes the last element a "0" terminator
+                       ctdl_readline(c, buf, sizeof(buf));
+                       msglist[num_msgs++] = atol(buf);
+               } while (strcmp(buf, "000"));   // this makes the last element a "0" terminator
+       }
        return msglist;
 }
 
@@ -55,14 +56,14 @@ long *get_msglist(struct ctdlsession *c, char *which_msgs)
 int match_etags(char *taglist, long msgnum)
 {
        int num_tags = num_tokens(taglist, ',');
-       int i=0;
+       int i = 0;
        char tag[1024];
 
        if (msgnum <= 0) {                                      // no msgnum?  no match.
-               return(0);
+               return (0);
        }
 
-       for (i=0; i<num_tags; ++i) {
+       for (i = 0; i < num_tags; ++i) {
                extract_token(tag, taglist, i, ',', sizeof tag);
                striplt(tag);
                char *lq = (strchr(tag, '"'));
@@ -72,16 +73,16 @@ int match_etags(char *taglist, long msgnum)
                        strcpy(tag, ++lq);
                }
                striplt(tag);
-               if (!strcmp(tag, "*")) {
-                       return(1);                              // wildcard match
+               if (!strcmp(tag, "*")) {                        // wildcard match
+                       return (1);
                }
                long tagmsgnum = atol(tag);
-               if ( (tagmsgnum > 0) && (tagmsgnum == msgnum) ) {       // match
-                       return(1);
+               if ((tagmsgnum > 0) && (tagmsgnum == msgnum)) { // match
+                       return (1);
                }
        }
 
-       return(0);                                              // no match
+       return (0);                                             // no match
 }
 
 
@@ -95,14 +96,14 @@ void json_msglist(struct http_transaction *h, struct ctdlsession *c, char *which
        JsonValue *j = NewJsonArray(HKEY("msgs"));
 
        if (msglist != NULL) {
-               for (i=0; msglist[i]>0 ; ++i) {
-                       JsonArrayAppend(j, NewJsonNumber( HKEY("m"), msglist[i]));
+               for (i = 0; msglist[i] > 0; ++i) {
+                       JsonArrayAppend(j, NewJsonNumber(HKEY("m"), msglist[i]));
                }
                free(msglist);
        }
 
        StrBuf *sj = NewStrBuf();
-       SerializeJson(sj, j, 1);                        // '1' == free the source array
+       SerializeJson(sj, j, 1);        // '1' == free the source array
 
        add_response_header(h, strdup("Content-type"), strdup("application/json"));
        h->response_code = 200;
@@ -110,8 +111,6 @@ void json_msglist(struct http_transaction *h, struct ctdlsession *c, char *which
        h->response_body_length = StrLength(sj);
        h->response_body = SmashStrBuf(&sj);
        return;
-
-
 }
 
 
@@ -126,32 +125,40 @@ void object_in_room(struct http_transaction *h, struct ctdlsession *c)
 
        extract_token(buf, h->uri, 4, '/', sizeof buf);
 
-       if (!strncasecmp(buf, "msgs.", 5)) {                    // Client is requesting a list of message numbers
+       if (!strncasecmp(buf, "msgs.", 5))      // Client is requesting a list of message numbers
+       {
                json_msglist(h, c, &buf[5]);
                return;
        }
-
-       if (!strncasecmp(buf, "threads", 5)) {                  // Client is requesting a threaded view (still kind of fuzzy here)
+#if 0
+       if (!strncasecmp(buf, "threads", 5))    // Client is requesting a threaded view (still kind of fuzzy here)
+       {
                threaded_view(h, c, &buf[5]);
                return;
        }
 
-       if (    (c->room_default_view == VIEW_CALENDAR)         // room types where objects are referenced by EUID
-               || (c->room_default_view == VIEW_TASKS)
-               || (c->room_default_view == VIEW_ADDRESSBOOK)
-       ) {
+       if (!strncasecmp(buf, "flat", 5))       // Client is requesting a flat view (still kind of fuzzy here)
+       {
+               flat_view(h, c, &buf[5]);
+               return;
+       }
+#endif
+
+       if ((c->room_default_view == VIEW_CALENDAR)     // room types where objects are referenced by EUID
+           || (c->room_default_view == VIEW_TASKS)
+           || (c->room_default_view == VIEW_ADDRESSBOOK)
+           ) {
                safestrncpy(unescaped_euid, buf, sizeof unescaped_euid);
                unescape_input(unescaped_euid);
                msgnum = locate_message_by_uid(c, unescaped_euid);
-       }
-       else {
+       } else {
                msgnum = atol(buf);
        }
 
        /*
         * All methods except PUT require the message to already exist
         */
-       if ( (msgnum <= 0) && (strcasecmp(h->method, "PUT")) ) {
+       if ((msgnum <= 0) && (strcasecmp(h->method, "PUT"))) {
                do_404(h);
        }
 
@@ -160,14 +167,19 @@ void object_in_room(struct http_transaction *h, struct ctdlsession *c)
         */
        syslog(LOG_DEBUG, "msgnum is %ld, method is %s", msgnum, h->method);
 
-
        /*
-        * Was the client actually requesting a specific component within the message?
+        * A sixth component in the URL can be one of two things:
+        * (1) a MIME part specifier, in which case the client wants to download that component within the message
+        * (2) a content-type, in which ase the client wants us to try to render it a certain way
         */
        if (num_tokens(h->uri, '/') == 6) {
                extract_token(buf, h->uri, 5, '/', sizeof buf);
                if (!IsEmptyStr(buf)) {
-                       download_mime_component(h, c, msgnum, buf);
+                       if (!strcasecmp(buf, "json")) {
+                               json_render_one_message(h, c, msgnum);
+                       } else {
+                               download_mime_component(h, c, msgnum, buf);
+                       }
                        return;
                }
        }
@@ -176,13 +188,13 @@ void object_in_room(struct http_transaction *h, struct ctdlsession *c)
         * Ok, we want a full message, but first let's check for the if[-none]-match headers.
         */
        char *if_match = header_val(h, "If-Match");
-       if ( (if_match != NULL) && (!match_etags(if_match, msgnum)) ) {
+       if ((if_match != NULL) && (!match_etags(if_match, msgnum))) {
                do_412(h);
                return;
        }
 
        char *if_none_match = header_val(h, "If-None-Match");
-       if ( (if_none_match != NULL) && (match_etags(if_none_match, msgnum)) ) {
+       if ((if_none_match != NULL) && (match_etags(if_none_match, msgnum))) {
                do_412(h);
                return;
        }
@@ -193,15 +205,12 @@ void object_in_room(struct http_transaction *h, struct ctdlsession *c)
 
        if (!strcasecmp(h->method, "DELETE")) {
                dav_delete_message(h, c, msgnum);
-       }
-       else if (!strcasecmp(h->method, "GET")) {
+       } else if (!strcasecmp(h->method, "GET")) {
                dav_get_message(h, c, msgnum);
-       }
-       else if (!strcasecmp(h->method, "PUT")) {
+       } else if (!strcasecmp(h->method, "PUT")) {
                dav_put_message(h, c, unescaped_euid, msgnum);
-       }
-       else {
-               do_404(h);                                      // Got this far but the method made no sense?  Bummer.
+       } else {
+               do_404(h);      // Got this far but the method made no sense?  Bummer.
        }
 
 }
@@ -213,11 +222,11 @@ void object_in_room(struct http_transaction *h, struct ctdlsession *c)
 void report_the_room_itself(struct http_transaction *h, struct ctdlsession *c)
 {
        if (c->room_default_view == VIEW_CALENDAR) {
-               caldav_report(h, c);                            // CalDAV REPORTs ... fmgwac
+               caldav_report(h, c);    // CalDAV REPORTs ... fmgwac
                return;
        }
 
-       do_404(h);      // future implementations like CardDAV will require code paths here
+       do_404(h);              // future implementations like CardDAV will require code paths here
 }
 
 
@@ -230,12 +239,10 @@ void options_the_room_itself(struct http_transaction *h, struct ctdlsession *c)
        h->response_string = strdup("OK");
        if (c->room_default_view == VIEW_CALENDAR) {
                add_response_header(h, strdup("DAV"), strdup("1, calendar-access"));    // offer CalDAV
-       }
-       else if (c->room_default_view == VIEW_ADDRESSBOOK) {
+       } else if (c->room_default_view == VIEW_ADDRESSBOOK) {
                add_response_header(h, strdup("DAV"), strdup("1, addressbook"));        // offer CardDAV
-       }
-       else {
-               add_response_header(h, strdup("DAV"), strdup("1"));                     // ordinary WebDAV for all other room types
+       } else {
+               add_response_header(h, strdup("DAV"), strdup("1"));     // ordinary WebDAV for all other room types
        }
        add_response_header(h, strdup("Allow"), strdup("OPTIONS, PROPFIND, GET, PUT, REPORT, DELETE"));
 }
@@ -253,11 +260,7 @@ void propfind_the_room_itself(struct http_transaction *h, struct ctdlsession *c)
        StrBuf *Buf = NewStrBuf();
 
        StrBufAppendPrintf(Buf, "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
-               "<D:multistatus "
-                       "xmlns:D=\"DAV:\" "
-                       "xmlns:C=\"urn:ietf:params:xml:ns:caldav\""
-               ">"
-       );
+                          "<D:multistatus " "xmlns:D=\"DAV:\" " "xmlns:C=\"urn:ietf:params:xml:ns:caldav\"" ">");
 
        /* Transmit the collection resource */
        StrBufAppendPrintf(Buf, "<D:response>");
@@ -274,46 +277,48 @@ void propfind_the_room_itself(struct http_transaction *h, struct ctdlsession *c)
        StrBufXMLEscAppend(Buf, NULL, c->room, strlen(c->room), 0);
        StrBufAppendPrintf(Buf, "</D:displayname>");
 
-       StrBufAppendPrintf(Buf, "<D:owner />");         // empty owner ought to be legal; see rfc3744 section 5.1
+       StrBufAppendPrintf(Buf, "<D:owner />"); // empty owner ought to be legal; see rfc3744 section 5.1
 
        StrBufAppendPrintf(Buf, "<D:resourcetype><D:collection />");
-       switch(c->room_default_view) {
-               case VIEW_CALENDAR:
-                       StrBufAppendPrintf(Buf, "<C:calendar />");      // RFC4791 section 4.2
-                       break;
+       switch (c->room_default_view) {
+       case VIEW_CALENDAR:
+               StrBufAppendPrintf(Buf, "<C:calendar />");      // RFC4791 section 4.2
+               break;
        }
        StrBufAppendPrintf(Buf, "</D:resourcetype>");
 
-       int enumerate_by_euid = 0;                      // nonzero if messages will be retrieved by euid instead of msgnum
-       switch(c->room_default_view) {
-               case VIEW_CALENDAR:                     // RFC4791 section 5.2
-                       StrBufAppendPrintf(Buf, "<C:supported-calendar-component-set><C:comp name=\"VEVENT\"/></C:supported-calendar-component-set>");
-                       StrBufAppendPrintf(Buf, "<C:supported-calendar-data>");
-                       StrBufAppendPrintf(Buf,         "<C:calendar-data content-type=\"text/calendar\" version=\"2.0\"/>");
-                       StrBufAppendPrintf(Buf, "</C:supported-calendar-data>");
-                       enumerate_by_euid = 1;
-                       break;
-               case VIEW_TASKS:                        // RFC4791 section 5.2
-                       StrBufAppendPrintf(Buf, "<C:supported-calendar-component-set><C:comp name=\"VTODO\"/></C:supported-calendar-component-set>");
-                       StrBufAppendPrintf(Buf, "<C:supported-calendar-data>");
-                       StrBufAppendPrintf(Buf,         "<C:calendar-data content-type=\"text/calendar\" version=\"2.0\"/>");
-                       StrBufAppendPrintf(Buf, "</C:supported-calendar-data>");
-                       enumerate_by_euid = 1;
-                       break;
-               case VIEW_ADDRESSBOOK:                  // FIXME put some sort of CardDAV crapola here when we implement it
-                       enumerate_by_euid = 1;
-                       break;
-               case VIEW_WIKI:                         // FIXME invent "WikiDAV" ?
-                       enumerate_by_euid = 1;
-                       break;
+       int enumerate_by_euid = 0;      // nonzero if messages will be retrieved by euid instead of msgnum
+       switch (c->room_default_view) {
+       case VIEW_CALENDAR:     // RFC4791 section 5.2
+               StrBufAppendPrintf(Buf,
+                                  "<C:supported-calendar-component-set><C:comp name=\"VEVENT\"/></C:supported-calendar-component-set>");
+               StrBufAppendPrintf(Buf, "<C:supported-calendar-data>");
+               StrBufAppendPrintf(Buf, "<C:calendar-data content-type=\"text/calendar\" version=\"2.0\"/>");
+               StrBufAppendPrintf(Buf, "</C:supported-calendar-data>");
+               enumerate_by_euid = 1;
+               break;
+       case VIEW_TASKS:        // RFC4791 section 5.2
+               StrBufAppendPrintf(Buf,
+                                  "<C:supported-calendar-component-set><C:comp name=\"VTODO\"/></C:supported-calendar-component-set>");
+               StrBufAppendPrintf(Buf, "<C:supported-calendar-data>");
+               StrBufAppendPrintf(Buf, "<C:calendar-data content-type=\"text/calendar\" version=\"2.0\"/>");
+               StrBufAppendPrintf(Buf, "</C:supported-calendar-data>");
+               enumerate_by_euid = 1;
+               break;
+       case VIEW_ADDRESSBOOK:  // FIXME put some sort of CardDAV crapola here when we implement it
+               enumerate_by_euid = 1;
+               break;
+       case VIEW_WIKI: // FIXME invent "WikiDAV" ?
+               enumerate_by_euid = 1;
+               break;
        }
 
 
        /* FIXME get the mtime
-       StrBufAppendPrintf(Buf, "<D:getlastmodified>");
-       escputs(datestring);
-       StrBufAppendPrintf(Buf, "</D:getlastmodified>");
-       */
+          StrBufAppendPrintf(Buf, "<D:getlastmodified>");
+          escputs(datestring);
+          StrBufAppendPrintf(Buf, "</D:getlastmodified>");
+        */
 
        StrBufAppendPrintf(Buf, "</D:prop>");
        StrBufAppendPrintf(Buf, "</D:propstat>");
@@ -325,25 +330,27 @@ void propfind_the_room_itself(struct http_transaction *h, struct ctdlsession *c)
                long *msglist = get_msglist(c, "ALL");
                if (msglist) {
                        int i;
-                       for (i=0; (msglist[i] > 0); ++i) {
-                               if ((i%10) == 0) syslog(LOG_DEBUG, "PROPFIND enumerated %d messages", i);
+                       for (i = 0; (msglist[i] > 0); ++i) {
+                               if ((i % 10) == 0)
+                                       syslog(LOG_DEBUG, "PROPFIND enumerated %d messages", i);
                                e = NULL;       // EUID gets stored here
                                timestamp = 0;
 
                                char cbuf[1024];
                                ctdl_printf(c, "MSG0 %ld|3", msglist[i]);
                                ctdl_readline(c, cbuf, sizeof(cbuf));
-                               if (cbuf[0] == '1') while (ctdl_readline(c, cbuf, sizeof(cbuf)), strcmp(cbuf, "000")) {
-                                       if ( (enumerate_by_euid) && (!strncasecmp(cbuf, "exti=", 5)) ) {
-                                               // e = strdup(&cbuf[5]);
-                                               int elen = (2 * strlen(&cbuf[5]));
-                                               e = malloc(elen);
-                                               urlesc(e, elen, &cbuf[5]);
-                                       }
-                                       if (!strncasecmp(cbuf, "time=", 5)) {
-                                               timestamp = atol(&cbuf[5]);
+                               if (cbuf[0] == '1')
+                                       while (ctdl_readline(c, cbuf, sizeof(cbuf)), strcmp(cbuf, "000")) {
+                                               if ((enumerate_by_euid) && (!strncasecmp(cbuf, "exti=", 5))) {
+                                                       // e = strdup(&cbuf[5]);
+                                                       int elen = (2 * strlen(&cbuf[5]));
+                                                       e = malloc(elen);
+                                                       urlesc(e, elen, &cbuf[5]);
+                                               }
+                                               if (!strncasecmp(cbuf, "time=", 5)) {
+                                                       timestamp = atol(&cbuf[5]);
+                                               }
                                        }
-                               }
                                if (e == NULL) {
                                        e = malloc(20);
                                        sprintf(e, "%ld", msglist[i]);
@@ -362,16 +369,18 @@ void propfind_the_room_itself(struct http_transaction *h, struct ctdlsession *c)
                                StrBufAppendPrintf(Buf, "<D:status>HTTP/1.1 200 OK</D:status>");
                                StrBufAppendPrintf(Buf, "<D:prop>");
 
-                               switch(c->room_default_view) {
-                                       case VIEW_CALENDAR:
-                                               StrBufAppendPrintf(Buf, "<D:getcontenttype>text/calendar; component=vevent</D:getcontenttype>");
-                                               break;
-                                       case VIEW_TASKS:
-                                               StrBufAppendPrintf(Buf, "<D:getcontenttype>text/calendar; component=vtodo</D:getcontenttype>");
-                                               break;
-                                       case VIEW_ADDRESSBOOK:
-                                               StrBufAppendPrintf(Buf, "<D:getcontenttype>text/x-vcard</D:getcontenttype>");
-                                               break;
+                               switch (c->room_default_view) {
+                               case VIEW_CALENDAR:
+                                       StrBufAppendPrintf(Buf,
+                                                          "<D:getcontenttype>text/calendar; component=vevent</D:getcontenttype>");
+                                       break;
+                               case VIEW_TASKS:
+                                       StrBufAppendPrintf(Buf,
+                                                          "<D:getcontenttype>text/calendar; component=vtodo</D:getcontenttype>");
+                                       break;
+                               case VIEW_ADDRESSBOOK:
+                                       StrBufAppendPrintf(Buf, "<D:getcontenttype>text/x-vcard</D:getcontenttype>");
+                                       break;
                                }
 
                                if (timestamp > 0) {
@@ -382,7 +391,8 @@ void propfind_the_room_itself(struct http_transaction *h, struct ctdlsession *c)
                                                StrBufAppendPrintf(Buf, "</D:getlastmodified>");
                                                free(datestring);
                                        }
-                                       if (enumerate_by_euid) {                // FIXME ajc 2017oct30 should this really be inside the timestamp conditional?
+                                       if (enumerate_by_euid)  // FIXME ajc 2017oct30 should this be inside the timestamp conditional?
+                                       {
                                                StrBufAppendPrintf(Buf, "<D:getetag>\"%ld\"</D:getetag>", msglist[i]);
                                        }
                                }
@@ -414,14 +424,15 @@ void get_the_room_itself(struct http_transaction *h, struct ctdlsession *c)
 {
        JsonValue *j = NewJsonObject(HKEY("gotoroom"));
 
-       JsonObjectAppend(j, NewJsonPlainString( HKEY("name"),           c->room,                -1));
-       JsonObjectAppend(j, NewJsonNumber(      HKEY("current_view"),   c->room_current_view    ));
-       JsonObjectAppend(j, NewJsonNumber(      HKEY("default_view"),   c->room_default_view    ));
-       JsonObjectAppend(j, NewJsonNumber(      HKEY("new_messages"),   c->new_messages         ));
-       JsonObjectAppend(j, NewJsonNumber(      HKEY("total_messages"), c->total_messages       ));
+       JsonObjectAppend(j, NewJsonPlainString(HKEY("name"), c->room, -1));
+       JsonObjectAppend(j, NewJsonNumber(HKEY("current_view"), c->room_current_view));
+       JsonObjectAppend(j, NewJsonNumber(HKEY("default_view"), c->room_default_view));
+       JsonObjectAppend(j, NewJsonNumber(HKEY("new_messages"), c->new_messages));
+       JsonObjectAppend(j, NewJsonNumber(HKEY("total_messages"), c->total_messages));
+       JsonObjectAppend(j, NewJsonNumber(HKEY("last_seen"), c->last_seen));
 
        StrBuf *sj = NewStrBuf();
-       SerializeJson(sj, j, 1);                        // '1' == free the source array
+       SerializeJson(sj, j, 1);        // '1' == free the source array
 
        add_response_header(h, strdup("Content-type"), strdup("application/json"));
        h->response_code = 200;
@@ -444,28 +455,24 @@ void the_room_itself(struct http_transaction *h, struct ctdlsession *c)
                options_the_room_itself(h, c);
                return;
        }
-
        // PROPFIND method on the room itself could be looking for a directory
 
        if (!strcasecmp(h->method, "PROPFIND")) {
                propfind_the_room_itself(h, c);
                return;
        }
-
        // REPORT method on the room itself is probably the dreaded CalDAV tower-of-crapola
 
        if (!strcasecmp(h->method, "REPORT")) {
                report_the_room_itself(h, c);
                return;
        }
-
        // GET method on the room itself is an API call, possibly from our JavaScript front end
 
        if (!strcasecmp(h->method, "get")) {
                get_the_room_itself(h, c);
                return;
        }
-
        // we probably want a "go to this room" for interactive access
        do_404(h);
 }
@@ -487,29 +494,29 @@ void room_list(struct http_transaction *h, struct ctdlsession *c)
        }
 
        JsonValue *j = NewJsonArray(HKEY("lkra"));
-       while (ctdl_readline(c, buf, sizeof(buf)) , strcmp(buf, "000")) {
+       while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) {
 
                // name|QRflags|QRfloor|QRorder|QRflags2|ra|current_view|default_view|mtime
                JsonValue *jr = NewJsonObject(HKEY("room"));
 
                extract_token(roomname, buf, 0, '|', sizeof roomname);
-               JsonObjectAppend(jr, NewJsonPlainString( HKEY("name"),  roomname, -1));
+               JsonObjectAppend(jr, NewJsonPlainString(HKEY("name"), roomname, -1));
 
                int ra = extract_int(buf, 5);
-               JsonObjectAppend(jr, NewJsonBool( HKEY("known"), (ra && UA_KNOWN)));
-               JsonObjectAppend(jr, NewJsonBool( HKEY("hasnewmsgs"), (ra && UA_HASNEWMSGS)));
+               JsonObjectAppend(jr, NewJsonBool(HKEY("known"), (ra & UA_KNOWN)));
+               JsonObjectAppend(jr, NewJsonBool(HKEY("hasnewmsgs"), (ra & UA_HASNEWMSGS)));
 
                int floor = extract_int(buf, 2);
-               JsonObjectAppend(jr, NewJsonNumber( HKEY("floor"), floor));
+               JsonObjectAppend(jr, NewJsonNumber(HKEY("floor"), floor));
 
                int rorder = extract_int(buf, 3);
-               JsonObjectAppend(jr, NewJsonNumber( HKEY("rorder"), rorder));
+               JsonObjectAppend(jr, NewJsonNumber(HKEY("rorder"), rorder));
 
-               JsonArrayAppend(j, jr);                 // add the room to the array
+               JsonArrayAppend(j, jr); // add the room to the array
        }
 
        StrBuf *sj = NewStrBuf();
-       SerializeJson(sj, j, 1);                        // '1' == free the source array
+       SerializeJson(sj, j, 1);        // '1' == free the source array
 
        add_response_header(h, strdup("Content-type"), strdup("application/json"));
        h->response_code = 200;
@@ -531,11 +538,11 @@ void ctdl_r(struct http_transaction *h, struct ctdlsession *c)
        extract_token(requested_roomname, h->uri, 3, '/', sizeof requested_roomname);
        unescape_input(requested_roomname);
 
-       if (IsEmptyStr(requested_roomname)) {                   //      /ctdl/r/
+       if (IsEmptyStr(requested_roomname))     //      /ctdl/r/
+       {
                room_list(h, c);
                return;
        }
-
        // If not, try to go there.
        if (strcasecmp(requested_roomname, c->room)) {
                ctdl_printf(c, "GOTO %s", requested_roomname);
@@ -543,31 +550,30 @@ void ctdl_r(struct http_transaction *h, struct ctdlsession *c)
                if (buf[0] == '2') {
                        // buf[3] will indicate whether any instant messages are waiting
                        extract_token(c->room, &buf[4], 0, '|', sizeof c->room);
-                       c->new_messages = extract_int(&buf[4], 1);      
-                       c->total_messages = extract_int(&buf[4], 2);    
-                        //     3       (int)info                       Info flag: set to nonzero if the user needs to read this room's info file
-                        //     4       (int)CCC->room.QRflags          Various flags associated with this room.
-                        //     5       (long)CCC->room.QRhighest       The highest message number present in this room
-                        //     6       (long)vbuf.v_lastseen           The highest message number the user has read in this room
-                        //     7       (int)rmailflag                  Boolean flag: 1 if this is a Mail> room, 0 otherwise.
-                        //     8       (int)raideflag                  Nonzero if user is either Aide or a Room Aide in this room
-                        //     9       (int)newmailcount               The number of new Mail messages the user has
-                        //     10      (int)CCC->room.QRfloor          The floor number this room resides on
+                       c->new_messages = extract_int(&buf[4], 1);
+                       c->total_messages = extract_int(&buf[4], 2);
+                       //      3       (int)info                       Info flag: set to nonzero if the user needs to read this room's info file
+                       //      4       (int)CCC->room.QRflags          Various flags associated with this room.
+                       //      5       (long)CCC->room.QRhighest       The highest message number present in this room
+                       c->last_seen = extract_long(&buf[4], 6);        // The highest message number the user has read in this room
+                       //      7       (int)rmailflag                  Boolean flag: 1 if this is a Mail> room, 0 otherwise.
+                       //      8       (int)raideflag                  Nonzero if user is either Aide or a Room Aide in this room
+                       //      9       (int)newmailcount               The number of new Mail messages the user has
+                       //      10      (int)CCC->room.QRfloor          The floor number this room resides on
                        c->room_current_view = extract_int(&buf[4], 11);
                        c->room_default_view = extract_int(&buf[4], 12);
-                        //     13      (int)is_trash                   Boolean flag: 1 if this is the user's Trash folder, 0 otherwise.
-                        //     14      (int)CCC->room.QRflags2         More flags associated with this room
-                        //     15      (long)CCC->room.QRmtime         Timestamp of the last write activity in this room
-               }
-               else {
+                       //      13      (int)is_trash                   Boolean flag: 1 if this is the user's Trash folder, 0 otherwise.
+                       //      14      (int)CCC->room.QRflags2         More flags associated with this room
+                       //      15      (long)CCC->room.QRmtime         Timestamp of the last write activity in this room
+               } else {
                        do_404(h);
                        return;
                }
        }
-
        // At this point our Citadel client session is "in" the specified room.
 
-       if (num_tokens(h->uri, '/') == 4) {                     //      /ctdl/r/roomname
+       if (num_tokens(h->uri, '/') == 4)       //      /ctdl/r/roomname
+       {
                the_room_itself(h, c);
                return;
        }
@@ -575,18 +581,16 @@ void ctdl_r(struct http_transaction *h, struct ctdlsession *c)
        extract_token(buf, h->uri, 4, '/', sizeof buf);
        if (num_tokens(h->uri, '/') == 5) {
                if (IsEmptyStr(buf)) {
-                       the_room_itself(h, c);                  //      /ctdl/r/roomname/       ( same as /ctdl/r/roomname )
-               }
-               else {
-                       object_in_room(h, c);                   //      /ctdl/r/roomname/object
+                       the_room_itself(h, c);  //      /ctdl/r/roomname/       ( same as /ctdl/r/roomname )
+               } else {
+                       object_in_room(h, c);   //      /ctdl/r/roomname/object
                }
                return;
        }
        if (num_tokens(h->uri, '/') == 6) {
-               object_in_room(h, c);                           //      /ctdl/r/roomname/object/ or possibly /ctdl/r/roomname/object/component
+               object_in_room(h, c);   //      /ctdl/r/roomname/object/ or possibly /ctdl/r/roomname/object/component
                return;
        }
-
        // If we get to this point, the client specified a valid room but requested an action we don't know how to perform.
        do_404(h);
 }