]> code.citadel.org Git - citadel.git/blob - citadel/proxy.c
*** empty log message ***
[citadel.git] / citadel / proxy.c
1 /*
2  * Session layer proxy for Citadel
3  * (c) 1998 by Art Cancro, All Rights Reserved, released under GNU GPL v2
4  */
5
6 /*
7  * NOTE: this isn't finished, so don't use it!!
8  *
9  */
10
11 #define CACHE_DIR       "/var/citadelproxy"
12
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <sys/types.h>
16 #include <fcntl.h>
17 #include <stdio.h>
18 #include <errno.h>
19 #include "citadel.h"
20
21 void logoff(int code) {
22         exit(code);
23         }
24
25
26 void do_msg0(char cmd[]) {
27         long msgid;
28         char filename[32];
29         char temp[32];
30         char buf[256];
31         FILE *fp;
32
33         msgid = atol(&cmd[5]);
34         sprintf(filename, "%ld", msgid);
35
36         /* If the message is cached, use the copy on disk */
37         fp = fopen(filename, "r");
38         if (fp != NULL) {
39                 printf("%d Cached message %ld:\n", LISTING_FOLLOWS, msgid);
40                 while (fgets(buf, 256, fp) != NULL) {
41                         buf[strlen(buf)-1]=0;
42                         printf("%s\n", buf);
43                         }
44                 fclose(fp);
45                 printf("000\n");
46                 fflush(stdout);
47                 }
48
49         /* Otherwise, fetch the message from the server and cache it */
50         else {
51                 sprintf(buf, "MSG0 %ld", msgid);
52                 serv_puts(buf); 
53                 serv_gets(buf);
54                 printf("%s\n", buf);
55                 fflush(stdout);
56                 if (buf[0] != '1') {
57                         return;
58                         }
59
60                 /* The message is written to a file with a temporary name, in
61                  * order to avoid another user accidentally fetching a
62                  * partially written message from the cache.
63                  */
64                 sprintf(temp, "%ld.%d", msgid, getpid());
65                 fp = fopen(temp, "w");
66                 while (serv_gets(buf), strcmp(buf, "000")) {
67                         printf("%s\n", buf);
68                         fprintf(fp, "%s\n", buf);
69                         }
70                 printf("%s\n", buf);
71                 fflush(stdout);
72                 fclose(fp);
73
74                 /* Now that the message is complete, it can be renamed to the
75                  * filename that the cache manager will recognize it with.
76                  */
77                 link(temp, filename);
78                 unlink(temp);
79                 }
80
81         }
82
83
84 void do_mainloop() {
85         char cmd[256];
86         char resp[256];
87         char buf[4096];
88         int bytes;
89
90         while(1) {
91                 fflush(stdout);
92                 if (fgets(cmd, 256, stdin) == NULL) {
93                         serv_puts("QUIT");
94                         exit(1);
95                         }
96                 cmd[strlen(cmd)-1] = 0;
97
98                 /* QUIT commands are handled specially */
99                 if (!strncasecmp(cmd, "QUIT", 4)) {
100                         serv_puts("QUIT");
101                         printf("%d Proxy says: Bye!\n", OK);
102                         fflush(stdout);
103                         exit(0);
104                         }
105
106                 else if (!strncasecmp(cmd, "CHAT", 4)) {
107                         printf("%d Can't chat through the proxy ... yet.\n",
108                                 ERROR);
109                         }
110
111                 else if (!strncasecmp(cmd, "MSG0", 4)) {
112                         do_msg0(cmd);
113                         }
114
115                 /* Other commands, just pass through. */
116                 else {
117                         
118                         serv_puts(cmd);
119                         serv_gets(resp);
120                         printf("%s\n", resp);
121                         fflush(stdout);
122
123                         /* Simple command-response... */
124                         if ( (resp[0]=='2')||(resp[0]=='3')||(resp[0]=='5') ) {
125                                 }
126
127                         /* Textual input... */
128                         else if (resp[0] == '4') {
129                                 do {
130                                         if (fgets(buf, 256, stdin) == NULL) {
131                                                 exit(errno);
132                                                 }
133                                         buf[strlen(buf)-1] = 0;
134                                         serv_puts(buf);
135                                         } while (strcmp(buf, "000"));
136                                 }
137
138                         /* Textual output... */
139                         else if (resp[0] == '1') {
140                                 do {
141                                         serv_gets(buf);
142                                         printf("%s\n", buf);
143                                         } while (strcmp(buf, "000"));
144                                 }
145
146                         /* Binary output... */
147                         else if (resp[0] == '6') {
148                                 bytes = atol(&resp[4]);
149                                 serv_read(buf, bytes);
150                                 fwrite(buf, bytes, 1, stdout);
151                                 fflush(stdout);
152                                 }
153
154                         /* Binary input... */
155                         else if (resp[0] == '7') {
156                                 bytes = atol(&resp[4]);
157                                 fread(buf, bytes, 1, stdin);
158                                 serv_write(buf, bytes);
159                                 }
160
161                         /* chat... */
162                         else if (resp[0] == '8') {
163                                 sleep(2);
164                                 serv_puts("/quit");
165                                 do {
166                                         fgets(buf, 256, stdin);
167                                         buf[strlen(buf)-1] = 0;
168                                         serv_puts(buf);
169                                         } while (strcmp(buf, "000"));
170                                 }
171
172
173                         }
174                 }
175         }
176
177
178
179 void main(int argc, char *argv[]) {
180         char buf[256];
181
182         /* Create the cache directory.  Ignore any error return, 'cuz that
183          * just means it's already there.  FIX... this really should check
184          * for that particular error.
185          */
186         mkdir(CACHE_DIR, 0700);
187
188         /* Now go there */
189         if (chdir(CACHE_DIR) != 0) exit(errno);
190
191         attach_to_server(argc, argv);
192
193         serv_gets(buf);
194         strcat(buf, " (VIA PROXY)");
195         printf("%s\n", buf);
196         fflush(stdout);
197         if (buf[0] != '2') exit(0);
198
199         do_mainloop();
200         }