Problem with merge: this is called Buf in stable, not LineBuf
[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("aide_display_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         wc_printf("<div id=\"banner\">\n");
39         wc_printf("<h1>");
40         wc_printf(_("Enter a server command"));
41         wc_printf("</h1>");
42         wc_printf("</div>\n");
43
44         wc_printf("<div id=\"content\" class=\"service\">\n");
45
46         wc_printf("<div class=\"fix_scrollbar_bug\">"
47                 "<table class=\"mainmenu_background\"><tr><td>\n");
48
49         wc_printf("<center>");
50         wc_printf(_("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         wc_printf("<br />\n");
54
55         wc_printf("<form method=\"post\" action=\"do_generic\">\n");
56         wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
57
58         wc_printf(_("Enter command:"));
59         wc_printf("<br /><input type=\"text\" name=\"g_cmd\" size=80 maxlength=\"250\"><br />\n");
60
61         wc_printf(_("Command input (if requesting SEND_LISTING transfer mode):"));
62         wc_printf("<br /><textarea name=\"g_input\" rows=10 cols=80 width=80></textarea><br />\n");
63
64         wc_printf("<font size=-2>");
65         wc_printf(_("Detected host header is %s://%s"), (is_https ? "https" : "http"), ChrPtr(WC->Hdr->HR.http_host));
66         wc_printf("</font>\n");
67         wc_printf("<input type=\"submit\" name=\"sc_button\" value=\"%s\">", _("Send command"));
68         wc_printf("&nbsp;");
69         wc_printf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\"><br />\n", _("Cancel"));
70
71         wc_printf("</form></center>\n");
72         wc_printf("</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         wc_printf("<table border=0><tr><td>Command:</td><td><tt>");
101         StrEscAppend(WCC->WBuf, sbstr("g_cmd"), NULL, 0, 0);
102         wc_printf("</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         wc_printf("</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                         if (StrBuf_ServGetln(Buf) < 0)
118                                 break;
119                         if ( (StrLength(Buf)==3) && 
120                              !strcmp(ChrPtr(Buf), "000")) {
121                                 Done = 1;
122                         }
123                         StrEscAppend(WCC->WBuf, Buf, NULL, 0, 0);
124                         StrBufAppendBufPlain(WCC->WBuf, HKEY("<br>\n"), 0);
125                 }
126                 break;
127         case 4:
128                 text_to_server(bstr("g_input"));
129                 serv_puts("000");
130                 break;
131         case 6:
132                 len = atol(&ChrPtr(Buf)[4]);
133                 StrBuf_ServGetBLOBBuffered(Buf, len);
134                 break;
135         case 7:
136                 len = atol(&ChrPtr(Buf)[4]);
137                 junk = malloc(len);
138                 memset(junk, 0, len);
139                 serv_write(junk, len);
140                 free(junk);
141         }
142         
143         wc_printf("<hr />");
144         wc_printf("<a href=\"display_generic\">Enter another command</a><br />\n");
145         wc_printf("<a href=\"display_advanced\">Return to menu</a>\n");
146         do_template("endbox", NULL);
147         FreeStrBuf(&Buf);
148         wDumpContent(1);
149 }
150
151
152 /*
153  * Display the menubar.  
154  *
155  * Set 'as_single_page' to display HTML headers and footers -- otherwise it's assumed
156  * that the menubar is being embedded in another page.
157  */
158 void display_menubar(int as_single_page) {
159
160         if (as_single_page) {
161                 output_headers(0, 0, 0, 0, 0, 0);
162                 wc_printf("<html>\n"
163                         "<head>\n"
164                         "<title>MenuBar</title>\n"
165                         "<style type=\"text/css\">\n"
166                         "body   { text-decoration: none; }\n"
167                         "</style>\n"
168                         "</head>\n");
169                 do_template("background", NULL);
170         }
171
172         do_template("menubar", NULL);
173
174         if (as_single_page) {
175                 wDumpContent(2);
176         }
177
178
179 }
180
181
182 /*
183  * Display the wait / input dialog while restarting the server.
184  */
185 void display_shutdown(void)
186 {
187         char buf[SIZ];
188         char *when;
189         
190         when=bstr("when");
191         if (strcmp(when, "now") == 0){
192                 serv_printf("DOWN 1");
193                 serv_getln(buf, sizeof buf);
194                 if (atol(buf) == 500)
195                 { /* upsie. maybe the server is not running as daemon? */
196                         
197                         safestrncpy(WC->ImportantMessage,
198                                     &buf[4],
199                                     sizeof WC->ImportantMessage);
200                 }
201                 begin_burst();
202                 output_headers(1, 0, 0, 0, 1, 0);
203                 DoTemplate(HKEY("aide_display_serverrestart"), NULL, &NoCtx);
204                 end_burst();
205                 lingering_close(WC->Hdr->http_sock);
206                 sleeeeeeeeeep(10);
207                 serv_printf("NOOP");
208                 serv_printf("NOOP");
209         }
210         else if (strcmp(when, "page") == 0) {
211                 char *message;
212                
213                 message = bstr("message");
214                 if ((message == NULL) || (IsEmptyStr(message)))
215                 {
216                         begin_burst();
217                         output_headers(1, 0, 0, 0, 1, 0);
218                         DoTemplate(HKEY("aide_display_serverrestart_page"), NULL, &NoCtx);
219                         end_burst();
220                 }
221                 else
222                 {
223                         serv_printf("SEXP broadcast|%s", message);
224                         serv_getln(buf, sizeof buf); /* TODO: should we care? */
225                         begin_burst();
226                         output_headers(1, 0, 0, 0, 1, 0);
227                         DoTemplate(HKEY("aide_display_serverrestart_page"), NULL, &NoCtx);
228                         end_burst();                    
229                 }
230         }
231         else if (!strcmp(when, "idle")) {
232                 serv_printf("SCDN 3");
233                 serv_getln(buf, sizeof buf);
234
235                 if (atol(buf) == 500)
236                 { /* upsie. maybe the server is not running as daemon? */
237                         safestrncpy(WC->ImportantMessage,
238                                     &buf[4],
239                                     sizeof WC->ImportantMessage);
240                 }
241                 begin_burst();
242                 output_headers(1, 0, 0, 0, 1, 0);
243                 DoTemplate(HKEY("aide_display_menu"), NULL, &NoCtx);
244                 end_burst();                    
245         }
246 }
247
248 void _display_menubar(void) { display_menubar(0); }
249
250 void 
251 InitModule_MAINMENU
252 (void)
253 {
254         WebcitAddUrlHandler(HKEY("display_aide_menu"), "", 0, display_aide_menu, 0);
255         WebcitAddUrlHandler(HKEY("server_shutdown"), "", 0, display_shutdown, 0);
256         WebcitAddUrlHandler(HKEY("display_main_menu"), "", 0, display_main_menu, 0);
257         WebcitAddUrlHandler(HKEY("display_generic"), "", 0, display_generic, 0);
258         WebcitAddUrlHandler(HKEY("do_generic"), "", 0, do_generic, 0);
259         WebcitAddUrlHandler(HKEY("display_menubar"), "", 0, _display_menubar, 0);
260 }