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);
77 strcpy(serv_info.serv_nodename, buf);
80 strcpy(serv_info.serv_humannode, buf);
83 strcpy(serv_info.serv_fqdn, buf);
86 strcpy(serv_info.serv_software, buf);
89 serv_info.serv_rev_level = atoi(buf);
92 strcpy(serv_info.serv_bbs_city, buf);
95 strcpy(serv_info.serv_sysadm, buf);
98 strcpy(serv_info.serv_moreprompt, buf);
101 serv_info.serv_supports_ldap = atoi(buf);
111 * Function to spit out Citadel variformat text in HTML
112 * If fp is non-null, it is considered to be the file handle to read the
113 * text from. Otherwise, text is read from the server.
115 void fmout(FILE *fp, char *align)
122 wprintf("<DIV ALIGN=%s>\n", align);
127 if (fgets(buf, SIZ, fp) == NULL)
129 buf[strlen(buf) - 1] = 0;
131 if (!strcmp(buf, "000")) {
134 wprintf("</DIV><BR>\n");
137 if ((intext == 1) && (isspace(buf[0]))) {
142 /* Quoted text should be displayed in italics and in a
143 * different colour. This code understands both Citadel/UX
144 * style " >" quotes and FordBoard-style " :-)" quotes.
147 ((!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
148 wprintf("<SPAN CLASS=\"pull_quote\">");
150 } else if ((bq == 1) &&
151 (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
155 /* Activate embedded URL's */
167 * Transmit message text (in memory) to the server.
168 * If convert_to_html is set to 1, the message is converted into something
169 * which kind of resembles HTML.
171 void text_to_server(char *ptr, int convert_to_html)
176 if (convert_to_html) {
177 serv_puts("<HTML><BODY>");
183 while (ptr[pos] != 0) {
186 while ( (isspace(buf[strlen(buf) - 1]))
187 && (strlen(buf) > 1) )
188 buf[strlen(buf) - 1] = 0;
191 if (convert_to_html) {
195 if (ptr[pos] != 0) strcat(buf, " ");
201 if ((ch == 32) && (strlen(buf) > 200)) {
206 if (strlen(buf) > 250) {
214 if (convert_to_html) {
215 serv_puts("</BODY></HTML>\n");
223 * translate server message output to text
224 * (used for editing room info files and such)
226 void server_to_text()
232 while (serv_gets(buf), strcmp(buf, "000")) {
233 if ((buf[0] == 32) && (count > 0)) {
244 * Read binary data from server into memory using a series of
245 * server READ commands.
247 void read_server_binary(char *buffer, size_t total_len) {
250 size_t thisblock = 0;
252 memset(buffer, 0, total_len);
253 while (bytes < total_len) {
255 if ((total_len - bytes) < thisblock) {
256 thisblock = total_len - bytes;
257 if (thisblock == 0) return;
259 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
262 thisblock = (size_t)atoi(&buf[4]);
263 if (!WC->connected) return;
264 serv_read(&buffer[bytes], thisblock);
268 lprintf(3, "Error: %s\n", &buf[4]);
276 * Read text from server, appending to a string buffer until the
277 * usual 000 terminator is found. Caller is responsible for freeing
278 * the returned pointer.
280 char *read_server_text(void) {
282 size_t bytes_allocated = 0;
283 size_t bytes_read = 0;
292 bytes_allocated = SIZ;
294 while (serv_gets(buf), strcmp(buf, "000")) {
295 linelen = strlen(buf);
300 if ((bytes_read + linelen) >= (bytes_allocated - 2)) {
301 bytes_allocated = 2 * bytes_allocated;
302 text = realloc(text, bytes_allocated);
305 strcpy(&text[bytes_read], buf);
306 bytes_read += linelen;