Added -Wno-format-truncation to the CFLAGS to make it shut up about calls to snprintf...
[citadel] / webcit-ng / ctdl_commands.c
1 //
2 // Copyright (c) 1996-2018 by the citadel.org team
3 //
4 // This program is open source software.  It runs great on the
5 // Linux operating system (and probably elsewhere).  You can use,
6 // copy, and run it under the terms of the GNU General Public
7 // License version 3.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13
14 #include "webcit.h"
15
16
17 // /ctdl/c/info returns a JSON representation of the output of an INFO command.
18 void serv_info(struct http_transaction *h, struct ctdlsession *c) {
19         char buf[1024];
20
21         ctdl_printf(c, "INFO");
22         ctdl_readline(c, buf, sizeof(buf));
23         if (buf[0] != '1') {
24                 do_502(h);
25                 return;
26         }
27
28         JsonValue *j = NewJsonObject(HKEY("serv_info"));
29         int i = 0;
30         while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000"))
31                 switch (i++) {
32                 case 0:
33                         JsonObjectAppend(j, NewJsonNumber(HKEY("serv_pid"), atol(buf)));
34                         break;
35                 case 1:
36                         JsonObjectAppend(j, NewJsonPlainString(HKEY("serv_nodename"), buf, -1));
37                         break;
38                 case 2:
39                         JsonObjectAppend(j, NewJsonPlainString(HKEY("serv_humannode"), buf, -1));
40                         break;
41                 case 3:
42                         JsonObjectAppend(j, NewJsonPlainString(HKEY("serv_fqdn"), buf, -1));
43                         break;
44                 case 4:
45                         JsonObjectAppend(j, NewJsonPlainString(HKEY("serv_software"), buf, -1));
46                         break;
47                 case 5:
48                         JsonObjectAppend(j, NewJsonNumber(HKEY("serv_rev_level"), atol(buf)));
49                         break;
50                 case 6:
51                         JsonObjectAppend(j, NewJsonPlainString(HKEY("serv_bbs_city"), buf, -1));
52                         break;
53                 case 7:
54                         JsonObjectAppend(j, NewJsonPlainString(HKEY("serv_sysadm"), buf, -1));
55                         break;
56                 case 14:
57                         JsonObjectAppend(j, NewJsonBool(HKEY("serv_supports_ldap"), atoi(buf)));
58                         break;
59                 case 15:
60                         JsonObjectAppend(j, NewJsonBool(HKEY("serv_newuser_disabled"), atoi(buf)));
61                         break;
62                 case 16:
63                         JsonObjectAppend(j, NewJsonPlainString(HKEY("serv_default_cal_zone"), buf, -1));
64                         break;
65                 case 20:
66                         JsonObjectAppend(j, NewJsonBool(HKEY("serv_supports_sieve"), atoi(buf)));
67                         break;
68                 case 21:
69                         JsonObjectAppend(j, NewJsonBool(HKEY("serv_fulltext_enabled"), atoi(buf)));
70                         break;
71                 case 22:
72                         JsonObjectAppend(j, NewJsonPlainString(HKEY("serv_svn_revision"), buf, -1));
73                         break;
74                 case 23:
75                         JsonObjectAppend(j, NewJsonBool(HKEY("serv_supports_openid"), atoi(buf)));
76                         break;
77                 case 24:
78                         JsonObjectAppend(j, NewJsonBool(HKEY("serv_supports_guest"), atoi(buf)));
79                         break;
80                 }
81
82         StrBuf *sj = NewStrBuf();
83         SerializeJson(sj, j, 1);        // '1' == free the source array
84
85         add_response_header(h, strdup("Content-type"), strdup("application/json"));
86         h->response_code = 200;
87         h->response_string = strdup("OK");
88         h->response_body_length = StrLength(sj);
89         h->response_body = SmashStrBuf(&sj);
90 }
91
92
93 // Dispatcher for paths starting with /ctdl/c/
94 void ctdl_c(struct http_transaction *h, struct ctdlsession *c) {
95         if (!strcasecmp(h->uri, "/ctdl/c/info")) {
96                 serv_info(h, c);
97         } else {
98                 do_404(h);
99         }
100 }