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 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
56 /* Tell the server that when we save a calendar event, we
57 * want invitations to be generated by the Citadel server
58 * instead of by the client.
60 serv_puts("ICAL sgi|1");
64 /* Now ask the server to tell us a little bit about itself... */
71 while (serv_gets(buf), strcmp(buf, "000")) {
74 serv_info.serv_pid = atoi(buf);
75 WC->ctdl_pid = serv_info.serv_pid;
78 strcpy(serv_info.serv_nodename, buf);
81 strcpy(serv_info.serv_humannode, buf);
84 strcpy(serv_info.serv_fqdn, buf);
87 strcpy(serv_info.serv_software, buf);
90 serv_info.serv_rev_level = atoi(buf);
93 strcpy(serv_info.serv_bbs_city, buf);
96 strcpy(serv_info.serv_sysadm, buf);
99 strcpy(serv_info.serv_moreprompt, buf);
102 serv_info.serv_supports_ldap = atoi(buf);
112 * Function to spit out Citadel variformat text in HTML
113 * If fp is non-null, it is considered to be the file handle to read the
114 * text from. Otherwise, text is read from the server.
116 void fmout(FILE *fp, char *align)
123 wprintf("<DIV ALIGN=%s>\n", align);
128 if (fgets(buf, SIZ, fp) == NULL)
130 buf[strlen(buf) - 1] = 0;
132 if (!strcmp(buf, "000")) {
135 wprintf("</DIV><br />\n");
138 if ((intext == 1) && (isspace(buf[0]))) {
143 /* Quoted text should be displayed in italics and in a
144 * different colour. This code understands Citadel-style
145 * " >" quotes and will convert to <BLOCKQUOTE> tags.
147 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
148 wprintf("<BLOCKQUOTE>");
150 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
151 wprintf("</BLOCKQUOTE>");
154 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
155 strcpy(buf, &buf[2]);
157 /* Activate embedded URL's */
169 * Transmit message text (in memory) to the server.
170 * If convert_to_html is set to 1, the message is converted into something
171 * which kind of resembles HTML.
173 void text_to_server(char *ptr, int convert_to_html)
182 while (ptr[pos] != 0) {
185 while ( (isspace(buf[strlen(buf) - 1]))
186 && (strlen(buf) > 1) )
187 buf[strlen(buf) - 1] = 0;
190 if (convert_to_html) {
191 strcat(buf, "<br />");
194 if (ptr[pos] != 0) strcat(buf, " ");
196 } else if ((convert_to_html)&&(strchr("#&;`'|*?-~<>^()[]{}$\\", ch) != NULL)) {
197 sprintf(conv, "%c", ch);
198 stresc(&buf[strlen(buf)], conv, 0, 0);
203 if ((ch == 32) && (strlen(buf) > 200)) {
208 if (strlen(buf) > 250) {
221 * translate server message output to text
222 * (used for editing room info files and such)
224 void server_to_text()
230 while (serv_gets(buf), strcmp(buf, "000")) {
231 if ((buf[0] == 32) && (count > 0)) {
242 * Read binary data from server into memory using a series of
243 * server READ commands.
245 void read_server_binary(char *buffer, size_t total_len) {
248 size_t thisblock = 0;
250 memset(buffer, 0, total_len);
251 while (bytes < total_len) {
253 if ((total_len - bytes) < thisblock) {
254 thisblock = total_len - bytes;
255 if (thisblock == 0) return;
257 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
260 thisblock = (size_t)atoi(&buf[4]);
261 if (!WC->connected) return;
262 serv_read(&buffer[bytes], thisblock);
266 lprintf(3, "Error: %s\n", &buf[4]);
274 * Read text from server, appending to a string buffer until the
275 * usual 000 terminator is found. Caller is responsible for freeing
276 * the returned pointer.
278 char *read_server_text(void) {
280 size_t bytes_allocated = 0;
281 size_t bytes_read = 0;
290 bytes_allocated = SIZ;
292 while (serv_gets(buf), strcmp(buf, "000")) {
293 linelen = strlen(buf);
298 if ((bytes_read + linelen) >= (bytes_allocated - 2)) {
299 bytes_allocated = 2 * bytes_allocated;
300 text = realloc(text, bytes_allocated);
303 strcpy(&text[bytes_read], buf);
304 bytes_read += linelen;