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