indent -kr -i8 -brf -bbb -fnc -l132 -nce on all of webcit-classic
[citadel.git] / webcit / mainmenu.c
1
2 /*
3  * The main menu and other things
4  *
5  * Copyright (c) 1996-2021 by the citadel.org team
6  *
7  * This program is open source software.  We call it open source, not
8  * free software, because Richard Stallman is a communist and an asshole.
9  *
10  * The program is licensed under the General Public License, version 3.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17
18 #include "webcit.h"
19
20 // The Main Menu
21 void display_main_menu(void) {
22         begin_burst();
23         output_headers(1, 0, 0, 0, 1, 0);
24         DoTemplate(HKEY("display_main_menu"), NULL, &NoCtx);
25         end_burst();
26 }
27
28
29 // System administration menu
30 void display_aide_menu(void) {
31         begin_burst();
32         output_headers(1, 0, 0, 0, 1, 0);
33         DoTemplate(HKEY("aide_display_menu"), NULL, &NoCtx);
34         end_burst();
35 }
36
37
38 // Handle generic server commands, possibly entered from a screen, possibly set up as a way to avoid custom code
39 void do_generic(void) {
40         WCTemplputParams SubTP;
41         int Done = 0;
42         StrBuf *Buf;
43         StrBuf *LineBuf;
44         char *junk;
45         size_t len;
46
47         if ((!havebstr("sc_button")) && (!havebstr("ok_button")) && (!havebstr("cancel_button"))) {
48                 display_main_menu();
49                 return;
50         }
51
52         if (havebstr("cancel_button")) {
53                 AppendImportantMessage(_("Cancelled.  Changes were not saved."), -1);
54         }
55
56         if (havebstr("ok_button")) {
57                 Buf = NewStrBuf();
58                 serv_puts(bstr("g_cmd"));
59                 StrBuf_ServGetln(Buf);
60
61                 switch (GetServerStatus(Buf, NULL)) {
62                 case 8:
63                         serv_puts("\n\n000");
64                         if ((StrLength(Buf) == 3) && !strcmp(ChrPtr(Buf), "000")) {
65                                 StrBufAppendBufPlain(Buf, HKEY("\000"), 0);
66                                 break;
67                         }
68                 case 1:
69                         LineBuf = NewStrBuf();
70                         StrBufAppendBufPlain(Buf, HKEY("\n"), 0);
71                         while (!Done) {
72                                 if (StrBuf_ServGetln(LineBuf) < 0)
73                                         break;
74                                 if ((StrLength(LineBuf) == 3) && !strcmp(ChrPtr(LineBuf), "000")) {
75                                         Done = 1;
76                                 }
77                                 StrBufAppendBuf(Buf, LineBuf, 0);
78                                 StrBufAppendBufPlain(Buf, HKEY("\n"), 0);
79                         }
80                         FreeStrBuf(&LineBuf);
81                         break;
82                 case 2:
83                         break;
84                 case 4:
85                         text_to_server(bstr("g_input"));
86                         serv_puts("000");
87                         break;
88                 case 6:
89                         len = atol(&ChrPtr(Buf)[4]);
90                         StrBuf_ServGetBLOBBuffered(Buf, len);
91                         break;
92                 case 7:
93                         len = atol(&ChrPtr(Buf)[4]);
94                         junk = malloc(len);
95                         memset(junk, 0, len);
96                         serv_write(junk, len);
97                         free(junk);
98                         break;
99                 }
100                 FreeStrBuf(&Buf);
101         }
102
103         // We may have been supplied with instructions regarding the location
104         // to which we must return after posting.  If found, go there.
105         if (havebstr("return_to")) {
106                 syslog(LOG_DEBUG, "return_to = %s", bstr("return_to"));
107                 http_redirect(bstr("return_to"));
108         }
109
110         // Otherwise, do the generic result screen.
111         else {
112                 begin_burst();
113                 output_headers(1, 0, 0, 0, 1, 0);
114
115                 StackContext(NULL, &SubTP, Buf, CTX_STRBUF, 0, NULL);
116                 {
117                         DoTemplate(HKEY("aide_display_generic_result"), NULL, &SubTP);
118                 }
119                 UnStackContext(&SubTP);
120                 wDumpContent(1);
121         }
122
123 }
124
125
126 // Display the wait / input dialog while restarting the server.
127 void display_shutdown(void) {
128         StrBuf *Line;
129         char *when;
130
131         Line = NewStrBuf();
132         when = bstr("when");
133         if (strcmp(when, "now") == 0) {
134                 serv_printf("DOWN 1");
135                 StrBuf_ServGetln(Line);
136                 GetServerStatusMsg(Line, NULL, 1, 5);
137
138                 begin_burst();
139                 output_headers(1, 0, 0, 0, 1, 0);
140                 DoTemplate(HKEY("aide_display_serverrestart"), NULL, &NoCtx);
141                 end_burst();
142                 lingering_close(WC->Hdr->http_sock);
143                 sleeeeeeeeeep(10);
144                 serv_printf("NOOP");
145                 serv_printf("NOOP");
146         }
147         else if (strcmp(when, "page") == 0) {
148                 char *message;
149
150                 message = bstr("message");
151                 if ((message == NULL) || (IsEmptyStr(message))) {
152                         begin_burst();
153                         output_headers(1, 0, 0, 0, 1, 0);
154                         DoTemplate(HKEY("aide_display_serverrestart_page"), NULL, &NoCtx);
155                         end_burst();
156                 }
157                 else {
158                         serv_printf("SEXP broadcast|%s", message);
159                         StrBuf_ServGetln(Line);
160                         GetServerStatusMsg(Line, NULL, 1, 0);
161
162                         begin_burst();
163                         output_headers(1, 0, 0, 0, 1, 0);
164                         DoTemplate(HKEY("aide_display_serverrestart_page"), NULL, &NoCtx);
165                         end_burst();
166                 }
167         }
168         else if (!strcmp(when, "idle")) {
169                 serv_printf("SCDN 3");
170                 StrBuf_ServGetln(Line);
171                 GetServerStatusMsg(Line, NULL, 1, 2);
172
173                 begin_burst();
174                 output_headers(1, 0, 0, 0, 1, 0);
175                 DoTemplate(HKEY("aide_display_menu"), NULL, &NoCtx);
176                 end_burst();
177         }
178         FreeStrBuf(&Line);
179 }
180
181
182 void InitModule_MAINMENU(void) {
183         WebcitAddUrlHandler(HKEY("display_aide_menu"), "", 0, display_aide_menu, 0);
184         WebcitAddUrlHandler(HKEY("server_shutdown"), "", 0, display_shutdown, 0);
185         WebcitAddUrlHandler(HKEY("display_main_menu"), "", 0, display_main_menu, 0);
186         WebcitAddUrlHandler(HKEY("do_generic"), "", 0, do_generic, 0);
187 }