6 char serv_sock_name[PATH_MAX] = "";
8 HashList *EmbeddableMimes = NULL;
9 StrBuf *EmbeddableMimeStrs = NULL;
12 void SetInlinMimeRenderers(void)
18 /* Tell the server what kind of richtext we prefer */
19 serv_putbuf(EmbeddableMimeStrs);
20 StrBuf_ServGetln(Buf);
26 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)
57 /* Tell the server what kind of client is connecting */
58 serv_printf("IDEN %d|%d|%d|%s|%s",
65 StrBuf_ServGetln(Buf);
66 if (GetServerStatus(Buf, NULL) != 2) {
67 syslog(0, "get_serv_info(IDEN): unexpected answer [%s]\n",
74 * Tell the server that when we save a calendar event, we
75 * want invitations to be generated by the Citadel server
76 * instead of by the client.
78 serv_puts("ICAL sgi|1");
79 StrBuf_ServGetln(Buf);
80 if (GetServerStatus(Buf, NULL) != 2) {
81 syslog(0, "get_serv_info(ICAL sgi|1): unexpected answer [%s]\n",
87 /* Now ask the server to tell us a little bit about itself... */
89 StrBuf_ServGetln(Buf);
90 if (GetServerStatus(Buf, NULL) != 1) {
91 syslog(0, "get_serv_info(INFO sgi|1): unexpected answer [%s]\n",
97 info = (ServInfo*)malloc(sizeof(ServInfo));
98 memset(info, 0, sizeof(ServInfo));
100 while (rc = StrBuf_ServGetln(Buf),
103 strcmp(ChrPtr(Buf), "000")))
107 info->serv_pid = StrToi(Buf);
108 WC->ctdl_pid = info->serv_pid;
111 info->serv_nodename = NewStrBufDup(Buf);
114 info->serv_humannode = NewStrBufDup(Buf);
117 info->serv_fqdn = NewStrBufDup(Buf);
120 info->serv_software = NewStrBufDup(Buf);
123 info->serv_rev_level = StrToi(Buf);
126 info->serv_bbs_city = NewStrBufDup(Buf);
129 info->serv_sysadm = NewStrBufDup(Buf);
132 info->serv_supports_ldap = StrToi(Buf);
135 info->serv_newuser_disabled = StrToi(Buf);
138 info->serv_default_cal_zone = NewStrBufDup(Buf);
141 info->serv_supports_sieve = StrToi(Buf);
144 info->serv_fulltext_enabled = StrToi(Buf);
147 info->serv_svn_revision = NewStrBufDup(Buf);
150 info->serv_supports_openid = StrToi(Buf);
153 info->serv_supports_guest = StrToi(Buf);
162 int GetConnected (void)
167 if (WCC->ReadBuf == NULL)
168 WCC->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
169 if (is_uds) /* unix domain socket */
170 WCC->serv_sock = uds_connectsock(serv_sock_name);
171 else /* tcp socket */
172 WCC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
174 if (WCC->serv_sock < 0) {
176 FreeStrBuf(&WCC->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(WCC->Hdr->HR.browser_host) == 0) ) {
215 if (WCC->Hdr->HR.browser_host == NULL) {
216 WCC->Hdr->HR.browser_host = NewStrBuf();
217 Put(WCC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"),
218 WCC->Hdr->HR.browser_host, HFreeStrBuf);
220 locate_host(WCC->Hdr->HR.browser_host, WCC->Hdr->http_sock);
222 if (WCC->serv_info == NULL) {
223 WCC->serv_info = get_serv_info(WCC->Hdr->HR.browser_host, WCC->Hdr->HR.user_agent);
225 if (WCC->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 (WCC->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 WCC->serv_info->serv_rev_level / 100,
242 WCC->serv_info->serv_rev_level % 100,
243 MINIMUM_CIT_VERSION / 100,
244 MINIMUM_CIT_VERSION % 100
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)
260 const char *ptr, *pte;
261 const char *BufPtr = NULL;
262 StrBuf *Line = NewStrBufPlain(NULL, SIZ);
263 StrBuf *Line1 = NewStrBufPlain(NULL, SIZ);
264 StrBuf *Line2 = NewStrBufPlain(NULL, SIZ);
271 StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
273 if (StrLength(Source) > 0)
276 StrBufSipLine(Line, Source, &BufPtr);
280 len = StrLength(Line);
283 if ((intext == 1) && (isspace(*ptr))) {
284 StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
287 if (isspace(*ptr)) while ((ptr < pte) &&
298 * Quoted text should be displayed in italics and in a
299 * different colour. This code understands Citadel-style
300 * " >" quotes and will convert to <BLOCKQUOTE> tags.
302 if (i > 0) StrBufCutLeft(Line, i);
305 for (i = bn; i < bq; i++)
306 StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
307 for (i = bq; i < bn; i++)
308 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
311 if (StrLength(Line) == 0)
314 /* Activate embedded URL's */
315 UrlizeText(Line1, Line, Line2);
317 StrEscAppend(Target, Line1, NULL, 0, 0);
319 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
321 while ((BufPtr != StrBufNOTNULL) &&
324 for (i = 0; i < bn; i++) {
325 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
327 StrBufAppendBufPlain(Target, HKEY("</div><br>\n"), 0);
336 * Transmit message text (in memory) to the server.
338 void text_to_server(char *ptr)
346 while (ptr[pos] != 0) {
350 while ( (isspace(buf[len - 1]))
352 && (buf[1] != '\0') )
356 if (ptr[pos] != 0) strcat(buf, " ");
361 if ((ch == 32) && (strlen(buf) > 200)) {
366 if (strlen(buf) > 250) {
377 * Transmit message text (in memory) to the server, converting to Quoted-Printable encoding as we go.
379 void text_to_server_qp(char *ptr)
381 unsigned char ch, buf[256];
389 while (ptr[pos] != 0) {
390 ch = (unsigned char)(ptr[pos++]);
393 /* ignore carriage returns */
396 /* hard line break */
397 if (output_len > 0) {
398 if (isspace(buf[output_len-1])) {
399 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
403 buf[output_len++] = 0;
404 serv_puts((char *)buf);
408 buf[output_len++] = ch;
410 else if ( (ch >= 32) && (ch <= 60) ) {
411 buf[output_len++] = ch;
413 else if ( (ch >= 62) && (ch <= 126) ) {
414 buf[output_len++] = ch;
417 sprintf((char *)&buf[output_len], "=%02X", ch);
421 if (output_len > 72) {
422 /* soft line break */
423 if (isspace(buf[output_len-1])) {
424 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
427 buf[output_len++] = '=';
428 buf[output_len++] = 0;
429 serv_puts((char *)buf);
434 /* end of data - transmit anything that's left */
435 if (output_len > 0) {
436 if (isspace(buf[output_len-1])) {
437 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
440 buf[output_len++] = 0;
441 serv_puts((char *)buf);
450 * translate server message output to text (used for editing room info files and such)
452 void server_to_text()
458 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
459 if ((buf[0] == 32) && (count > 0)) {
462 wc_printf("%s", buf);
471 * Read text from server, appending to a string buffer until the
472 * usual 000 terminator is found. Caller is responsible for freeing
473 * the returned pointer.
475 int read_server_text(StrBuf *Buf, long *nLines)
484 ReadBuf = NewStrBuf();
485 while ((WCC->serv_sock!=-1) &&
486 (nRead = StrBuf_ServGetln(ReadBuf), (nRead >= 0) &&
487 ((nRead != 3)||(strcmp(ChrPtr(ReadBuf), "000") != 0))))
489 StrBufAppendBuf(Buf, ReadBuf, 0);
490 StrBufAppendBufPlain(Buf, HKEY("\n"), 0);
494 FreeStrBuf(&ReadBuf);
500 int GetServerStatusMsg(StrBuf *Line, long* FullState, int PutImportantMessage, int MajorOK)
503 if (FullState != NULL)
504 *FullState = StrTol(Line);
505 rc = ChrPtr(Line)[0] - 48;
506 if ((!PutImportantMessage) ||
508 (StrLength(Line) <= 4))
511 AppendImportantMessage(ChrPtr(Line) + 4, StrLength(Line) - 4);
516 void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP)
518 StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
521 void tmplput_serv_admin(StrBuf *Target, WCTemplputParams *TP)
524 if (WCC->serv_info == NULL)
526 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_sysadm, 0);
529 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP)
532 if (WCC->serv_info == NULL)
534 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_nodename, 0);
537 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP)
540 if (WCC->serv_info == NULL)
542 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_humannode, 0);
545 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP)
548 if (WCC->serv_info == NULL)
550 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_fqdn, 0);
553 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP)
556 if (WCC->serv_info == NULL)
558 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_software, 0);
561 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP)
564 if (WCC->serv_info == NULL)
566 StrBufAppendPrintf(Target, "%d.%02d",
567 WCC->serv_info->serv_rev_level / 100,
568 WCC->serv_info->serv_rev_level % 100);
570 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP)
573 if (WCC->serv_info == NULL)
575 return WCC->serv_info->serv_newuser_disabled != 0;
578 int conditional_serv_supports_guest(StrBuf *Target, WCTemplputParams *TP)
581 if (WCC->serv_info == NULL)
583 return WCC->serv_info->serv_supports_guest != 0;
586 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP)
589 if (WCC->serv_info == NULL)
591 return WCC->serv_info->serv_supports_openid != 0;
594 int conditional_serv_fulltext_enabled(StrBuf *Target, WCTemplputParams *TP)
597 if (WCC->serv_info == NULL)
599 return WCC->serv_info->serv_fulltext_enabled != 0;
602 int conditional_serv_ldap_enabled(StrBuf *Target, WCTemplputParams *TP)
605 if (WCC->serv_info == NULL)
607 return WCC->serv_info->serv_supports_ldap != 0;
610 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP)
613 if (WCC->serv_info == NULL)
615 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
618 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP)
627 serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
629 StrBuf_ServGetln(Line);
630 if (GetServerStatus(Line, NULL) == 1) {
631 while (!Done && (StrBuf_ServGetln(Line)>=0)) {
632 if ( (StrLength(Line)==3) &&
633 !strcmp(ChrPtr(Line), "000"))
638 StrBufAppendBufPlain(Buf, "\n", 1, 0);
639 StrBufAppendBuf(Buf, Line, 0);
645 FmOut(Line, "center", Buf);
646 StrBufAppendTemplate(Target, TP, Line, 1);
653 void RegisterEmbeddableMimeType(const char *MimeType, long MTLen, int Priority)
656 MT = NewStrBufPlain(MimeType, MTLen);
657 Put(EmbeddableMimes, IKEY(Priority), MT, HFreeStrBuf);
660 void CreateMimeStr(void)
667 it = GetNewHashPos(EmbeddableMimes, 0);
668 while (GetNextHashPos(EmbeddableMimes, it, &len, &Key, &vMime) &&
670 if (StrLength(EmbeddableMimeStrs) > 0)
671 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("|"), 0);
673 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("MSGP "), 0);
674 StrBufAppendBuf(EmbeddableMimeStrs, (StrBuf*) vMime, 0);
680 ServerStartModule_SERV_FUNC
683 EmbeddableMimes = NewHash(1, Flathash);
684 EmbeddableMimeStrs = NewStrBuf();
689 ServerShutdownModule_SERV_FUNC
692 FreeStrBuf(&EmbeddableMimeStrs);
693 DeleteHash(&EmbeddableMimes);
700 is_uds = strcasecmp(ctdlhost, "uds") == 0;
702 snprintf(serv_sock_name, PATH_MAX, "%s/citadel.socket", ctdlport);
704 RegisterConditional(HKEY("COND:SERV:OPENID"), 2, conditional_serv_supports_openid, CTX_NONE);
705 RegisterConditional(HKEY("COND:SERV:NEWU"), 2, conditional_serv_newuser_disabled, CTX_NONE);
706 RegisterConditional(HKEY("COND:SERV:FULLTEXT_ENABLED"), 2, conditional_serv_fulltext_enabled, CTX_NONE);
707 RegisterConditional(HKEY("COND:SERV:LDAP_ENABLED"), 2, conditional_serv_ldap_enabled, CTX_NONE);
708 RegisterConditional(HKEY("COND:SERV:SUPPORTS_GUEST"), 2, conditional_serv_supports_guest, CTX_NONE);
709 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, NULL, CTX_NONE);
710 RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, NULL, CTX_NONE);
711 RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, NULL, CTX_NONE);
712 RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, NULL, CTX_NONE);
713 RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, NULL, CTX_NONE);
714 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, NULL, CTX_NONE);
715 RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, NULL, CTX_NONE);
716 RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, NULL, CTX_NONE);
717 RegisterNamespace("SERV:ADMIN", 0, 1, tmplput_serv_admin, NULL, CTX_NONE);
723 SessionDestroyModule_SERVFUNC
726 DeleteServInfo(&sess->serv_info);