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(LOG_WARNING, "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(LOG_WARNING, "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(LOG_WARNING, "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(const StrBuf *SendMeEncoded)
383 ServBuf = StrBufRFC2047encodeMessage(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()
400 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
401 if ((buf[0] == 32) && (count > 0)) {
404 wc_printf("%s", buf);
413 * Read text from server, appending to a string buffer until the
414 * usual 000 terminator is found. Caller is responsible for freeing
415 * the returned pointer.
417 int read_server_text(StrBuf *Buf, long *nLines)
426 ReadBuf = NewStrBuf();
427 while ((WCC->serv_sock!=-1) &&
428 (nRead = StrBuf_ServGetln(ReadBuf), (nRead >= 0) &&
429 ((nRead != 3)||(strcmp(ChrPtr(ReadBuf), "000") != 0))))
431 StrBufAppendBuf(Buf, ReadBuf, 0);
432 StrBufAppendBufPlain(Buf, HKEY("\n"), 0);
436 FreeStrBuf(&ReadBuf);
442 int GetServerStatusMsg(StrBuf *Line, long* FullState, int PutImportantMessage, int MajorOK)
445 if (FullState != NULL)
446 *FullState = StrTol(Line);
447 rc = ChrPtr(Line)[0] - 48;
448 if ((!PutImportantMessage) ||
450 (StrLength(Line) <= 4))
453 AppendImportantMessage(ChrPtr(Line) + 4, StrLength(Line) - 4);
458 void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP)
460 StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
463 void tmplput_serv_admin(StrBuf *Target, WCTemplputParams *TP)
466 if (WCC->serv_info == NULL)
468 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_sysadm, 0);
471 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP)
474 if (WCC->serv_info == NULL)
476 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_nodename, 0);
479 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP)
482 if (WCC->serv_info == NULL)
484 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_humannode, 0);
487 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP)
490 if (WCC->serv_info == NULL)
492 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_fqdn, 0);
495 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP)
498 if (WCC->serv_info == NULL)
500 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_software, 0);
503 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP)
506 if (WCC->serv_info == NULL)
508 StrBufAppendPrintf(Target, "%d.%02d",
509 WCC->serv_info->serv_rev_level / 100,
510 WCC->serv_info->serv_rev_level % 100);
512 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP)
515 if (WCC->serv_info == NULL)
517 return WCC->serv_info->serv_newuser_disabled != 0;
520 int conditional_serv_supports_guest(StrBuf *Target, WCTemplputParams *TP)
523 if (WCC->serv_info == NULL)
525 return WCC->serv_info->serv_supports_guest != 0;
528 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP)
531 if (WCC->serv_info == NULL)
533 return WCC->serv_info->serv_supports_openid != 0;
536 int conditional_serv_fulltext_enabled(StrBuf *Target, WCTemplputParams *TP)
539 if (WCC->serv_info == NULL)
541 return WCC->serv_info->serv_fulltext_enabled != 0;
544 int conditional_serv_ldap_enabled(StrBuf *Target, WCTemplputParams *TP)
547 if (WCC->serv_info == NULL)
549 return WCC->serv_info->serv_supports_ldap != 0;
552 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP)
555 if (WCC->serv_info == NULL)
557 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
560 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP)
569 serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
571 StrBuf_ServGetln(Line);
572 if (GetServerStatus(Line, NULL) == 1) {
573 while (!Done && (StrBuf_ServGetln(Line)>=0)) {
574 if ( (StrLength(Line)==3) &&
575 !strcmp(ChrPtr(Line), "000"))
580 StrBufAppendBufPlain(Buf, "\n", 1, 0);
581 StrBufAppendBuf(Buf, Line, 0);
587 FmOut(Line, "center", Buf);
588 StrBufAppendTemplate(Target, TP, Line, 1);
595 void RegisterEmbeddableMimeType(const char *MimeType, long MTLen, int Priority)
598 MT = NewStrBufPlain(MimeType, MTLen);
599 Put(EmbeddableMimes, IKEY(Priority), MT, HFreeStrBuf);
602 void CreateMimeStr(void)
609 it = GetNewHashPos(EmbeddableMimes, 0);
610 while (GetNextHashPos(EmbeddableMimes, it, &len, &Key, &vMime) &&
612 if (StrLength(EmbeddableMimeStrs) > 0)
613 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("|"), 0);
615 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("MSGP "), 0);
616 StrBufAppendBuf(EmbeddableMimeStrs, (StrBuf*) vMime, 0);
622 ServerStartModule_SERV_FUNC
625 EmbeddableMimes = NewHash(1, Flathash);
626 EmbeddableMimeStrs = NewStrBuf();
631 ServerShutdownModule_SERV_FUNC
634 FreeStrBuf(&EmbeddableMimeStrs);
635 DeleteHash(&EmbeddableMimes);
642 is_uds = strcasecmp(ctdlhost, "uds") == 0;
644 snprintf(serv_sock_name, PATH_MAX, "%s/citadel.socket", ctdlport);
646 RegisterConditional("COND:SERV:OPENID", 2, conditional_serv_supports_openid, CTX_NONE);
647 RegisterConditional("COND:SERV:NEWU", 2, conditional_serv_newuser_disabled, CTX_NONE);
648 RegisterConditional("COND:SERV:FULLTEXT_ENABLED", 2, conditional_serv_fulltext_enabled, CTX_NONE);
649 RegisterConditional("COND:SERV:LDAP_ENABLED", 2, conditional_serv_ldap_enabled, CTX_NONE);
650 RegisterConditional("COND:SERV:SUPPORTS_GUEST", 2, conditional_serv_supports_guest, CTX_NONE);
651 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, NULL, CTX_NONE);
652 RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, NULL, CTX_NONE);
653 RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, NULL, CTX_NONE);
654 RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, NULL, CTX_NONE);
655 RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, NULL, CTX_NONE);
656 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, NULL, CTX_NONE);
657 RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, NULL, CTX_NONE);
658 RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, NULL, CTX_NONE);
659 RegisterNamespace("SERV:ADMIN", 0, 1, tmplput_serv_admin, NULL, CTX_NONE);
665 SessionDestroyModule_SERVFUNC
668 DeleteServInfo(&sess->serv_info);