3959e27a65624d4039e9e2e47be6be23e5361619
[citadel.git] / webcit / mainmenu.c
1 /*
2  * $Id$
3  */
4
5 #include "webcit.h"
6
7 /*
8  * The Main Menu
9  */
10 void display_main_menu(void)
11 {
12         begin_burst();
13         output_headers(1, 0, 0, 0, 1, 0);
14         DoTemplate(HKEY("display_main_menu"), NULL, &NoCtx);
15         end_burst();
16 }
17
18
19 /*
20  * System administration menu
21  */
22 void display_aide_menu(void)
23 {
24         begin_burst();
25         output_headers(1, 0, 0, 0, 1, 0);
26         DoTemplate(HKEY("display_aide_menu"), NULL, &NoCtx);
27         end_burst();
28 }
29
30
31
32 /*
33  * Display the screen to enter a generic server command
34  */
35 void display_generic(void)
36 {
37         output_headers(1, 1, 2, 0, 0, 0);
38         wprintf("<div id=\"banner\">\n");
39         wprintf("<h1>");
40         wprintf(_("Enter a server command"));
41         wprintf("</h1>");
42         wprintf("</div>\n");
43
44         wprintf("<div id=\"content\" class=\"service\">\n");
45
46         wprintf("<div class=\"fix_scrollbar_bug\">"
47                 "<table class=\"mainmenu_background\"><tr><td>\n");
48
49         wprintf("<center>");
50         wprintf(_("This screen allows you to enter Citadel server commands which are "
51                 "not supported by WebCit.  If you do not know what that means, "
52                 "then this screen will not be of much use to you."));
53         wprintf("<br />\n");
54
55         wprintf("<form method=\"post\" action=\"do_generic\">\n");
56         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
57
58         wprintf(_("Enter command:"));
59         wprintf("<br /><input type=\"text\" name=\"g_cmd\" size=80 maxlength=\"250\"><br />\n");
60
61         wprintf(_("Command input (if requesting SEND_LISTING transfer mode):"));
62         wprintf("<br /><textarea name=\"g_input\" rows=10 cols=80 width=80></textarea><br />\n");
63
64         wprintf("<font size=-2>");
65         wprintf(_("Detected host header is %s://%s"), (is_https ? "https" : "http"), ChrPtr(WC->Hdr->HR.http_host));
66         wprintf("</font>\n");
67         wprintf("<input type=\"submit\" name=\"sc_button\" value=\"%s\">", _("Send command"));
68         wprintf("&nbsp;");
69         wprintf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\"><br />\n", _("Cancel"));
70
71         wprintf("</form></center>\n");
72         wprintf("</td></tr></table></div>\n");
73         wDumpContent(1);
74 }
75
76 /*
77  * Interactive window to perform generic Citadel server commands.
78  */
79 void do_generic(void)
80 {
81
82         wcsession *WCC = WC;
83         int Done = 0;
84         StrBuf *Buf;
85         char *junk;
86         size_t len;
87
88         if (!havebstr("sc_button")) {
89                 display_main_menu();
90                 return;
91         }
92
93         output_headers(1, 1, 0, 0, 0, 0);
94         Buf = NewStrBuf();
95         serv_puts(bstr("g_cmd"));
96         StrBuf_ServGetln(Buf);
97         svput("BOXTITLE", WCS_STRING, _("Server command results"));
98         do_template("beginboxx", NULL);
99
100         wprintf("<table border=0><tr><td>Command:</td><td><tt>");
101         StrEscAppend(WCC->WBuf, sbstr("g_cmd"), NULL, 0, 0);
102         wprintf("</tt></td></tr><tr><td>Result:</td><td><tt>");
103         StrEscAppend(WCC->WBuf, Buf, NULL, 0, 0);
104         StrBufAppendBufPlain(WCC->WBuf, HKEY("<br>\n"), 0);
105         wprintf("</tt></td></tr></table><br />\n");
106         
107         switch (GetServerStatus(Buf, NULL)) {
108         case 8:
109                 serv_puts("\n\n000");
110                 if ( (StrLength(Buf)==3) && 
111                      !strcmp(ChrPtr(Buf), "000")) {
112                         StrBufAppendBufPlain(WCC->WBuf, HKEY("\000"), 0);
113                         break;
114                 }
115         case 1:
116                 while (!Done) {
117                         StrBuf_ServGetln(Buf);
118                         if ( (StrLength(Buf)==3) && 
119                              !strcmp(ChrPtr(Buf), "000")) {
120                                 Done = 1;
121                         }
122                         StrEscAppend(WCC->WBuf, Buf, NULL, 0, 0);
123                         StrBufAppendBufPlain(WCC->WBuf, HKEY("<br>\n"), 0);
124                 }
125                 break;
126         case 4:
127                 text_to_server(bstr("g_input"));
128                 serv_puts("000");
129                 break;
130         case 6:
131                 len = atol(&ChrPtr(Buf)[4]);
132                 StrBuf_ServGetBLOBBuffered(Buf, len);
133                 break;
134         case 7:
135                 len = atol(&ChrPtr(Buf)[4]);
136                 junk = malloc(len);
137                 memset(junk, 0, len);
138                 serv_write(junk, len);
139                 free(junk);
140         }
141         
142         wprintf("<hr />");
143         wprintf("<a href=\"display_generic\">Enter another command</a><br />\n");
144         wprintf("<a href=\"display_advanced\">Return to menu</a>\n");
145         do_template("endbox", NULL);
146         FreeStrBuf(&Buf);
147         wDumpContent(1);
148 }
149
150
151 /*
152  * Display the menubar.  
153  *
154  * Set 'as_single_page' to display HTML headers and footers -- otherwise it's assumed
155  * that the menubar is being embedded in another page.
156  */
157 void display_menubar(int as_single_page) {
158
159         if (as_single_page) {
160                 output_headers(0, 0, 0, 0, 0, 0);
161                 wprintf("<html>\n"
162                         "<head>\n"
163                         "<title>MenuBar</title>\n"
164                         "<style type=\"text/css\">\n"
165                         "body   { text-decoration: none; }\n"
166                         "</style>\n"
167                         "</head>\n");
168                 do_template("background", NULL);
169         }
170
171         do_template("menubar", NULL);
172
173         if (as_single_page) {
174                 wDumpContent(2);
175         }
176
177
178 }
179
180
181 /*
182  * Display the wait / input dialog while restarting the server.
183  */
184 void display_shutdown(void)
185 {
186         char buf[SIZ];
187         char *when;
188         
189         when=bstr("when");
190         if (strcmp(when, "now") == 0){
191                 serv_printf("DOWN 1");
192                 serv_getln(buf, sizeof buf);
193                 if (atol(buf) == 500)
194                 { /* upsie. maybe the server is not running as daemon? */
195                         
196                         safestrncpy(WC->ImportantMessage,
197                                     &buf[4],
198                                     sizeof WC->ImportantMessage);
199                 }
200                 begin_burst();
201                 output_headers(1, 0, 0, 0, 1, 0);
202                 DoTemplate(HKEY("display_serverrestart"), NULL, &NoCtx);
203                 end_burst();
204                 lingering_close(WC->Hdr->http_sock);
205                 sleeeeeeeeeep(10);
206                 serv_printf("NOOP");
207                 serv_printf("NOOP");
208         }
209         else if (strcmp(when, "page") == 0) {
210                 char *message;
211                
212                 message = bstr("message");
213                 if ((message == NULL) || (IsEmptyStr(message)))
214                 {
215                         begin_burst();
216                         output_headers(1, 0, 0, 0, 1, 0);
217                         DoTemplate(HKEY("display_serverrestartpage"), NULL, &NoCtx);
218                         end_burst();
219                 }
220                 else
221                 {
222                         serv_printf("SEXP broadcast|%s", message);
223                         serv_getln(buf, sizeof buf); /* TODO: should we care? */
224                         begin_burst();
225                         output_headers(1, 0, 0, 0, 1, 0);
226                         DoTemplate(HKEY("display_serverrestartpagedo"), NULL, &NoCtx);
227                         end_burst();                    
228                 }
229         }
230         else if (!strcmp(when, "idle")) {
231                 serv_printf("SCDN 3");
232                 serv_getln(buf, sizeof buf);
233
234                 if (atol(buf) == 500)
235                 { /* upsie. maybe the server is not running as daemon? */
236                         safestrncpy(WC->ImportantMessage,
237                                     &buf[4],
238                                     sizeof WC->ImportantMessage);
239                 }
240                 begin_burst();
241                 output_headers(1, 0, 0, 0, 1, 0);
242                 DoTemplate(HKEY("display_aide_menu"), NULL, &NoCtx);
243                 end_burst();                    
244         }
245 }
246
247 void _display_menubar(void) { display_menubar(0); }
248
249 void 
250 InitModule_MAINMENU
251 (void)
252 {
253         WebcitAddUrlHandler(HKEY("display_aide_menu"), display_aide_menu, 0);
254         WebcitAddUrlHandler(HKEY("server_shutdown"), display_shutdown, 0);
255         WebcitAddUrlHandler(HKEY("display_main_menu"), display_main_menu, 0);
256         WebcitAddUrlHandler(HKEY("display_generic"), display_generic, 0);
257         WebcitAddUrlHandler(HKEY("do_generic"), do_generic, 0);
258         WebcitAddUrlHandler(HKEY("display_menubar"), _display_menubar, 0);
259 }