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.
11 HashList *EmbeddableMimes = NULL;
12 StrBuf *EmbeddableMimeStrs = NULL;
14 void SetInlinMimeRenderers(void) {
19 /* Tell the server what kind of richtext we prefer */
20 serv_putbuf(EmbeddableMimeStrs);
21 StrBuf_ServGetln(Buf);
27 void DeleteServInfo(ServInfo **FreeMe) {
30 FreeStrBuf(&(*FreeMe)->serv_nodename);
31 FreeStrBuf(&(*FreeMe)->serv_humannode);
32 FreeStrBuf(&(*FreeMe)->serv_fqdn);
33 FreeStrBuf(&(*FreeMe)->serv_software);
34 FreeStrBuf(&(*FreeMe)->serv_bbs_city);
35 FreeStrBuf(&(*FreeMe)->serv_sysadm);
36 FreeStrBuf(&(*FreeMe)->serv_default_cal_zone);
37 FreeStrBuf(&(*FreeMe)->serv_svn_revision);
43 * get info about the server we've connected to
45 * browser_host the citadel we want to connect to
46 * user_agent which browser uses our client?
48 ServInfo *get_serv_info(StrBuf *browser_host, StrBuf *user_agent) {
56 /* Tell the server what kind of client is connecting */
57 serv_printf("IDEN %d|%d|%d|%s|%s",
64 StrBuf_ServGetln(Buf);
65 if (GetServerStatus(Buf, NULL) != 2) {
66 syslog(LOG_WARNING, "get_serv_info(IDEN): unexpected answer [%s]\n",
73 * Tell the server that when we save a calendar event, we
74 * want invitations to be generated by the Citadel server
75 * instead of by the client.
77 serv_puts("ICAL sgi|1");
78 StrBuf_ServGetln(Buf);
79 if (GetServerStatus(Buf, NULL) != 2) {
80 syslog(LOG_WARNING, "get_serv_info(ICAL sgi|1): unexpected answer [%s]\n",
86 /* Now ask the server to tell us a little bit about itself... */
88 StrBuf_ServGetln(Buf);
89 if (GetServerStatus(Buf, NULL) != 1) {
90 syslog(LOG_WARNING, "get_serv_info(INFO sgi|1): unexpected answer [%s]\n",
96 info = (ServInfo*)malloc(sizeof(ServInfo));
97 memset(info, 0, sizeof(ServInfo));
99 while (rc = StrBuf_ServGetln(Buf),
102 strcmp(ChrPtr(Buf), "000")))
106 info->serv_pid = StrToi(Buf);
107 WC->ctdl_pid = info->serv_pid;
110 info->serv_nodename = NewStrBufDup(Buf);
113 info->serv_humannode = NewStrBufDup(Buf);
116 info->serv_fqdn = NewStrBufDup(Buf);
119 info->serv_software = NewStrBufDup(Buf);
122 info->serv_rev_level = StrToi(Buf);
125 info->serv_bbs_city = NewStrBufDup(Buf);
128 info->serv_sysadm = NewStrBufDup(Buf);
131 info->serv_supports_ldap = StrToi(Buf);
134 info->serv_newuser_disabled = StrToi(Buf);
137 info->serv_default_cal_zone = NewStrBufDup(Buf);
140 info->serv_supports_sieve = StrToi(Buf);
143 info->serv_fulltext_enabled = StrToi(Buf);
146 info->serv_svn_revision = NewStrBufDup(Buf);
149 info->serv_supports_openid = StrToi(Buf);
152 info->serv_supports_guest = StrToi(Buf);
161 int GetConnected (void) {
164 if (WC->ReadBuf == NULL) {
165 WC->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
168 static char serv_sock_name[PATH_MAX] = "";
169 if (IsEmptyStr(serv_sock_name)) {
170 snprintf(serv_sock_name, sizeof serv_sock_name, "%s/citadel.socket", ctdl_dir);
172 WC->serv_sock = connect_to_citadel(serv_sock_name);
174 if (WC->serv_sock < 0) {
176 FreeStrBuf(&WC->ReadBuf);
184 StrBuf_ServGetln(Buf); /* get the server greeting */
185 short_status = GetServerStatus(Buf, &Status);
188 /* Server isn't ready for us? */
189 if (short_status != 2) {
191 hprintf("HTTP/1.1 503 Service Unavailable\r\n");
192 hprintf("Content-type: text/plain; charset=utf-8\r\n");
193 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."));
196 wc_printf("%ld %s\n",
198 _("Received unexpected answer from Citadel server; bailing out.")
200 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
201 hprintf("Content-type: text/plain; charset=utf-8\r\n");
204 end_webcit_session();
209 * From what host is our user connecting? Go with
210 * the host at the other end of the HTTP socket,
211 * unless we are following X-Forwarded-For: headers
212 * and such a header has already turned up something.
214 if ( (!follow_xff) || (StrLength(WC->Hdr->HR.browser_host) == 0) ) {
215 if (WC->Hdr->HR.browser_host == NULL) {
216 WC->Hdr->HR.browser_host = NewStrBuf();
217 Put(WC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"),
218 WC->Hdr->HR.browser_host, HFreeStrBuf);
220 locate_host(WC->Hdr->HR.browser_host, WC->Hdr->http_sock);
222 if (WC->serv_info == NULL) {
223 WC->serv_info = get_serv_info(WC->Hdr->HR.browser_host, WC->Hdr->HR.user_agent);
225 if (WC->serv_info == NULL){
227 wc_printf(_("Received unexpected answer from Citadel server; bailing out."));
228 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
229 hprintf("Content-type: text/plain; charset=utf-8\r\n");
231 end_webcit_session();
234 if (WC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
236 wc_printf(_("You are connected to a Citadel "
237 "server running Citadel %d.%02d. \n"
238 "In order to run this version of WebCit "
239 "you must also have Citadel %d.%02d or"
241 WC->serv_info->serv_rev_level,
246 hprintf("HTTP/1.1 200 OK\r\n");
247 hprintf("Content-type: text/plain; charset=utf-8\r\n");
249 end_webcit_session();
252 SetInlinMimeRenderers();
258 void FmOut(StrBuf *Target, const char *align, const StrBuf *Source) {
259 const char *ptr, *pte;
260 const char *BufPtr = NULL;
261 StrBuf *Line = NewStrBufPlain(NULL, SIZ);
262 StrBuf *Line1 = NewStrBufPlain(NULL, SIZ);
263 StrBuf *Line2 = NewStrBufPlain(NULL, SIZ);
270 StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
272 if (StrLength(Source) > 0)
275 StrBufSipLine(Line, Source, &BufPtr);
279 len = StrLength(Line);
282 if ((intext == 1) && (isspace(*ptr))) {
283 StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
286 if (isspace(*ptr)) while ((ptr < pte) &&
297 * Quoted text should be displayed in italics and in a
298 * different colour. This code understands Citadel-style
299 * " >" quotes and will convert to <BLOCKQUOTE> tags.
301 if (i > 0) StrBufCutLeft(Line, i);
304 for (i = bn; i < bq; i++)
305 StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
306 for (i = bq; i < bn; i++)
307 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
310 if (StrLength(Line) == 0)
313 /* Activate embedded URL's */
314 UrlizeText(Line1, Line, Line2);
316 StrEscAppend(Target, Line1, NULL, 0, 0);
318 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
320 while ((BufPtr != StrBufNOTNULL) &&
323 for (i = 0; i < bn; i++) {
324 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
326 StrBufAppendBufPlain(Target, HKEY("</div><br>\n"), 0);
335 * Transmit message text (in memory) to the server.
337 void text_to_server(char *ptr) {
344 while (ptr[pos] != 0) {
347 // ignore CR characters
351 while ( (isspace(buf[len - 1]))
353 && (buf[1] != '\0') )
357 if (ptr[pos] != 0) strcat(buf, " ");
362 if ((ch == 32) && (strlen(buf) > 200)) {
367 if (strlen(buf) > 250) {
378 * Transmit message text (in memory) to the server, converting to Quoted-Printable encoding as we go.
380 void text_to_server_qp(const StrBuf *SendMeEncoded) {
383 ServBuf = StrBufQuotedPrintableEncode(SendMeEncoded);
384 serv_putbuf(ServBuf);
385 FreeStrBuf(&ServBuf);
392 * translate server message output to text (used for editing room info files and such)
394 void server_to_text() {
399 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
400 if ((buf[0] == 32) && (count > 0)) {
403 wc_printf("%s", buf);
412 * Read text from server, appending to a string buffer until the
413 * usual 000 terminator is found. Caller is responsible for freeing
414 * the returned pointer.
416 int read_server_text(StrBuf *Buf, long *nLines) {
423 ReadBuf = NewStrBuf();
424 while ((WC->serv_sock!=-1) &&
425 (nRead = StrBuf_ServGetln(ReadBuf), (nRead >= 0) &&
426 ((nRead != 3)||(strcmp(ChrPtr(ReadBuf), "000") != 0))))
428 StrBufAppendBuf(Buf, ReadBuf, 0);
429 StrBufAppendBufPlain(Buf, HKEY("\n"), 0);
433 FreeStrBuf(&ReadBuf);
439 int GetServerStatusMsg(StrBuf *Line, long* FullState, int PutImportantMessage, int MajorOK) {
441 if (FullState != NULL)
442 *FullState = StrTol(Line);
443 rc = ChrPtr(Line)[0] - 48;
444 if ((!PutImportantMessage) ||
446 (StrLength(Line) <= 4))
449 AppendImportantMessage(ChrPtr(Line) + 4, StrLength(Line) - 4);
454 void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP) {
455 StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
458 void tmplput_serv_admin(StrBuf *Target, WCTemplputParams *TP) {
459 if (WC->serv_info == NULL)
461 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_sysadm, 0);
464 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP) {
465 if (WC->serv_info == NULL)
467 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_nodename, 0);
470 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP) {
471 if (WC->serv_info == NULL)
473 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_humannode, 0);
476 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP) {
477 if (WC->serv_info == NULL)
479 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_fqdn, 0);
482 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP) {
483 if (WC->serv_info == NULL)
485 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_software, 0);
488 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP) {
489 if (WC->serv_info == NULL) return;
490 StrBufAppendPrintf(Target, "%d", WC->serv_info->serv_rev_level);
492 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP) {
493 if (WC->serv_info == NULL)
495 return WC->serv_info->serv_newuser_disabled != 0;
498 int conditional_serv_supports_guest(StrBuf *Target, WCTemplputParams *TP) {
499 if (WC->serv_info == NULL)
501 return WC->serv_info->serv_supports_guest != 0;
504 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP) {
505 if (WC->serv_info == NULL)
507 return WC->serv_info->serv_supports_openid != 0;
510 int conditional_serv_fulltext_enabled(StrBuf *Target, WCTemplputParams *TP) {
511 if (WC->serv_info == NULL)
513 return WC->serv_info->serv_fulltext_enabled != 0;
516 int conditional_serv_ldap_enabled(StrBuf *Target, WCTemplputParams *TP) {
517 if (WC->serv_info == NULL)
519 return WC->serv_info->serv_supports_ldap != 0;
522 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP) {
523 if (WC->serv_info == NULL)
525 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
528 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP) {
536 serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
538 StrBuf_ServGetln(Line);
539 if (GetServerStatus(Line, NULL) == 1) {
540 while (!Done && (StrBuf_ServGetln(Line)>=0)) {
541 if ( (StrLength(Line)==3) &&
542 !strcmp(ChrPtr(Line), "000"))
547 StrBufAppendBufPlain(Buf, "\n", 1, 0);
548 StrBufAppendBuf(Buf, Line, 0);
554 FmOut(Line, "center", Buf);
555 StrBufAppendTemplate(Target, TP, Line, 1);
561 void tmplput_site_prefix(StrBuf *Target, WCTemplputParams *TP) {
562 if ((WC != NULL) && (WC->Hdr->HostHeader != NULL)) {
563 StrBufAppendTemplate(Target, TP, WC->Hdr->HostHeader, 0);
567 void RegisterEmbeddableMimeType(const char *MimeType, long MTLen, int Priority) {
569 MT = NewStrBufPlain(MimeType, MTLen);
570 Put(EmbeddableMimes, IKEY(Priority), MT, HFreeStrBuf);
573 void CreateMimeStr(void) {
579 it = GetNewHashPos(EmbeddableMimes, 0);
580 while (GetNextHashPos(EmbeddableMimes, it, &len, &Key, &vMime) &&
582 if (StrLength(EmbeddableMimeStrs) > 0)
583 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("|"), 0);
585 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("MSGP "), 0);
586 StrBufAppendBuf(EmbeddableMimeStrs, (StrBuf*) vMime, 0);
592 ServerStartModule_SERV_FUNC
595 EmbeddableMimes = NewHash(1, Flathash);
596 EmbeddableMimeStrs = NewStrBuf();
601 ServerShutdownModule_SERV_FUNC
604 FreeStrBuf(&EmbeddableMimeStrs);
605 DeleteHash(&EmbeddableMimes);
612 RegisterConditional("COND:SERV:OPENID", 2, conditional_serv_supports_openid, CTX_NONE);
613 RegisterConditional("COND:SERV:NEWU", 2, conditional_serv_newuser_disabled, CTX_NONE);
614 RegisterConditional("COND:SERV:FULLTEXT_ENABLED", 2, conditional_serv_fulltext_enabled, CTX_NONE);
615 RegisterConditional("COND:SERV:LDAP_ENABLED", 2, conditional_serv_ldap_enabled, CTX_NONE);
616 RegisterConditional("COND:SERV:SUPPORTS_GUEST", 2, conditional_serv_supports_guest, CTX_NONE);
617 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, NULL, CTX_NONE);
618 RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, NULL, CTX_NONE);
619 RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, NULL, CTX_NONE);
620 RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, NULL, CTX_NONE);
622 RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, NULL, CTX_NONE);
623 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, NULL, CTX_NONE);
624 RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, NULL, CTX_NONE);
625 RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, NULL, CTX_NONE);
626 RegisterNamespace("SERV:ADMIN", 0, 1, tmplput_serv_admin, NULL, CTX_NONE);
628 RegisterNamespace("SERV:SITE:PREFIX", 0, 1, tmplput_site_prefix, NULL, CTX_NONE);
636 SessionDestroyModule_SERVFUNC
639 DeleteServInfo(&sess->serv_info);