]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
* Got the Zap and List-Zapped commands working
[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");       /* FIX -- add BBS name here */
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)-4], ".jpg", 4))
360                         printf("Content-type: image/jpeg\n");
361                 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
362                         printf("Content-type: text/html\n");
363                 else
364                         printf("Content-type: application/octet-stream\n");
365
366                 fstat(fileno(fp), &statbuf);
367                 bytes = statbuf.st_size;
368                 printf("Content-length: %ld\n", (long)bytes);
369                 printf("\n");
370                 while (bytes--) {
371                         putc(getc(fp), stdout);
372                         }
373                 fflush(stdout);
374                 fclose(fp);
375                 }
376         }
377
378 void output_image() {
379         char buf[256];
380         char xferbuf[4096];
381         off_t bytes;
382         off_t thisblock;
383         off_t accomplished = 0L;
384
385
386         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
387         serv_gets(buf);
388         if (buf[0]=='2') {
389                 bytes = extract_long(&buf[4], 0);
390                 printf("HTTP/1.0 200 OK\n");
391                 output_headers(0);
392                 printf("Content-type: image/gif\n");
393                 printf("Content-length: %ld\n", bytes);
394                 printf("\n");
395
396                 while (bytes > (off_t)0) {
397                         thisblock = (off_t)sizeof(xferbuf);
398                         if (thisblock > bytes) thisblock = bytes;
399                         serv_printf("READ %ld|%ld", accomplished, thisblock);
400                         serv_gets(buf);
401                         if (buf[0]=='6') thisblock = extract_long(&buf[4],0);
402                         serv_read(xferbuf, (int)thisblock);
403                         fwrite(xferbuf, thisblock, 1, stdout);
404                         bytes = bytes - thisblock;
405                         accomplished = accomplished + thisblock;
406                         }
407                 fflush(stdout);
408                 serv_puts("CLOS");
409                 serv_gets(buf);
410                 }
411         else {
412                 printf("HTTP/1.0 404 %s\n", strerror(errno));
413                 output_headers(0);
414                 printf("Content-Type: text/plain\n");
415                 sprintf(buf, "Error retrieving image\n");
416                 printf("Content-length: %d\n", strlen(buf));
417                 printf("\n");
418                 fwrite(buf, strlen(buf), 1, stdout);
419                 }
420
421         }
422
423
424 /*
425  * Convenience function to display a page containing only an error string
426  */
427 void display_error(char *errormessage) {
428         printf("HTTP/1.0 200 OK\n");
429         output_headers(1);
430         wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=770000><TR><TD>");
431         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
432         wprintf("<B>Error</B>\n");
433         wprintf("</FONT></TD></TR></TABLE><BR>\n");
434         escputs(errormessage);
435         wprintf("</BODY></HTML>\n");
436         wDumpContent();
437         }
438
439
440
441
442 void extract_action(char *actbuf, char *cmdbuf) {
443         int i;
444
445         strcpy(actbuf, cmdbuf);
446         if (!strncasecmp(actbuf, "GET /", 5)) strcpy(actbuf, &actbuf[5]);
447         if (!strncasecmp(actbuf, "PUT /", 5)) strcpy(actbuf, &actbuf[5]);
448         if (!strncasecmp(actbuf, "POST /", 6)) strcpy(actbuf, &actbuf[6]);
449
450         for (i=0; i<strlen(actbuf); ++i) {
451                 if (actbuf[i]==' ') { actbuf[i]=0; i=0; }
452                 if (actbuf[i]=='/') { actbuf[i]=0; i=0; }
453                 if (actbuf[i]=='?') { actbuf[i]=0; i=0; }
454                 if (actbuf[i]=='&') { actbuf[i]=0; i=0; }
455                 }
456         }
457
458
459 void session_loop(void) {
460         char cmd[256];
461         char action[256];
462         char buf[256];
463         int a, b;
464         int ContentLength = 0;
465         char *content;
466 FILE *fp;
467
468         /* We stuff these with the values coming from the client cookies,
469          * so we can use them to reconnect a timed out session if we have to.
470          */
471         char c_host[256];
472         char c_port[256];
473         char c_username[256];
474         char c_password[256];
475         char c_roomname[256];
476
477         strcpy(c_host, defaulthost);
478         strcpy(c_port, defaultport);
479         strcpy(c_username, "");
480         strcpy(c_password, "");
481         strcpy(c_roomname, "");
482
483         getz(cmd);
484         extract_action(action, cmd);
485
486         do {
487                 getz(buf);
488
489                 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
490                         strcpy(c_host, &buf[16]);
491                 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
492                         strcpy(c_port, &buf[16]);
493                 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
494                         strcpy(c_username, &buf[20]);
495                 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
496                         strcpy(c_password, &buf[20]);
497                 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
498                         strcpy(c_roomname, &buf[20]);
499
500                 if (!strncasecmp(buf, "Content-length: ", 16)) {
501                         ContentLength = atoi(&buf[16]);
502                         }
503
504                 } while(strlen(buf)>0);
505
506         ++TransactionCount;
507
508         if (ContentLength > 0) {
509                 content = malloc(ContentLength+1);
510                 fread(content, ContentLength, 1, stdin);
511 fp = fopen("content", "wb");
512 fwrite(content, ContentLength, 1, fp);
513 fclose(fp);
514                 content[ContentLength] = 0;
515                 addurls(content);
516                 }
517         else {
518                 content = NULL;
519                 }
520
521         /*
522          * If we're not connected to a Citadel server, try to hook up the
523          * connection now.  Preference is given to the host and port specified
524          * by browser cookies, if cookies have been supplied.
525          */
526         if (!connected) {
527                 serv_sock = connectsock(c_host, c_port, "tcp");
528                 connected = 1;
529                 serv_gets(buf); /* get the server welcome message */
530                 strcpy(wc_host, c_host);
531                 strcpy(wc_port, c_port);
532                 get_serv_info();
533                 }
534
535         check_for_express_messages();
536
537         /*
538          * If we're not logged in, but we have username and password cookies
539          * supplied by the browser, try using them to log in.
540          */
541         if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
542                 serv_printf("USER %s", c_username);
543                 serv_gets(buf);
544                 if (buf[0]=='3') {
545                         serv_printf("PASS %s", c_password);
546                         serv_gets(buf);
547                         if (buf[0]=='2') {
548                                 become_logged_in(c_username, c_password, buf);
549                                 }
550                         }
551                 }
552
553         /*
554          * If we don't have a current room, but a cookie specifying the
555          * current room is supplied, make an effort to go there.
556          */
557         if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
558                 serv_printf("GOTO %s", c_roomname);
559                 serv_gets(buf);
560                 if (buf[0]=='2') {
561                         strcpy(wc_roomname, c_roomname);
562                         }
563                 }
564
565         /* If there are variables in the URL, we must grab them now */  
566         for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
567                 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
568                 addurls(&cmd[a+1]);
569                 cmd[a] = 0;
570                 }
571
572         if (!strcasecmp(action, "static")) {
573                 strcpy(buf, &cmd[12]);
574                 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
575                 output_static(buf);
576                 }
577
578         else if (!strcasecmp(action, "image")) {
579                 output_image();
580                 }
581
582         else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
583                 do_login();
584                 }
585
586         else if (!logged_in) {
587                 display_login(NULL);
588                 }
589
590         /* Various commands... */
591         
592         else if (!strcasecmp(action, "do_welcome")) {
593                 do_welcome();
594                 }
595
596         else if (!strcasecmp(action, "display_main_menu")) {
597                 display_main_menu();
598                 }
599
600         else if (!strcasecmp(action, "advanced")) {
601                 display_advanced_menu();
602                 }
603
604         else if (!strcasecmp(action, "whobbs")) {
605                 whobbs();
606                 }
607
608         else if (!strcasecmp(action, "knrooms")) {
609                 list_all_rooms_by_floor();
610                 }
611
612         else if (!strcasecmp(action, "gotonext")) {
613                 slrp_highest();
614                 gotonext();
615                 }
616
617         else if (!strcasecmp(action, "skip")) {
618                 gotonext();
619                 }
620
621         else if (!strcasecmp(action, "ungoto")) {
622                 ungoto();
623                 }
624
625         else if (!strcasecmp(action, "dotgoto")) {
626                 slrp_highest();
627                 dotgoto();
628                 }
629
630         else if (!strcasecmp(action, "termquit")) {
631                 do_logout();
632                 }
633
634         else if (!strcasecmp(action, "readnew")) {
635                 readloop("readnew");
636                 }
637
638         else if (!strcasecmp(action, "readold")) {
639                 readloop("readold");
640                 }
641
642         else if (!strcasecmp(action, "readfwd")) {
643                 readloop("readfwd");
644                 }
645
646         else if (!strcasecmp(action, "display_enter")) {
647                 display_enter();
648                 }
649
650         else if (!strcasecmp(action, "post")) {
651                 post_message();
652                 }
653
654         else if (!strcasecmp(action, "confirm_delete_msg")) {
655                 confirm_delete_msg();
656                 }
657
658         else if (!strcasecmp(action, "delete_msg")) {
659                 delete_msg();
660                 }
661
662         else if (!strcasecmp(action, "confirm_move_msg")) {
663                 confirm_move_msg();
664                 }
665
666         else if (!strcasecmp(action, "move_msg")) {
667                 move_msg();
668                 }
669
670         else if (!strcasecmp(action, "userlist")) {
671                 userlist();
672                 }
673
674         else if (!strcasecmp(action, "showuser")) {
675                 showuser();
676                 }
677
678         else if (!strcasecmp(action, "display_page")) {
679                 display_page();
680                 }
681
682         else if (!strcasecmp(action, "page_user")) {
683                 page_user();
684                 }
685
686         else if (!strcasecmp(action, "chat")) {
687                 do_chat();
688                 }
689
690         else if (!strcasecmp(action, "display_private")) {
691                 display_private("", 0);
692                 }
693
694         else if (!strcasecmp(action, "goto_private")) {
695                 goto_private();
696                 }
697
698         else if (!strcasecmp(action, "zapped_list")) {
699                 zapped_list();
700                 }
701
702         else if (!strcasecmp(action, "display_zap")) {
703                 display_zap();
704                 }
705
706         else if (!strcasecmp(action, "zap")) {
707                 zap();
708                 }
709
710         /* When all else fails... */
711         else {
712                 printf("HTTP/1.0 200 OK\n");
713                 output_headers(1);
714         
715                 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
716                 wprintf("You're in session %d<HR>\n", wc_session);
717                 wprintf("Command: <BR><PRE>\n");
718                 escputs(cmd);
719                 wprintf("</PRE><HR>\n");
720                 wprintf("Variables: <BR><PRE>\n");
721                 dump_vars();
722                 wprintf("</PRE><HR>\n");
723                 wprintf("</BODY></HTML>\n");
724                 wDumpContent();
725                 }
726
727         fflush(stdout);
728         if (content != NULL) {
729                 free(content);
730                 content = NULL;
731                 }
732         free_urls();
733         }
734
735 int main(int argc, char *argv[]) {
736
737         if (argc < 2 || argc > 4) {
738                 fprintf(stderr,
739                         "webcit: usage: webcit <session_id> [host [port]]\n");
740                 return 1;
741                 }
742
743         wc_session = atoi(argv[1]);
744
745         if (argc > 2) {
746                 defaulthost = argv[2];
747                 if (argc > 3)
748                         defaultport = argv[3];
749                 }
750
751         strcpy(wc_host, "");
752         strcpy(wc_port, "");
753         strcpy(wc_username, "");
754         strcpy(wc_password, "");
755         strcpy(wc_roomname, "");
756
757         while (1) {
758                 session_loop();
759                 }
760         }