4 * Handles various types of data transfer operations with the Citadel service.
11 struct serv_info serv_info;
14 * get info about the server we've connected to
16 void get_serv_info(char *browser_host, char *user_agent)
21 /* Tell the server what kind of client is connecting */
22 serv_printf("IDEN %d|%d|%d|%s|%s",
29 serv_getln(buf, sizeof buf);
31 /* Tell the server what kind of richtext we prefer */
32 serv_puts("MSGP text/html|text/plain");
33 serv_getln(buf, sizeof buf);
35 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
36 /* Tell the server that when we save a calendar event, we
37 * want invitations to be generated by the Citadel server
38 * instead of by the client.
40 serv_puts("ICAL sgi|1");
41 serv_getln(buf, sizeof buf);
44 /* Now ask the server to tell us a little bit about itself... */
46 serv_getln(buf, sizeof buf);
51 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
54 serv_info.serv_pid = atoi(buf);
55 WC->ctdl_pid = serv_info.serv_pid;
58 safestrncpy(serv_info.serv_nodename, buf, sizeof serv_info.serv_nodename);
61 safestrncpy(serv_info.serv_humannode, buf, sizeof serv_info.serv_humannode);
64 safestrncpy(serv_info.serv_fqdn, buf, sizeof serv_info.serv_fqdn);
67 safestrncpy(serv_info.serv_software, buf, sizeof serv_info.serv_software);
70 serv_info.serv_rev_level = atoi(buf);
73 safestrncpy(serv_info.serv_bbs_city, buf, sizeof serv_info.serv_bbs_city);
76 safestrncpy(serv_info.serv_sysadm, buf, sizeof serv_info.serv_sysadm);
79 safestrncpy(serv_info.serv_moreprompt, buf, sizeof serv_info.serv_moreprompt);
82 serv_info.serv_supports_ldap = atoi(buf);
92 * Function to spit out Citadel variformat text in HTML
93 * If fp is non-null, it is considered to be the file handle to read the
94 * text from. Otherwise, text is read from the server.
96 void fmout(FILE *fp, char *align)
103 wprintf("<div align=%s>\n", align);
106 serv_getln(buf, sizeof buf);
108 if (fgets(buf, SIZ, fp) == NULL)
109 safestrncpy(buf, "000", sizeof buf);
110 buf[strlen(buf) - 1] = 0;
112 if (!strcmp(buf, "000")) {
115 wprintf("</div><br />\n");
118 if ((intext == 1) && (isspace(buf[0]))) {
123 /* Quoted text should be displayed in italics and in a
124 * different colour. This code understands Citadel-style
125 * " >" quotes and will convert to <BLOCKQUOTE> tags.
127 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
128 wprintf("<BLOCKQUOTE>");
130 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
131 wprintf("</BLOCKQUOTE>");
134 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
135 strcpy(buf, &buf[2]);
137 /* Activate embedded URL's */
149 * Transmit message text (in memory) to the server.
150 * If convert_to_html is set to 1, the message is converted into something
151 * which kind of resembles HTML.
153 void text_to_server(char *ptr, int convert_to_html)
162 while (ptr[pos] != 0) {
165 while ( (isspace(buf[strlen(buf) - 1]))
166 && (strlen(buf) > 1) )
167 buf[strlen(buf) - 1] = 0;
170 if (convert_to_html) {
171 strcat(buf, "<br />");
174 if (ptr[pos] != 0) strcat(buf, " ");
176 } else if ((convert_to_html)&&(strchr("#&;`'|*?-~<>^()[]{}$\\", ch) != NULL)) {
177 sprintf(conv, "%c", ch);
178 stresc(&buf[strlen(buf)], conv, 0, 0);
183 if ((ch == 32) && (strlen(buf) > 200)) {
188 if (strlen(buf) > 250) {
201 * translate server message output to text
202 * (used for editing room info files and such)
204 void server_to_text()
210 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
211 if ((buf[0] == 32) && (count > 0)) {
222 * Read binary data from server into memory using a series of
223 * server READ commands.
225 void read_server_binary(char *buffer, size_t total_len) {
228 size_t thisblock = 0;
230 memset(buffer, 0, total_len);
231 while (bytes < total_len) {
233 if ((total_len - bytes) < thisblock) {
234 thisblock = total_len - bytes;
235 if (thisblock == 0) return;
237 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
238 serv_getln(buf, sizeof buf);
240 thisblock = (size_t)atoi(&buf[4]);
241 if (!WC->connected) return;
242 serv_read(&buffer[bytes], thisblock);
246 lprintf(3, "Error: %s\n", &buf[4]);
254 * Read text from server, appending to a string buffer until the
255 * usual 000 terminator is found. Caller is responsible for freeing
256 * the returned pointer.
258 char *read_server_text(void) {
260 size_t bytes_allocated = 0;
261 size_t bytes_read = 0;
270 bytes_allocated = SIZ;
272 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
273 linelen = strlen(buf);
278 if ((bytes_read + linelen) >= (bytes_allocated - 2)) {
279 bytes_allocated = 2 * bytes_allocated;
280 text = realloc(text, bytes_allocated);
283 strcpy(&text[bytes_read], buf);
284 bytes_read += linelen;