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 * Read Citadel variformat text and spit it out as HTML.
259 * align html align string
261 inline void fmout(const char *align)
263 _fmout(WC->WBuf, align);
266 void _fmout(StrBuf *Target, const char *align)
272 StrBufAppendPrintf(Target, "<div align=%s>\n", align);
273 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
275 if ((intext == 1) && (isspace(buf[0]))) {
281 * Quoted text should be displayed in italics and in a
282 * different colour. This code understands Citadel-style
283 * " >" quotes and will convert to <BLOCKQUOTE> tags.
285 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
286 StrBufAppendBufPlain(Target, HKEY("<BLOCKQUOTE>"), 0);
288 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
289 StrBufAppendBufPlain(Target, HKEY("</BLOCKQUOTE>"), 0);
292 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
293 strcpy(buf, &buf[2]);
295 /* Activate embedded URL's */
296 url(buf, sizeof(buf));
299 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
304 wc_printf("</div><br>\n");
307 void FmOut(StrBuf *Target, const char *align, const StrBuf *Source)
309 const char *ptr, *pte;
310 const char *BufPtr = NULL;
311 StrBuf *Line = NewStrBufPlain(NULL, SIZ);
312 StrBuf *Line1 = NewStrBufPlain(NULL, SIZ);
313 StrBuf *Line2 = NewStrBufPlain(NULL, SIZ);
320 StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
322 if (StrLength(Source) > 0)
325 StrBufSipLine(Line, Source, &BufPtr);
329 len = StrLength(Line);
332 if ((intext == 1) && (isspace(*ptr))) {
333 StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
336 if (isspace(*ptr)) while ((ptr < pte) &&
347 * Quoted text should be displayed in italics and in a
348 * different colour. This code understands Citadel-style
349 * " >" quotes and will convert to <BLOCKQUOTE> tags.
351 if (i > 0) StrBufCutLeft(Line, i);
354 for (i = bn; i < bq; i++)
355 StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
356 for (i = bq; i < bn; i++)
357 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
360 if (StrLength(Line) == 0)
363 /* Activate embedded URL's */
364 UrlizeText(Line1, Line, Line2);
366 StrEscAppend(Target, Line1, NULL, 0, 0);
368 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
370 while ((BufPtr != StrBufNOTNULL) &&
373 for (i = 0; i < bn; i++) {
374 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
376 StrBufAppendBufPlain(Target, HKEY("</div><br>\n"), 0);
385 * Transmit message text (in memory) to the server.
387 void text_to_server(char *ptr)
395 while (ptr[pos] != 0) {
399 while ( (isspace(buf[len - 1]))
401 && (buf[1] != '\0') )
405 if (ptr[pos] != 0) strcat(buf, " ");
410 if ((ch == 32) && (strlen(buf) > 200)) {
415 if (strlen(buf) > 250) {
426 * Transmit message text (in memory) to the server, converting to Quoted-Printable encoding as we go.
428 void text_to_server_qp(char *ptr)
430 unsigned char ch, buf[256];
438 while (ptr[pos] != 0) {
439 ch = (unsigned char)(ptr[pos++]);
442 /* ignore carriage returns */
445 /* hard line break */
446 if (output_len > 0) {
447 if (isspace(buf[output_len-1])) {
448 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
452 buf[output_len++] = 0;
453 serv_puts((char *)buf);
457 buf[output_len++] = ch;
459 else if ( (ch >= 32) && (ch <= 60) ) {
460 buf[output_len++] = ch;
462 else if ( (ch >= 62) && (ch <= 126) ) {
463 buf[output_len++] = ch;
466 sprintf((char *)&buf[output_len], "=%02X", ch);
470 if (output_len > 72) {
471 /* soft line break */
472 if (isspace(buf[output_len-1])) {
473 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
476 buf[output_len++] = '=';
477 buf[output_len++] = 0;
478 serv_puts((char *)buf);
483 /* end of data - transmit anything that's left */
484 if (output_len > 0) {
485 if (isspace(buf[output_len-1])) {
486 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
489 buf[output_len++] = 0;
490 serv_puts((char *)buf);
499 * translate server message output to text (used for editing room info files and such)
501 void server_to_text()
507 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
508 if ((buf[0] == 32) && (count > 0)) {
511 wc_printf("%s", buf);
520 * Read text from server, appending to a string buffer until the
521 * usual 000 terminator is found. Caller is responsible for freeing
522 * the returned pointer.
524 int read_server_text(StrBuf *Buf, long *nLines)
532 while ((WCC->serv_sock!=-1) &&
533 (nRead = StrBuf_ServGetln(Buf), (nRead >= 0) ))
535 if (strcmp(ChrPtr(Buf) + nTotal, "000") != 0) {
536 StrBufCutRight(Buf, nRead);
548 int GetServerStatusMsg(StrBuf *Line, long* FullState, int PutImportantMessage, int MajorOK)
551 if (FullState != NULL)
552 *FullState = StrTol(Line);
553 rc = ChrPtr(Line)[0] - 48;
554 if ((!PutImportantMessage) ||
556 (StrLength(Line) <= 4))
559 AppendImportantMessage(ChrPtr(Line) + 4, StrLength(Line) - 4);
564 void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP)
566 StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
569 void tmplput_serv_admin(StrBuf *Target, WCTemplputParams *TP)
572 if (WCC->serv_info == NULL)
574 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_sysadm, 0);
577 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP)
580 if (WCC->serv_info == NULL)
582 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_nodename, 0);
585 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP)
588 if (WCC->serv_info == NULL)
590 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_humannode, 0);
593 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP)
596 if (WCC->serv_info == NULL)
598 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_fqdn, 0);
601 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP)
604 if (WCC->serv_info == NULL)
606 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_software, 0);
609 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP)
612 if (WCC->serv_info == NULL)
614 StrBufAppendPrintf(Target, "%d.%02d",
615 WCC->serv_info->serv_rev_level / 100,
616 WCC->serv_info->serv_rev_level % 100);
618 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP)
621 if (WCC->serv_info == NULL)
623 return WCC->serv_info->serv_newuser_disabled != 0;
626 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP)
629 if (WCC->serv_info == NULL)
631 return WCC->serv_info->serv_supports_openid != 0;
634 int conditional_serv_fulltext_enabled(StrBuf *Target, WCTemplputParams *TP)
637 if (WCC->serv_info == NULL)
639 return WCC->serv_info->serv_fulltext_enabled != 0;
642 int conditional_serv_ldap_enabled(StrBuf *Target, WCTemplputParams *TP)
645 if (WCC->serv_info == NULL)
647 return WCC->serv_info->serv_supports_ldap != 0;
650 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP)
653 if (WCC->serv_info == NULL)
655 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
658 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP)
667 serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
669 StrBuf_ServGetln(Line);
670 if (GetServerStatus(Line, NULL) == 1) {
671 while (!Done && (StrBuf_ServGetln(Line)>=0)) {
672 if ( (StrLength(Line)==3) &&
673 !strcmp(ChrPtr(Line), "000"))
678 StrBufAppendBufPlain(Buf, "\n", 1, 0);
679 StrBufAppendBuf(Buf, Line, 0);
685 FmOut(Line, "center", Buf);
686 StrBufAppendTemplate(Target, TP, Line, 1);
693 void RegisterEmbeddableMimeType(const char *MimeType, long MTLen, int Priority)
696 MT = NewStrBufPlain(MimeType, MTLen);
697 Put(EmbeddableMimes, IKEY(Priority), MT, HFreeStrBuf);
700 void CreateMimeStr(void)
707 it = GetNewHashPos(EmbeddableMimes, 0);
708 while (GetNextHashPos(EmbeddableMimes, it, &len, &Key, &vMime) &&
710 if (StrLength(EmbeddableMimeStrs) > 0)
711 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("|"), 0);
713 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("MSGP "), 0);
714 StrBufAppendBuf(EmbeddableMimeStrs, (StrBuf*) vMime, 0);
720 ServerStartModule_SERV_FUNC
723 EmbeddableMimes = NewHash(1, Flathash);
724 EmbeddableMimeStrs = NewStrBuf();
729 ServerShutdownModule_SERV_FUNC
732 FreeStrBuf(&EmbeddableMimeStrs);
733 DeleteHash(&EmbeddableMimes);
740 is_uds = strcasecmp(ctdlhost, "uds") == 0;
742 snprintf(serv_sock_name, PATH_MAX, "%s/citadel.socket", ctdlport);
744 RegisterConditional(HKEY("COND:SERV:OPENID"), 2, conditional_serv_supports_openid, CTX_NONE);
745 RegisterConditional(HKEY("COND:SERV:NEWU"), 2, conditional_serv_newuser_disabled, CTX_NONE);
746 RegisterConditional(HKEY("COND:SERV:FULLTEXT_ENABLED"), 2, conditional_serv_fulltext_enabled, CTX_NONE);
747 RegisterConditional(HKEY("COND:SERV:LDAP_ENABLED"), 2, conditional_serv_ldap_enabled, CTX_NONE);
748 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, NULL, CTX_NONE);
749 RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, NULL, CTX_NONE);
750 RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, NULL, CTX_NONE);
751 RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, NULL, CTX_NONE);
752 RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, NULL, CTX_NONE);
753 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, NULL, CTX_NONE);
754 RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, NULL, CTX_NONE);
755 RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, NULL, CTX_NONE);
756 RegisterNamespace("SERV:ADMIN", 0, 1, tmplput_serv_admin, NULL, CTX_NONE);
762 SessionDestroyModule_SERVFUNC
765 DeleteServInfo(&sess->serv_info);