When a client of any protocol handler sends a command to initiate SSL/TLS on a connec...
[citadel.git] / 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                 syslog(LOG_DEBUG, "return_to = %s", bstr("return_to"));
101                 http_redirect(bstr("return_to"));
102         }
103
104         // Otherwise, do the generic result screen.
105         else {
106                 begin_burst();
107                 output_headers(1, 0, 0, 0, 1, 0);
108         
109                 StackContext(NULL, &SubTP, Buf, CTX_STRBUF, 0, NULL);
110                 {
111                         DoTemplate(HKEY("aide_display_generic_result"), NULL, &SubTP);
112                 }
113                 UnStackContext(&SubTP);
114                 wDumpContent(1);
115         }
116
117         FreeStrBuf(&Buf);
118 }
119
120
121 // Display the wait / input dialog while restarting the server.
122 void display_shutdown(void) {
123         StrBuf *Line;
124         char *when;
125         
126         Line = NewStrBuf();
127         when=bstr("when");
128         if (strcmp(when, "now") == 0){
129                 serv_printf("DOWN 1");
130                 StrBuf_ServGetln(Line);
131                 GetServerStatusMsg(Line, NULL, 1, 5);
132
133                 begin_burst();
134                 output_headers(1, 0, 0, 0, 1, 0);
135                 DoTemplate(HKEY("aide_display_serverrestart"), NULL, &NoCtx);
136                 end_burst();
137                 lingering_close(WC->Hdr->http_sock);
138                 sleeeeeeeeeep(10);
139                 serv_printf("NOOP");
140                 serv_printf("NOOP");
141         }
142         else if (strcmp(when, "page") == 0) {
143                 char *message;
144                
145                 message = bstr("message");
146                 if ((message == NULL) || (IsEmptyStr(message)))
147                 {
148                         begin_burst();
149                         output_headers(1, 0, 0, 0, 1, 0);
150                         DoTemplate(HKEY("aide_display_serverrestart_page"), NULL, &NoCtx);
151                         end_burst();
152                 }
153                 else
154                 {
155                         serv_printf("SEXP broadcast|%s", message);
156                         StrBuf_ServGetln(Line);
157                         GetServerStatusMsg(Line, NULL, 1, 0);
158
159                         begin_burst();
160                         output_headers(1, 0, 0, 0, 1, 0);
161                         DoTemplate(HKEY("aide_display_serverrestart_page"), NULL, &NoCtx);
162                         end_burst();                    
163                 }
164         }
165         else if (!strcmp(when, "idle")) {
166                 serv_printf("SCDN 3");
167                 StrBuf_ServGetln(Line);
168                 GetServerStatusMsg(Line, NULL, 1, 2);
169
170                 begin_burst();
171                 output_headers(1, 0, 0, 0, 1, 0);
172                 DoTemplate(HKEY("aide_display_menu"), NULL, &NoCtx);
173                 end_burst();                    
174         }
175         FreeStrBuf(&Line);
176 }
177
178
179 void 
180 InitModule_MAINMENU
181 (void)
182 {
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 }