5 * \defgroup ServFuncs Handles various types of data transfer operations with the Citadel service.
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);
97 * \brief Read Citadel variformat text and spit it out as HTML.
98 * \param align html align string
100 void fmout(char *align)
106 wprintf("<div align=%s>\n", align);
107 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
109 if ((intext == 1) && (isspace(buf[0]))) {
115 * Quoted text should be displayed in italics and in a
116 * different colour. This code understands Citadel-style
117 * " >" quotes and will convert to <BLOCKQUOTE> tags.
119 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
120 wprintf("<BLOCKQUOTE>");
122 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
123 wprintf("</BLOCKQUOTE>");
126 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
127 strcpy(buf, &buf[2]);
129 /** Activate embedded URL's */
138 wprintf("</div><br />\n");
145 * \brief Read Citadel variformat text and spit it out as HTML in a form
146 * suitable for embedding in another message (forward/quote).
147 * (NO LINEBREAKS ALLOWED HERE!)
149 void pullquote_fmout(void) {
154 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
156 if ((intext == 1) && (isspace(buf[0]))) {
162 * Quoted text should be displayed in italics and in a
163 * different colour. This code understands Citadel-style
164 * " >" quotes and will convert to <BLOCKQUOTE> tags.
166 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
167 wprintf("<BLOCKQUOTE>");
169 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
170 wprintf("</BLOCKQUOTE>");
173 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
174 strcpy(buf, &buf[2]);
188 * \brief Transmit message text (in memory) to the server.
189 * \param ptr the output buffer
190 * \param convert_to_html if set to 1, the message is converted into something
191 * which kind of resembles HTML.
193 void text_to_server(char *ptr, int convert_to_html)
202 while (ptr[pos] != 0) {
205 while ( (isspace(buf[strlen(buf) - 1]))
206 && (strlen(buf) > 1) )
207 buf[strlen(buf) - 1] = 0;
210 if (convert_to_html) {
211 strcat(buf, "<br />");
214 if (ptr[pos] != 0) strcat(buf, " ");
216 } else if ((convert_to_html)&&(strchr("#&;`'|*?-~<>^()[]{}$\\", ch) != NULL)) {
217 sprintf(conv, "%c", ch);
218 stresc(&buf[strlen(buf)], conv, 0, 0);
223 if ((ch == 32) && (strlen(buf) > 200)) {
228 if (strlen(buf) > 250) {
241 * \brief translate server message output to text
242 * (used for editing room info files and such)
244 void server_to_text()
250 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
251 if ((buf[0] == 32) && (count > 0)) {
262 * Read binary data from server into memory using a series of
263 * server READ commands.
264 * \param buffer the output buffer
265 * \param total_len the maximal length of buffer
267 void read_server_binary(char *buffer, size_t total_len) {
270 size_t thisblock = 0;
272 memset(buffer, 0, total_len);
273 while (bytes < total_len) {
275 if ((total_len - bytes) < thisblock) {
276 thisblock = total_len - bytes;
277 if (thisblock == 0) return;
279 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
280 serv_getln(buf, sizeof buf);
282 thisblock = (size_t)atoi(&buf[4]);
283 if (!WC->connected) return;
284 serv_read(&buffer[bytes], thisblock);
288 lprintf(3, "Error: %s\n", &buf[4]);
296 * \brief Read text from server, appending to a string buffer until the
297 * usual 000 terminator is found. Caller is responsible for freeing
298 * the returned pointer.
300 char *read_server_text(void) {
302 size_t bytes_allocated = 0;
303 size_t bytes_read = 0;
312 bytes_allocated = SIZ;
314 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
315 linelen = strlen(buf);
320 if ((bytes_read + linelen) >= (bytes_allocated - 2)) {
321 bytes_allocated = 2 * bytes_allocated;
322 text = realloc(text, bytes_allocated);
325 strcpy(&text[bytes_read], buf);
326 bytes_read += linelen;