fd51936941e127b88f1640ae9942e08cde86b3e6
[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 WC->connected to
36  */
37 void get_serv_info(char *browser_host, char *user_agent)
38 {
39         char buf[SIZ];
40         int a;
41
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         serv_puts("INFO");
52         serv_gets(buf);
53         if (buf[0] != '1')
54                 return;
55
56         a = 0;
57         while (serv_gets(buf), strcmp(buf, "000")) {
58                 switch (a) {
59                 case 0:
60                         serv_info.serv_pid = atoi(buf);
61                         break;
62                 case 1:
63                         strcpy(serv_info.serv_nodename, buf);
64                         break;
65                 case 2:
66                         strcpy(serv_info.serv_humannode, buf);
67                         break;
68                 case 3:
69                         strcpy(serv_info.serv_fqdn, buf);
70                         break;
71                 case 4:
72                         strcpy(serv_info.serv_software, buf);
73                         break;
74                 case 5:
75                         serv_info.serv_rev_level = atoi(buf);
76                         break;
77                 case 6:
78                         strcpy(serv_info.serv_bbs_city, buf);
79                         break;
80                 case 7:
81                         strcpy(serv_info.serv_sysadm, buf);
82                         break;
83                 case 9:
84                         strcpy(serv_info.serv_moreprompt, buf);
85                         break;
86                 }
87                 ++a;
88         }
89 }
90
91
92
93 /* 
94  * Function to spit out Citadel variformat text in HTML
95  * If fp is non-null, it is considered to be the file handle to read the
96  * text from.  Otherwise, text is read from the server.
97  */
98 void fmout(FILE * fp)
99 {
100
101         int intext = 0;
102         int bq = 0;
103         char buf[SIZ];
104
105         wprintf("<DIV ALIGN=JUSTIFY>\n");
106         while (1) {
107                 if (fp == NULL)
108                         serv_gets(buf);
109                 if (fp != NULL) {
110                         if (fgets(buf, SIZ, fp) == NULL)
111                                 strcpy(buf, "000");
112                         buf[strlen(buf) - 1] = 0;
113                 }
114                 if (!strcmp(buf, "000")) {
115                         if (bq == 1)
116                                 wprintf("</I>");
117                         wprintf("</DIV><BR>\n");
118                         return;
119                 }
120                 if ((intext == 1) && (isspace(buf[0]))) {
121                         wprintf("<BR>");
122                 }
123                 intext = 1;
124
125                 /* Quoted text should be displayed in italics and in a
126                  * different colour.  This code understands both Citadel/UX
127                  * style " >" quotes and FordBoard-style " :-)" quotes.
128                  */
129                 if ((bq == 0) &&
130                     ((!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
131                         wprintf("<FONT COLOR=\"000044\"><I>");
132                         bq = 1;
133                 } else if ((bq == 1) &&
134                   (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
135                         wprintf("</FONT></I>");
136                         bq = 0;
137                 }
138                 /* Activate embedded URL's */
139                 url(buf);
140
141                 escputs(buf);
142                 wprintf("\n");
143         }
144 }
145
146
147
148
149
150
151 /*
152  * Transmit message text (in memory) to the server.
153  * If convert_to_html is set to 1, the message is converted into something
154  * which kind of resembles HTML.
155  */
156 void text_to_server(char *ptr, int convert_to_html)
157 {
158         char buf[SIZ];
159         int ch, a, pos;
160
161         if (convert_to_html) {
162                 serv_puts("<HTML><BODY>");
163         }
164
165         pos = 0;
166         strcpy(buf, "");
167
168         while (ptr[pos] != 0) {
169                 ch = ptr[pos++];
170                 if (ch == 10) {
171                         while ( (isspace(buf[strlen(buf) - 1]))
172                           && (strlen(buf) > 1) )
173                                 buf[strlen(buf) - 1] = 0;
174                         serv_puts(buf);
175                         strcpy(buf, "");
176                         if (convert_to_html) {
177                                 strcat(buf, "<BR>");
178                         }
179                         else {
180                                 if (ptr[pos] != 0) strcat(buf, " ");
181                         }
182                 } else {
183                         a = strlen(buf);
184                         buf[a + 1] = 0;
185                         buf[a] = ch;
186                         if ((ch == 32) && (strlen(buf) > 200)) {
187                                 buf[a] = 0;
188                                 serv_puts(buf);
189                                 strcpy(buf, "");
190                         }
191                         if (strlen(buf) > 250) {
192                                 serv_puts(buf);
193                                 strcpy(buf, "");
194                         }
195                 }
196         }
197         serv_puts(buf);
198
199         if (convert_to_html) {
200                 serv_puts("</BODY></HTML>\n");
201         }
202
203 }
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 }