1 // Functions which handle communication with the Citadel server.
3 // Copyright (c) 1996-2021 by the citadel.org team
5 // This program is open source software. You can redistribute it and/or
6 // modify it under the terms of the GNU General Public License, version 3.
12 char serv_sock_name[PATH_MAX] = "";
14 HashList *EmbeddableMimes = NULL;
15 StrBuf *EmbeddableMimeStrs = NULL;
18 void SetInlinMimeRenderers(void) {
23 /* Tell the server what kind of richtext we prefer */
24 serv_putbuf(EmbeddableMimeStrs);
25 StrBuf_ServGetln(Buf);
31 void DeleteServInfo(ServInfo **FreeMe) {
34 FreeStrBuf(&(*FreeMe)->serv_nodename);
35 FreeStrBuf(&(*FreeMe)->serv_humannode);
36 FreeStrBuf(&(*FreeMe)->serv_fqdn);
37 FreeStrBuf(&(*FreeMe)->serv_software);
38 FreeStrBuf(&(*FreeMe)->serv_bbs_city);
39 FreeStrBuf(&(*FreeMe)->serv_sysadm);
40 FreeStrBuf(&(*FreeMe)->serv_default_cal_zone);
41 FreeStrBuf(&(*FreeMe)->serv_svn_revision);
47 * get info about the server we've connected to
49 * browser_host the citadel we want to connect to
50 * user_agent which browser uses our client?
52 ServInfo *get_serv_info(StrBuf *browser_host, StrBuf *user_agent) {
60 /* Tell the server what kind of client is connecting */
61 serv_printf("IDEN %d|%d|%d|%s|%s",
68 StrBuf_ServGetln(Buf);
69 if (GetServerStatus(Buf, NULL) != 2) {
70 syslog(LOG_WARNING, "get_serv_info(IDEN): unexpected answer [%s]\n",
77 * Tell the server that when we save a calendar event, we
78 * want invitations to be generated by the Citadel server
79 * instead of by the client.
81 serv_puts("ICAL sgi|1");
82 StrBuf_ServGetln(Buf);
83 if (GetServerStatus(Buf, NULL) != 2) {
84 syslog(LOG_WARNING, "get_serv_info(ICAL sgi|1): unexpected answer [%s]\n",
90 /* Now ask the server to tell us a little bit about itself... */
92 StrBuf_ServGetln(Buf);
93 if (GetServerStatus(Buf, NULL) != 1) {
94 syslog(LOG_WARNING, "get_serv_info(INFO sgi|1): unexpected answer [%s]\n",
100 info = (ServInfo*)malloc(sizeof(ServInfo));
101 memset(info, 0, sizeof(ServInfo));
103 while (rc = StrBuf_ServGetln(Buf),
106 strcmp(ChrPtr(Buf), "000")))
110 info->serv_pid = StrToi(Buf);
111 WC->ctdl_pid = info->serv_pid;
114 info->serv_nodename = NewStrBufDup(Buf);
117 info->serv_humannode = NewStrBufDup(Buf);
120 info->serv_fqdn = NewStrBufDup(Buf);
123 info->serv_software = NewStrBufDup(Buf);
126 info->serv_rev_level = StrToi(Buf);
129 info->serv_bbs_city = NewStrBufDup(Buf);
132 info->serv_sysadm = NewStrBufDup(Buf);
135 info->serv_supports_ldap = StrToi(Buf);
138 info->serv_newuser_disabled = StrToi(Buf);
141 info->serv_default_cal_zone = NewStrBufDup(Buf);
144 info->serv_supports_sieve = StrToi(Buf);
147 info->serv_fulltext_enabled = StrToi(Buf);
150 info->serv_svn_revision = NewStrBufDup(Buf);
153 info->serv_supports_openid = StrToi(Buf);
156 info->serv_supports_guest = StrToi(Buf);
165 int GetConnected (void) {
169 if (WCC->ReadBuf == NULL)
170 WCC->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
171 if (is_uds) /* unix domain socket */
172 WCC->serv_sock = uds_connectsock(serv_sock_name);
173 else /* tcp socket */
174 WCC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
176 if (WCC->serv_sock < 0) {
178 FreeStrBuf(&WCC->ReadBuf);
186 StrBuf_ServGetln(Buf); /* get the server greeting */
187 short_status = GetServerStatus(Buf, &Status);
190 /* Server isn't ready for us? */
191 if (short_status != 2) {
193 hprintf("HTTP/1.1 503 Service Unavailable\r\n");
194 hprintf("Content-type: text/plain; charset=utf-8\r\n");
195 wc_printf(_("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."));
198 wc_printf("%ld %s\n",
200 _("Received unexpected answer from Citadel server; bailing out.")
202 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
203 hprintf("Content-type: text/plain; charset=utf-8\r\n");
206 end_webcit_session();
211 * From what host is our user connecting? Go with
212 * the host at the other end of the HTTP socket,
213 * unless we are following X-Forwarded-For: headers
214 * and such a header has already turned up something.
216 if ( (!follow_xff) || (StrLength(WCC->Hdr->HR.browser_host) == 0) ) {
217 if (WCC->Hdr->HR.browser_host == NULL) {
218 WCC->Hdr->HR.browser_host = NewStrBuf();
219 Put(WCC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"),
220 WCC->Hdr->HR.browser_host, HFreeStrBuf);
222 locate_host(WCC->Hdr->HR.browser_host, WCC->Hdr->http_sock);
224 if (WCC->serv_info == NULL) {
225 WCC->serv_info = get_serv_info(WCC->Hdr->HR.browser_host, WCC->Hdr->HR.user_agent);
227 if (WCC->serv_info == NULL){
229 wc_printf(_("Received unexpected answer from Citadel server; bailing out."));
230 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
231 hprintf("Content-type: text/plain; charset=utf-8\r\n");
233 end_webcit_session();
236 if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
238 wc_printf(_("You are connected to a Citadel "
239 "server running Citadel %d.%02d. \n"
240 "In order to run this version of WebCit "
241 "you must also have Citadel %d.%02d or"
243 WCC->serv_info->serv_rev_level,
248 hprintf("HTTP/1.1 200 OK\r\n");
249 hprintf("Content-type: text/plain; charset=utf-8\r\n");
251 end_webcit_session();
254 SetInlinMimeRenderers();
260 void FmOut(StrBuf *Target, const char *align, const StrBuf *Source) {
261 const char *ptr, *pte;
262 const char *BufPtr = NULL;
263 StrBuf *Line = NewStrBufPlain(NULL, SIZ);
264 StrBuf *Line1 = NewStrBufPlain(NULL, SIZ);
265 StrBuf *Line2 = NewStrBufPlain(NULL, SIZ);
272 StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
274 if (StrLength(Source) > 0)
277 StrBufSipLine(Line, Source, &BufPtr);
281 len = StrLength(Line);
284 if ((intext == 1) && (isspace(*ptr))) {
285 StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
288 if (isspace(*ptr)) while ((ptr < pte) &&
299 * Quoted text should be displayed in italics and in a
300 * different colour. This code understands Citadel-style
301 * " >" quotes and will convert to <BLOCKQUOTE> tags.
303 if (i > 0) StrBufCutLeft(Line, i);
306 for (i = bn; i < bq; i++)
307 StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
308 for (i = bq; i < bn; i++)
309 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
312 if (StrLength(Line) == 0)
315 /* Activate embedded URL's */
316 UrlizeText(Line1, Line, Line2);
318 StrEscAppend(Target, Line1, NULL, 0, 0);
320 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
322 while ((BufPtr != StrBufNOTNULL) &&
325 for (i = 0; i < bn; i++) {
326 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
328 StrBufAppendBufPlain(Target, HKEY("</div><br>\n"), 0);
337 * Transmit message text (in memory) to the server.
339 void text_to_server(char *ptr) {
346 while (ptr[pos] != 0) {
349 // ignore CR characters
353 while ( (isspace(buf[len - 1]))
355 && (buf[1] != '\0') )
359 if (ptr[pos] != 0) strcat(buf, " ");
364 if ((ch == 32) && (strlen(buf) > 200)) {
369 if (strlen(buf) > 250) {
380 * Transmit message text (in memory) to the server, converting to Quoted-Printable encoding as we go.
382 void text_to_server_qp(const StrBuf *SendMeEncoded) {
385 ServBuf = StrBufRFC2047encodeMessage(SendMeEncoded);
386 serv_putbuf(ServBuf);
387 FreeStrBuf(&ServBuf);
394 * translate server message output to text (used for editing room info files and such)
396 void server_to_text() {
401 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
402 if ((buf[0] == 32) && (count > 0)) {
405 wc_printf("%s", buf);
414 * Read text from server, appending to a string buffer until the
415 * usual 000 terminator is found. Caller is responsible for freeing
416 * the returned pointer.
418 int read_server_text(StrBuf *Buf, long *nLines) {
426 ReadBuf = NewStrBuf();
427 while ((WCC->serv_sock!=-1) &&
428 (nRead = StrBuf_ServGetln(ReadBuf), (nRead >= 0) &&
429 ((nRead != 3)||(strcmp(ChrPtr(ReadBuf), "000") != 0))))
431 StrBufAppendBuf(Buf, ReadBuf, 0);
432 StrBufAppendBufPlain(Buf, HKEY("\n"), 0);
436 FreeStrBuf(&ReadBuf);
442 int GetServerStatusMsg(StrBuf *Line, long* FullState, int PutImportantMessage, int MajorOK) {
444 if (FullState != NULL)
445 *FullState = StrTol(Line);
446 rc = ChrPtr(Line)[0] - 48;
447 if ((!PutImportantMessage) ||
449 (StrLength(Line) <= 4))
452 AppendImportantMessage(ChrPtr(Line) + 4, StrLength(Line) - 4);
457 void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP) {
458 StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
461 void tmplput_serv_admin(StrBuf *Target, WCTemplputParams *TP) {
463 if (WCC->serv_info == NULL)
465 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_sysadm, 0);
468 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP) {
470 if (WCC->serv_info == NULL)
472 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_nodename, 0);
475 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP) {
477 if (WCC->serv_info == NULL)
479 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_humannode, 0);
482 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP) {
484 if (WCC->serv_info == NULL)
486 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_fqdn, 0);
489 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP) {
491 if (WCC->serv_info == NULL)
493 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_software, 0);
496 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP) {
497 if (WC->serv_info == NULL) return;
498 StrBufAppendPrintf(Target, "%d", WC->serv_info->serv_rev_level);
500 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP) {
502 if (WCC->serv_info == NULL)
504 return WCC->serv_info->serv_newuser_disabled != 0;
507 int conditional_serv_supports_guest(StrBuf *Target, WCTemplputParams *TP) {
509 if (WCC->serv_info == NULL)
511 return WCC->serv_info->serv_supports_guest != 0;
514 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP) {
516 if (WCC->serv_info == NULL)
518 return WCC->serv_info->serv_supports_openid != 0;
521 int conditional_serv_fulltext_enabled(StrBuf *Target, WCTemplputParams *TP) {
523 if (WCC->serv_info == NULL)
525 return WCC->serv_info->serv_fulltext_enabled != 0;
528 int conditional_serv_ldap_enabled(StrBuf *Target, WCTemplputParams *TP) {
530 if (WCC->serv_info == NULL)
532 return WCC->serv_info->serv_supports_ldap != 0;
535 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP) {
537 if (WCC->serv_info == NULL)
539 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
542 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP) {
550 serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
552 StrBuf_ServGetln(Line);
553 if (GetServerStatus(Line, NULL) == 1) {
554 while (!Done && (StrBuf_ServGetln(Line)>=0)) {
555 if ( (StrLength(Line)==3) &&
556 !strcmp(ChrPtr(Line), "000"))
561 StrBufAppendBufPlain(Buf, "\n", 1, 0);
562 StrBufAppendBuf(Buf, Line, 0);
568 FmOut(Line, "center", Buf);
569 StrBufAppendTemplate(Target, TP, Line, 1);
575 void tmplput_site_prefix(StrBuf *Target, WCTemplputParams *TP) {
577 if ((WCC != NULL) && (WCC->Hdr->HostHeader != NULL)) {
578 StrBufAppendTemplate(Target, TP, WCC->Hdr->HostHeader, 0);
582 void RegisterEmbeddableMimeType(const char *MimeType, long MTLen, int Priority) {
584 MT = NewStrBufPlain(MimeType, MTLen);
585 Put(EmbeddableMimes, IKEY(Priority), MT, HFreeStrBuf);
588 void CreateMimeStr(void) {
594 it = GetNewHashPos(EmbeddableMimes, 0);
595 while (GetNextHashPos(EmbeddableMimes, it, &len, &Key, &vMime) &&
597 if (StrLength(EmbeddableMimeStrs) > 0)
598 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("|"), 0);
600 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("MSGP "), 0);
601 StrBufAppendBuf(EmbeddableMimeStrs, (StrBuf*) vMime, 0);
607 ServerStartModule_SERV_FUNC
610 EmbeddableMimes = NewHash(1, Flathash);
611 EmbeddableMimeStrs = NewStrBuf();
616 ServerShutdownModule_SERV_FUNC
619 FreeStrBuf(&EmbeddableMimeStrs);
620 DeleteHash(&EmbeddableMimes);
627 is_uds = strcasecmp(ctdlhost, "uds") == 0;
629 snprintf(serv_sock_name, PATH_MAX, "%s/citadel.socket", ctdlport);
631 RegisterConditional("COND:SERV:OPENID", 2, conditional_serv_supports_openid, CTX_NONE);
632 RegisterConditional("COND:SERV:NEWU", 2, conditional_serv_newuser_disabled, CTX_NONE);
633 RegisterConditional("COND:SERV:FULLTEXT_ENABLED", 2, conditional_serv_fulltext_enabled, CTX_NONE);
634 RegisterConditional("COND:SERV:LDAP_ENABLED", 2, conditional_serv_ldap_enabled, CTX_NONE);
635 RegisterConditional("COND:SERV:SUPPORTS_GUEST", 2, conditional_serv_supports_guest, CTX_NONE);
636 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, NULL, CTX_NONE);
637 RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, NULL, CTX_NONE);
638 RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, NULL, CTX_NONE);
639 RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, NULL, CTX_NONE);
641 RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, NULL, CTX_NONE);
642 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, NULL, CTX_NONE);
643 RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, NULL, CTX_NONE);
644 RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, NULL, CTX_NONE);
645 RegisterNamespace("SERV:ADMIN", 0, 1, tmplput_serv_admin, NULL, CTX_NONE);
647 RegisterNamespace("SERV:SITE:PREFIX", 0, 1, tmplput_site_prefix, NULL, CTX_NONE);
655 SessionDestroyModule_SERVFUNC
658 DeleteServInfo(&sess->serv_info);