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)
56 /* Tell the server what kind of client is connecting */
57 serv_printf("IDEN %d|%d|%d|%s|%s",
64 StrBuf_ServGetln(Buf);
65 if (GetServerStatus(Buf, NULL) != 2) {
66 lprintf(0, "get_serv_info(IDEN): unexpected answer [%s]\n",
73 * Tell the server that when we save a calendar event, we
74 * want invitations to be generated by the Citadel server
75 * instead of by the client.
77 serv_puts("ICAL sgi|1");
78 StrBuf_ServGetln(Buf);
79 if (GetServerStatus(Buf, NULL) != 2) {
80 lprintf(0, "get_serv_info(ICAL sgi|1): unexpected answer [%s]\n",
86 /* Now ask the server to tell us a little bit about itself... */
88 StrBuf_ServGetln(Buf);
89 if (GetServerStatus(Buf, NULL) != 1) {
90 lprintf(0, "get_serv_info(INFO sgi|1): unexpected answer [%s]\n",
96 info = (ServInfo*)malloc(sizeof(ServInfo));
97 memset(info, 0, sizeof(ServInfo));
99 while (StrBuf_ServGetln(Buf), (strcmp(ChrPtr(Buf), "000")!= 0)) {
102 info->serv_pid = StrToi(Buf);
103 WC->ctdl_pid = info->serv_pid;
106 info->serv_nodename = NewStrBufDup(Buf);
109 info->serv_humannode = NewStrBufDup(Buf);
112 info->serv_fqdn = NewStrBufDup(Buf);
115 info->serv_software = NewStrBufDup(Buf);
118 info->serv_rev_level = StrToi(Buf);
121 info->serv_bbs_city = NewStrBufDup(Buf);
124 info->serv_sysadm = NewStrBufDup(Buf);
127 info->serv_supports_ldap = StrToi(Buf);
130 info->serv_newuser_disabled = StrToi(Buf);
133 info->serv_default_cal_zone = NewStrBufDup(Buf);
136 info->serv_supports_sieve = StrToi(Buf);
139 info->serv_fulltext_enabled = StrToi(Buf);
142 info->serv_svn_revision = NewStrBufDup(Buf);
145 info->serv_supports_openid = StrToi(Buf);
148 info->serv_supports_guest = StrToi(Buf);
157 int GetConnected (void)
162 if (WCC->ReadBuf == NULL)
163 WCC->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
164 if (is_uds) /* unix domain socket */
165 WCC->serv_sock = uds_connectsock(serv_sock_name);
166 else /* tcp socket */
167 WCC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
169 if (WCC->serv_sock < 0) {
171 FreeStrBuf(&WCC->ReadBuf);
179 StrBuf_ServGetln(Buf); /* get the server greeting */
180 short_status = GetServerStatus(Buf, &Status);
183 /* Server isn't ready for us? */
184 if (short_status != 2) {
186 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."));
189 wc_printf("%ld %s\n",
191 _("Received unexpected answer from Citadel server; bailing out.")
195 end_webcit_session();
199 /* 2010jun03: every now and then the connection to Citadel dies before this point. why? */
202 * From what host is our user connecting? Go with
203 * the host at the other end of the HTTP socket,
204 * unless we are following X-Forwarded-For: headers
205 * and such a header has already turned up something.
207 if ( (!follow_xff) || (StrLength(WCC->Hdr->HR.browser_host) == 0) ) {
208 if (WCC->Hdr->HR.browser_host == NULL) {
209 WCC->Hdr->HR.browser_host = NewStrBuf();
210 Put(WCC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"),
211 WCC->Hdr->HR.browser_host, HFreeStrBuf);
213 locate_host(WCC->Hdr->HR.browser_host, WCC->Hdr->http_sock);
215 if (WCC->serv_info == NULL) {
216 WCC->serv_info = get_serv_info(WCC->Hdr->HR.browser_host, WCC->Hdr->HR.user_agent);
218 if (WCC->serv_info == NULL){
220 wc_printf(_("Received unexpected answer from Citadel server; bailing out."));
221 hprintf("HTTP/1.1 200 OK\r\n");
222 hprintf("Content-type: text/plain; charset=utf-8\r\n");
224 end_webcit_session();
227 if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
229 wc_printf(_("You are connected to a Citadel "
230 "server running Citadel %d.%02d. \n"
231 "In order to run this version of WebCit "
232 "you must also have Citadel %d.%02d or"
234 WCC->serv_info->serv_rev_level / 100,
235 WCC->serv_info->serv_rev_level % 100,
236 MINIMUM_CIT_VERSION / 100,
237 MINIMUM_CIT_VERSION % 100
239 hprintf("HTTP/1.1 200 OK\r\n");
240 hprintf("Content-type: text/plain; charset=utf-8\r\n");
242 end_webcit_session();
245 SetInlinMimeRenderers();
251 * Read Citadel variformat text and spit it out as HTML.
252 * align html align string
254 inline void fmout(char *align)
256 _fmout(WC->WBuf, align);
259 void _fmout(StrBuf *Target, char *align)
265 StrBufAppendPrintf(Target, "<div align=%s>\n", align);
266 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
268 if ((intext == 1) && (isspace(buf[0]))) {
274 * Quoted text should be displayed in italics and in a
275 * different colour. This code understands Citadel-style
276 * " >" quotes and will convert to <BLOCKQUOTE> tags.
278 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
279 StrBufAppendBufPlain(Target, HKEY("<BLOCKQUOTE>"), 0);
281 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
282 StrBufAppendBufPlain(Target, HKEY("</BLOCKQUOTE>"), 0);
285 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
286 strcpy(buf, &buf[2]);
288 /* Activate embedded URL's */
289 url(buf, sizeof(buf));
292 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
297 wc_printf("</div><br />\n");
300 void FmOut(StrBuf *Target, char *align, StrBuf *Source)
302 const char *ptr, *pte;
303 const char *BufPtr = NULL;
304 StrBuf *Line = NewStrBufPlain(NULL, SIZ);
305 StrBuf *Line1 = NewStrBufPlain(NULL, SIZ);
306 StrBuf *Line2 = NewStrBufPlain(NULL, SIZ);
313 StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
315 if (StrLength(Source) > 0)
318 StrBufSipLine(Line, Source, &BufPtr);
322 len = StrLength(Line);
325 if ((intext == 1) && (isspace(*ptr))) {
326 StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
329 if (isspace(*ptr)) while ((ptr < pte) &&
340 * Quoted text should be displayed in italics and in a
341 * different colour. This code understands Citadel-style
342 * " >" quotes and will convert to <BLOCKQUOTE> tags.
344 if (i > 0) StrBufCutLeft(Line, i);
347 for (i = bn; i < bq; i++)
348 StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
349 for (i = bq; i < bn; i++)
350 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
353 if (StrLength(Line) == 0)
356 /* Activate embedded URL's */
357 UrlizeText(Line1, Line, Line2);
359 StrEscAppend(Target, Line1, NULL, 0, 0);
361 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
363 while ((BufPtr != StrBufNOTNULL) &&
366 for (i = 0; i < bn; i++) {
367 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
369 StrBufAppendBufPlain(Target, HKEY("</div><br>\n"), 0);
377 * Read Citadel variformat text and spit it out as HTML in a form
378 * suitable for embedding in another message (forward/quote).
379 * (NO LINEBREAKS ALLOWED HERE!)
381 void pullquote_fmout(void) {
386 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
388 if ((intext == 1) && (isspace(buf[0]))) {
394 * Quoted text should be displayed in italics and in a
395 * different colour. This code understands Citadel-style
396 * " >" quotes and will convert to <BLOCKQUOTE> tags.
398 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
399 wc_printf("<BLOCKQUOTE>");
401 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
402 wc_printf("</BLOCKQUOTE>");
405 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
406 strcpy(buf, &buf[2]);
420 * Transmit message text (in memory) to the server.
422 void text_to_server(char *ptr)
430 while (ptr[pos] != 0) {
434 while ( (isspace(buf[len - 1]))
436 && (buf[1] != '\0') )
440 if (ptr[pos] != 0) strcat(buf, " ");
445 if ((ch == 32) && (strlen(buf) > 200)) {
450 if (strlen(buf) > 250) {
461 * Transmit message text (in memory) to the server, converting to Quoted-Printable encoding as we go.
463 void text_to_server_qp(char *ptr)
465 unsigned char ch, buf[256];
473 while (ptr[pos] != 0) {
474 ch = (unsigned char)(ptr[pos++]);
477 /* ignore carriage returns */
480 /* hard line break */
481 if (output_len > 0) {
482 if (isspace(buf[output_len-1])) {
483 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
487 buf[output_len++] = 0;
488 serv_puts((char *)buf);
492 buf[output_len++] = ch;
494 else if ( (ch >= 32) && (ch <= 60) ) {
495 buf[output_len++] = ch;
497 else if ( (ch >= 62) && (ch <= 126) ) {
498 buf[output_len++] = ch;
501 sprintf((char *)&buf[output_len], "=%02X", ch);
505 if (output_len > 72) {
506 /* soft line break */
507 if (isspace(buf[output_len-1])) {
508 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
511 buf[output_len++] = '=';
512 buf[output_len++] = 0;
513 serv_puts((char *)buf);
518 /* end of data - transmit anything that's left */
519 if (output_len > 0) {
520 if (isspace(buf[output_len-1])) {
521 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
524 buf[output_len++] = 0;
525 serv_puts((char *)buf);
534 * translate server message output to text (used for editing room info files and such)
536 void server_to_text()
542 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
543 if ((buf[0] == 32) && (count > 0)) {
546 wc_printf("%s", buf);
555 * Read text from server, appending to a string buffer until the
556 * usual 000 terminator is found. Caller is responsible for freeing
557 * the returned pointer.
559 int read_server_text(StrBuf *Buf, long *nLines)
567 while ((WCC->serv_sock!=-1) &&
568 (nRead = StrBuf_ServGetln(Buf), (nRead >= 0) ))
570 if (strcmp(ChrPtr(Buf) + nTotal, "000") != 0) {
571 StrBufCutRight(Buf, nRead);
583 int GetServerStatus(StrBuf *Line, long* FullState)
585 if (FullState != NULL)
586 *FullState = StrTol(Line);
587 return ChrPtr(Line)[0] - 48;
591 void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP)
593 StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
596 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP)
599 if (WCC->serv_info == NULL)
601 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_nodename, 0);
604 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP)
607 if (WCC->serv_info == NULL)
609 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_humannode, 0);
612 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP)
615 if (WCC->serv_info == NULL)
617 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_fqdn, 0);
620 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP)
623 if (WCC->serv_info == NULL)
625 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_software, 0);
628 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP)
631 if (WCC->serv_info == NULL)
633 StrBufAppendPrintf(Target, "%d.%02d",
634 WCC->serv_info->serv_rev_level / 100,
635 WCC->serv_info->serv_rev_level % 100);
637 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP)
640 if (WCC->serv_info == NULL)
642 return WCC->serv_info->serv_newuser_disabled != 0;
645 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP)
648 if (WCC->serv_info == NULL)
650 return WCC->serv_info->serv_supports_openid != 0;
653 int conditional_serv_fulltext_enabled(StrBuf *Target, WCTemplputParams *TP)
656 if (WCC->serv_info == NULL)
658 return WCC->serv_info->serv_fulltext_enabled != 0;
661 int conditional_serv_ldap_enabled(StrBuf *Target, WCTemplputParams *TP)
664 if (WCC->serv_info == NULL)
666 return WCC->serv_info->serv_supports_ldap != 0;
669 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP)
672 if (WCC->serv_info == NULL)
674 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
677 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP)
686 serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
688 StrBuf_ServGetln(Line);
689 if (GetServerStatus(Line, NULL) == 1) {
690 while (!Done && (StrBuf_ServGetln(Line)>=0)) {
691 if ( (StrLength(Line)==3) &&
692 !strcmp(ChrPtr(Line), "000"))
697 StrBufAppendBufPlain(Buf, "\n", 1, 0);
698 StrBufAppendBuf(Buf, Line, 0);
704 FmOut(Line, "center", Buf);
705 StrBufAppendTemplate(Target, TP, Line, 1);
712 void RegisterEmbeddableMimeType(const char *MimeType, long MTLen, int Priority)
715 MT = NewStrBufPlain(MimeType, MTLen);
716 Put(EmbeddableMimes, IKEY(Priority), MT, HFreeStrBuf);
719 void CreateMimeStr(void)
726 it = GetNewHashPos(EmbeddableMimes, 0);
727 while (GetNextHashPos(EmbeddableMimes, it, &len, &Key, &vMime) &&
729 if (StrLength(EmbeddableMimeStrs) > 0)
730 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("|"), 0);
732 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("MSGP "), 0);
733 StrBufAppendBuf(EmbeddableMimeStrs, (StrBuf*) vMime, 0);
739 ServerStartModule_SERV_FUNC
742 EmbeddableMimes = NewHash(1, Flathash);
743 EmbeddableMimeStrs = NewStrBuf();
748 ServerShutdownModule_SERV_FUNC
751 FreeStrBuf(&EmbeddableMimeStrs);
752 DeleteHash(&EmbeddableMimes);
759 is_uds = strcasecmp(ctdlhost, "uds") == 0;
761 snprintf(serv_sock_name, PATH_MAX, "%s/citadel.socket", ctdlport);
763 RegisterConditional(HKEY("COND:SERV:OPENID"), 2, conditional_serv_supports_openid, CTX_NONE);
764 RegisterConditional(HKEY("COND:SERV:NEWU"), 2, conditional_serv_newuser_disabled, CTX_NONE);
765 RegisterConditional(HKEY("COND:SERV:FULLTEXT_ENABLED"), 2, conditional_serv_fulltext_enabled, CTX_NONE);
766 RegisterConditional(HKEY("COND:SERV:LDAP_ENABLED"), 2, conditional_serv_ldap_enabled, CTX_NONE);
767 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, NULL, CTX_NONE);
768 RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, NULL, CTX_NONE);
769 RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, NULL, CTX_NONE);
770 RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, NULL, CTX_NONE);
771 RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, NULL, CTX_NONE);
772 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, NULL, CTX_NONE);
773 RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, NULL, CTX_NONE);
774 RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, NULL, CTX_NONE);
780 SessionDestroyModule_SERVFUNC
783 DeleteServInfo(&sess->serv_info);