4 * Handles various types of data transfer operations with the Citadel service.
15 #include <sys/types.h>
17 #include <sys/socket.h>
20 #include <netinet/in.h>
29 #include "webserver.h"
31 struct serv_info serv_info;
34 * get info about the server we've connected to
36 void get_serv_info(char *browser_host, char *user_agent)
41 /* Tell the server what kind of client is connecting */
42 serv_printf("IDEN %d|%d|%d|%s|%s",
51 /* Tell the server what kind of richtext we prefer */
52 serv_puts("MSGP text/html|text/plain");
55 /* Now ask the server to tell us a little bit about itself... */
62 while (serv_gets(buf), strcmp(buf, "000")) {
65 serv_info.serv_pid = atoi(buf);
68 strcpy(serv_info.serv_nodename, buf);
71 strcpy(serv_info.serv_humannode, buf);
74 strcpy(serv_info.serv_fqdn, buf);
77 strcpy(serv_info.serv_software, buf);
80 serv_info.serv_rev_level = atoi(buf);
83 strcpy(serv_info.serv_bbs_city, buf);
86 strcpy(serv_info.serv_sysadm, buf);
89 strcpy(serv_info.serv_moreprompt, buf);
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.
103 void fmout(FILE *fp, char *align)
110 wprintf("<DIV ALIGN=%s>\n", align);
115 if (fgets(buf, SIZ, fp) == NULL)
117 buf[strlen(buf) - 1] = 0;
119 if (!strcmp(buf, "000")) {
122 wprintf("</DIV><BR>\n");
125 if ((intext == 1) && (isspace(buf[0]))) {
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.
135 ((!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
136 wprintf("<SPAN CLASS=\"pull_quote\">");
138 } else if ((bq == 1) &&
139 (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
143 /* Activate embedded URL's */
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.
159 void text_to_server(char *ptr, int convert_to_html)
164 if (convert_to_html) {
165 serv_puts("<HTML><BODY>");
171 while (ptr[pos] != 0) {
174 while ( (isspace(buf[strlen(buf) - 1]))
175 && (strlen(buf) > 1) )
176 buf[strlen(buf) - 1] = 0;
179 if (convert_to_html) {
183 if (ptr[pos] != 0) strcat(buf, " ");
189 if ((ch == 32) && (strlen(buf) > 200)) {
194 if (strlen(buf) > 250) {
202 if (convert_to_html) {
203 serv_puts("</BODY></HTML>\n");
211 * translate server message output to text
212 * (used for editing room info files and such)
214 void server_to_text()
220 while (serv_gets(buf), strcmp(buf, "000")) {
221 if ((buf[0] == 32) && (count > 0)) {
232 * Read binary data from server into memory using a series of
233 * server READ commands.
235 void read_server_binary(char *buffer, size_t total_len) {
238 size_t thisblock = 0;
240 memset(buffer, 0, total_len);
241 while (bytes < total_len) {
243 if ((total_len - bytes) < thisblock) {
244 thisblock = total_len - bytes;
245 if (thisblock == 0) return;
247 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
250 thisblock = (size_t)atoi(&buf[4]);
251 if (!WC->connected) return;
252 serv_read(&buffer[bytes], thisblock);
256 lprintf(3, "Error: %s\n", &buf[4]);
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.
268 char *read_server_text(void) {
270 size_t bytes_allocated = 0;
271 size_t bytes_read = 0;
280 bytes_allocated = SIZ;
282 while (serv_gets(buf), strcmp(buf, "000")) {
283 linelen = strlen(buf);
288 if ((bytes_read + linelen) >= (bytes_allocated - 2)) {
289 bytes_allocated = 2 * bytes_allocated;
290 text = realloc(text, bytes_allocated);
293 strcpy(&text[bytes_read], buf);
294 bytes_read += linelen;