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) {
175 FreeStrBuf(&WCC->ReadBuf);
183 StrBuf_ServGetln(Buf); /* get the server greeting */
184 short_status = GetServerStatus(Buf, &Status);
187 /* Server isn't ready for us? */
188 if (short_status != 2) {
190 hprintf("HTTP/1.1 503 Service Unavailable\r\n");
191 hprintf("Content-type: text/plain; charset=utf-8\r\n");
192 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."));
195 wc_printf("%ld %s\n",
197 _("Received unexpected answer from Citadel server; bailing out.")
199 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
200 hprintf("Content-type: text/plain; charset=utf-8\r\n");
203 end_webcit_session();
208 * From what host is our user connecting? Go with
209 * the host at the other end of the HTTP socket,
210 * unless we are following X-Forwarded-For: headers
211 * and such a header has already turned up something.
213 if ( (!follow_xff) || (StrLength(WCC->Hdr->HR.browser_host) == 0) ) {
214 if (WCC->Hdr->HR.browser_host == NULL) {
215 WCC->Hdr->HR.browser_host = NewStrBuf();
216 Put(WCC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"),
217 WCC->Hdr->HR.browser_host, HFreeStrBuf);
219 locate_host(WCC->Hdr->HR.browser_host, WCC->Hdr->http_sock);
221 if (WCC->serv_info == NULL) {
222 WCC->serv_info = get_serv_info(WCC->Hdr->HR.browser_host, WCC->Hdr->HR.user_agent);
224 if (WCC->serv_info == NULL){
226 wc_printf(_("Received unexpected answer from Citadel server; bailing out."));
227 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
228 hprintf("Content-type: text/plain; charset=utf-8\r\n");
230 end_webcit_session();
233 if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
235 wc_printf(_("You are connected to a Citadel "
236 "server running Citadel %d.%02d. \n"
237 "In order to run this version of WebCit "
238 "you must also have Citadel %d.%02d or"
240 WCC->serv_info->serv_rev_level / 100,
241 WCC->serv_info->serv_rev_level % 100,
242 MINIMUM_CIT_VERSION / 100,
243 MINIMUM_CIT_VERSION % 100
245 hprintf("HTTP/1.1 200 OK\r\n");
246 hprintf("Content-type: text/plain; charset=utf-8\r\n");
248 end_webcit_session();
251 SetInlinMimeRenderers();
257 * Read Citadel variformat text and spit it out as HTML.
258 * align html align string
260 inline void fmout(const char *align)
262 _fmout(WC->WBuf, align);
265 void _fmout(StrBuf *Target, const char *align)
271 StrBufAppendPrintf(Target, "<div align=%s>\n", align);
272 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
274 if ((intext == 1) && (isspace(buf[0]))) {
280 * Quoted text should be displayed in italics and in a
281 * different colour. This code understands Citadel-style
282 * " >" quotes and will convert to <BLOCKQUOTE> tags.
284 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
285 StrBufAppendBufPlain(Target, HKEY("<BLOCKQUOTE>"), 0);
287 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
288 StrBufAppendBufPlain(Target, HKEY("</BLOCKQUOTE>"), 0);
291 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
292 strcpy(buf, &buf[2]);
294 /* Activate embedded URL's */
295 url(buf, sizeof(buf));
298 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
303 wc_printf("</div><br>\n");
306 void FmOut(StrBuf *Target, const char *align, const StrBuf *Source)
308 const char *ptr, *pte;
309 const char *BufPtr = NULL;
310 StrBuf *Line = NewStrBufPlain(NULL, SIZ);
311 StrBuf *Line1 = NewStrBufPlain(NULL, SIZ);
312 StrBuf *Line2 = NewStrBufPlain(NULL, SIZ);
319 StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
321 if (StrLength(Source) > 0)
324 StrBufSipLine(Line, Source, &BufPtr);
328 len = StrLength(Line);
331 if ((intext == 1) && (isspace(*ptr))) {
332 StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
335 if (isspace(*ptr)) while ((ptr < pte) &&
346 * Quoted text should be displayed in italics and in a
347 * different colour. This code understands Citadel-style
348 * " >" quotes and will convert to <BLOCKQUOTE> tags.
350 if (i > 0) StrBufCutLeft(Line, i);
353 for (i = bn; i < bq; i++)
354 StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
355 for (i = bq; i < bn; i++)
356 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
359 if (StrLength(Line) == 0)
362 /* Activate embedded URL's */
363 UrlizeText(Line1, Line, Line2);
365 StrEscAppend(Target, Line1, NULL, 0, 0);
367 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
369 while ((BufPtr != StrBufNOTNULL) &&
372 for (i = 0; i < bn; i++) {
373 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
375 StrBufAppendBufPlain(Target, HKEY("</div><br>\n"), 0);
384 * Transmit message text (in memory) to the server.
386 void text_to_server(char *ptr)
394 while (ptr[pos] != 0) {
398 while ( (isspace(buf[len - 1]))
400 && (buf[1] != '\0') )
404 if (ptr[pos] != 0) strcat(buf, " ");
409 if ((ch == 32) && (strlen(buf) > 200)) {
414 if (strlen(buf) > 250) {
425 * Transmit message text (in memory) to the server, converting to Quoted-Printable encoding as we go.
427 void text_to_server_qp(char *ptr)
429 unsigned char ch, buf[256];
437 while (ptr[pos] != 0) {
438 ch = (unsigned char)(ptr[pos++]);
441 /* ignore carriage returns */
444 /* hard line break */
445 if (output_len > 0) {
446 if (isspace(buf[output_len-1])) {
447 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
451 buf[output_len++] = 0;
452 serv_puts((char *)buf);
456 buf[output_len++] = ch;
458 else if ( (ch >= 32) && (ch <= 60) ) {
459 buf[output_len++] = ch;
461 else if ( (ch >= 62) && (ch <= 126) ) {
462 buf[output_len++] = ch;
465 sprintf((char *)&buf[output_len], "=%02X", ch);
469 if (output_len > 72) {
470 /* soft line break */
471 if (isspace(buf[output_len-1])) {
472 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
475 buf[output_len++] = '=';
476 buf[output_len++] = 0;
477 serv_puts((char *)buf);
482 /* end of data - transmit anything that's left */
483 if (output_len > 0) {
484 if (isspace(buf[output_len-1])) {
485 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
488 buf[output_len++] = 0;
489 serv_puts((char *)buf);
498 * translate server message output to text (used for editing room info files and such)
500 void server_to_text()
506 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
507 if ((buf[0] == 32) && (count > 0)) {
510 wc_printf("%s", buf);
519 * Read text from server, appending to a string buffer until the
520 * usual 000 terminator is found. Caller is responsible for freeing
521 * the returned pointer.
523 int read_server_text(StrBuf *Buf, long *nLines)
531 while ((WCC->serv_sock!=-1) &&
532 (nRead = StrBuf_ServGetln(Buf), (nRead >= 0) ))
534 if (strcmp(ChrPtr(Buf) + nTotal, "000") != 0) {
535 StrBufCutRight(Buf, nRead);
547 int GetServerStatus(StrBuf *Line, long* FullState)
549 if (FullState != NULL)
550 *FullState = StrTol(Line);
551 return ChrPtr(Line)[0] - 48;
555 void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP)
557 StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
560 void tmplput_serv_admin(StrBuf *Target, WCTemplputParams *TP)
563 if (WCC->serv_info == NULL)
565 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_sysadm, 0);
568 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP)
571 if (WCC->serv_info == NULL)
573 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_nodename, 0);
576 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP)
579 if (WCC->serv_info == NULL)
581 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_humannode, 0);
584 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP)
587 if (WCC->serv_info == NULL)
589 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_fqdn, 0);
592 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP)
595 if (WCC->serv_info == NULL)
597 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_software, 0);
600 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP)
603 if (WCC->serv_info == NULL)
605 StrBufAppendPrintf(Target, "%d.%02d",
606 WCC->serv_info->serv_rev_level / 100,
607 WCC->serv_info->serv_rev_level % 100);
609 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP)
612 if (WCC->serv_info == NULL)
614 return WCC->serv_info->serv_newuser_disabled != 0;
617 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP)
620 if (WCC->serv_info == NULL)
622 return WCC->serv_info->serv_supports_openid != 0;
625 int conditional_serv_fulltext_enabled(StrBuf *Target, WCTemplputParams *TP)
628 if (WCC->serv_info == NULL)
630 return WCC->serv_info->serv_fulltext_enabled != 0;
633 int conditional_serv_ldap_enabled(StrBuf *Target, WCTemplputParams *TP)
636 if (WCC->serv_info == NULL)
638 return WCC->serv_info->serv_supports_ldap != 0;
641 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP)
644 if (WCC->serv_info == NULL)
646 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
649 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP)
658 serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
660 StrBuf_ServGetln(Line);
661 if (GetServerStatus(Line, NULL) == 1) {
662 while (!Done && (StrBuf_ServGetln(Line)>=0)) {
663 if ( (StrLength(Line)==3) &&
664 !strcmp(ChrPtr(Line), "000"))
669 StrBufAppendBufPlain(Buf, "\n", 1, 0);
670 StrBufAppendBuf(Buf, Line, 0);
676 FmOut(Line, "center", Buf);
677 StrBufAppendTemplate(Target, TP, Line, 1);
684 void RegisterEmbeddableMimeType(const char *MimeType, long MTLen, int Priority)
687 MT = NewStrBufPlain(MimeType, MTLen);
688 Put(EmbeddableMimes, IKEY(Priority), MT, HFreeStrBuf);
691 void CreateMimeStr(void)
698 it = GetNewHashPos(EmbeddableMimes, 0);
699 while (GetNextHashPos(EmbeddableMimes, it, &len, &Key, &vMime) &&
701 if (StrLength(EmbeddableMimeStrs) > 0)
702 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("|"), 0);
704 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("MSGP "), 0);
705 StrBufAppendBuf(EmbeddableMimeStrs, (StrBuf*) vMime, 0);
711 ServerStartModule_SERV_FUNC
714 EmbeddableMimes = NewHash(1, Flathash);
715 EmbeddableMimeStrs = NewStrBuf();
720 ServerShutdownModule_SERV_FUNC
723 FreeStrBuf(&EmbeddableMimeStrs);
724 DeleteHash(&EmbeddableMimes);
731 is_uds = strcasecmp(ctdlhost, "uds") == 0;
733 snprintf(serv_sock_name, PATH_MAX, "%s/citadel.socket", ctdlport);
735 RegisterConditional(HKEY("COND:SERV:OPENID"), 2, conditional_serv_supports_openid, CTX_NONE);
736 RegisterConditional(HKEY("COND:SERV:NEWU"), 2, conditional_serv_newuser_disabled, CTX_NONE);
737 RegisterConditional(HKEY("COND:SERV:FULLTEXT_ENABLED"), 2, conditional_serv_fulltext_enabled, CTX_NONE);
738 RegisterConditional(HKEY("COND:SERV:LDAP_ENABLED"), 2, conditional_serv_ldap_enabled, CTX_NONE);
739 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, NULL, CTX_NONE);
740 RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, NULL, CTX_NONE);
741 RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, NULL, CTX_NONE);
742 RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, NULL, CTX_NONE);
743 RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, NULL, CTX_NONE);
744 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, NULL, CTX_NONE);
745 RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, NULL, CTX_NONE);
746 RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, NULL, CTX_NONE);
747 RegisterNamespace("SERV:ADMIN", 0, 1, tmplput_serv_admin, NULL, CTX_NONE);
753 SessionDestroyModule_SERVFUNC
756 DeleteServInfo(&sess->serv_info);