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