ab3d2f38ab51ca6ebd484a74b41b52ffa924e335
[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->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
95         serv_puts(bstr("g_cmd"));
96
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         wDumpContent(1);
147 }
148
149
150 /*
151  * Display the menubar.  
152  *
153  * Set 'as_single_page' to display HTML headers and footers -- otherwise it's assumed
154  * that the menubar is being embedded in another page.
155  */
156 void display_menubar(int as_single_page) {
157
158         if (as_single_page) {
159                 output_headers(0, 0, 0, 0, 0, 0);
160                 wprintf("<html>\n"
161                         "<head>\n"
162                         "<title>MenuBar</title>\n"
163                         "<style type=\"text/css\">\n"
164                         "body   { text-decoration: none; }\n"
165                         "</style>\n"
166                         "</head>\n");
167                 do_template("background", NULL);
168         }
169
170         do_template("menubar", NULL);
171
172         if (as_single_page) {
173                 wDumpContent(2);
174         }
175
176
177 }
178
179
180 /*
181  * Display the wait / input dialog while restarting the server.
182  */
183 void display_shutdown(void)
184 {
185         char buf[SIZ];
186         char *when;
187         
188         when=bstr("when");
189         if (strcmp(when, "now") == 0){
190                 serv_printf("DOWN 1");
191                 serv_getln(buf, sizeof buf);
192                 if (atol(buf) == 500)
193                 { /* upsie. maybe the server is not running as daemon? */
194                         
195                         safestrncpy(WC->ImportantMessage,
196                                     &buf[4],
197                                     sizeof WC->ImportantMessage);
198                 }
199                 begin_burst();
200                 output_headers(1, 0, 0, 0, 1, 0);
201                 DoTemplate(HKEY("display_serverrestart"), NULL, &NoCtx);
202                 end_burst();
203                 lingering_close(WC->Hdr->http_sock);
204                 sleeeeeeeeeep(10);
205                 serv_printf("NOOP");
206                 serv_printf("NOOP");
207         }
208         else if (strcmp(when, "page") == 0) {
209                 char *message;
210                
211                 message = bstr("message");
212                 if ((message == NULL) || (IsEmptyStr(message)))
213                 {
214                         begin_burst();
215                         output_headers(1, 0, 0, 0, 1, 0);
216                         DoTemplate(HKEY("display_serverrestartpage"), NULL, &NoCtx);
217                         end_burst();
218                 }
219                 else
220                 {
221                         serv_printf("SEXP broadcast|%s", message);
222                         serv_getln(buf, sizeof buf); /* TODO: should we care? */
223                         begin_burst();
224                         output_headers(1, 0, 0, 0, 1, 0);
225                         DoTemplate(HKEY("display_serverrestartpagedo"), NULL, &NoCtx);
226                         end_burst();                    
227                 }
228         }
229         else if (!strcmp(when, "idle")) {
230                 serv_printf("SCDN 3");
231                 serv_getln(buf, sizeof buf);
232
233                 if (atol(buf) == 500)
234                 { /* upsie. maybe the server is not running as daemon? */
235                         safestrncpy(WC->ImportantMessage,
236                                     &buf[4],
237                                     sizeof WC->ImportantMessage);
238                 }
239                 begin_burst();
240                 output_headers(1, 0, 0, 0, 1, 0);
241                 DoTemplate(HKEY("display_aide_menu"), NULL, &NoCtx);
242                 end_burst();                    
243         }
244 }
245
246 void _display_menubar(void) { display_menubar(0); }
247
248 void 
249 InitModule_MAINMENU
250 (void)
251 {
252         WebcitAddUrlHandler(HKEY("display_aide_menu"), display_aide_menu, 0);
253         WebcitAddUrlHandler(HKEY("server_shutdown"), display_shutdown, 0);
254         WebcitAddUrlHandler(HKEY("display_main_menu"), display_main_menu, 0);
255         WebcitAddUrlHandler(HKEY("display_generic"), display_generic, 0);
256         WebcitAddUrlHandler(HKEY("do_generic"), do_generic, 0);
257         WebcitAddUrlHandler(HKEY("display_menubar"), _display_menubar, 0);
258 }