5 * \defgroup ServFuncs Handles various types of data transfer operations with the Citadel service.
6 * \ingroup CitadelCommunitacion
11 #include "webserver.h"
13 struct serv_info serv_info; /**< our connection data to the server */
16 * \brief get info about the server we've connected to
17 * \param browser_host the citadell we want to connect to
18 * \param user_agent which browser uses our client?
20 void get_serv_info(char *browser_host, char *user_agent)
25 /** Tell the server what kind of client is connecting */
26 serv_printf("IDEN %d|%d|%d|%s|%s",
33 serv_getln(buf, sizeof buf);
35 /** Tell the server what kind of richtext we prefer */
36 serv_puts("MSGP text/html|text/plain");
37 serv_getln(buf, sizeof buf);
39 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
41 * Tell the server that when we save a calendar event, we
42 * want invitations to be generated by the Citadel server
43 * instead of by the client.
45 serv_puts("ICAL sgi|1");
46 serv_getln(buf, sizeof buf);
49 /** Now ask the server to tell us a little bit about itself... */
51 serv_getln(buf, sizeof buf);
56 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
59 serv_info.serv_pid = atoi(buf);
60 WC->ctdl_pid = serv_info.serv_pid;
63 safestrncpy(serv_info.serv_nodename, buf, sizeof serv_info.serv_nodename);
66 safestrncpy(serv_info.serv_humannode, buf, sizeof serv_info.serv_humannode);
69 safestrncpy(serv_info.serv_fqdn, buf, sizeof serv_info.serv_fqdn);
72 safestrncpy(serv_info.serv_software, buf, sizeof serv_info.serv_software);
75 serv_info.serv_rev_level = atoi(buf);
78 safestrncpy(serv_info.serv_bbs_city, buf, sizeof serv_info.serv_bbs_city);
81 safestrncpy(serv_info.serv_sysadm, buf, sizeof serv_info.serv_sysadm);
84 safestrncpy(serv_info.serv_moreprompt, buf, sizeof serv_info.serv_moreprompt);
87 serv_info.serv_supports_ldap = atoi(buf);
90 serv_info.serv_newuser_disabled = atoi(buf);
93 safestrncpy(serv_info.serv_default_cal_zone, buf, sizeof serv_info.serv_default_cal_zone);
103 * \brief Read Citadel variformat text and spit it out as HTML.
104 * \param align html align string
106 void fmout(char *align)
112 wprintf("<div align=%s>\n", align);
113 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
115 if ((intext == 1) && (isspace(buf[0]))) {
121 * Quoted text should be displayed in italics and in a
122 * different colour. This code understands Citadel-style
123 * " >" quotes and will convert to <BLOCKQUOTE> tags.
125 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
126 wprintf("<BLOCKQUOTE>");
128 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
129 wprintf("</BLOCKQUOTE>");
132 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
133 strcpy(buf, &buf[2]);
135 /** Activate embedded URL's */
144 wprintf("</div><br />\n");
151 * \brief Read Citadel variformat text and spit it out as HTML in a form
152 * suitable for embedding in another message (forward/quote).
153 * (NO LINEBREAKS ALLOWED HERE!)
155 void pullquote_fmout(void) {
160 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
162 if ((intext == 1) && (isspace(buf[0]))) {
168 * Quoted text should be displayed in italics and in a
169 * different colour. This code understands Citadel-style
170 * " >" quotes and will convert to <BLOCKQUOTE> tags.
172 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
173 wprintf("<BLOCKQUOTE>");
175 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
176 wprintf("</BLOCKQUOTE>");
179 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
180 strcpy(buf, &buf[2]);
194 * \brief Transmit message text (in memory) to the server.
196 * \param ptr Pointer to the message being transmitted
198 void text_to_server(char *ptr)
206 while (ptr[pos] != 0) {
210 while ( (isspace(buf[len - 1]))
212 && (buf[1] != '\0') )
216 if (ptr[pos] != 0) strcat(buf, " ");
221 if ((ch == 32) && (strlen(buf) > 200)) {
226 if (strlen(buf) > 250) {
237 * \brief Transmit message text (in memory) to the server,
238 * converting to Quoted-Printable encoding as we go.
240 * \param ptr Pointer to the message being transmitted
242 void text_to_server_qp(char *ptr)
244 unsigned char ch, buf[256];
252 while (ptr[pos] != 0) {
253 ch = (unsigned char)(ptr[pos++]);
256 /* ignore carriage returns */
259 /* hard line break */
260 if (output_len > 0) {
261 if (isspace(buf[output_len-1])) {
262 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
266 buf[output_len++] = 0;
267 serv_puts((char *)buf);
271 buf[output_len++] = ch;
273 else if ( (ch >= 32) && (ch <= 60) ) {
274 buf[output_len++] = ch;
276 else if ( (ch >= 62) && (ch <= 126) ) {
277 buf[output_len++] = ch;
280 sprintf((char *)&buf[output_len], "=%02X", ch);
284 if (output_len > 72) {
285 /* soft line break */
286 if (isspace(buf[output_len-1])) {
287 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
290 buf[output_len++] = '=';
291 buf[output_len++] = 0;
292 serv_puts((char *)buf);
297 /* end of data - transmit anything that's left */
298 if (output_len > 0) {
299 if (isspace(buf[output_len-1])) {
300 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
303 buf[output_len++] = 0;
304 serv_puts((char *)buf);
313 * \brief translate server message output to text
314 * (used for editing room info files and such)
316 void server_to_text()
322 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
323 if ((buf[0] == 32) && (count > 0)) {
334 * Read binary data from server into memory using a series of
335 * server READ commands.
336 * \param buffer the output buffer
337 * \param total_len the maximal length of buffer
339 void read_server_binary(char *buffer, size_t total_len) {
342 size_t thisblock = 0;
344 memset(buffer, 0, total_len);
345 while (bytes < total_len) {
347 if ((total_len - bytes) < thisblock) {
348 thisblock = total_len - bytes;
349 if (thisblock == 0) return;
351 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
352 serv_getln(buf, sizeof buf);
354 thisblock = (size_t)atoi(&buf[4]);
355 if (!WC->connected) return;
356 serv_read(&buffer[bytes], thisblock);
360 lprintf(3, "Error: %s\n", &buf[4]);
368 * \brief Read text from server, appending to a string buffer until the
369 * usual 000 terminator is found. Caller is responsible for freeing
370 * the returned pointer.
372 char *read_server_text(void) {
374 size_t bytes_allocated = 0;
375 size_t bytes_read = 0;
384 bytes_allocated = SIZ;
386 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
387 linelen = strlen(buf);
392 if ((bytes_read + linelen) >= (bytes_allocated - 2)) {
393 bytes_allocated = 2 * bytes_allocated;
394 text = realloc(text, bytes_allocated);
397 strcpy(&text[bytes_read], buf);
398 bytes_read += linelen;