2 * Functions which handle communication with the Citadel server.
4 * Copyright (c) 1996-2018 by the citadel.org team
6 * This program is open source software. You can redistribute it and/or
7 * modify it under the terms of the GNU General Public License, version 3.
11 #include "webserver.h"
14 char serv_sock_name[PATH_MAX] = "";
16 HashList *EmbeddableMimes = NULL;
17 StrBuf *EmbeddableMimeStrs = NULL;
20 void SetInlinMimeRenderers(void)
26 /* Tell the server what kind of richtext we prefer */
27 serv_putbuf(EmbeddableMimeStrs);
28 StrBuf_ServGetln(Buf);
34 void DeleteServInfo(ServInfo **FreeMe)
38 FreeStrBuf(&(*FreeMe)->serv_nodename);
39 FreeStrBuf(&(*FreeMe)->serv_humannode);
40 FreeStrBuf(&(*FreeMe)->serv_fqdn);
41 FreeStrBuf(&(*FreeMe)->serv_software);
42 FreeStrBuf(&(*FreeMe)->serv_bbs_city);
43 FreeStrBuf(&(*FreeMe)->serv_sysadm);
44 FreeStrBuf(&(*FreeMe)->serv_default_cal_zone);
45 FreeStrBuf(&(*FreeMe)->serv_svn_revision);
51 * get info about the server we've connected to
53 * browser_host the citadel we want to connect to
54 * user_agent which browser uses our client?
56 ServInfo *get_serv_info(StrBuf *browser_host, StrBuf *user_agent)
65 /* Tell the server what kind of client is connecting */
66 serv_printf("IDEN %d|%d|%d|%s|%s",
73 StrBuf_ServGetln(Buf);
74 if (GetServerStatus(Buf, NULL) != 2) {
75 syslog(LOG_WARNING, "get_serv_info(IDEN): unexpected answer [%s]\n",
82 * Tell the server that when we save a calendar event, we
83 * want invitations to be generated by the Citadel server
84 * instead of by the client.
86 serv_puts("ICAL sgi|1");
87 StrBuf_ServGetln(Buf);
88 if (GetServerStatus(Buf, NULL) != 2) {
89 syslog(LOG_WARNING, "get_serv_info(ICAL sgi|1): unexpected answer [%s]\n",
95 /* Now ask the server to tell us a little bit about itself... */
97 StrBuf_ServGetln(Buf);
98 if (GetServerStatus(Buf, NULL) != 1) {
99 syslog(LOG_WARNING, "get_serv_info(INFO sgi|1): unexpected answer [%s]\n",
105 info = (ServInfo*)malloc(sizeof(ServInfo));
106 memset(info, 0, sizeof(ServInfo));
108 while (rc = StrBuf_ServGetln(Buf),
111 strcmp(ChrPtr(Buf), "000")))
115 info->serv_pid = StrToi(Buf);
116 WC->ctdl_pid = info->serv_pid;
119 info->serv_nodename = NewStrBufDup(Buf);
122 info->serv_humannode = NewStrBufDup(Buf);
125 info->serv_fqdn = NewStrBufDup(Buf);
128 info->serv_software = NewStrBufDup(Buf);
131 info->serv_rev_level = StrToi(Buf);
134 info->serv_bbs_city = NewStrBufDup(Buf);
137 info->serv_sysadm = NewStrBufDup(Buf);
140 info->serv_supports_ldap = StrToi(Buf);
143 info->serv_newuser_disabled = StrToi(Buf);
146 info->serv_default_cal_zone = NewStrBufDup(Buf);
149 info->serv_supports_sieve = StrToi(Buf);
152 info->serv_fulltext_enabled = StrToi(Buf);
155 info->serv_svn_revision = NewStrBufDup(Buf);
158 info->serv_supports_openid = StrToi(Buf);
161 info->serv_supports_guest = StrToi(Buf);
170 int GetConnected (void)
175 if (WCC->ReadBuf == NULL)
176 WCC->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
177 if (is_uds) /* unix domain socket */
178 WCC->serv_sock = uds_connectsock(serv_sock_name);
179 else /* tcp socket */
180 WCC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
182 if (WCC->serv_sock < 0) {
184 FreeStrBuf(&WCC->ReadBuf);
192 StrBuf_ServGetln(Buf); /* get the server greeting */
193 short_status = GetServerStatus(Buf, &Status);
196 /* Server isn't ready for us? */
197 if (short_status != 2) {
199 hprintf("HTTP/1.1 503 Service Unavailable\r\n");
200 hprintf("Content-type: text/plain; charset=utf-8\r\n");
201 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."));
204 wc_printf("%ld %s\n",
206 _("Received unexpected answer from Citadel server; bailing out.")
208 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
209 hprintf("Content-type: text/plain; charset=utf-8\r\n");
212 end_webcit_session();
217 * From what host is our user connecting? Go with
218 * the host at the other end of the HTTP socket,
219 * unless we are following X-Forwarded-For: headers
220 * and such a header has already turned up something.
222 if ( (!follow_xff) || (StrLength(WCC->Hdr->HR.browser_host) == 0) ) {
223 if (WCC->Hdr->HR.browser_host == NULL) {
224 WCC->Hdr->HR.browser_host = NewStrBuf();
225 Put(WCC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"),
226 WCC->Hdr->HR.browser_host, HFreeStrBuf);
228 locate_host(WCC->Hdr->HR.browser_host, WCC->Hdr->http_sock);
230 if (WCC->serv_info == NULL) {
231 WCC->serv_info = get_serv_info(WCC->Hdr->HR.browser_host, WCC->Hdr->HR.user_agent);
233 if (WCC->serv_info == NULL){
235 wc_printf(_("Received unexpected answer from Citadel server; bailing out."));
236 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
237 hprintf("Content-type: text/plain; charset=utf-8\r\n");
239 end_webcit_session();
242 if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
244 wc_printf(_("You are connected to a Citadel "
245 "server running Citadel %d.%02d. \n"
246 "In order to run this version of WebCit "
247 "you must also have Citadel %d.%02d or"
249 WCC->serv_info->serv_rev_level,
254 hprintf("HTTP/1.1 200 OK\r\n");
255 hprintf("Content-type: text/plain; charset=utf-8\r\n");
257 end_webcit_session();
260 SetInlinMimeRenderers();
266 void FmOut(StrBuf *Target, const char *align, const StrBuf *Source)
268 const char *ptr, *pte;
269 const char *BufPtr = NULL;
270 StrBuf *Line = NewStrBufPlain(NULL, SIZ);
271 StrBuf *Line1 = NewStrBufPlain(NULL, SIZ);
272 StrBuf *Line2 = NewStrBufPlain(NULL, SIZ);
279 StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
281 if (StrLength(Source) > 0)
284 StrBufSipLine(Line, Source, &BufPtr);
288 len = StrLength(Line);
291 if ((intext == 1) && (isspace(*ptr))) {
292 StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
295 if (isspace(*ptr)) while ((ptr < pte) &&
306 * Quoted text should be displayed in italics and in a
307 * different colour. This code understands Citadel-style
308 * " >" quotes and will convert to <BLOCKQUOTE> tags.
310 if (i > 0) StrBufCutLeft(Line, i);
313 for (i = bn; i < bq; i++)
314 StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
315 for (i = bq; i < bn; i++)
316 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
319 if (StrLength(Line) == 0)
322 /* Activate embedded URL's */
323 UrlizeText(Line1, Line, Line2);
325 StrEscAppend(Target, Line1, NULL, 0, 0);
327 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
329 while ((BufPtr != StrBufNOTNULL) &&
332 for (i = 0; i < bn; i++) {
333 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
335 StrBufAppendBufPlain(Target, HKEY("</div><br>\n"), 0);
344 * Transmit message text (in memory) to the server.
346 void text_to_server(char *ptr)
354 while (ptr[pos] != 0) {
358 while ( (isspace(buf[len - 1]))
360 && (buf[1] != '\0') )
364 if (ptr[pos] != 0) strcat(buf, " ");
369 if ((ch == 32) && (strlen(buf) > 200)) {
374 if (strlen(buf) > 250) {
385 * Transmit message text (in memory) to the server, converting to Quoted-Printable encoding as we go.
387 void text_to_server_qp(const StrBuf *SendMeEncoded)
391 ServBuf = StrBufRFC2047encodeMessage(SendMeEncoded);
392 serv_putbuf(ServBuf);
393 FreeStrBuf(&ServBuf);
400 * translate server message output to text (used for editing room info files and such)
402 void server_to_text()
408 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
409 if ((buf[0] == 32) && (count > 0)) {
412 wc_printf("%s", buf);
421 * Read text from server, appending to a string buffer until the
422 * usual 000 terminator is found. Caller is responsible for freeing
423 * the returned pointer.
425 int read_server_text(StrBuf *Buf, long *nLines)
434 ReadBuf = NewStrBuf();
435 while ((WCC->serv_sock!=-1) &&
436 (nRead = StrBuf_ServGetln(ReadBuf), (nRead >= 0) &&
437 ((nRead != 3)||(strcmp(ChrPtr(ReadBuf), "000") != 0))))
439 StrBufAppendBuf(Buf, ReadBuf, 0);
440 StrBufAppendBufPlain(Buf, HKEY("\n"), 0);
444 FreeStrBuf(&ReadBuf);
450 int GetServerStatusMsg(StrBuf *Line, long* FullState, int PutImportantMessage, int MajorOK)
453 if (FullState != NULL)
454 *FullState = StrTol(Line);
455 rc = ChrPtr(Line)[0] - 48;
456 if ((!PutImportantMessage) ||
458 (StrLength(Line) <= 4))
461 AppendImportantMessage(ChrPtr(Line) + 4, StrLength(Line) - 4);
466 void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP)
468 StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
471 void tmplput_serv_admin(StrBuf *Target, WCTemplputParams *TP)
474 if (WCC->serv_info == NULL)
476 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_sysadm, 0);
479 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP)
482 if (WCC->serv_info == NULL)
484 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_nodename, 0);
487 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP)
490 if (WCC->serv_info == NULL)
492 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_humannode, 0);
495 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP)
498 if (WCC->serv_info == NULL)
500 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_fqdn, 0);
503 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP)
506 if (WCC->serv_info == NULL)
508 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_software, 0);
511 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP)
513 if (WC->serv_info == NULL) return;
514 StrBufAppendPrintf(Target, "%d", WC->serv_info->serv_rev_level);
516 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP)
519 if (WCC->serv_info == NULL)
521 return WCC->serv_info->serv_newuser_disabled != 0;
524 int conditional_serv_supports_guest(StrBuf *Target, WCTemplputParams *TP)
527 if (WCC->serv_info == NULL)
529 return WCC->serv_info->serv_supports_guest != 0;
532 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP)
535 if (WCC->serv_info == NULL)
537 return WCC->serv_info->serv_supports_openid != 0;
540 int conditional_serv_fulltext_enabled(StrBuf *Target, WCTemplputParams *TP)
543 if (WCC->serv_info == NULL)
545 return WCC->serv_info->serv_fulltext_enabled != 0;
548 int conditional_serv_ldap_enabled(StrBuf *Target, WCTemplputParams *TP)
551 if (WCC->serv_info == NULL)
553 return WCC->serv_info->serv_supports_ldap != 0;
556 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP)
559 if (WCC->serv_info == NULL)
561 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
564 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP)
573 serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
575 StrBuf_ServGetln(Line);
576 if (GetServerStatus(Line, NULL) == 1) {
577 while (!Done && (StrBuf_ServGetln(Line)>=0)) {
578 if ( (StrLength(Line)==3) &&
579 !strcmp(ChrPtr(Line), "000"))
584 StrBufAppendBufPlain(Buf, "\n", 1, 0);
585 StrBufAppendBuf(Buf, Line, 0);
591 FmOut(Line, "center", Buf);
592 StrBufAppendTemplate(Target, TP, Line, 1);
598 void tmplput_site_prefix(StrBuf *Target, WCTemplputParams *TP) {
600 if ((WCC != NULL) && (WCC->Hdr->HostHeader != NULL)) {
601 StrBufAppendTemplate(Target, TP, WCC->Hdr->HostHeader, 0);
605 void RegisterEmbeddableMimeType(const char *MimeType, long MTLen, int Priority)
608 MT = NewStrBufPlain(MimeType, MTLen);
609 Put(EmbeddableMimes, IKEY(Priority), MT, HFreeStrBuf);
612 void CreateMimeStr(void)
619 it = GetNewHashPos(EmbeddableMimes, 0);
620 while (GetNextHashPos(EmbeddableMimes, it, &len, &Key, &vMime) &&
622 if (StrLength(EmbeddableMimeStrs) > 0)
623 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("|"), 0);
625 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("MSGP "), 0);
626 StrBufAppendBuf(EmbeddableMimeStrs, (StrBuf*) vMime, 0);
632 ServerStartModule_SERV_FUNC
635 EmbeddableMimes = NewHash(1, Flathash);
636 EmbeddableMimeStrs = NewStrBuf();
641 ServerShutdownModule_SERV_FUNC
644 FreeStrBuf(&EmbeddableMimeStrs);
645 DeleteHash(&EmbeddableMimes);
652 is_uds = strcasecmp(ctdlhost, "uds") == 0;
654 snprintf(serv_sock_name, PATH_MAX, "%s/citadel.socket", ctdlport);
656 RegisterConditional("COND:SERV:OPENID", 2, conditional_serv_supports_openid, CTX_NONE);
657 RegisterConditional("COND:SERV:NEWU", 2, conditional_serv_newuser_disabled, CTX_NONE);
658 RegisterConditional("COND:SERV:FULLTEXT_ENABLED", 2, conditional_serv_fulltext_enabled, CTX_NONE);
659 RegisterConditional("COND:SERV:LDAP_ENABLED", 2, conditional_serv_ldap_enabled, CTX_NONE);
660 RegisterConditional("COND:SERV:SUPPORTS_GUEST", 2, conditional_serv_supports_guest, CTX_NONE);
661 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, NULL, CTX_NONE);
662 RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, NULL, CTX_NONE);
663 RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, NULL, CTX_NONE);
664 RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, NULL, CTX_NONE);
666 RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, NULL, CTX_NONE);
667 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, NULL, CTX_NONE);
668 RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, NULL, CTX_NONE);
669 RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, NULL, CTX_NONE);
670 RegisterNamespace("SERV:ADMIN", 0, 1, tmplput_serv_admin, NULL, CTX_NONE);
672 RegisterNamespace("SERV:SITE:PREFIX", 0, 1, tmplput_site_prefix, NULL, CTX_NONE);
680 SessionDestroyModule_SERVFUNC
683 DeleteServInfo(&sess->serv_info);