]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
Display express messages in cute little JavaScript popup window.
[citadel.git] / webcit / webcit.c
1 /*
2  * webcit.c
3  *
4  * This is the actual program called by the webserver.  It maintains a
5  * persistent session to the Citadel server, handling HTTP WebCit requests as
6  * they arrive and presenting a user interface.
7  *
8  * $Id$
9  */
10
11 #include <stdlib.h>
12 #ifdef HAVE_UNISTD_H
13 #include <unistd.h>
14 #endif
15 #include <stdio.h>
16 #include <ctype.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <sys/stat.h>
20 #include <stdarg.h>
21 #include "webcit.h"
22 #include "child.h"
23
24 int wc_session;
25 char wc_host[256];
26 char wc_port[256];
27 char wc_username[256];
28 char wc_password[256];
29 char wc_roomname[256];
30 int TransactionCount = 0;
31 int connected = 0;
32 int logged_in = 0;
33 int axlevel;
34 char *ExpressMessages = NULL;
35
36 struct webcontent *wlist = NULL;
37 struct webcontent *wlast = NULL;
38
39 struct urlcontent *urlstrings = NULL;
40
41 static const char *defaulthost = DEFAULT_HOST;
42 static const char *defaultport = DEFAULT_PORT;
43
44
45 void unescape_input(char *buf)
46 {
47         int a,b;
48         char hex[3];
49
50         while ((isspace(buf[strlen(buf)-1]))&&(strlen(buf)>0))
51                 buf[strlen(buf)-1] = 0;
52
53         for (a=0; a<strlen(buf); ++a) {
54                 if (buf[a]=='+') buf[a]=' ';    
55                 if (buf[a]=='%') {
56                         hex[0]=buf[a+1];
57                         hex[1]=buf[a+2];
58                         hex[2]=0;
59                         sscanf(hex,"%02x",&b);
60                         buf[a] = (char) b;
61                         strcpy(&buf[a+1],&buf[a+3]);
62                         }
63                 }
64
65         }
66
67
68 void addurls(char *url) {
69         char *up, *ptr;
70         char buf[256];
71         int a,b;
72         struct urlcontent *u;
73
74         up = url;
75         while (strlen(up)>0) {
76                 
77                 /* locate the = sign */
78                 strncpy(buf,up,255);
79                 b = (-1);
80                 for (a=255; a>=0; --a) if (buf[a]=='=') b=a;
81                 if (b<0) return;
82                 buf[b]=0;
83         
84                 u = (struct urlcontent *)malloc(sizeof(struct urlcontent));
85                 u->next = urlstrings;
86                 urlstrings = u;
87                 strcpy(u->url_key, buf);
88         
89                 /* now chop that part off */
90                 for (a=0; a<=b; ++a) ++up;
91         
92                 /* locate the & sign */
93                 ptr = up;
94                 b = strlen(up);
95                 for (a=0; a<strlen(up); ++a) {
96                         if (!strncmp(ptr,"&",1)) {
97                                 b=a;
98                                 break;
99                                 }
100                         ++ptr;
101                         }
102                 ptr = up;
103                 for (a=0; a<b; ++a) ++ptr;
104                 strcpy(ptr,"");
105                 
106                 u->url_data = malloc(strlen(up)+1);
107                 strcpy(u->url_data, up);
108                 u->url_data[b] = 0;
109                 unescape_input(u->url_data);
110                 up = ptr;
111                 ++up;
112                 }
113         }
114
115 void free_urls(void) {
116         struct urlcontent *u;
117
118         while (urlstrings != NULL) {
119                 free(urlstrings->url_data);
120                 u = urlstrings->next;
121                 free(urlstrings);
122                 urlstrings = u;
123                 }
124         }
125
126 /*
127  * Diagnostic function to display the contents of all variables
128  */
129 void dump_vars(void) {
130         struct urlcontent *u;
131
132         for (u = urlstrings; u != NULL; u = u->next) {
133                 wprintf("%38s = %s\n", u->url_key, u->url_data);
134                 }
135         }
136
137 char *bstr(char *key) {
138         struct urlcontent *u;
139
140         for (u = urlstrings; u != NULL; u = u->next) {
141                 if (!strcasecmp(u->url_key, key)) return(u->url_data);
142                 }
143         return("");
144         }
145
146
147 void wprintf(const char *format, ...) {   
148         va_list arg_ptr;   
149         struct webcontent *wptr;
150
151         wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
152         wptr->next = NULL;
153         if (wlist == NULL) {
154                 wlist = wptr;
155                 wlast = wptr;
156                 }
157         else {
158                 wlast->next = wptr;
159                 wlast = wptr;
160                 }
161   
162         va_start(arg_ptr, format);   
163         vsprintf(wptr->w_data, format, arg_ptr);   
164         va_end(arg_ptr);   
165         }
166
167 int wContentLength(void) {
168         struct webcontent *wptr;
169         int len = 0;
170
171         for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
172                 len = len + strlen(wptr->w_data);
173                 }
174
175         return(len);
176         }
177
178 void wDumpContent(void) {
179         struct webcontent *wptr;
180
181         printf("Content-type: text/html\n");
182         printf("Content-length: %d\n", wContentLength());
183         printf("\n");
184
185         while (wlist != NULL) {
186                 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
187                 wptr = wlist->next;
188                 free(wlist);
189                 wlist = wptr;
190                 }
191         wlast = NULL;
192         }
193
194
195 void escputs1(char *strbuf, int nbsp)
196 {
197         int a;
198
199         for (a=0; a<strlen(strbuf); ++a) {
200                 if (strbuf[a]=='<') wprintf("&lt;");
201                 else if (strbuf[a]=='>') wprintf("&gt;");
202                 else if (strbuf[a]=='&') wprintf("&amp;");
203                 else if (strbuf[a]==34) wprintf("&quot;");
204                 else if (strbuf[a]==LB) wprintf("<");
205                 else if (strbuf[a]==RB) wprintf(">");
206                 else if (strbuf[a]==QU) wprintf("\"");
207                 else if ((strbuf[a]==32)&&(nbsp==1)) {
208                         wprintf("&nbsp;");
209                         }
210                 else {
211                         wprintf("%c", strbuf[a]);
212                         }
213                 }
214         }
215
216 void escputs(char *strbuf)
217 {
218         escputs1(strbuf,0);
219         }
220
221
222
223 char *urlesc(char *strbuf)
224 {
225         int a,b,c;
226         char *ec = " #&;`'|*?-~<>^()[]{}$\\";
227         static char outbuf[512];
228         
229         strcpy(outbuf,"");
230
231         for (a=0; a<strlen(strbuf); ++a) {
232                 c = 0;
233                 for (b=0; b<strlen(ec); ++b) {
234                         if (strbuf[a]==ec[b]) c=1;
235                         }
236                 b = strlen(outbuf);
237                 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
238                 else sprintf(&outbuf[b],"%c",strbuf[a]);
239                 }
240         return(outbuf);
241         }
242
243 void urlescputs(char *strbuf)
244 {
245         wprintf("%s",urlesc(strbuf));
246         }
247
248
249 void getz(char *buf) {
250         if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
251         else {
252                 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
253                         buf[strlen(buf)-1] = 0;
254                 }
255         }
256
257 /*
258  * Output all that important stuff that the browser will want to see
259  */
260 void output_headers(int print_standard_html_head) {
261
262         static char *unset = "; expires=28-May-1971 18:10:00 GMT";
263
264         printf("Server: %s\n", SERVER);
265         printf("Connection: close\n");
266         printf("Set-cookie: wc_session=%d\n", wc_session);
267
268         if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
269         else printf("Set-cookie: wc_host=%s\n", unset);
270
271         if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
272         else printf("Set-cookie: wc_port=%s\n", unset);
273
274         if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
275                 wc_username);
276         else printf("Set-cookie: wc_username=%s\n", unset);
277
278         if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
279                 wc_password);
280         else printf("Set-cookie: wc_password=%s\n", unset);
281
282         if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
283                 wc_roomname);
284         else printf("Set-cookie: wc_roomname=%s\n", unset);
285
286         if (print_standard_html_head) {
287                 wprintf("<HTML><HEAD><TITLE>");
288                 escputs("WebCit");
289                 wprintf("</TITLE></HEAD>");
290                 if (ExpressMessages != NULL) {
291                         wprintf("<SCRIPT language=\"javascript\">\n");
292                         wprintf("function ExpressMessage() {\n");
293                         wprintf(" alert(\"");
294                         escputs(ExpressMessages);
295                         wprintf("\")\n");
296                         wprintf(" }\n </SCRIPT>\n");
297                         }
298                 wprintf("<BODY ");
299                 if (ExpressMessages != NULL) {
300                         wprintf("onload=\"ExpressMessage()\" ");
301                         free(ExpressMessages);
302                         ExpressMessages = NULL;
303                         }
304                 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
305                 }
306
307         }
308
309
310
311
312 void check_for_express_messages() {
313         char buf[256];
314
315         serv_puts("PEXP");
316         serv_gets(buf);
317         if (buf[0]=='1') {
318                 while (serv_gets(buf), strcmp(buf, "000")) {
319                         if (ExpressMessages == NULL) {
320                                 ExpressMessages = malloc(strlen(buf) + 4);
321                                 strcpy(ExpressMessages, "");
322                                 }
323                         else {
324                                 ExpressMessages = realloc(ExpressMessages,
325                                  (strlen(ExpressMessages) + strlen(buf) + 4) );
326                                 }
327                         strcat(ExpressMessages, buf);
328                         strcat(ExpressMessages, "\\n");
329                         }
330                 }
331         }
332
333
334
335
336 void output_static(char *what) {
337         char buf[256];
338         FILE *fp;
339         struct stat statbuf;
340         off_t bytes;
341
342         sprintf(buf, "static/%s", what);
343         fp = fopen(buf, "rb");
344         if (fp == NULL) {
345                 printf("HTTP/1.0 404 %s\n", strerror(errno));
346                 output_headers(0);
347                 printf("Content-Type: text/plain\n");
348                 sprintf(buf, "%s: %s\n", what, strerror(errno));
349                 printf("Content-length: %d\n", strlen(buf));
350                 printf("\n");
351                 fwrite(buf, strlen(buf), 1, stdout);
352                 }
353         else {
354                 printf("HTTP/1.0 200 OK\n");
355                 output_headers(0);
356
357                 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
358                         printf("Content-type: image/gif\n");
359                 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
360                         printf("Content-type: text/html\n");
361                 else
362                         printf("Content-type: application/octet-stream\n");
363
364                 fstat(fileno(fp), &statbuf);
365                 bytes = statbuf.st_size;
366                 printf("Content-length: %ld\n", (long)bytes);
367                 printf("\n");
368                 while (bytes--) {
369                         putc(getc(fp), stdout);
370                         }
371                 fflush(stdout);
372                 fclose(fp);
373                 }
374         }
375
376 void output_image() {
377         char buf[256];
378         char xferbuf[4096];
379         off_t bytes;
380         off_t thisblock;
381         off_t accomplished = 0L;
382
383
384         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
385         serv_gets(buf);
386         if (buf[0]=='2') {
387                 bytes = extract_long(&buf[4], 0);
388                 printf("HTTP/1.0 200 OK\n");
389                 output_headers(0);
390                 printf("Content-type: image/gif\n");
391                 printf("Content-length: %ld\n", bytes);
392                 printf("\n");
393
394                 while (bytes > (off_t)0) {
395                         thisblock = (off_t)sizeof(xferbuf);
396                         if (thisblock > bytes) thisblock = bytes;
397                         serv_printf("READ %ld|%ld", accomplished, thisblock);
398                         serv_gets(buf);
399                         if (buf[0]=='6') thisblock = extract_long(&buf[4],0);
400                         serv_read(xferbuf, (int)thisblock);
401                         fwrite(xferbuf, thisblock, 1, stdout);
402                         bytes = bytes - thisblock;
403                         accomplished = accomplished + thisblock;
404                         }
405                 fflush(stdout);
406                 serv_puts("CLOS");
407                 serv_gets(buf);
408                 }
409         else {
410                 printf("HTTP/1.0 404 %s\n", strerror(errno));
411                 output_headers(0);
412                 printf("Content-Type: text/plain\n");
413                 sprintf(buf, "Error retrieving image\n");
414                 printf("Content-length: %d\n", strlen(buf));
415                 printf("\n");
416                 fwrite(buf, strlen(buf), 1, stdout);
417                 }
418
419         }
420
421 void extract_action(char *actbuf, char *cmdbuf) {
422         int i;
423
424         strcpy(actbuf, cmdbuf);
425         if (!strncasecmp(actbuf, "GET /", 5)) strcpy(actbuf, &actbuf[5]);
426         if (!strncasecmp(actbuf, "PUT /", 5)) strcpy(actbuf, &actbuf[5]);
427         if (!strncasecmp(actbuf, "POST /", 6)) strcpy(actbuf, &actbuf[6]);
428
429         for (i=0; i<strlen(actbuf); ++i) {
430                 if (actbuf[i]==' ') { actbuf[i]=0; i=0; }
431                 if (actbuf[i]=='/') { actbuf[i]=0; i=0; }
432                 if (actbuf[i]=='?') { actbuf[i]=0; i=0; }
433                 if (actbuf[i]=='&') { actbuf[i]=0; i=0; }
434                 }
435         }
436
437
438 void session_loop(void) {
439         char cmd[256];
440         char action[256];
441         char buf[256];
442         int a, b;
443         int ContentLength = 0;
444         char *content;
445 FILE *fp;
446
447         /* We stuff these with the values coming from the client cookies,
448          * so we can use them to reconnect a timed out session if we have to.
449          */
450         char c_host[256];
451         char c_port[256];
452         char c_username[256];
453         char c_password[256];
454         char c_roomname[256];
455
456         strcpy(c_host, defaulthost);
457         strcpy(c_port, defaultport);
458         strcpy(c_username, "");
459         strcpy(c_password, "");
460         strcpy(c_roomname, "");
461
462         getz(cmd);
463         extract_action(action, cmd);
464
465         do {
466                 getz(buf);
467
468                 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
469                         strcpy(c_host, &buf[16]);
470                 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
471                         strcpy(c_port, &buf[16]);
472                 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
473                         strcpy(c_username, &buf[20]);
474                 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
475                         strcpy(c_password, &buf[20]);
476                 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
477                         strcpy(c_roomname, &buf[20]);
478
479                 if (!strncasecmp(buf, "Content-length: ", 16)) {
480                         ContentLength = atoi(&buf[16]);
481                         }
482
483                 } while(strlen(buf)>0);
484
485         ++TransactionCount;
486
487         if (ContentLength > 0) {
488                 content = malloc(ContentLength+1);
489                 fread(content, ContentLength, 1, stdin);
490 fp = fopen("content", "wb");
491 fwrite(content, ContentLength, 1, fp);
492 fclose(fp);
493                 content[ContentLength] = 0;
494                 addurls(content);
495                 }
496         else {
497                 content = NULL;
498                 }
499
500         /*
501          * If we're not connected to a Citadel server, try to hook up the
502          * connection now.  Preference is given to the host and port specified
503          * by browser cookies, if cookies have been supplied.
504          */
505         if (!connected) {
506                 serv_sock = connectsock(c_host, c_port, "tcp");
507                 connected = 1;
508                 serv_gets(buf); /* get the server welcome message */
509                 strcpy(wc_host, c_host);
510                 strcpy(wc_port, c_port);
511                 get_serv_info();
512                 }
513
514         check_for_express_messages();
515
516         /*
517          * If we're not logged in, but we have username and password cookies
518          * supplied by the browser, try using them to log in.
519          */
520         if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
521                 serv_printf("USER %s", c_username);
522                 serv_gets(buf);
523                 if (buf[0]=='3') {
524                         serv_printf("PASS %s", c_password);
525                         serv_gets(buf);
526                         if (buf[0]=='2') {
527                                 become_logged_in(c_username, c_password, buf);
528                                 }
529                         }
530                 }
531
532         /*
533          * If we don't have a current room, but a cookie specifying the
534          * current room is supplied, make an effort to go there.
535          */
536         if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
537                 serv_printf("GOTO %s", c_roomname);
538                 serv_gets(buf);
539                 if (buf[0]=='2') {
540                         strcpy(wc_roomname, c_roomname);
541                         }
542                 }
543
544         /* If there are variables in the URL, we must grab them now */  
545         for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
546                 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
547                 addurls(&cmd[a+1]);
548                 cmd[a] = 0;
549                 }
550
551         if (!strcasecmp(action, "static")) {
552                 strcpy(buf, &cmd[12]);
553                 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
554                 output_static(buf);
555                 }
556
557         else if (!strcasecmp(action, "image")) {
558                 output_image();
559                 }
560
561         else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
562                 do_login();
563                 }
564
565         else if (!logged_in) {
566                 display_login(NULL);
567                 }
568
569         /* Various commands... */
570         
571         else if (!strcasecmp(action, "do_welcome")) {
572                 do_welcome();
573                 }
574
575         else if (!strcasecmp(action, "display_main_menu")) {
576                 display_main_menu();
577                 }
578
579         else if (!strcasecmp(action, "advanced")) {
580                 display_advanced_menu();
581                 }
582
583         else if (!strcasecmp(action, "whobbs")) {
584                 whobbs();
585                 }
586
587         else if (!strcasecmp(action, "knrooms")) {
588                 list_all_rooms_by_floor();
589                 }
590
591         else if (!strcasecmp(action, "gotonext")) {
592                 slrp_highest();
593                 gotonext();
594                 }
595
596         else if (!strcasecmp(action, "skip")) {
597                 gotonext();
598                 }
599
600         else if (!strcasecmp(action, "ungoto")) {
601                 ungoto();
602                 }
603
604         else if (!strcasecmp(action, "dotgoto")) {
605                 slrp_highest();
606                 dotgoto();
607                 }
608
609         else if (!strcasecmp(action, "termquit")) {
610                 do_logout();
611                 }
612
613         else if (!strcasecmp(action, "readnew")) {
614                 readloop("readnew");
615                 }
616
617         else if (!strcasecmp(action, "readold")) {
618                 readloop("readold");
619                 }
620
621         else if (!strcasecmp(action, "readfwd")) {
622                 readloop("readfwd");
623                 }
624
625         else if (!strcasecmp(action, "display_enter")) {
626                 display_enter();
627                 }
628
629         else if (!strcasecmp(action, "post")) {
630                 post_message();
631                 }
632
633         else if (!strcasecmp(action, "confirm_delete_msg")) {
634                 confirm_delete_msg();
635                 }
636
637         else if (!strcasecmp(action, "delete_msg")) {
638                 delete_msg();
639                 }
640
641         else if (!strcasecmp(action, "confirm_move_msg")) {
642                 confirm_move_msg();
643                 }
644
645         else if (!strcasecmp(action, "move_msg")) {
646                 move_msg();
647                 }
648
649         else if (!strcasecmp(action, "userlist")) {
650                 userlist();
651                 }
652
653         else if (!strcasecmp(action, "showuser")) {
654                 showuser();
655                 }
656
657         /* When all else fails... */
658         else {
659                 printf("HTTP/1.0 200 OK\n");
660                 output_headers(1);
661         
662                 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
663                 wprintf("You're in session %d<HR>\n", wc_session);
664                 wprintf("Command: <BR><PRE>\n");
665                 escputs(cmd);
666                 wprintf("</PRE><HR>\n");
667                 wprintf("Variables: <BR><PRE>\n");
668                 dump_vars();
669                 wprintf("</PRE><HR>\n");
670                 wprintf("</BODY></HTML>\n");
671                 wDumpContent();
672                 }
673
674         fflush(stdout);
675         if (content != NULL) {
676                 free(content);
677                 content = NULL;
678                 }
679         free_urls();
680         }
681
682 int main(int argc, char *argv[]) {
683
684         if (argc < 2 || argc > 4) {
685                 fprintf(stderr,
686                         "webcit: usage: webcit <session_id> [host [port]]\n");
687                 return 1;
688                 }
689
690         wc_session = atoi(argv[1]);
691
692         if (argc > 2) {
693                 defaulthost = argv[2];
694                 if (argc > 3)
695                         defaultport = argv[3];
696                 }
697
698         strcpy(wc_host, "");
699         strcpy(wc_port, "");
700         strcpy(wc_username, "");
701         strcpy(wc_password, "");
702         strcpy(wc_roomname, "");
703
704         while (1) {
705                 session_loop();
706                 }
707         }