* *** HUGE CHANGES *** *** WARNING: NOT FULLY FUNCTIONAL ***
[citadel.git] / webcit / serv_func.c
1 /*
2  * serv_func.c
3  *
4  * Handles various types of data transfer operations with the Citadel service.
5  *
6  * $Id$
7  */
8
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <sys/socket.h>
18 #include <sys/time.h>
19 #include <limits.h>
20 #include <netinet/in.h>
21 #include <netdb.h>
22 #include <string.h>
23 #include <pwd.h>
24 #include <errno.h>
25 #include <stdarg.h>
26 #include <pthread.h>
27 #include <signal.h>
28 #include "webcit.h"
29 #include "webserver.h"
30
31 struct serv_info serv_info;
32
33 /*
34  * get info about the server we've connected to
35  */
36 void get_serv_info(char *browser_host, char *user_agent)
37 {
38         char buf[SIZ];
39         int a;
40
41         /* Tell the server what kind of client is connecting */
42         serv_printf("IDEN %d|%d|%d|%s|%s",
43                 DEVELOPER_ID,
44                 CLIENT_ID,
45                 CLIENT_VERSION,
46                 user_agent,
47                 browser_host
48         );
49         serv_gets(buf);
50
51         /* Tell the server what kind of richtext we prefer */
52         serv_puts("MSGP text/html|text/plain");
53         serv_gets(buf);
54
55 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
56         /* Tell the server that when we save a calendar event, we
57          * want invitations to be generated by the Citadel server
58          * instead of by the client.
59          */
60         serv_puts("ICAL sgi|1");
61         serv_gets(buf);
62 #endif
63
64         /* Now ask the server to tell us a little bit about itself... */
65         serv_puts("INFO");
66         serv_gets(buf);
67         if (buf[0] != '1')
68                 return;
69
70         a = 0;
71         while (serv_gets(buf), strcmp(buf, "000")) {
72                 switch (a) {
73                 case 0:
74                         serv_info.serv_pid = atoi(buf);
75                         WC->ctdl_pid = serv_info.serv_pid;
76                         break;
77                 case 1:
78                         strcpy(serv_info.serv_nodename, buf);
79                         break;
80                 case 2:
81                         strcpy(serv_info.serv_humannode, buf);
82                         break;
83                 case 3:
84                         strcpy(serv_info.serv_fqdn, buf);
85                         break;
86                 case 4:
87                         strcpy(serv_info.serv_software, buf);
88                         break;
89                 case 5:
90                         serv_info.serv_rev_level = atoi(buf);
91                         break;
92                 case 6:
93                         strcpy(serv_info.serv_bbs_city, buf);
94                         break;
95                 case 7:
96                         strcpy(serv_info.serv_sysadm, buf);
97                         break;
98                 case 9:
99                         strcpy(serv_info.serv_moreprompt, buf);
100                         break;
101                 case 14:
102                         serv_info.serv_supports_ldap = atoi(buf);
103                         break;
104                 }
105                 ++a;
106         }
107 }
108
109
110
111 /* 
112  * Function to spit out Citadel variformat text in HTML
113  * If fp is non-null, it is considered to be the file handle to read the
114  * text from.  Otherwise, text is read from the server.
115  */
116 void fmout(FILE *fp, char *align)
117 {
118
119         int intext = 0;
120         int bq = 0;
121         char buf[SIZ];
122
123         wprintf("<DIV ALIGN=%s>\n", align);
124         while (1) {
125                 if (fp == NULL)
126                         serv_gets(buf);
127                 if (fp != NULL) {
128                         if (fgets(buf, SIZ, fp) == NULL)
129                                 strcpy(buf, "000");
130                         buf[strlen(buf) - 1] = 0;
131                 }
132                 if (!strcmp(buf, "000")) {
133                         if (bq == 1)
134                                 wprintf("</I>");
135                         wprintf("</DIV><br />\n");
136                         return;
137                 }
138                 if ((intext == 1) && (isspace(buf[0]))) {
139                         wprintf("<br />");
140                 }
141                 intext = 1;
142
143                 /* Quoted text should be displayed in italics and in a
144                  * different colour.  This code understands Citadel-style
145                  * " >" quotes and will convert to <BLOCKQUOTE> tags.
146                  */
147                 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
148                         wprintf("<BLOCKQUOTE>");
149                         bq = 1;
150                 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
151                         wprintf("</BLOCKQUOTE>");
152                         bq = 0;
153                 }
154                 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
155                         strcpy(buf, &buf[2]);
156                 }
157                 /* Activate embedded URL's */
158                 url(buf);
159
160                 escputs(buf);
161                 wprintf("\n");
162         }
163 }
164
165
166
167
168 /*
169  * Transmit message text (in memory) to the server.
170  * If convert_to_html is set to 1, the message is converted into something
171  * which kind of resembles HTML.
172  */
173 void text_to_server(char *ptr, int convert_to_html)
174 {
175         char buf[SIZ];
176         char conv[4];
177         int ch, a, pos;
178
179         pos = 0;
180         strcpy(buf, "");
181
182         while (ptr[pos] != 0) {
183                 ch = ptr[pos++];
184                 if (ch == 10) {
185                         while ( (isspace(buf[strlen(buf) - 1]))
186                           && (strlen(buf) > 1) )
187                                 buf[strlen(buf) - 1] = 0;
188                         serv_puts(buf);
189                         strcpy(buf, "");
190                         if (convert_to_html) {
191                                 strcat(buf, "<br />");
192                         }
193                         else {
194                                 if (ptr[pos] != 0) strcat(buf, " ");
195                         }
196                 } else if ((convert_to_html)&&(strchr("#&;`'|*?-~<>^()[]{}$\\", ch) != NULL)) {
197                         sprintf(conv, "%c", ch);
198                         stresc(&buf[strlen(buf)], conv, 0, 0);
199                 } else {
200                         a = strlen(buf);
201                         buf[a + 1] = 0;
202                         buf[a] = ch;
203                         if ((ch == 32) && (strlen(buf) > 200)) {
204                                 buf[a] = 0;
205                                 serv_puts(buf);
206                                 strcpy(buf, "");
207                         }
208                         if (strlen(buf) > 250) {
209                                 serv_puts(buf);
210                                 strcpy(buf, "");
211                         }
212                 }
213         }
214         serv_puts(buf);
215
216 }
217
218
219
220 /*
221  * translate server message output to text
222  * (used for editing room info files and such)
223  */
224 void server_to_text()
225 {
226         char buf[SIZ];
227
228         int count = 0;
229
230         while (serv_gets(buf), strcmp(buf, "000")) {
231                 if ((buf[0] == 32) && (count > 0)) {
232                         wprintf("\n");
233                 }
234                 wprintf("%s", buf);
235                 ++count;
236         }
237 }
238
239
240
241 /*
242  * Read binary data from server into memory using a series of
243  * server READ commands.
244  */
245 void read_server_binary(char *buffer, size_t total_len) {
246         char buf[SIZ];
247         size_t bytes = 0;
248         size_t thisblock = 0;
249
250         memset(buffer, 0, total_len);
251         while (bytes < total_len) {
252                 thisblock = 4095;
253                 if ((total_len - bytes) < thisblock) {
254                         thisblock = total_len - bytes;
255                         if (thisblock == 0) return;
256                 }
257                 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
258                 serv_gets(buf);
259                 if (buf[0] == '6') {
260                         thisblock = (size_t)atoi(&buf[4]);
261                         if (!WC->connected) return;
262                         serv_read(&buffer[bytes], thisblock);
263                         bytes += thisblock;
264                 }
265                 else {
266                         lprintf(3, "Error: %s\n", &buf[4]);
267                         return;
268                 }
269         }
270 }
271
272
273 /*
274  * Read text from server, appending to a string buffer until the
275  * usual 000 terminator is found.  Caller is responsible for freeing
276  * the returned pointer.
277  */
278 char *read_server_text(void) {
279         char *text = NULL;
280         size_t bytes_allocated = 0;
281         size_t bytes_read = 0;
282         int linelen;
283         char buf[SIZ];
284
285         text = malloc(SIZ);
286         if (text == NULL) {
287                 return(NULL);
288         }
289         strcpy(text, "");
290         bytes_allocated = SIZ;
291
292         while (serv_gets(buf), strcmp(buf, "000")) {
293                 linelen = strlen(buf);
294                 buf[linelen] = '\n';
295                 buf[linelen+1] = 0;
296                 ++linelen;
297
298                 if ((bytes_read + linelen) >= (bytes_allocated - 2)) {
299                         bytes_allocated = 2 * bytes_allocated;
300                         text = realloc(text, bytes_allocated);
301                 }
302
303                 strcpy(&text[bytes_read], buf);
304                 bytes_read += linelen;
305         }
306
307         return(text);
308 }