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