2 * Session layer proxy for Citadel
3 * (c) 1998 by Art Cancro, All Rights Reserved, released under GNU GPL v2
7 * NOTE: this isn't finished, so don't use it!!
11 #define CACHE_DIR "/var/citadelproxy"
15 #include <sys/types.h>
21 void logoff(int code) {
26 void do_msg0(char cmd[]) {
33 msgid = atol(&cmd[5]);
34 sprintf(filename, "%ld", msgid);
36 /* If the message is cached, use the copy on disk */
37 fp = fopen(filename, "r");
39 printf("%d Cached message %ld:\n", LISTING_FOLLOWS, msgid);
40 while (fgets(buf, 256, fp) != NULL) {
49 /* Otherwise, fetch the message from the server and cache it */
51 sprintf(buf, "MSG0 %ld", msgid);
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.
64 sprintf(temp, "%ld.%d", msgid, getpid());
65 fp = fopen(temp, "w");
66 while (serv_gets(buf), strcmp(buf, "000")) {
68 fprintf(fp, "%s\n", buf);
74 /* Now that the message is complete, it can be renamed to the
75 * filename that the cache manager will recognize it with.
92 if (fgets(cmd, 256, stdin) == NULL) {
96 cmd[strlen(cmd)-1] = 0;
98 /* QUIT commands are handled specially */
99 if (!strncasecmp(cmd, "QUIT", 4)) {
101 printf("%d Proxy says: Bye!\n", OK);
106 else if (!strncasecmp(cmd, "CHAT", 4)) {
107 printf("%d Can't chat through the proxy ... yet.\n",
111 else if (!strncasecmp(cmd, "MSG0", 4)) {
115 /* Other commands, just pass through. */
120 printf("%s\n", resp);
123 /* Simple command-response... */
124 if ( (resp[0]=='2')||(resp[0]=='3')||(resp[0]=='5') ) {
127 /* Textual input... */
128 else if (resp[0] == '4') {
130 if (fgets(buf, 256, stdin) == NULL) {
133 buf[strlen(buf)-1] = 0;
135 } while (strcmp(buf, "000"));
138 /* Textual output... */
139 else if (resp[0] == '1') {
143 } while (strcmp(buf, "000"));
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);
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);
162 else if (resp[0] == '8') {
166 fgets(buf, 256, stdin);
167 buf[strlen(buf)-1] = 0;
169 } while (strcmp(buf, "000"));
179 void main(int argc, char *argv[]) {
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.
186 mkdir(CACHE_DIR, 0700);
189 if (chdir(CACHE_DIR) != 0) exit(errno);
191 attach_to_server(argc, argv);
194 strcat(buf, " (VIA PROXY)");
197 if (buf[0] != '2') exit(0);