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)
180 while (ptr[pos] != 0) {
183 while ( (isspace(buf[strlen(buf) - 1]))
184 && (strlen(buf) > 1) )
185 buf[strlen(buf) - 1] = 0;
188 if (convert_to_html) {
192 if (ptr[pos] != 0) strcat(buf, " ");
194 } else if ((convert_to_html)&&(strchr("#&;`'|*?-~<>^()[]{}$\\", ch) != NULL)) {
195 sprintf(conv, "%c", ch);
196 stresc(&buf[strlen(buf)], conv, 0, 0);
201 if ((ch == 32) && (strlen(buf) > 200)) {
206 if (strlen(buf) > 250) {
219 * translate server message output to text
220 * (used for editing room info files and such)
222 void server_to_text()
228 while (serv_gets(buf), strcmp(buf, "000")) {
229 if ((buf[0] == 32) && (count > 0)) {
240 * Read binary data from server into memory using a series of
241 * server READ commands.
243 void read_server_binary(char *buffer, size_t total_len) {
246 size_t thisblock = 0;
248 memset(buffer, 0, total_len);
249 while (bytes < total_len) {
251 if ((total_len - bytes) < thisblock) {
252 thisblock = total_len - bytes;
253 if (thisblock == 0) return;
255 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
258 thisblock = (size_t)atoi(&buf[4]);
259 if (!WC->connected) return;
260 serv_read(&buffer[bytes], thisblock);
264 lprintf(3, "Error: %s\n", &buf[4]);
272 * Read text from server, appending to a string buffer until the
273 * usual 000 terminator is found. Caller is responsible for freeing
274 * the returned pointer.
276 char *read_server_text(void) {
278 size_t bytes_allocated = 0;
279 size_t bytes_read = 0;
288 bytes_allocated = SIZ;
290 while (serv_gets(buf), strcmp(buf, "000")) {
291 linelen = strlen(buf);
296 if ((bytes_read + linelen) >= (bytes_allocated - 2)) {
297 bytes_allocated = 2 * bytes_allocated;
298 text = realloc(text, bytes_allocated);
301 strcpy(&text[bytes_read], buf);
302 bytes_read += linelen;