]> code.citadel.org Git - citadel.git/blob - citadel/proxy.c
Added proxy.c
[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 #include <stdlib.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include "citadel.h"
15
16
17 extern int serv_sock;
18
19 void logoff(int code) {
20         exit(code);
21         }
22
23
24 void do_mainloop() {
25         char cmd[256];
26         char resp[256];
27         char buf[4096];
28         int bytes;
29
30         while(1) {
31                 fflush(stdout);
32                 if (fgets(cmd, 256, stdin) == NULL) {
33                         serv_puts("QUIT");
34                         exit(1);
35                         }
36                 cmd[strlen(cmd)-1] = 0;
37
38                 /* QUIT commands are handled specially */
39                 if (!strncasecmp(cmd, "QUIT", 4)) {
40                         serv_puts("QUIT");
41                         printf("%d Proxy says: Bye!\n", OK);
42                         fflush(stdout);
43                         exit(0);
44                         }
45
46                 /* Other commands, just pass through. */
47                 else {
48                         serv_puts(cmd);
49                         serv_gets(resp);
50                         printf("%s\n", resp);
51
52                         /* Simple command-response... */
53                         if ( (resp[0]=='2')||(resp[0]=='3')||(resp[0]=='5') ) {
54                                 }
55
56                         /* Textual output... */
57                         else if (resp[0] == '1') {
58                                 do {
59                                         serv_gets(buf);
60                                         printf("%s\n", buf);
61                                         } while (strcmp(buf, "000"));
62                                 }
63
64                         /* Textual input... */
65                         else if (resp[0] == '1') {
66                                 do {
67                                         fgets(buf, 256, stdin);
68                                         buf[strlen(buf)-1] = 0;
69                                         serv_puts(buf);
70                                         } while (strcmp(buf, "000"));
71                                 }
72
73                         /* Binary output... */
74                         else if (resp[0] == '6') {
75                                 bytes = atol(&resp[4]);
76                                 serv_read(buf, bytes);
77                                 fwrite(buf, bytes, 1, stdout);
78                                 }
79
80                         /* Binary input... */
81                         else if (resp[0] == '7') {
82                                 bytes = atol(&resp[4]);
83                                 fread(buf, bytes, 1, stdin);
84                                 serv_write(buf, bytes);
85                                 }
86
87                         /* chat... */
88                         else if (resp[0] == '8') {
89                                 serv_puts("/quit");
90                                 do {
91                                         fgets(buf, 256, stdin);
92                                         buf[strlen(buf)-1] = 0;
93                                         serv_puts(buf);
94                                         } while (strcmp(buf, "000"));
95                                 }
96
97
98                         }
99                 }
100         }
101
102
103
104 void main(int argc, char *argv[]) {
105         char buf[256];
106         
107         attach_to_server(argc, argv);
108
109         serv_gets(buf);
110         strcat(buf, " (VIA PROXY)");
111         printf("%s\n", buf);
112         fflush(stdout);
113         if (buf[0] != '2') exit(0);
114
115         do_mainloop();
116         }