9 char serv_sock_name[PATH_MAX] = "";
11 void DeleteServInfo(ServInfo **FreeMe)
15 FreeStrBuf(&(*FreeMe)->serv_nodename);
16 FreeStrBuf(&(*FreeMe)->serv_humannode);
17 FreeStrBuf(&(*FreeMe)->serv_fqdn);
18 FreeStrBuf(&(*FreeMe)->serv_software);
19 FreeStrBuf(&(*FreeMe)->serv_bbs_city);
20 FreeStrBuf(&(*FreeMe)->serv_sysadm);
21 FreeStrBuf(&(*FreeMe)->serv_moreprompt);
22 FreeStrBuf(&(*FreeMe)->serv_default_cal_zone);
23 FreeStrBuf(&(*FreeMe)->serv_svn_revision);
29 * get info about the server we've connected to
31 * browser_host the citadell we want to connect to
32 * user_agent which browser uses our client?
34 ServInfo *get_serv_info(StrBuf *browser_host, StrBuf *user_agent)
41 /** Tell the server what kind of client is connecting */
42 serv_printf("IDEN %d|%d|%d|%s|%s",
49 StrBuf_ServGetln(Buf);
51 /** Tell the server what kind of richtext we prefer */
52 serv_puts("MSGP text/calendar|text/vnote|text/html|text/plain");//// TODO: register me...
53 StrBuf_ServGetln(Buf);
56 * Tell the server that when we save a calendar event, we
57 * want invitations to be generated by the Citadel server
58 * instead of by the client.
60 serv_puts("ICAL sgi|1");
61 StrBuf_ServGetln(Buf);
63 /** Now ask the server to tell us a little bit about itself... */
65 StrBuf_ServGetln(Buf);
66 if (GetServerStatus(Buf, NULL) != 1) {
71 info = (ServInfo*)malloc(sizeof(ServInfo));
72 memset(info, 0, sizeof(ServInfo));
74 while (StrBuf_ServGetln(Buf), (strcmp(ChrPtr(Buf), "000")!= 0)) {
75 /* lprintf (1, "a: %d [%s]", a, ChrPtr(Buf));*/
78 info->serv_pid = StrToi(Buf);
79 WC->ctdl_pid = info->serv_pid;
82 info->serv_nodename = NewStrBufDup(Buf);
85 info->serv_humannode = NewStrBufDup(Buf);
88 info->serv_fqdn = NewStrBufDup(Buf);
91 info->serv_software = NewStrBufDup(Buf);
94 info->serv_rev_level = StrToi(Buf);
97 info->serv_bbs_city = NewStrBufDup(Buf);
100 info->serv_sysadm = NewStrBufDup(Buf);
103 info->serv_moreprompt = NewStrBufDup(Buf);
106 info->serv_supports_ldap = StrToi(Buf);
109 info->serv_newuser_disabled = StrToi(Buf);
112 info->serv_default_cal_zone = NewStrBufDup(Buf);
115 info->serv_supports_sieve = StrToi(Buf);
118 info->serv_fulltext_enabled = StrToi(Buf);
121 info->serv_svn_revision = NewStrBufDup(Buf);
124 info->serv_supports_openid = StrToi(Buf);
133 int GetConnected (void)
138 if (WCC->ReadBuf == NULL)
139 WCC->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
140 if (is_uds) /* unix domain socket */
141 WCC->serv_sock = uds_connectsock(serv_sock_name);
142 else /* tcp socket */
143 WCC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
145 if (WCC->serv_sock < 0) {
147 FreeStrBuf(&WCC->ReadBuf);
154 StrBuf_ServGetln(Buf); /* get the server greeting */
155 GetServerStatus(Buf, &Status);
157 /* Are there too many users already logged in? */
159 wprintf(_("This server is already serving its maximum number of users and cannot accept any additional logins at this time. Please try again later or contact your system administrator."));
161 end_webcit_session();
166 * From what host is our user connecting? Go with
167 * the host at the other end of the HTTP socket,
168 * unless we are following X-Forwarded-For: headers
169 * and such a header has already turned up something.
171 if ( (!follow_xff) || (StrLength(WCC->Hdr->HR.browser_host) == 0) ) {
172 if (WCC->Hdr->HR.browser_host == NULL) {
173 WCC->Hdr->HR.browser_host = NewStrBuf();
174 Put(WCC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"),
175 WCC->Hdr->HR.browser_host, HFreeStrBuf);
177 locate_host(WCC->Hdr->HR.browser_host, WCC->Hdr->http_sock);
179 if (WCC->serv_info == NULL)
180 WCC->serv_info = get_serv_info(WCC->Hdr->HR.browser_host, WCC->Hdr->HR.user_agent);
181 if (WCC->serv_info == NULL){
183 wprintf(_("Received unexpected answer from Citadel "
184 "server; bailing out."));
185 hprintf("HTTP/1.1 200 OK\r\n");
186 hprintf("Content-type: text/plain; charset=utf-8\r\n");
188 end_webcit_session();
191 if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
193 wprintf(_("You are connected to a Citadel "
194 "server running Citadel %d.%02d. \n"
195 "In order to run this version of WebCit "
196 "you must also have Citadel %d.%02d or"
198 WCC->serv_info->serv_rev_level / 100,
199 WCC->serv_info->serv_rev_level % 100,
200 MINIMUM_CIT_VERSION / 100,
201 MINIMUM_CIT_VERSION % 100
203 hprintf("HTTP/1.1 200 OK\r\n");
204 hprintf("Content-type: text/plain; charset=utf-8\r\n");
206 end_webcit_session();
214 * Read Citadel variformat text and spit it out as HTML.
215 * align html align string
217 inline void fmout(char *align)
219 _fmout(WC->WBuf, align);
222 void _fmout(StrBuf *Target, char *align)
228 StrBufAppendPrintf(Target, "<div align=%s>\n", align);
229 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
231 if ((intext == 1) && (isspace(buf[0]))) {
237 * Quoted text should be displayed in italics and in a
238 * different colour. This code understands Citadel-style
239 * " >" quotes and will convert to <BLOCKQUOTE> tags.
241 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
242 StrBufAppendBufPlain(Target, HKEY("<BLOCKQUOTE>"), 0);
244 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
245 StrBufAppendBufPlain(Target, HKEY("</BLOCKQUOTE>"), 0);
248 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
249 strcpy(buf, &buf[2]);
251 /** Activate embedded URL's */
252 url(buf, sizeof(buf));
255 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
260 wprintf("</div><br />\n");
263 void FmOut(StrBuf *Target, char *align, StrBuf *Source)
265 const char *ptr, *pte;
266 const char *BufPtr = NULL;
267 StrBuf *Line = NewStrBufPlain(NULL, SIZ);
268 StrBuf *Line1 = NewStrBufPlain(NULL, SIZ);
269 StrBuf *Line2 = NewStrBufPlain(NULL, SIZ);
276 StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
278 if (StrLength(Source) > 0)
281 StrBufSipLine(Line, Source, &BufPtr);
285 len = StrLength(Line);
288 if ((intext == 1) && (isspace(*ptr))) {
289 StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
292 if (isspace(*ptr)) while ((ptr < pte) &&
303 * Quoted text should be displayed in italics and in a
304 * different colour. This code understands Citadel-style
305 * " >" quotes and will convert to <BLOCKQUOTE> tags.
307 if (i > 0) StrBufCutLeft(Line, i);
310 for (i = bn; i < bq; i++)
311 StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
312 for (i = bq; i < bn; i++)
313 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
316 if (StrLength(Line) == 0)
318 /** Activate embedded URL's */
319 UrlizeText(Line1, Line, Line2);
321 StrEscAppend(Target, Line1, NULL, 0, 0);
323 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
325 while ((BufPtr != StrBufNOTNULL) &&
328 for (i = 0; i < bn; i++)
329 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
330 StrBufAppendBufPlain(Target, HKEY("</div><br>\n"), 0);
340 * Read Citadel variformat text and spit it out as HTML in a form
341 * suitable for embedding in another message (forward/quote).
342 * (NO LINEBREAKS ALLOWED HERE!)
344 void pullquote_fmout(void) {
349 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
351 if ((intext == 1) && (isspace(buf[0]))) {
357 * Quoted text should be displayed in italics and in a
358 * different colour. This code understands Citadel-style
359 * " >" quotes and will convert to <BLOCKQUOTE> tags.
361 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
362 wprintf("<BLOCKQUOTE>");
364 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
365 wprintf("</BLOCKQUOTE>");
368 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
369 strcpy(buf, &buf[2]);
383 * Transmit message text (in memory) to the server.
385 * ptr Pointer to the message being transmitted
387 void text_to_server(char *ptr)
395 while (ptr[pos] != 0) {
399 while ( (isspace(buf[len - 1]))
401 && (buf[1] != '\0') )
405 if (ptr[pos] != 0) strcat(buf, " ");
410 if ((ch == 32) && (strlen(buf) > 200)) {
415 if (strlen(buf) > 250) {
426 * Transmit message text (in memory) to the server,
427 * converting to Quoted-Printable encoding as we go.
429 * ptr Pointer to the message being transmitted
431 void text_to_server_qp(char *ptr)
433 unsigned char ch, buf[256];
441 while (ptr[pos] != 0) {
442 ch = (unsigned char)(ptr[pos++]);
445 /* ignore carriage returns */
448 /* hard line break */
449 if (output_len > 0) {
450 if (isspace(buf[output_len-1])) {
451 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
455 buf[output_len++] = 0;
456 serv_puts((char *)buf);
460 buf[output_len++] = ch;
462 else if ( (ch >= 32) && (ch <= 60) ) {
463 buf[output_len++] = ch;
465 else if ( (ch >= 62) && (ch <= 126) ) {
466 buf[output_len++] = ch;
469 sprintf((char *)&buf[output_len], "=%02X", ch);
473 if (output_len > 72) {
474 /* soft line break */
475 if (isspace(buf[output_len-1])) {
476 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
479 buf[output_len++] = '=';
480 buf[output_len++] = 0;
481 serv_puts((char *)buf);
486 /* end of data - transmit anything that's left */
487 if (output_len > 0) {
488 if (isspace(buf[output_len-1])) {
489 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
492 buf[output_len++] = 0;
493 serv_puts((char *)buf);
502 * translate server message output to text
503 * (used for editing room info files and such)
505 void server_to_text()
511 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
512 if ((buf[0] == 32) && (count > 0)) {
523 * Read binary data from server into memory using a series of
524 * server READ commands.
525 * \return the read content as StrBuf
527 int read_server_binary(StrBuf *Ret, size_t total_len, StrBuf *Buf)
531 size_t thisblock = 0;
536 while (bytes < total_len) {
538 if ((total_len - bytes) < thisblock) {
539 thisblock = total_len - bytes;
540 if (thisblock == 0) {
546 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
547 if (StrBuf_ServGetln(Buf) > 0)
549 if (GetServerStatus(Buf, NULL) == 6)
551 StrBufCutLeft(Buf, 4);
552 thisblock = StrTol(Buf);
553 if (!WC->connected) {
558 StrBuf_ServGetBLOBBuffered(Ret, thisblock);
563 lprintf(3, "Error: %s\n", &buf[4]);
568 return StrLength(Ret);
573 * Read text from server, appending to a string buffer until the
574 * usual 000 terminator is found. Caller is responsible for freeing
575 * the returned pointer.
577 int read_server_text(StrBuf *Buf, long *nLines)
585 while ((WCC->serv_sock!=-1) &&
586 (nRead = StrBuf_ServGetln(Buf), (nRead >= 0) ))
588 if (strcmp(ChrPtr(Buf) + nTotal, "000") != 0) {
589 StrBufCutRight(Buf, nRead);
605 int GetServerStatus(StrBuf *Line, long* FullState)
607 if (FullState != NULL)
608 *FullState = StrTol(Line);
609 return ChrPtr(Line)[0] - 48;
613 void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP)
615 StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
618 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP)
621 if (WCC->serv_info == NULL)
623 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_nodename, 0);
626 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP)
629 if (WCC->serv_info == NULL)
631 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_humannode, 0);
634 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP)
637 if (WCC->serv_info == NULL)
639 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_fqdn, 0);
642 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP)
645 if (WCC->serv_info == NULL)
647 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_software, 0);
650 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP)
653 if (WCC->serv_info == NULL)
655 StrBufAppendPrintf(Target, "%d.%02d",
656 WCC->serv_info->serv_rev_level / 100,
657 WCC->serv_info->serv_rev_level % 100);
659 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP)
662 if (WCC->serv_info == NULL)
664 return WCC->serv_info->serv_newuser_disabled != 0;
666 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP)
669 if (WCC->serv_info == NULL)
671 return WCC->serv_info->serv_supports_openid != 0;
673 int conditional_serv_fulltext_enabled(StrBuf *Target, WCTemplputParams *TP)
676 if (WCC->serv_info == NULL)
678 return WCC->serv_info->serv_fulltext_enabled != 0;
683 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP)
686 if (WCC->serv_info == NULL)
688 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
692 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP)
701 serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
703 StrBuf_ServGetln(Line);
704 if (GetServerStatus(Line, NULL) == 1) {
705 while (!Done && (StrBuf_ServGetln(Line)>=0)) {
706 if ( (StrLength(Line)==3) &&
707 !strcmp(ChrPtr(Line), "000"))
712 StrBufAppendBufPlain(Buf, "\n", 1, 0);
713 StrBufAppendBuf(Buf, Line, 0);
719 FmOut(Line, "center", Buf);
720 StrBufAppendTemplate(Target, TP, Line, 1);
730 is_uds = strcasecmp(ctdlhost, "uds") == 0;
732 snprintf(serv_sock_name, PATH_MAX, "%s/citadel.socket", ctdlport);
734 RegisterConditional(HKEY("COND:SERV:OPENID"), 2, conditional_serv_supports_openid, CTX_NONE);
735 RegisterConditional(HKEY("COND:SERV:NEWU"), 2, conditional_serv_newuser_disabled, CTX_NONE);
736 RegisterConditional(HKEY("COND:SERV:HAVEFULLTEXT"), 2, conditional_serv_fulltext_enabled, CTX_NONE);
737 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, CTX_NONE);
738 RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, CTX_NONE);
739 RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, CTX_NONE);
740 RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, CTX_NONE);
741 RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, CTX_NONE);
742 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, CTX_NONE);
743 RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, CTX_NONE);
744 RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, CTX_NONE);
745 /*TODO // RegisterNamespace("SERV:LDAP_SUPP", 0, 0, tmplput_serv_ldap_enabled, 0); */
751 SessionDestroyModule_SERVFUNC
754 DeleteServInfo(&sess->serv_info);