4 * Handles various types of data transfer operations with the Citadel service.
14 #include <sys/types.h>
16 #include <sys/socket.h>
19 #include <netinet/in.h>
28 #include "webserver.h"
30 struct serv_info serv_info;
33 * get info about the server we've connected to
35 void get_serv_info(char *browser_host, char *user_agent)
40 /* Tell the server what kind of client is connecting */
41 serv_printf("IDEN %d|%d|%d|%s|%s",
48 serv_getln(buf, sizeof buf);
50 /* Tell the server what kind of richtext we prefer */
51 serv_puts("MSGP text/html|text/plain");
52 serv_getln(buf, sizeof buf);
54 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
55 /* Tell the server that when we save a calendar event, we
56 * want invitations to be generated by the Citadel server
57 * instead of by the client.
59 serv_puts("ICAL sgi|1");
60 serv_getln(buf, sizeof buf);
63 /* Now ask the server to tell us a little bit about itself... */
65 serv_getln(buf, sizeof buf);
70 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
73 serv_info.serv_pid = atoi(buf);
74 WC->ctdl_pid = serv_info.serv_pid;
77 safestrncpy(serv_info.serv_nodename, buf, sizeof serv_info.serv_nodename);
80 safestrncpy(serv_info.serv_humannode, buf, sizeof serv_info.serv_humannode);
83 safestrncpy(serv_info.serv_fqdn, buf, sizeof serv_info.serv_fqdn);
86 safestrncpy(serv_info.serv_software, buf, sizeof serv_info.serv_software);
89 serv_info.serv_rev_level = atoi(buf);
92 safestrncpy(serv_info.serv_bbs_city, buf, sizeof serv_info.serv_bbs_city);
95 safestrncpy(serv_info.serv_sysadm, buf, sizeof serv_info.serv_sysadm);
98 safestrncpy(serv_info.serv_moreprompt, buf, sizeof serv_info.serv_moreprompt);
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);
125 serv_getln(buf, sizeof buf);
127 if (fgets(buf, SIZ, fp) == NULL)
128 safestrncpy(buf, "000", sizeof buf);
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 Citadel-style
144 * " >" quotes and will convert to <BLOCKQUOTE> tags.
146 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
147 wprintf("<BLOCKQUOTE>");
149 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
150 wprintf("</BLOCKQUOTE>");
153 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
154 strcpy(buf, &buf[2]);
156 /* Activate embedded URL's */
168 * Transmit message text (in memory) to the server.
169 * If convert_to_html is set to 1, the message is converted into something
170 * which kind of resembles HTML.
172 void text_to_server(char *ptr, int convert_to_html)
181 while (ptr[pos] != 0) {
184 while ( (isspace(buf[strlen(buf) - 1]))
185 && (strlen(buf) > 1) )
186 buf[strlen(buf) - 1] = 0;
189 if (convert_to_html) {
190 strcat(buf, "<br />");
193 if (ptr[pos] != 0) strcat(buf, " ");
195 } else if ((convert_to_html)&&(strchr("#&;`'|*?-~<>^()[]{}$\\", ch) != NULL)) {
196 sprintf(conv, "%c", ch);
197 stresc(&buf[strlen(buf)], conv, 0, 0);
202 if ((ch == 32) && (strlen(buf) > 200)) {
207 if (strlen(buf) > 250) {
220 * translate server message output to text
221 * (used for editing room info files and such)
223 void server_to_text()
229 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
230 if ((buf[0] == 32) && (count > 0)) {
241 * Read binary data from server into memory using a series of
242 * server READ commands.
244 void read_server_binary(char *buffer, size_t total_len) {
247 size_t thisblock = 0;
249 memset(buffer, 0, total_len);
250 while (bytes < total_len) {
252 if ((total_len - bytes) < thisblock) {
253 thisblock = total_len - bytes;
254 if (thisblock == 0) return;
256 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
257 serv_getln(buf, sizeof buf);
259 thisblock = (size_t)atoi(&buf[4]);
260 if (!WC->connected) return;
261 serv_read(&buffer[bytes], thisblock);
265 lprintf(3, "Error: %s\n", &buf[4]);
273 * Read text from server, appending to a string buffer until the
274 * usual 000 terminator is found. Caller is responsible for freeing
275 * the returned pointer.
277 char *read_server_text(void) {
279 size_t bytes_allocated = 0;
280 size_t bytes_read = 0;
289 bytes_allocated = SIZ;
291 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
292 linelen = strlen(buf);
297 if ((bytes_read + linelen) >= (bytes_allocated - 2)) {
298 bytes_allocated = 2 * bytes_allocated;
299 text = realloc(text, bytes_allocated);
302 strcpy(&text[bytes_read], buf);
303 bytes_read += linelen;