7dff4dbece5c9710982928e21569242faa82f89c
[citadel.git] / webcit-ng / server / floor_functions.c
1 // Floor functions
2 //
3 // Copyright (c) 1996-2022 by the citadel.org team
4 //
5 // This program is open source software.  Use, duplication, or
6 // disclosure are subject to the GNU General Public License v3.
7
8 #include "webcit.h"
9
10
11 // Dispatcher for "/ctdl/f" and "/ctdl/f/" for the floor list
12 void floor_list(struct http_transaction *h, struct ctdlsession *c) {
13         char buf[1024];
14         char floorname[1024];
15
16         ctdl_printf(c, "LFLR");
17         ctdl_readline(c, buf, sizeof(buf));
18         if (buf[0] != '1') {
19                 do_502(h);
20                 return;
21         }
22
23         JsonValue *j = NewJsonArray(HKEY("lflr"));
24         while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) {
25
26                 // 0  |1   |2
27                 // num|name|refcount
28                 JsonValue *jr = NewJsonObject(HKEY("floor"));
29
30                 extract_token(floorname, buf, 1, '|', sizeof floorname);
31                 JsonObjectAppend(jr, NewJsonPlainString(HKEY("name"), floorname, -1));
32
33                 JsonObjectAppend(jr, NewJsonNumber(HKEY("num"), extract_int(buf, 0)));
34                 JsonObjectAppend(jr, NewJsonNumber(HKEY("refcount"), extract_int(buf, 2)));
35
36                 JsonArrayAppend(j, jr); // add the room to the array
37         }
38
39         StrBuf *sj = NewStrBuf();
40         SerializeJson(sj, j, 1);        // '1' == free the source array
41
42         add_response_header(h, strdup("Content-type"), strdup("application/json"));
43         h->response_code = 200;
44         h->response_string = strdup("OK");
45         h->response_body_length = StrLength(sj);
46         h->response_body = SmashStrBuf(&sj);
47 }
48
49
50 // Dispatcher for paths starting with /ctdl/f/
51 // (This is a stub ... we will need to add more functions when we can do more than just a floor list)
52 void ctdl_f(struct http_transaction *h, struct ctdlsession *c) {
53         floor_list(h, c);
54         return;
55 }