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);
100 * \brief Read Citadel variformat text and spit it out as HTML.
101 * \param align html align string
103 void fmout(char *align)
109 wprintf("<div align=%s>\n", align);
110 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
112 if ((intext == 1) && (isspace(buf[0]))) {
118 * Quoted text should be displayed in italics and in a
119 * different colour. This code understands Citadel-style
120 * " >" quotes and will convert to <BLOCKQUOTE> tags.
122 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
123 wprintf("<BLOCKQUOTE>");
125 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
126 wprintf("</BLOCKQUOTE>");
129 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
130 strcpy(buf, &buf[2]);
132 /** Activate embedded URL's */
141 wprintf("</div><br />\n");
148 * \brief Read Citadel variformat text and spit it out as HTML in a form
149 * suitable for embedding in another message (forward/quote).
150 * (NO LINEBREAKS ALLOWED HERE!)
152 void pullquote_fmout(void) {
157 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
159 if ((intext == 1) && (isspace(buf[0]))) {
165 * Quoted text should be displayed in italics and in a
166 * different colour. This code understands Citadel-style
167 * " >" quotes and will convert to <BLOCKQUOTE> tags.
169 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
170 wprintf("<BLOCKQUOTE>");
172 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
173 wprintf("</BLOCKQUOTE>");
176 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
177 strcpy(buf, &buf[2]);
191 * \brief Transmit message text (in memory) to the server.
193 * \param ptr Pointer to the message being transmitted
195 void text_to_server(char *ptr)
203 while (ptr[pos] != 0) {
206 while ( (isspace(buf[strlen(buf) - 1]))
207 && (strlen(buf) > 1) )
208 buf[strlen(buf) - 1] = 0;
211 if (ptr[pos] != 0) strcat(buf, " ");
216 if ((ch == 32) && (strlen(buf) > 200)) {
221 if (strlen(buf) > 250) {
232 * \brief Transmit message text (in memory) to the server,
233 * converting to Quoted-Printable encoding as we go.
235 * \param ptr Pointer to the message being transmitted
237 void text_to_server_qp(char *ptr)
247 while (ptr[pos] != 0) {
251 /* ignore carriage returns */
254 /* hard line break */
255 if (output_len > 0) {
256 if (isspace(buf[output_len-1])) {
257 sprintf(&buf[output_len-1], "=%02X", buf[output_len-1]);
261 buf[output_len++] = 0;
266 buf[output_len++] = ch;
268 else if ( (ch >= 32) && (ch <= 60) ) {
269 buf[output_len++] = ch;
271 else if ( (ch >= 62) && (ch <= 126) ) {
272 buf[output_len++] = ch;
275 sprintf(&buf[output_len], "=%02X", ch);
279 if (output_len > 72) {
280 /* soft line break */
281 if (isspace(buf[output_len-1])) {
282 sprintf(&buf[output_len-1], "=%02X", buf[output_len-1]);
285 buf[output_len++] = '=';
286 buf[output_len++] = 0;
292 /* end of data - transmit anything that's left */
293 if (output_len > 0) {
294 if (isspace(buf[output_len-1])) {
295 sprintf(&buf[output_len-1], "=%02X", buf[output_len-1]);
298 buf[output_len++] = 0;
308 * \brief translate server message output to text
309 * (used for editing room info files and such)
311 void server_to_text()
317 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
318 if ((buf[0] == 32) && (count > 0)) {
329 * Read binary data from server into memory using a series of
330 * server READ commands.
331 * \param buffer the output buffer
332 * \param total_len the maximal length of buffer
334 void read_server_binary(char *buffer, size_t total_len) {
337 size_t thisblock = 0;
339 memset(buffer, 0, total_len);
340 while (bytes < total_len) {
342 if ((total_len - bytes) < thisblock) {
343 thisblock = total_len - bytes;
344 if (thisblock == 0) return;
346 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
347 serv_getln(buf, sizeof buf);
349 thisblock = (size_t)atoi(&buf[4]);
350 if (!WC->connected) return;
351 serv_read(&buffer[bytes], thisblock);
355 lprintf(3, "Error: %s\n", &buf[4]);
363 * \brief Read text from server, appending to a string buffer until the
364 * usual 000 terminator is found. Caller is responsible for freeing
365 * the returned pointer.
367 char *read_server_text(void) {
369 size_t bytes_allocated = 0;
370 size_t bytes_read = 0;
379 bytes_allocated = SIZ;
381 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
382 linelen = strlen(buf);
387 if ((bytes_read + linelen) >= (bytes_allocated - 2)) {
388 bytes_allocated = 2 * bytes_allocated;
389 text = realloc(text, bytes_allocated);
392 strcpy(&text[bytes_read], buf);
393 bytes_read += linelen;