]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
fixed da framez
[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 void getz(char *buf) {
178         if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
179         else {
180                 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
181                         buf[strlen(buf)-1] = 0;
182                 }
183         }
184
185 /*
186  * Output all that important stuff that the browser will want to see
187  */
188 void output_headers() {
189         printf("Server: %s\n", SERVER);
190         printf("Connection: close\n");
191         printf("Set-cookie: wc_session=%d\n", wc_session);
192         if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
193         if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
194         if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
195                 wc_username);
196         if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
197                 wc_password);
198         if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
199                 wc_roomname);
200         }
201
202 void output_static(char *what) {
203         char buf[256];
204         FILE *fp;
205         struct stat statbuf;
206         off_t bytes;
207
208         sprintf(buf, "static/%s", what);
209         fp = fopen(buf, "rb");
210         if (fp == NULL) {
211                 printf("HTTP/1.0 404 %s\n", strerror(errno));
212                 output_headers();
213                 printf("Content-Type: text/plain\n");
214                 sprintf(buf, "%s: %s\n", what, strerror(errno));
215                 printf("Content-length: %d\n", strlen(buf));
216                 printf("\n");
217                 fwrite(buf, strlen(buf), 1, stdout);
218                 }
219         else {
220                 printf("HTTP/1.0 200 OK\n");
221                 output_headers();
222
223                 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
224                         printf("Content-type: image/gif\n");
225                 else
226                         printf("Content-type: application/octet-stream\n");
227
228                 fstat(fileno(fp), &statbuf);
229                 bytes = statbuf.st_size;
230                 printf("Content-length: %d\n", bytes);
231                 printf("\n");
232                 while (bytes--) {
233                         putc(getc(fp), stdout);
234                         }
235                 fclose(fp);
236                 }
237         }
238
239
240 void session_loop() {
241         char cmd[256];
242         char buf[256];
243         int a, b;
244         int ContentLength = 0;
245         char *content;
246
247         /* We stuff these with the values coming from the client cookies,
248          * so we can use them to reconnect a timed out session if we have to.
249          */
250         char c_host[256];
251         char c_port[256];
252         char c_username[256];
253         char c_password[256];
254         char c_roomname[256];
255
256         strcpy(c_host, DEFAULT_HOST);
257         strcpy(c_port, DEFAULT_PORT);
258         strcpy(c_username, "");
259         strcpy(c_password, "");
260         strcpy(c_roomname, "");
261
262         getz(cmd);
263         fprintf(stderr, "\nCmd: %s\n", cmd);
264         fflush(stderr);
265
266         do {
267                 getz(buf);
268                 fprintf(stderr, "Buf: %s\n", buf);
269                 fflush(stderr);
270
271                 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
272                         strcpy(c_host, &buf[16]);
273                 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
274                         strcpy(c_port, &buf[16]);
275                 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
276                         strcpy(c_username, &buf[20]);
277                 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
278                         strcpy(c_password, &buf[20]);
279                 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
280                         strcpy(c_roomname, &buf[20]);
281
282                 if (!strncasecmp(buf, "Content-length: ", 16)) {
283                         ContentLength = atoi(&buf[16]);
284                         }
285
286                 } while(strlen(buf)>0);
287
288         ++TransactionCount;
289
290         if (ContentLength > 0) {
291                 content = malloc(ContentLength+1);
292                 fread(content, ContentLength, 1, stdin);
293                 content[ContentLength] = 0;
294                 fprintf(stderr, "CONTENT:\n%s\n", content);
295                 addurls(content);
296                 }
297         else {
298                 content = NULL;
299                 }
300
301         /*
302          * If we're not connected to a Citadel server, try to hook up the
303          * connection now.  Preference is given to the host and port specified
304          * by browser cookies, if cookies have been supplied.
305          */
306         if (!connected) {
307                 serv_sock = connectsock(c_host, c_port, "tcp");
308                 connected = 1;
309                 serv_gets(buf); /* get the server welcome message */
310                 strcpy(wc_host, c_host);
311                 strcpy(wc_port, c_port);
312                 serv_printf("IDEN %d|%d|%d|%s|%s",
313                         DEVELOPER_ID,
314                         CLIENT_ID,
315                         CLIENT_VERSION,
316                         SERVER,
317                         ""
318                         );
319                 serv_gets(buf);
320                 /* FIX find out where the user is */
321                 }
322
323
324         /*
325          * If we're not logged in, but we have username and password cookies
326          * supplied by the browser, try using them to log in.
327          */
328         if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
329                 serv_printf("USER %s", c_username);
330                 serv_gets(buf);
331                 if (buf[0]=='3') {
332                         serv_printf("PASS %s", c_password);
333                         serv_gets(buf);
334                         if (buf[0]=='2') {
335                                 logged_in = 1;
336                                 strcpy(wc_username, c_username);
337                                 strcpy(wc_password, c_password);
338                                 }
339                         }
340                 }
341
342         /*
343          * If we don't have a current room, but a cookie specifying the
344          * current room is supplied, make an effort to go there.
345          */
346         if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
347                 serv_printf("GOTO %s", c_roomname);
348                 serv_gets(buf);
349                 if (buf[0]=='2') {
350                         strcpy(wc_roomname, c_roomname);
351                         }
352                 }
353
354         /* If there are variables in the URL, we must grab them now */  
355         for (a=0; a<strlen(cmd); ++a) if (cmd[a]=='?') {
356                 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
357                 addurls(&cmd[a+1]);
358                 cmd[a] = 0;
359                 }
360
361         if (!strncasecmp(cmd, "GET /static/", 12)) {
362                 strcpy(buf, &cmd[12]);
363                 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
364                 output_static(buf);
365                 }
366
367         if (!strncasecmp(cmd, "GET /nothing", 12)) {
368                 printf("HTTP/1.0 200 OK\n");
369                 output_headers();
370         
371                 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
372                 wprintf("TransactionCount is %d<HR>\n", TransactionCount);
373                 wprintf("You're in session %d<BR>\n", wc_session);
374                 wprintf("</BODY></HTML>\n");
375                 wDumpContent();
376                 }
377
378         else if ((!logged_in)&&(!strncasecmp(cmd, "POST /login", 11))) {
379                 do_login();
380                 }
381
382         else if (!logged_in) {
383                 display_login_page();
384                 }
385
386         /* Various commands... */
387
388         /* When all else fails, display the login page. */
389         else {
390                 display_login_page();
391                 }
392
393         fflush(stdout);
394         if (content != NULL) {
395                 free(content);
396                 content = NULL;
397                 }
398         free_urls();
399         }
400
401
402
403 int main(int argc, char *argv[]) {
404
405         if (argc != 2) {
406                 printf("%s: usage: %s <session_id>\n", argv[0], argv[0]);
407                 exit(1);
408                 }
409
410         wc_session = atoi(argv[1]);
411         strcpy(wc_host, "");
412         strcpy(wc_port, "");
413         strcpy(wc_username, "");
414         strcpy(wc_password, "");
415         strcpy(wc_roomname, "");
416
417         while (1) {
418                 session_loop();
419                 }
420
421         exit(0);
422         }