38b4fdd80c18069c9f8954a9c9426b793985b92e
[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[256];
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[256];
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, 256, 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  */
154 void text_to_server(char *ptr)
155 {
156         char buf[256];
157         int ch, a, pos;
158
159         pos = 0;
160
161         strcpy(buf, "");
162         while (ptr[pos] != 0) {
163                 ch = ptr[pos++];
164                 if (ch == 10) {
165                         while ( (isspace(buf[strlen(buf) - 1]))
166                           && (strlen(buf) > 1) )
167                                 buf[strlen(buf) - 1] = 0;
168                         serv_puts(buf);
169                         strcpy(buf, "");
170                         if (ptr[pos] != 0) strcat(buf, " ");
171                 } else {
172                         a = strlen(buf);
173                         buf[a + 1] = 0;
174                         buf[a] = ch;
175                         if ((ch == 32) && (strlen(buf) > 200)) {
176                                 buf[a] = 0;
177                                 serv_puts(buf);
178                                 strcpy(buf, "");
179                         }
180                         if (strlen(buf) > 250) {
181                                 serv_puts(buf);
182                                 strcpy(buf, "");
183                         }
184                 }
185         }
186         serv_puts(buf);
187 }
188
189
190
191
192
193
194 /*
195  * translate server message output to text
196  * (used for editing room info files and such)
197  */
198 void server_to_text()
199 {
200         char buf[256];
201
202         int count = 0;
203
204         while (serv_gets(buf), strcmp(buf, "000")) {
205                 if ((buf[0] == 32) && (count > 0)) {
206                         wprintf("\n");
207                 }
208                 wprintf("%s", buf);
209                 ++count;
210         }
211 }