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) {
209 while ( (isspace(buf[strlen(buf) - 1]))
210 && (strlen(buf) > 1) )
211 buf[strlen(buf) - 1] = 0;
214 if (ptr[pos] != 0) strcat(buf, " ");
219 if ((ch == 32) && (strlen(buf) > 200)) {
224 if (strlen(buf) > 250) {
235 * \brief Transmit message text (in memory) to the server,
236 * converting to Quoted-Printable encoding as we go.
238 * \param ptr Pointer to the message being transmitted
240 void text_to_server_qp(char *ptr)
242 unsigned char ch, buf[256];
250 while (ptr[pos] != 0) {
251 ch = (unsigned char)(ptr[pos++]);
254 /* ignore carriage returns */
257 /* hard line break */
258 if (output_len > 0) {
259 if (isspace(buf[output_len-1])) {
260 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
264 buf[output_len++] = 0;
265 serv_puts((char *)buf);
269 buf[output_len++] = ch;
271 else if ( (ch >= 32) && (ch <= 60) ) {
272 buf[output_len++] = ch;
274 else if ( (ch >= 62) && (ch <= 126) ) {
275 buf[output_len++] = ch;
278 sprintf((char *)&buf[output_len], "=%02X", ch);
282 if (output_len > 72) {
283 /* soft line break */
284 if (isspace(buf[output_len-1])) {
285 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
288 buf[output_len++] = '=';
289 buf[output_len++] = 0;
290 serv_puts((char *)buf);
295 /* end of data - transmit anything that's left */
296 if (output_len > 0) {
297 if (isspace(buf[output_len-1])) {
298 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
301 buf[output_len++] = 0;
302 serv_puts((char *)buf);
311 * \brief translate server message output to text
312 * (used for editing room info files and such)
314 void server_to_text()
320 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
321 if ((buf[0] == 32) && (count > 0)) {
332 * Read binary data from server into memory using a series of
333 * server READ commands.
334 * \param buffer the output buffer
335 * \param total_len the maximal length of buffer
337 void read_server_binary(char *buffer, size_t total_len) {
340 size_t thisblock = 0;
342 memset(buffer, 0, total_len);
343 while (bytes < total_len) {
345 if ((total_len - bytes) < thisblock) {
346 thisblock = total_len - bytes;
347 if (thisblock == 0) return;
349 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
350 serv_getln(buf, sizeof buf);
352 thisblock = (size_t)atoi(&buf[4]);
353 if (!WC->connected) return;
354 serv_read(&buffer[bytes], thisblock);
358 lprintf(3, "Error: %s\n", &buf[4]);
366 * \brief Read text from server, appending to a string buffer until the
367 * usual 000 terminator is found. Caller is responsible for freeing
368 * the returned pointer.
370 char *read_server_text(void) {
372 size_t bytes_allocated = 0;
373 size_t bytes_read = 0;
382 bytes_allocated = SIZ;
384 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
385 linelen = strlen(buf);
390 if ((bytes_read + linelen) >= (bytes_allocated - 2)) {
391 bytes_allocated = 2 * bytes_allocated;
392 text = realloc(text, bytes_allocated);
395 strcpy(&text[bytes_read], buf);
396 bytes_read += linelen;