* Updated message reading to use the new MSG4 command, so we can do output
[citadel.git] / webcit / serv_func.c
1 /* $Id$ */
2
3
4
5 #include <ctype.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <fcntl.h>
10 #include <signal.h>
11 #include <sys/types.h>
12 #include <sys/wait.h>
13 #include <sys/socket.h>
14 #include <sys/time.h>
15 #include <limits.h>
16 #include <netinet/in.h>
17 #include <netdb.h>
18 #include <string.h>
19 #include <pwd.h>
20 #include <errno.h>
21 #include <stdarg.h>
22 #include <pthread.h>
23 #include <signal.h>
24 #include "webcit.h"
25
26
27
28
29
30
31
32 struct serv_info serv_info;
33
34 /*
35  * get info about the server we've connected to
36  */
37 void get_serv_info(char *browser_host, char *user_agent)
38 {
39         char buf[SIZ];
40         int a;
41
42         /* Tell the server what kind of client is connecting */
43         serv_printf("IDEN %d|%d|%d|%s|%s",
44                 DEVELOPER_ID,
45                 CLIENT_ID,
46                 CLIENT_VERSION,
47                 user_agent,
48                 browser_host
49         );
50         serv_gets(buf);
51
52         /* Tell the server what kind of richtext we prefer */
53         serv_puts("MSGP text/html|text/plain");
54         serv_gets(buf);
55
56         /* Now ask the server to tell us a little bit about itself... */
57         serv_puts("INFO");
58         serv_gets(buf);
59         if (buf[0] != '1')
60                 return;
61
62         a = 0;
63         while (serv_gets(buf), strcmp(buf, "000")) {
64                 switch (a) {
65                 case 0:
66                         serv_info.serv_pid = atoi(buf);
67                         break;
68                 case 1:
69                         strcpy(serv_info.serv_nodename, buf);
70                         break;
71                 case 2:
72                         strcpy(serv_info.serv_humannode, buf);
73                         break;
74                 case 3:
75                         strcpy(serv_info.serv_fqdn, buf);
76                         break;
77                 case 4:
78                         strcpy(serv_info.serv_software, buf);
79                         break;
80                 case 5:
81                         serv_info.serv_rev_level = atoi(buf);
82                         break;
83                 case 6:
84                         strcpy(serv_info.serv_bbs_city, buf);
85                         break;
86                 case 7:
87                         strcpy(serv_info.serv_sysadm, buf);
88                         break;
89                 case 9:
90                         strcpy(serv_info.serv_moreprompt, buf);
91                         break;
92                 }
93                 ++a;
94         }
95 }
96
97
98
99 /* 
100  * Function to spit out Citadel variformat text in HTML
101  * If fp is non-null, it is considered to be the file handle to read the
102  * text from.  Otherwise, text is read from the server.
103  */
104 void fmout(FILE * fp)
105 {
106
107         int intext = 0;
108         int bq = 0;
109         char buf[SIZ];
110
111         wprintf("<DIV ALIGN=JUSTIFY>\n");
112         while (1) {
113                 if (fp == NULL)
114                         serv_gets(buf);
115                 if (fp != NULL) {
116                         if (fgets(buf, SIZ, fp) == NULL)
117                                 strcpy(buf, "000");
118                         buf[strlen(buf) - 1] = 0;
119                 }
120                 if (!strcmp(buf, "000")) {
121                         if (bq == 1)
122                                 wprintf("</I>");
123                         wprintf("</DIV><BR>\n");
124                         return;
125                 }
126                 if ((intext == 1) && (isspace(buf[0]))) {
127                         wprintf("<BR>");
128                 }
129                 intext = 1;
130
131                 /* Quoted text should be displayed in italics and in a
132                  * different colour.  This code understands both Citadel/UX
133                  * style " >" quotes and FordBoard-style " :-)" quotes.
134                  */
135                 if ((bq == 0) &&
136                     ((!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
137                         wprintf("<FONT COLOR=\"000044\"><I>");
138                         bq = 1;
139                 } else if ((bq == 1) &&
140                   (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
141                         wprintf("</FONT></I>");
142                         bq = 0;
143                 }
144                 /* Activate embedded URL's */
145                 url(buf);
146
147                 escputs(buf);
148                 wprintf("\n");
149         }
150 }
151
152
153
154
155
156
157 /*
158  * Transmit message text (in memory) to the server.
159  * If convert_to_html is set to 1, the message is converted into something
160  * which kind of resembles HTML.
161  */
162 void text_to_server(char *ptr, int convert_to_html)
163 {
164         char buf[SIZ];
165         int ch, a, pos;
166
167         if (convert_to_html) {
168                 serv_puts("<HTML><BODY>");
169         }
170
171         pos = 0;
172         strcpy(buf, "");
173
174         while (ptr[pos] != 0) {
175                 ch = ptr[pos++];
176                 if (ch == 10) {
177                         while ( (isspace(buf[strlen(buf) - 1]))
178                           && (strlen(buf) > 1) )
179                                 buf[strlen(buf) - 1] = 0;
180                         serv_puts(buf);
181                         strcpy(buf, "");
182                         if (convert_to_html) {
183                                 strcat(buf, "<BR>");
184                         }
185                         else {
186                                 if (ptr[pos] != 0) strcat(buf, " ");
187                         }
188                 } else {
189                         a = strlen(buf);
190                         buf[a + 1] = 0;
191                         buf[a] = ch;
192                         if ((ch == 32) && (strlen(buf) > 200)) {
193                                 buf[a] = 0;
194                                 serv_puts(buf);
195                                 strcpy(buf, "");
196                         }
197                         if (strlen(buf) > 250) {
198                                 serv_puts(buf);
199                                 strcpy(buf, "");
200                         }
201                 }
202         }
203         serv_puts(buf);
204
205         if (convert_to_html) {
206                 serv_puts("</BODY></HTML>\n");
207         }
208
209 }
210
211
212
213
214
215
216 /*
217  * translate server message output to text
218  * (used for editing room info files and such)
219  */
220 void server_to_text()
221 {
222         char buf[SIZ];
223
224         int count = 0;
225
226         while (serv_gets(buf), strcmp(buf, "000")) {
227                 if ((buf[0] == 32) && (count > 0)) {
228                         wprintf("\n");
229                 }
230                 wprintf("%s", buf);
231                 ++count;
232         }
233 }