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(char *align)
262 _fmout(WC->WBuf, align);
265 void _fmout(StrBuf *Target, 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, char *align, 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);
383 * Read Citadel variformat text and spit it out as HTML in a form
384 * suitable for embedding in another message (forward/quote).
385 * (NO LINEBREAKS ALLOWED HERE!)
387 void pullquote_fmout(void) {
392 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
394 if ((intext == 1) && (isspace(buf[0]))) {
400 * Quoted text should be displayed in italics and in a
401 * different colour. This code understands Citadel-style
402 * " >" quotes and will convert to <BLOCKQUOTE> tags.
404 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
405 wc_printf("<BLOCKQUOTE>");
407 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
408 wc_printf("</BLOCKQUOTE>");
411 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
412 strcpy(buf, &buf[2]);
426 * Transmit message text (in memory) to the server.
428 void text_to_server(char *ptr)
436 while (ptr[pos] != 0) {
440 while ( (isspace(buf[len - 1]))
442 && (buf[1] != '\0') )
446 if (ptr[pos] != 0) strcat(buf, " ");
451 if ((ch == 32) && (strlen(buf) > 200)) {
456 if (strlen(buf) > 250) {
467 * Transmit message text (in memory) to the server, converting to Quoted-Printable encoding as we go.
469 void text_to_server_qp(char *ptr)
471 unsigned char ch, buf[256];
479 while (ptr[pos] != 0) {
480 ch = (unsigned char)(ptr[pos++]);
483 /* ignore carriage returns */
486 /* hard line break */
487 if (output_len > 0) {
488 if (isspace(buf[output_len-1])) {
489 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
493 buf[output_len++] = 0;
494 serv_puts((char *)buf);
498 buf[output_len++] = ch;
500 else if ( (ch >= 32) && (ch <= 60) ) {
501 buf[output_len++] = ch;
503 else if ( (ch >= 62) && (ch <= 126) ) {
504 buf[output_len++] = ch;
507 sprintf((char *)&buf[output_len], "=%02X", ch);
511 if (output_len > 72) {
512 /* soft line break */
513 if (isspace(buf[output_len-1])) {
514 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
517 buf[output_len++] = '=';
518 buf[output_len++] = 0;
519 serv_puts((char *)buf);
524 /* end of data - transmit anything that's left */
525 if (output_len > 0) {
526 if (isspace(buf[output_len-1])) {
527 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
530 buf[output_len++] = 0;
531 serv_puts((char *)buf);
540 * translate server message output to text (used for editing room info files and such)
542 void server_to_text()
548 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
549 if ((buf[0] == 32) && (count > 0)) {
552 wc_printf("%s", buf);
561 * Read text from server, appending to a string buffer until the
562 * usual 000 terminator is found. Caller is responsible for freeing
563 * the returned pointer.
565 int read_server_text(StrBuf *Buf, long *nLines)
573 while ((WCC->serv_sock!=-1) &&
574 (nRead = StrBuf_ServGetln(Buf), (nRead >= 0) ))
576 if (strcmp(ChrPtr(Buf) + nTotal, "000") != 0) {
577 StrBufCutRight(Buf, nRead);
589 int GetServerStatus(StrBuf *Line, long* FullState)
591 if (FullState != NULL)
592 *FullState = StrTol(Line);
593 return ChrPtr(Line)[0] - 48;
597 void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP)
599 StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
602 void tmplput_serv_admin(StrBuf *Target, WCTemplputParams *TP)
605 if (WCC->serv_info == NULL)
607 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_sysadm, 0);
610 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP)
613 if (WCC->serv_info == NULL)
615 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_nodename, 0);
618 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP)
621 if (WCC->serv_info == NULL)
623 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_humannode, 0);
626 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP)
629 if (WCC->serv_info == NULL)
631 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_fqdn, 0);
634 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP)
637 if (WCC->serv_info == NULL)
639 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_software, 0);
642 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP)
645 if (WCC->serv_info == NULL)
647 StrBufAppendPrintf(Target, "%d.%02d",
648 WCC->serv_info->serv_rev_level / 100,
649 WCC->serv_info->serv_rev_level % 100);
651 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP)
654 if (WCC->serv_info == NULL)
656 return WCC->serv_info->serv_newuser_disabled != 0;
659 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP)
662 if (WCC->serv_info == NULL)
664 return WCC->serv_info->serv_supports_openid != 0;
667 int conditional_serv_fulltext_enabled(StrBuf *Target, WCTemplputParams *TP)
670 if (WCC->serv_info == NULL)
672 return WCC->serv_info->serv_fulltext_enabled != 0;
675 int conditional_serv_ldap_enabled(StrBuf *Target, WCTemplputParams *TP)
678 if (WCC->serv_info == NULL)
680 return WCC->serv_info->serv_supports_ldap != 0;
683 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP)
686 if (WCC->serv_info == NULL)
688 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
691 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP)
700 serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
702 StrBuf_ServGetln(Line);
703 if (GetServerStatus(Line, NULL) == 1) {
704 while (!Done && (StrBuf_ServGetln(Line)>=0)) {
705 if ( (StrLength(Line)==3) &&
706 !strcmp(ChrPtr(Line), "000"))
711 StrBufAppendBufPlain(Buf, "\n", 1, 0);
712 StrBufAppendBuf(Buf, Line, 0);
718 FmOut(Line, "center", Buf);
719 StrBufAppendTemplate(Target, TP, Line, 1);
726 void RegisterEmbeddableMimeType(const char *MimeType, long MTLen, int Priority)
729 MT = NewStrBufPlain(MimeType, MTLen);
730 Put(EmbeddableMimes, IKEY(Priority), MT, HFreeStrBuf);
733 void CreateMimeStr(void)
740 it = GetNewHashPos(EmbeddableMimes, 0);
741 while (GetNextHashPos(EmbeddableMimes, it, &len, &Key, &vMime) &&
743 if (StrLength(EmbeddableMimeStrs) > 0)
744 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("|"), 0);
746 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("MSGP "), 0);
747 StrBufAppendBuf(EmbeddableMimeStrs, (StrBuf*) vMime, 0);
753 ServerStartModule_SERV_FUNC
756 EmbeddableMimes = NewHash(1, Flathash);
757 EmbeddableMimeStrs = NewStrBuf();
762 ServerShutdownModule_SERV_FUNC
765 FreeStrBuf(&EmbeddableMimeStrs);
766 DeleteHash(&EmbeddableMimes);
773 is_uds = strcasecmp(ctdlhost, "uds") == 0;
775 snprintf(serv_sock_name, PATH_MAX, "%s/citadel.socket", ctdlport);
777 RegisterConditional(HKEY("COND:SERV:OPENID"), 2, conditional_serv_supports_openid, CTX_NONE);
778 RegisterConditional(HKEY("COND:SERV:NEWU"), 2, conditional_serv_newuser_disabled, CTX_NONE);
779 RegisterConditional(HKEY("COND:SERV:FULLTEXT_ENABLED"), 2, conditional_serv_fulltext_enabled, CTX_NONE);
780 RegisterConditional(HKEY("COND:SERV:LDAP_ENABLED"), 2, conditional_serv_ldap_enabled, CTX_NONE);
781 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, NULL, CTX_NONE);
782 RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, NULL, CTX_NONE);
783 RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, NULL, CTX_NONE);
784 RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, NULL, CTX_NONE);
785 RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, NULL, CTX_NONE);
786 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, NULL, CTX_NONE);
787 RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, NULL, CTX_NONE);
788 RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, NULL, CTX_NONE);
789 RegisterNamespace("SERV:ADMIN", 1, 2, tmplput_serv_admin, NULL, CTX_NONE);
795 SessionDestroyModule_SERVFUNC
798 DeleteServInfo(&sess->serv_info);