]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
Initial revision
[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
15 int wc_session;
16 char wc_host[256];
17 int wc_port;
18 char wc_username[256];
19 char wc_password[256];
20 char wc_roomname[256];
21
22 void getz(char *buf) {
23         if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
24         else {
25                 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
26                         buf[strlen(buf)-1] = 0;
27                 }
28         }
29
30 void output_reconnect_cookies() {
31         printf("Set-cookie: wc_session=%d\n", wc_session);
32         if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
33         if (wc_port != 0) printf("Set-cookie: wc_port=%d\n", wc_port);
34         if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
35                 wc_username);
36         if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
37                 wc_password);
38         if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
39                 wc_roomname);
40         }
41
42 void session_loop() {
43         char buf[256];
44         char content[4096];
45         static int TransactionCount = 0;
46
47         do {
48                 getz(buf);
49                 } while(strlen(buf)>0);
50
51         printf("HTTP/1.0 200 OK\n");
52         printf("Server: WebCit v2 (Velma)\n");
53         printf("Connection: close\n");
54         output_reconnect_cookies();
55         printf("Content-Type: text/html\n");
56
57         strcpy(content, "");
58
59         sprintf(&content[strlen(content)],
60                 "<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
61         sprintf(&content[strlen(content)],
62                 "TransactionCount is %d<HR>\n", ++TransactionCount);
63         sprintf(&content[strlen(content)],
64                 "You're in session %d<BR>\n", wc_session);
65         sprintf(&content[strlen(content)],
66                 "</BODY></HTML>\n");
67
68         printf("Content-length: %d\n", strlen(content));
69         printf("\n");
70         fwrite(content, strlen(content), 1, stdout);
71         fflush(stdout);
72         }
73
74
75
76 int main(int argc, char *argv[]) {
77
78         if (argc != 2) {
79                 printf("%s: usage: %s <session_id>\n", argv[0], argv[0]);
80                 exit(1);
81                 }
82
83         wc_session = atoi(argv[1]);
84         strcpy(wc_host, "");
85         wc_port = 0;
86         strcpy(wc_username, "");
87         strcpy(wc_password, "");
88         strcpy(wc_roomname, "");
89
90         while (1) {
91                 session_loop();
92                 }
93
94         exit(0);
95         }