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