/ctdl/f/ to get a list of all floors
[citadel.git] / webcit-ng / floor_functions.c
1 //
2 // Floor functions
3 //
4 // Copyright (c) 1996-2022 by the citadel.org team
5 //
6 // This program is open source software.  It runs great on the
7 // Linux operating system (and probably elsewhere).  You can use,
8 // copy, and run it under the terms of the GNU General Public
9 // License version 3.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 #include "webcit.h"
17
18
19 // Dispatcher for "/ctdl/f" and "/ctdl/f/" for the floor list
20 void floor_list(struct http_transaction *h, struct ctdlsession *c) {
21         char buf[1024];
22         char floorname[1024];
23
24         ctdl_printf(c, "LFLR");
25         ctdl_readline(c, buf, sizeof(buf));
26         if (buf[0] != '1') {
27                 do_502(h);
28                 return;
29         }
30
31         JsonValue *j = NewJsonArray(HKEY("lflr"));
32         while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) {
33
34                 // 0  |1   |2
35                 // num|name|refcount
36                 JsonValue *jr = NewJsonObject(HKEY("floor"));
37
38                 extract_token(floorname, buf, 1, '|', sizeof floorname);
39                 JsonObjectAppend(jr, NewJsonPlainString(HKEY("name"), floorname, -1));
40
41                 JsonObjectAppend(jr, NewJsonNumber(HKEY("num"), extract_int(buf, 0)));
42                 JsonObjectAppend(jr, NewJsonNumber(HKEY("refcount"), extract_int(buf, 2)));
43
44                 JsonArrayAppend(j, jr); // add the room to the array
45         }
46
47         StrBuf *sj = NewStrBuf();
48         SerializeJson(sj, j, 1);        // '1' == free the source array
49
50         add_response_header(h, strdup("Content-type"), strdup("application/json"));
51         h->response_code = 200;
52         h->response_string = strdup("OK");
53         h->response_body_length = StrLength(sj);
54         h->response_body = SmashStrBuf(&sj);
55 }
56
57
58 // Dispatcher for paths starting with /ctdl/f/
59 // (This is a stub ... we will need to add more functions when we can do more than just a floor list)
60 void ctdl_f(struct http_transaction *h, struct ctdlsession *c) {
61         floor_list(h, c);
62         return;
63 }