Revert "serv_rssclient.c: style update"
[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")) && (!havebstr("ok_button")) && (!havebstr("cancel_button")) ) {
47                 display_main_menu();
48                 return;
49         }
50
51         if (havebstr("cancel_button")) {
52                 AppendImportantMessage(_("Cancelled.  Changes were not saved."), -1);
53         }
54
55         if (havebstr("ok_button")) {
56                 Buf = NewStrBuf();
57                 serv_puts(bstr("g_cmd"));
58                 StrBuf_ServGetln(Buf);
59                 
60                 switch (GetServerStatus(Buf, NULL)) {
61                 case 8:
62                         serv_puts("\n\n000");
63                         if ( (StrLength(Buf)==3) && 
64                         !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) && 
75                                 !strcmp(ChrPtr(LineBuf), "000")) {
76                                         Done = 1;
77                                 }
78                                 StrBufAppendBuf(Buf, LineBuf, 0);
79                                 StrBufAppendBufPlain(Buf, HKEY("\n"), 0);
80                         }
81                         FreeStrBuf(&LineBuf);
82                         break;
83                 case 2:
84                         break;
85                 case 4:
86                         text_to_server(bstr("g_input"));
87                         serv_puts("000");
88                         break;
89                 case 6:
90                         len = atol(&ChrPtr(Buf)[4]);
91                         StrBuf_ServGetBLOBBuffered(Buf, len);
92                         break;
93                 case 7:
94                         len = atol(&ChrPtr(Buf)[4]);
95                         junk = malloc(len);
96                         memset(junk, 0, len);
97                         serv_write(junk, len);
98                         free(junk);
99                         break;
100                 }
101                 FreeStrBuf(&Buf);
102         }
103         
104         // We may have been supplied with instructions regarding the location
105         // to which we must return after posting.  If found, go there.
106         if (havebstr("return_to")) {
107                 syslog(LOG_DEBUG, "return_to = %s", bstr("return_to"));
108                 http_redirect(bstr("return_to"));
109         }
110
111         // Otherwise, do the generic result screen.
112         else {
113                 begin_burst();
114                 output_headers(1, 0, 0, 0, 1, 0);
115         
116                 StackContext(NULL, &SubTP, Buf, CTX_STRBUF, 0, NULL);
117                 {
118                         DoTemplate(HKEY("aide_display_generic_result"), NULL, &SubTP);
119                 }
120                 UnStackContext(&SubTP);
121                 wDumpContent(1);
122         }
123
124 }
125
126
127 // Display the wait / input dialog while restarting the server.
128 void display_shutdown(void) {
129         StrBuf *Line;
130         char *when;
131         
132         Line = NewStrBuf();
133         when=bstr("when");
134         if (strcmp(when, "now") == 0){
135                 serv_printf("DOWN 1");
136                 StrBuf_ServGetln(Line);
137                 GetServerStatusMsg(Line, NULL, 1, 5);
138
139                 begin_burst();
140                 output_headers(1, 0, 0, 0, 1, 0);
141                 DoTemplate(HKEY("aide_display_serverrestart"), NULL, &NoCtx);
142                 end_burst();
143                 lingering_close(WC->Hdr->http_sock);
144                 sleeeeeeeeeep(10);
145                 serv_printf("NOOP");
146                 serv_printf("NOOP");
147         }
148         else if (strcmp(when, "page") == 0) {
149                 char *message;
150                
151                 message = bstr("message");
152                 if ((message == NULL) || (IsEmptyStr(message)))
153                 {
154                         begin_burst();
155                         output_headers(1, 0, 0, 0, 1, 0);
156                         DoTemplate(HKEY("aide_display_serverrestart_page"), NULL, &NoCtx);
157                         end_burst();
158                 }
159                 else
160                 {
161                         serv_printf("SEXP broadcast|%s", message);
162                         StrBuf_ServGetln(Line);
163                         GetServerStatusMsg(Line, NULL, 1, 0);
164
165                         begin_burst();
166                         output_headers(1, 0, 0, 0, 1, 0);
167                         DoTemplate(HKEY("aide_display_serverrestart_page"), NULL, &NoCtx);
168                         end_burst();                    
169                 }
170         }
171         else if (!strcmp(when, "idle")) {
172                 serv_printf("SCDN 3");
173                 StrBuf_ServGetln(Line);
174                 GetServerStatusMsg(Line, NULL, 1, 2);
175
176                 begin_burst();
177                 output_headers(1, 0, 0, 0, 1, 0);
178                 DoTemplate(HKEY("aide_display_menu"), NULL, &NoCtx);
179                 end_burst();                    
180         }
181         FreeStrBuf(&Line);
182 }
183
184
185 void 
186 InitModule_MAINMENU
187 (void)
188 {
189         WebcitAddUrlHandler(HKEY("display_aide_menu"), "", 0, display_aide_menu, 0);
190         WebcitAddUrlHandler(HKEY("server_shutdown"), "", 0, display_shutdown, 0);
191         WebcitAddUrlHandler(HKEY("display_main_menu"), "", 0, display_main_menu, 0);
192         WebcitAddUrlHandler(HKEY("do_generic"), "", 0, do_generic, 0);
193 }