]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
added new "action" variable to make main loop simpler
[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
41 void unescape_input(char *buf)
42 {
43         int a,b;
44         char hex[3];
45
46         while ((isspace(buf[strlen(buf)-1]))&&(strlen(buf)>0))
47                 buf[strlen(buf)-1] = 0;
48
49         for (a=0; a<strlen(buf); ++a) {
50                 if (buf[a]=='+') buf[a]=' ';    
51                 if (buf[a]=='%') {
52                         hex[0]=buf[a+1];
53                         hex[1]=buf[a+2];
54                         hex[2]=0;
55                         sscanf(hex,"%02x",&b);
56                         buf[a] = (char) b;
57                         strcpy(&buf[a+1],&buf[a+3]);
58                         }
59                 }
60
61         }
62
63
64 void addurls(char *url) {
65         char *up, *ptr;
66         char buf[256];
67         int a,b;
68         struct urlcontent *u;
69
70         up = url;
71         while (strlen(up)>0) {
72                 
73                 /* locate the = sign */
74                 strncpy(buf,up,255);
75                 b = (-1);
76                 for (a=255; a>=0; --a) if (buf[a]=='=') b=a;
77                 if (b<0) return;
78                 buf[b]=0;
79         
80                 u = (struct urlcontent *)malloc(sizeof(struct urlcontent));
81                 u->next = urlstrings;
82                 urlstrings = u;
83                 strcpy(u->url_key, buf);
84         
85                 /* now chop that part off */
86                 for (a=0; a<=b; ++a) ++up;
87         
88                 /* locate the & sign */
89                 ptr = up;
90                 b = strlen(up);
91                 for (a=0; a<strlen(up); ++a) {
92                         if (!strncmp(ptr,"&",1)) {
93                                 b=a;
94                                 break;
95                                 }
96                         ++ptr;
97                         }
98                 ptr = up;
99                 for (a=0; a<b; ++a) ++ptr;
100                 strcpy(ptr,"");
101                 
102                 u->url_data = malloc(strlen(up));
103                 strcpy(u->url_data, up);
104                 unescape_input(u->url_data);
105
106                 up = ptr;
107                 ++up;
108                 }
109         }
110
111 void free_urls(void) {
112         struct urlcontent *u;
113
114         while (urlstrings != NULL) {
115                 free(urlstrings->url_data);
116                 u = urlstrings->next;
117                 free(urlstrings);
118                 urlstrings = u;
119                 }
120         }
121
122 /*
123  * Diagnostic function to display the contents of all variables
124  */
125 void dump_vars() {
126         struct urlcontent *u;
127
128         for (u = urlstrings; u != NULL; u = u->next) {
129                 fprintf(stderr, "%38s = %s\n", u->url_key, u->url_data);
130                 }
131         }
132
133 char *bstr(char *key) {
134         struct urlcontent *u;
135
136         for (u = urlstrings; u != NULL; u = u->next) {
137                 if (!strcasecmp(u->url_key, key)) return(u->url_data);
138                 }
139         return("");
140         }
141
142
143 void wprintf(const char *format, ...) {   
144         va_list arg_ptr;   
145         struct webcontent *wptr;
146
147         wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
148         wptr->next = NULL;
149         if (wlist == NULL) {
150                 wlist = wptr;
151                 wlast = wptr;
152                 }
153         else {
154                 wlast->next = wptr;
155                 wlast = wptr;
156                 }
157   
158         va_start(arg_ptr, format);   
159         vsprintf(wptr->w_data, format, arg_ptr);   
160         va_end(arg_ptr);   
161   
162         }
163
164 int wContentLength(void) {
165         struct webcontent *wptr;
166         int len = 0;
167
168         for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
169                 len = len + strlen(wptr->w_data);
170                 }
171
172         return(len);
173         }
174
175 void wDumpContent(void) {
176         struct webcontent *wptr;
177
178         printf("Content-type: text/html\n");
179         printf("Content-length: %d\n", wContentLength());
180         printf("\n");
181
182         while (wlist != NULL) {
183                 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
184                 wptr = wlist->next;
185                 free(wlist);
186                 wlist = wptr;
187                 }
188         wlast = NULL;
189         }
190
191
192 void escputs1(char *strbuf, int nbsp)
193 {
194         int a;
195
196         for (a=0; a<strlen(strbuf); ++a) {
197                 if (strbuf[a]=='<') wprintf("&lt;");
198                 else if (strbuf[a]=='>') wprintf("&gt;");
199                 else if (strbuf[a]=='&') wprintf("&amp;");
200                 else if (strbuf[a]==34) wprintf("&quot;");
201                 else if (strbuf[a]==LB) wprintf("<");
202                 else if (strbuf[a]==RB) wprintf(">");
203                 else if (strbuf[a]==QU) wprintf("\"");
204                 else if ((strbuf[a]==32)&&(nbsp==1)) {
205                         wprintf("&nbsp;");
206                         }
207                 else {
208                         wprintf("%c", strbuf[a]);
209                         }
210                 }
211         }
212
213 void escputs(char *strbuf)
214 {
215         escputs1(strbuf,0);
216         }
217
218
219
220 char *urlesc(char *strbuf)
221 {
222         int a,b,c;
223         char *ec = " #&;`'|*?-~<>^()[]{}$\\";
224         static char outbuf[512];
225         
226         strcpy(outbuf,"");
227
228         for (a=0; a<strlen(strbuf); ++a) {
229                 c = 0;
230                 for (b=0; b<strlen(ec); ++b) {
231                         if (strbuf[a]==ec[b]) c=1;
232                         }
233                 b = strlen(outbuf);
234                 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
235                 else sprintf(&outbuf[b],"%c",strbuf[a]);
236                 }
237         return(outbuf);
238         }
239
240 void urlescputs(char *strbuf)
241 {
242         wprintf("%s",urlesc(strbuf));
243         }
244
245
246 /*
247  * Look for URL's embedded in a buffer and make them linkable.  We use a
248  * target window in order to keep the BBS session in its own window.
249  */
250 void url(char *buf)
251 {
252
253         int pos;
254         int start,end;
255         char ench;
256         char urlbuf[256];
257         char outbuf[256];
258
259         start = (-1);
260         end = strlen(buf);
261         ench = 0;
262
263         for (pos=0; pos<strlen(buf); ++pos) {
264                 if (!strncasecmp(&buf[pos],"http://",7)) start = pos;
265                 if (!strncasecmp(&buf[pos],"ftp://",6)) start = pos;
266                 }
267
268         if (start<0) return;
269
270         if ((start>0)&&(buf[start-1]=='<')) ench = '>';
271         if ((start>0)&&(buf[start-1]=='[')) ench = ']';
272         if ((start>0)&&(buf[start-1]=='(')) ench = ')';
273         if ((start>0)&&(buf[start-1]=='{')) ench = '}';
274
275         for (pos=strlen(buf); pos>start; --pos) {
276                 if ((buf[pos]==' ')||(buf[pos]==ench)) end = pos;
277                 }
278
279         strncpy(urlbuf,&buf[start],end-start);
280         urlbuf[end-start] = 0;
281
282
283         strncpy(outbuf,buf,start);
284         sprintf(&outbuf[start],"%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c", 
285                 LB,QU,urlbuf,QU,QU,TARGET,QU,RB,urlbuf,LB,RB);
286         strcat(outbuf,&buf[end]);
287         strcpy(buf,outbuf);
288         }
289
290
291
292
293 void getz(char *buf) {
294         if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
295         else {
296                 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
297                         buf[strlen(buf)-1] = 0;
298                 }
299         }
300
301 /*
302  * Output all that important stuff that the browser will want to see
303  */
304 void output_headers(void) {
305
306         static char *unset = "; expires=28-May-1971 18:10:00 GMT";
307
308         printf("Server: %s\n", SERVER);
309         printf("Connection: close\n");
310         printf("Set-cookie: wc_session=%d\n", wc_session);
311
312         if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
313         else printf("Set-cookie: wc_host=%s\n", unset);
314
315         if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
316         else printf("Set-cookie: wc_port=%s\n", unset);
317
318         if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
319                 wc_username);
320         else printf("Set-cookie: wc_username=%s\n", unset);
321
322         if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
323                 wc_password);
324         else printf("Set-cookie: wc_password=%s\n", unset);
325
326         if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
327                 wc_roomname);
328         else printf("Set-cookie: wc_roomname=%s\n", unset);
329         }
330
331 void output_static(char *what) {
332         char buf[256];
333         FILE *fp;
334         struct stat statbuf;
335         off_t bytes;
336
337         sprintf(buf, "static/%s", what);
338         fp = fopen(buf, "rb");
339         if (fp == NULL) {
340                 printf("HTTP/1.0 404 %s\n", strerror(errno));
341                 output_headers();
342                 printf("Content-Type: text/plain\n");
343                 sprintf(buf, "%s: %s\n", what, strerror(errno));
344                 printf("Content-length: %d\n", strlen(buf));
345                 printf("\n");
346                 fwrite(buf, strlen(buf), 1, stdout);
347                 }
348         else {
349                 printf("HTTP/1.0 200 OK\n");
350                 output_headers();
351
352                 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
353                         printf("Content-type: image/gif\n");
354                 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
355                         printf("Content-type: text/html\n");
356                 else
357                         printf("Content-type: application/octet-stream\n");
358
359                 fstat(fileno(fp), &statbuf);
360                 bytes = statbuf.st_size;
361                 printf("Content-length: %ld\n", (long)bytes);
362                 printf("\n");
363                 while (bytes--) {
364                         putc(getc(fp), stdout);
365                         }
366                 fflush(stdout);
367                 fclose(fp);
368                 }
369         }
370
371 static const char *defaulthost = DEFAULT_HOST;
372 static const char *defaultport = DEFAULT_PORT;
373
374
375 void extract_action(char *actbuf, char *cmdbuf) {
376         int i;
377
378         strcpy(actbuf, cmdbuf);
379         if (!strncasecmp(actbuf, "GET /", 5)) strcpy(actbuf, &actbuf[5]);
380         if (!strncasecmp(actbuf, "PUT /", 5)) strcpy(actbuf, &actbuf[5]);
381         if (!strncasecmp(actbuf, "POST /", 6)) strcpy(actbuf, &actbuf[6]);
382
383         for (i=0; i<strlen(actbuf); ++i) {
384                 if (actbuf[i]==' ') { actbuf[i]=0; i=0; }
385                 if (actbuf[i]=='/') { actbuf[i]=0; i=0; }
386                 if (actbuf[i]=='?') { actbuf[i]=0; i=0; }
387                 if (actbuf[i]=='&') { actbuf[i]=0; i=0; }
388                 }
389         }
390
391
392 void session_loop(void) {
393         char cmd[256];
394         char action[256];
395         char buf[256];
396         int a, b;
397         int ContentLength = 0;
398         char *content;
399
400         /* We stuff these with the values coming from the client cookies,
401          * so we can use them to reconnect a timed out session if we have to.
402          */
403         char c_host[256];
404         char c_port[256];
405         char c_username[256];
406         char c_password[256];
407         char c_roomname[256];
408
409         strcpy(c_host, defaulthost);
410         strcpy(c_port, defaultport);
411         strcpy(c_username, "");
412         strcpy(c_password, "");
413         strcpy(c_roomname, "");
414
415         getz(cmd);
416         extract_action(action, cmd);
417
418         do {
419                 getz(buf);
420
421                 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
422                         strcpy(c_host, &buf[16]);
423                 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
424                         strcpy(c_port, &buf[16]);
425                 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
426                         strcpy(c_username, &buf[20]);
427                 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
428                         strcpy(c_password, &buf[20]);
429                 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
430                         strcpy(c_roomname, &buf[20]);
431
432                 if (!strncasecmp(buf, "Content-length: ", 16)) {
433                         ContentLength = atoi(&buf[16]);
434                         }
435
436                 } while(strlen(buf)>0);
437
438         ++TransactionCount;
439
440         if (ContentLength > 0) {
441                 content = malloc(ContentLength+1);
442                 fread(content, ContentLength, 1, stdin);
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         /* Verbose but informative; uncomment if you want to trace variables */
501         /* dump_vars(); */
502
503         if (!strcasecmp(action, "static")) {
504                 strcpy(buf, &cmd[12]);
505                 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
506                 output_static(buf);
507                 }
508
509         else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
510                 do_login();
511                 }
512
513         else if (!logged_in) {
514                 display_login();
515                 }
516
517         /* Various commands... */
518         
519         else if (!strcasecmp(action, "do_welcome")) {
520                 do_welcome();
521                 }
522
523         else if (!strcasecmp(action, "display_main_menu")) {
524                 display_main_menu();
525                 }
526
527         else if (!strcasecmp(action, "advanced")) {
528                 display_advanced_menu();
529                 }
530
531         else if (!strcasecmp(action, "whobbs")) {
532                 whobbs();
533                 }
534
535         else if (!strcasecmp(action, "knrooms")) {
536                 list_all_rooms_by_floor();
537                 }
538
539         else if (!strcasecmp(action, "gotonext")) {
540                 slrp_highest();
541                 gotonext();
542                 }
543
544         else if (!strcasecmp(action, "skip")) {
545                 gotonext();
546                 }
547
548         else if (!strcasecmp(action, "ungoto")) {
549                 ungoto();
550                 }
551
552         else if (!strcasecmp(action, "dotgoto")) {
553                 slrp_highest();
554                 dotgoto();
555                 }
556
557         else if (!strcasecmp(action, "termquit")) {
558                 do_logout();
559                 }
560
561         /* When all else fails... */
562         else {
563                 printf("HTTP/1.0 200 OK\n");
564                 output_headers();
565         
566                 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
567                 wprintf("TransactionCount is %d<HR>\n", TransactionCount);
568                 wprintf("You're in session %d<BR>\n", wc_session);
569                 wprintf("</BODY></HTML>\n");
570                 wDumpContent();
571                 }
572
573         fflush(stdout);
574         if (content != NULL) {
575                 free(content);
576                 content = NULL;
577                 }
578         free_urls();
579         }
580
581 int main(int argc, char *argv[]) {
582
583         if (argc < 2 || argc > 4) {
584                 fprintf(stderr,
585                         "webcit: usage: webcit <session_id> [host [port]]\n");
586                 return 1;
587                 }
588
589         wc_session = atoi(argv[1]);
590
591         if (argc > 2) {
592                 defaulthost = argv[2];
593                 if (argc > 3)
594                         defaultport = argv[3];
595                 }
596
597         strcpy(wc_host, "");
598         strcpy(wc_port, "");
599         strcpy(wc_username, "");
600         strcpy(wc_password, "");
601         strcpy(wc_roomname, "");
602
603         while (1) {
604                 session_loop();
605                 }
606         }