* Message entry using Kevin Roth's excellent cross-platform richtext
[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                         break;
76                 case 1:
77                         strcpy(serv_info.serv_nodename, buf);
78                         break;
79                 case 2:
80                         strcpy(serv_info.serv_humannode, buf);
81                         break;
82                 case 3:
83                         strcpy(serv_info.serv_fqdn, buf);
84                         break;
85                 case 4:
86                         strcpy(serv_info.serv_software, buf);
87                         break;
88                 case 5:
89                         serv_info.serv_rev_level = atoi(buf);
90                         break;
91                 case 6:
92                         strcpy(serv_info.serv_bbs_city, buf);
93                         break;
94                 case 7:
95                         strcpy(serv_info.serv_sysadm, buf);
96                         break;
97                 case 9:
98                         strcpy(serv_info.serv_moreprompt, buf);
99                         break;
100                 case 14:
101                         serv_info.serv_supports_ldap = atoi(buf);
102                         break;
103                 }
104                 ++a;
105         }
106 }
107
108
109
110 /* 
111  * Function to spit out Citadel variformat text in HTML
112  * If fp is non-null, it is considered to be the file handle to read the
113  * text from.  Otherwise, text is read from the server.
114  */
115 void fmout(FILE *fp, char *align)
116 {
117
118         int intext = 0;
119         int bq = 0;
120         char buf[SIZ];
121
122         wprintf("<DIV ALIGN=%s>\n", align);
123         while (1) {
124                 if (fp == NULL)
125                         serv_gets(buf);
126                 if (fp != NULL) {
127                         if (fgets(buf, SIZ, fp) == NULL)
128                                 strcpy(buf, "000");
129                         buf[strlen(buf) - 1] = 0;
130                 }
131                 if (!strcmp(buf, "000")) {
132                         if (bq == 1)
133                                 wprintf("</I>");
134                         wprintf("</DIV><BR>\n");
135                         return;
136                 }
137                 if ((intext == 1) && (isspace(buf[0]))) {
138                         wprintf("<BR>");
139                 }
140                 intext = 1;
141
142                 /* Quoted text should be displayed in italics and in a
143                  * different colour.  This code understands both Citadel/UX
144                  * style " >" quotes and FordBoard-style " :-)" quotes.
145                  */
146                 if ((bq == 0) &&
147                     ((!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
148                         wprintf("<SPAN CLASS=\"pull_quote\">");
149                         bq = 1;
150                 } else if ((bq == 1) &&
151                   (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
152                         wprintf("</SPAN>");
153                         bq = 0;
154                 }
155                 /* Activate embedded URL's */
156                 url(buf);
157
158                 escputs(buf);
159                 wprintf("\n");
160         }
161 }
162
163
164
165
166 /*
167  * Transmit message text (in memory) to the server.
168  * If convert_to_html is set to 1, the message is converted into something
169  * which kind of resembles HTML.
170  */
171 void text_to_server(char *ptr, int convert_to_html)
172 {
173         char buf[SIZ];
174         char conv[4];
175         int ch, a, pos;
176
177         pos = 0;
178         strcpy(buf, "");
179
180         while (ptr[pos] != 0) {
181                 ch = ptr[pos++];
182                 if (ch == 10) {
183                         while ( (isspace(buf[strlen(buf) - 1]))
184                           && (strlen(buf) > 1) )
185                                 buf[strlen(buf) - 1] = 0;
186                         serv_puts(buf);
187                         strcpy(buf, "");
188                         if (convert_to_html) {
189                                 strcat(buf, "<BR>");
190                         }
191                         else {
192                                 if (ptr[pos] != 0) strcat(buf, " ");
193                         }
194                 } else if ((convert_to_html)&&(strchr("#&;`'|*?-~<>^()[]{}$\\", ch) != NULL)) {
195                         sprintf(conv, "%c", ch);
196                         stresc(&buf[strlen(buf)], conv, 0, 0);
197                 } else {
198                         a = strlen(buf);
199                         buf[a + 1] = 0;
200                         buf[a] = ch;
201                         if ((ch == 32) && (strlen(buf) > 200)) {
202                                 buf[a] = 0;
203                                 serv_puts(buf);
204                                 strcpy(buf, "");
205                         }
206                         if (strlen(buf) > 250) {
207                                 serv_puts(buf);
208                                 strcpy(buf, "");
209                         }
210                 }
211         }
212         serv_puts(buf);
213
214 }
215
216
217
218 /*
219  * translate server message output to text
220  * (used for editing room info files and such)
221  */
222 void server_to_text()
223 {
224         char buf[SIZ];
225
226         int count = 0;
227
228         while (serv_gets(buf), strcmp(buf, "000")) {
229                 if ((buf[0] == 32) && (count > 0)) {
230                         wprintf("\n");
231                 }
232                 wprintf("%s", buf);
233                 ++count;
234         }
235 }
236
237
238
239 /*
240  * Read binary data from server into memory using a series of
241  * server READ commands.
242  */
243 void read_server_binary(char *buffer, size_t total_len) {
244         char buf[SIZ];
245         size_t bytes = 0;
246         size_t thisblock = 0;
247
248         memset(buffer, 0, total_len);
249         while (bytes < total_len) {
250                 thisblock = 4095;
251                 if ((total_len - bytes) < thisblock) {
252                         thisblock = total_len - bytes;
253                         if (thisblock == 0) return;
254                 }
255                 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
256                 serv_gets(buf);
257                 if (buf[0] == '6') {
258                         thisblock = (size_t)atoi(&buf[4]);
259                         if (!WC->connected) return;
260                         serv_read(&buffer[bytes], thisblock);
261                         bytes += thisblock;
262                 }
263                 else {
264                         lprintf(3, "Error: %s\n", &buf[4]);
265                         return;
266                 }
267         }
268 }
269
270
271 /*
272  * Read text from server, appending to a string buffer until the
273  * usual 000 terminator is found.  Caller is responsible for freeing
274  * the returned pointer.
275  */
276 char *read_server_text(void) {
277         char *text = NULL;
278         size_t bytes_allocated = 0;
279         size_t bytes_read = 0;
280         int linelen;
281         char buf[SIZ];
282
283         text = malloc(SIZ);
284         if (text == NULL) {
285                 return(NULL);
286         }
287         strcpy(text, "");
288         bytes_allocated = SIZ;
289
290         while (serv_gets(buf), strcmp(buf, "000")) {
291                 linelen = strlen(buf);
292                 buf[linelen] = '\n';
293                 buf[linelen+1] = 0;
294                 ++linelen;
295
296                 if ((bytes_read + linelen) >= (bytes_allocated - 2)) {
297                         bytes_allocated = 2 * bytes_allocated;
298                         text = realloc(text, bytes_allocated);
299                 }
300
301                 strcpy(&text[bytes_read], buf);
302                 bytes_read += linelen;
303         }
304
305         return(text);
306 }