]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
got known rooms list 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
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <ctype.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <sys/stat.h>
16 #include <stdarg.h>
17 #include "webcit.h"
18
19 int wc_session;
20 char wc_host[256];
21 char wc_port[256];
22 char wc_username[256];
23 char wc_password[256];
24 char wc_roomname[256];
25 int TransactionCount = 0;
26 int connected = 0;
27 int logged_in = 0;
28
29 struct webcontent *wlist = NULL;
30 struct webcontent *wlast = NULL;
31
32 struct urlcontent *urlstrings = NULL;
33
34
35 void unescape_input(buf)
36 char buf[]; {
37         int a,b;
38         char hex[3];
39
40         while ((isspace(buf[strlen(buf)-1]))&&(strlen(buf)>0))
41                 buf[strlen(buf)-1] = 0;
42
43         for (a=0; a<strlen(buf); ++a) {
44                 if (buf[a]=='+') buf[a]=' ';    
45                 if (buf[a]=='%') {
46                         hex[0]=buf[a+1];
47                         hex[1]=buf[a+2];
48                         hex[2]=0;
49                         sscanf(hex,"%02x",&b);
50                         buf[a] = (char) b;
51                         strcpy(&buf[a+1],&buf[a+3]);
52                         }
53                 }
54
55         }
56
57
58 void addurls(char *url) {
59         char *up, *ptr;
60         char buf[256];
61         int a,b;
62         struct urlcontent *u;
63
64         up = url;
65         while (strlen(up)>0) {
66                 
67                 /* locate the = sign */
68                 strncpy(buf,up,255);
69                 b = (-1);
70                 for (a=255; a>=0; --a) if (buf[a]=='=') b=a;
71                 if (b<0) goto DONE;
72                 buf[b]=0;
73         
74                 u = (struct urlcontent *)malloc(sizeof(struct urlcontent));
75                 u->next = urlstrings;
76                 urlstrings = u;
77                 strcpy(u->url_key, buf);
78         
79                 /* now chop that part off */
80                 for (a=0; a<=b; ++a) ++up;
81         
82                 /* locate the & sign */
83                 ptr = up;
84                 b = strlen(up);
85                 for (a=0; a<strlen(up); ++a) {
86                         if (!strncmp(ptr,"&",1)) {
87                                 b=a;
88                                 goto FOUNDIT;
89                                 }
90                         ++ptr;
91                         }
92 FOUNDIT:        ptr = up;
93                 for (a=0; a<b; ++a) ++ptr;
94                 strcpy(ptr,"");
95                 
96                 u->url_data = malloc(strlen(up));
97                 strcpy(u->url_data, up);
98                 unescape_input(u->url_data);
99
100                 up = ptr;
101                 ++up;
102
103                 fprintf(stderr, "%s=%s\n", u->url_key, u->url_data);
104                 }
105 DONE:
106         }
107
108 void free_urls() {
109         struct urlcontent *u;
110
111         while (urlstrings != NULL) {
112                 free(urlstrings->url_data);
113                 u = urlstrings->next;
114                 free(urlstrings);
115                 urlstrings = u;
116                 }
117         }
118
119 char *bstr(char *key) {
120         struct urlcontent *u;
121
122         for (u = urlstrings; u != NULL; u = u->next) {
123                 if (!strcasecmp(u->url_key, key)) return(u->url_data);
124                 }
125         return("");
126         }
127
128
129 void wprintf(const char *format, ...) {   
130         va_list arg_ptr;   
131         struct webcontent *wptr;
132
133         wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
134         wptr->next = NULL;
135         if (wlist == NULL) {
136                 wlist = wptr;
137                 wlast = wptr;
138                 }
139         else {
140                 wlast->next = wptr;
141                 wlast = wptr;
142                 }
143   
144         va_start(arg_ptr, format);   
145         vsprintf(wptr->w_data, format, arg_ptr);   
146         va_end(arg_ptr);   
147   
148         }
149
150 int wContentLength() {
151         struct webcontent *wptr;
152         int len = 0;
153
154         for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
155                 len = len + strlen(wptr->w_data);
156                 }
157
158         return(len);
159         }
160
161 void wDumpContent() {
162         struct webcontent *wptr;
163
164         printf("Content-type: text/html\n");
165         printf("Content-length: %d\n", wContentLength());
166         printf("\n");
167
168         while (wlist != NULL) {
169                 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
170                 wptr = wlist->next;
171                 free(wlist);
172                 wlist = wptr;
173                 }
174         wlast = NULL;
175         }
176
177
178 void escputs1(strbuf,nbsp)
179 char strbuf[];
180 int nbsp; {
181         int a;
182
183         for (a=0; a<strlen(strbuf); ++a) {
184                 if (strbuf[a]=='<') wprintf("&lt;");
185                 else if (strbuf[a]=='>') wprintf("&gt;");
186                 else if (strbuf[a]=='&') wprintf("&amp;");
187                 else if (strbuf[a]==34) wprintf("&quot;");
188                 else if (strbuf[a]==LB) wprintf("<");
189                 else if (strbuf[a]==RB) wprintf(">");
190                 else if (strbuf[a]==QU) wprintf("\"");
191                 else if ((strbuf[a]==32)&&(nbsp==1)) {
192                         wprintf("&nbsp;");
193                         }
194                 else {
195                         wprintf("%c", strbuf[a]);
196                         }
197                 }
198         }
199
200 void escputs(strbuf)
201 char *strbuf; {
202         escputs1(strbuf,0);
203         }
204
205
206
207 char *urlesc(strbuf)
208 char strbuf[]; {
209         int a,b,c;
210         char *ec = " #&;`'|*?-~<>^()[]{}$\\";
211         static char outbuf[512];
212         
213         strcpy(outbuf,"");
214
215         for (a=0; a<strlen(strbuf); ++a) {
216                 c = 0;
217                 for (b=0; b<strlen(ec); ++b) {
218                         if (strbuf[a]==ec[b]) c=1;
219                         }
220                 b = strlen(outbuf);
221                 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
222                 else sprintf(&outbuf[b],"%c",strbuf[a]);
223                 }
224         return(outbuf);
225         }
226
227 void urlescputs(strbuf)
228 char strbuf[]; {
229         wprintf("%s",urlesc(strbuf));
230         }
231
232
233 /*
234  * Look for URL's embedded in a buffer and make them linkable.  We use a
235  * target window in order to keep the BBS session in its own window.
236  */
237 void url(buf)
238 char buf[]; {
239
240         int pos;
241         int start,end;
242         char ench;
243         char urlbuf[256];
244         char outbuf[256];
245
246         start = (-1);
247         end = strlen(buf);
248         ench = 0;
249
250         for (pos=0; pos<strlen(buf); ++pos) {
251                 if (!strncasecmp(&buf[pos],"http://",7)) start = pos;
252                 if (!strncasecmp(&buf[pos],"ftp://",6)) start = pos;
253                 }
254
255         if (start<0) return;
256
257         if ((start>0)&&(buf[start-1]=='<')) ench = '>';
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
262         for (pos=strlen(buf); pos>start; --pos) {
263                 if ((buf[pos]==' ')||(buf[pos]==ench)) end = pos;
264                 }
265
266         strncpy(urlbuf,&buf[start],end-start);
267         urlbuf[end-start] = 0;
268
269
270         strncpy(outbuf,buf,start);
271         sprintf(&outbuf[start],"%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c", 
272                 LB,QU,urlbuf,QU,QU,TARGET,QU,RB,urlbuf,LB,RB);
273         strcat(outbuf,&buf[end]);
274         strcpy(buf,outbuf);
275         }
276
277
278
279
280 void getz(char *buf) {
281         if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
282         else {
283                 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
284                         buf[strlen(buf)-1] = 0;
285                 }
286         }
287
288 /*
289  * Output all that important stuff that the browser will want to see
290  */
291 void output_headers() {
292         printf("Server: %s\n", SERVER);
293         printf("Connection: close\n");
294         printf("Set-cookie: wc_session=%d\n", wc_session);
295         if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
296         if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
297         if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
298                 wc_username);
299         if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
300                 wc_password);
301         if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
302                 wc_roomname);
303         }
304
305 void output_static(char *what) {
306         char buf[256];
307         FILE *fp;
308         struct stat statbuf;
309         off_t bytes;
310
311         sprintf(buf, "static/%s", what);
312         fp = fopen(buf, "rb");
313         if (fp == NULL) {
314                 printf("HTTP/1.0 404 %s\n", strerror(errno));
315                 output_headers();
316                 printf("Content-Type: text/plain\n");
317                 sprintf(buf, "%s: %s\n", what, strerror(errno));
318                 printf("Content-length: %d\n", strlen(buf));
319                 printf("\n");
320                 fwrite(buf, strlen(buf), 1, stdout);
321                 }
322         else {
323                 printf("HTTP/1.0 200 OK\n");
324                 output_headers();
325
326                 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
327                         printf("Content-type: image/gif\n");
328                 else
329                         printf("Content-type: application/octet-stream\n");
330
331                 fstat(fileno(fp), &statbuf);
332                 bytes = statbuf.st_size;
333                 printf("Content-length: %d\n", bytes);
334                 printf("\n");
335                 while (bytes--) {
336                         putc(getc(fp), stdout);
337                         }
338                 fclose(fp);
339                 }
340         }
341
342
343 void session_loop() {
344         char cmd[256];
345         char buf[256];
346         int a, b;
347         int ContentLength = 0;
348         char *content;
349
350         /* We stuff these with the values coming from the client cookies,
351          * so we can use them to reconnect a timed out session if we have to.
352          */
353         char c_host[256];
354         char c_port[256];
355         char c_username[256];
356         char c_password[256];
357         char c_roomname[256];
358
359         strcpy(c_host, DEFAULT_HOST);
360         strcpy(c_port, DEFAULT_PORT);
361         strcpy(c_username, "");
362         strcpy(c_password, "");
363         strcpy(c_roomname, "");
364
365         getz(cmd);
366         fprintf(stderr, "\nCmd: %s\n", cmd);
367         fflush(stderr);
368
369         do {
370                 getz(buf);
371                 fprintf(stderr, "Buf: %s\n", buf);
372                 fflush(stderr);
373
374                 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
375                         strcpy(c_host, &buf[16]);
376                 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
377                         strcpy(c_port, &buf[16]);
378                 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
379                         strcpy(c_username, &buf[20]);
380                 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
381                         strcpy(c_password, &buf[20]);
382                 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
383                         strcpy(c_roomname, &buf[20]);
384
385                 if (!strncasecmp(buf, "Content-length: ", 16)) {
386                         ContentLength = atoi(&buf[16]);
387                         }
388
389                 } while(strlen(buf)>0);
390
391         ++TransactionCount;
392
393         if (ContentLength > 0) {
394                 content = malloc(ContentLength+1);
395                 fread(content, ContentLength, 1, stdin);
396                 content[ContentLength] = 0;
397                 fprintf(stderr, "CONTENT:\n%s\n", content);
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_page();
467                 }
468
469         /* Various commands... */
470         
471         else if (!strncasecmp(cmd, "GET /display_main_menu", 22)) {
472                 display_main_menu();
473                 }
474
475         else if (!strncasecmp(cmd, "GET /whobbs", 11)) {
476                 whobbs();
477                 }
478
479         else if (!strncasecmp(cmd, "GET /knrooms", 12)) {
480                 list_all_rooms_by_floor();
481                 }
482
483         else if (!strncasecmp(cmd, "GET /test", 9)) {
484                 printf("HTTP/1.0 200 OK\n");
485                 output_headers();
486         
487                 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
488                 wprintf("TransactionCount is %d<HR>\n", TransactionCount);
489                 wprintf("You're in session %d<BR>\n", wc_session);
490                 wprintf("</BODY></HTML>\n");
491                 wDumpContent();
492                 }
493
494         /* When all else fails, display the frameset again. */
495         else {
496                 output_frameset();
497                 }
498
499         fflush(stdout);
500         if (content != NULL) {
501                 free(content);
502                 content = NULL;
503                 }
504         free_urls();
505         }
506
507
508
509 int main(int argc, char *argv[]) {
510
511         if (argc != 2) {
512                 printf("%s: usage: %s <session_id>\n", argv[0], argv[0]);
513                 exit(1);
514                 }
515
516         wc_session = atoi(argv[1]);
517         strcpy(wc_host, "");
518         strcpy(wc_port, "");
519         strcpy(wc_username, "");
520         strcpy(wc_password, "");
521         strcpy(wc_roomname, "");
522
523         while (1) {
524                 session_loop();
525                 }
526
527         exit(0);
528         }