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_moreprompt);
37 FreeStrBuf(&(*FreeMe)->serv_default_cal_zone);
38 FreeStrBuf(&(*FreeMe)->serv_svn_revision);
44 * get info about the server we've connected to
46 * browser_host the citadel we want to connect to
47 * user_agent which browser uses our client?
49 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 lprintf(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 lprintf(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 lprintf(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 (StrBuf_ServGetln(Buf), (strcmp(ChrPtr(Buf), "000")!= 0)) {
103 info->serv_pid = StrToi(Buf);
104 WC->ctdl_pid = info->serv_pid;
107 info->serv_nodename = NewStrBufDup(Buf);
110 info->serv_humannode = NewStrBufDup(Buf);
113 info->serv_fqdn = NewStrBufDup(Buf);
116 info->serv_software = NewStrBufDup(Buf);
119 info->serv_rev_level = StrToi(Buf);
122 info->serv_bbs_city = NewStrBufDup(Buf);
125 info->serv_sysadm = NewStrBufDup(Buf);
128 info->serv_moreprompt = NewStrBufDup(Buf);
131 info->serv_supports_ldap = StrToi(Buf);
134 info->serv_newuser_disabled = StrToi(Buf);
137 info->serv_default_cal_zone = NewStrBufDup(Buf);
140 info->serv_supports_sieve = StrToi(Buf);
143 info->serv_fulltext_enabled = StrToi(Buf);
146 info->serv_svn_revision = NewStrBufDup(Buf);
149 info->serv_supports_openid = StrToi(Buf);
158 int GetConnected (void)
163 if (WCC->ReadBuf == NULL)
164 WCC->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
165 if (is_uds) /* unix domain socket */
166 WCC->serv_sock = uds_connectsock(serv_sock_name);
167 else /* tcp socket */
168 WCC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
170 if (WCC->serv_sock < 0) {
172 FreeStrBuf(&WCC->ReadBuf);
180 StrBuf_ServGetln(Buf); /* get the server greeting */
181 short_status = GetServerStatus(Buf, &Status);
184 /* Server isn't ready for us? */
185 if (short_status != 2) {
187 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."));
190 wc_printf("%ld %s\n",
192 _("Received unexpected answer from Citadel server; bailing out.")
196 end_webcit_session();
200 /* 2010jun03: every now and then the connection to Citadel dies before this point. why? */
203 * From what host is our user connecting? Go with
204 * the host at the other end of the HTTP socket,
205 * unless we are following X-Forwarded-For: headers
206 * and such a header has already turned up something.
208 if ( (!follow_xff) || (StrLength(WCC->Hdr->HR.browser_host) == 0) ) {
209 if (WCC->Hdr->HR.browser_host == NULL) {
210 WCC->Hdr->HR.browser_host = NewStrBuf();
211 Put(WCC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"),
212 WCC->Hdr->HR.browser_host, HFreeStrBuf);
214 locate_host(WCC->Hdr->HR.browser_host, WCC->Hdr->http_sock);
216 if (WCC->serv_info == NULL) {
217 WCC->serv_info = get_serv_info(WCC->Hdr->HR.browser_host, WCC->Hdr->HR.user_agent);
219 if (WCC->serv_info == NULL){
221 wc_printf(_("Received unexpected answer from Citadel server; bailing out."));
222 hprintf("HTTP/1.1 200 OK\r\n");
223 hprintf("Content-type: text/plain; charset=utf-8\r\n");
225 end_webcit_session();
228 if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
230 wc_printf(_("You are connected to a Citadel "
231 "server running Citadel %d.%02d. \n"
232 "In order to run this version of WebCit "
233 "you must also have Citadel %d.%02d or"
235 WCC->serv_info->serv_rev_level / 100,
236 WCC->serv_info->serv_rev_level % 100,
237 MINIMUM_CIT_VERSION / 100,
238 MINIMUM_CIT_VERSION % 100
240 hprintf("HTTP/1.1 200 OK\r\n");
241 hprintf("Content-type: text/plain; charset=utf-8\r\n");
243 end_webcit_session();
246 SetInlinMimeRenderers();
252 * Read Citadel variformat text and spit it out as HTML.
253 * align html align string
255 inline void fmout(char *align)
257 _fmout(WC->WBuf, align);
260 void _fmout(StrBuf *Target, char *align)
266 StrBufAppendPrintf(Target, "<div align=%s>\n", align);
267 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
269 if ((intext == 1) && (isspace(buf[0]))) {
275 * Quoted text should be displayed in italics and in a
276 * different colour. This code understands Citadel-style
277 * " >" quotes and will convert to <BLOCKQUOTE> tags.
279 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
280 StrBufAppendBufPlain(Target, HKEY("<BLOCKQUOTE>"), 0);
282 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
283 StrBufAppendBufPlain(Target, HKEY("</BLOCKQUOTE>"), 0);
286 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
287 strcpy(buf, &buf[2]);
289 /* Activate embedded URL's */
290 url(buf, sizeof(buf));
293 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
298 wc_printf("</div><br />\n");
301 void FmOut(StrBuf *Target, char *align, StrBuf *Source)
303 const char *ptr, *pte;
304 const char *BufPtr = NULL;
305 StrBuf *Line = NewStrBufPlain(NULL, SIZ);
306 StrBuf *Line1 = NewStrBufPlain(NULL, SIZ);
307 StrBuf *Line2 = NewStrBufPlain(NULL, SIZ);
314 StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
316 if (StrLength(Source) > 0)
319 StrBufSipLine(Line, Source, &BufPtr);
323 len = StrLength(Line);
326 if ((intext == 1) && (isspace(*ptr))) {
327 StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
330 if (isspace(*ptr)) while ((ptr < pte) &&
341 * Quoted text should be displayed in italics and in a
342 * different colour. This code understands Citadel-style
343 * " >" quotes and will convert to <BLOCKQUOTE> tags.
345 if (i > 0) StrBufCutLeft(Line, i);
348 for (i = bn; i < bq; i++)
349 StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
350 for (i = bq; i < bn; i++)
351 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
354 if (StrLength(Line) == 0)
357 /* Activate embedded URL's */
358 UrlizeText(Line1, Line, Line2);
360 StrEscAppend(Target, Line1, NULL, 0, 0);
362 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
364 while ((BufPtr != StrBufNOTNULL) &&
367 for (i = 0; i < bn; i++) {
368 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
370 StrBufAppendBufPlain(Target, HKEY("</div><br>\n"), 0);
378 * Read Citadel variformat text and spit it out as HTML in a form
379 * suitable for embedding in another message (forward/quote).
380 * (NO LINEBREAKS ALLOWED HERE!)
382 void pullquote_fmout(void) {
387 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
389 if ((intext == 1) && (isspace(buf[0]))) {
395 * Quoted text should be displayed in italics and in a
396 * different colour. This code understands Citadel-style
397 * " >" quotes and will convert to <BLOCKQUOTE> tags.
399 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
400 wc_printf("<BLOCKQUOTE>");
402 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
403 wc_printf("</BLOCKQUOTE>");
406 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
407 strcpy(buf, &buf[2]);
421 * Transmit message text (in memory) to the server.
423 void text_to_server(char *ptr)
431 while (ptr[pos] != 0) {
435 while ( (isspace(buf[len - 1]))
437 && (buf[1] != '\0') )
441 if (ptr[pos] != 0) strcat(buf, " ");
446 if ((ch == 32) && (strlen(buf) > 200)) {
451 if (strlen(buf) > 250) {
462 * Transmit message text (in memory) to the server, converting to Quoted-Printable encoding as we go.
464 void text_to_server_qp(char *ptr)
466 unsigned char ch, buf[256];
474 while (ptr[pos] != 0) {
475 ch = (unsigned char)(ptr[pos++]);
478 /* ignore carriage returns */
481 /* hard line break */
482 if (output_len > 0) {
483 if (isspace(buf[output_len-1])) {
484 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
488 buf[output_len++] = 0;
489 serv_puts((char *)buf);
493 buf[output_len++] = ch;
495 else if ( (ch >= 32) && (ch <= 60) ) {
496 buf[output_len++] = ch;
498 else if ( (ch >= 62) && (ch <= 126) ) {
499 buf[output_len++] = ch;
502 sprintf((char *)&buf[output_len], "=%02X", ch);
506 if (output_len > 72) {
507 /* soft line break */
508 if (isspace(buf[output_len-1])) {
509 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
512 buf[output_len++] = '=';
513 buf[output_len++] = 0;
514 serv_puts((char *)buf);
519 /* end of data - transmit anything that's left */
520 if (output_len > 0) {
521 if (isspace(buf[output_len-1])) {
522 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
525 buf[output_len++] = 0;
526 serv_puts((char *)buf);
535 * translate server message output to text (used for editing room info files and such)
537 void server_to_text()
543 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
544 if ((buf[0] == 32) && (count > 0)) {
547 wc_printf("%s", buf);
556 * Read text from server, appending to a string buffer until the
557 * usual 000 terminator is found. Caller is responsible for freeing
558 * the returned pointer.
560 int read_server_text(StrBuf *Buf, long *nLines)
568 while ((WCC->serv_sock!=-1) &&
569 (nRead = StrBuf_ServGetln(Buf), (nRead >= 0) ))
571 if (strcmp(ChrPtr(Buf) + nTotal, "000") != 0) {
572 StrBufCutRight(Buf, nRead);
584 int GetServerStatus(StrBuf *Line, long* FullState)
586 if (FullState != NULL)
587 *FullState = StrTol(Line);
588 return ChrPtr(Line)[0] - 48;
592 void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP)
594 StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
597 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP)
600 if (WCC->serv_info == NULL)
602 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_nodename, 0);
605 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP)
608 if (WCC->serv_info == NULL)
610 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_humannode, 0);
613 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP)
616 if (WCC->serv_info == NULL)
618 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_fqdn, 0);
621 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP)
624 if (WCC->serv_info == NULL)
626 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_software, 0);
629 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP)
632 if (WCC->serv_info == NULL)
634 StrBufAppendPrintf(Target, "%d.%02d",
635 WCC->serv_info->serv_rev_level / 100,
636 WCC->serv_info->serv_rev_level % 100);
638 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP)
641 if (WCC->serv_info == NULL)
643 return WCC->serv_info->serv_newuser_disabled != 0;
646 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP)
649 if (WCC->serv_info == NULL)
651 return WCC->serv_info->serv_supports_openid != 0;
654 int conditional_serv_fulltext_enabled(StrBuf *Target, WCTemplputParams *TP)
657 if (WCC->serv_info == NULL)
659 return WCC->serv_info->serv_fulltext_enabled != 0;
662 int conditional_serv_ldap_enabled(StrBuf *Target, WCTemplputParams *TP)
665 if (WCC->serv_info == NULL)
667 return WCC->serv_info->serv_supports_ldap != 0;
670 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP)
673 if (WCC->serv_info == NULL)
675 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
678 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP)
687 serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
689 StrBuf_ServGetln(Line);
690 if (GetServerStatus(Line, NULL) == 1) {
691 while (!Done && (StrBuf_ServGetln(Line)>=0)) {
692 if ( (StrLength(Line)==3) &&
693 !strcmp(ChrPtr(Line), "000"))
698 StrBufAppendBufPlain(Buf, "\n", 1, 0);
699 StrBufAppendBuf(Buf, Line, 0);
705 FmOut(Line, "center", Buf);
706 StrBufAppendTemplate(Target, TP, Line, 1);
713 void RegisterEmbeddableMimeType(const char *MimeType, long MTLen, int Priority)
716 MT = NewStrBufPlain(MimeType, MTLen);
717 Put(EmbeddableMimes, IKEY(Priority), MT, HFreeStrBuf);
720 void CreateMimeStr(void)
727 it = GetNewHashPos(EmbeddableMimes, 0);
728 while (GetNextHashPos(EmbeddableMimes, it, &len, &Key, &vMime) &&
730 if (StrLength(EmbeddableMimeStrs) > 0)
731 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("|"), 0);
733 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("MSGP "), 0);
734 StrBufAppendBuf(EmbeddableMimeStrs, (StrBuf*) vMime, 0);
740 ServerStartModule_SERV_FUNC
743 EmbeddableMimes = NewHash(1, Flathash);
744 EmbeddableMimeStrs = NewStrBuf();
749 ServerShutdownModule_SERV_FUNC
752 FreeStrBuf(&EmbeddableMimeStrs);
753 DeleteHash(&EmbeddableMimes);
760 is_uds = strcasecmp(ctdlhost, "uds") == 0;
762 snprintf(serv_sock_name, PATH_MAX, "%s/citadel.socket", ctdlport);
764 RegisterConditional(HKEY("COND:SERV:OPENID"), 2, conditional_serv_supports_openid, CTX_NONE);
765 RegisterConditional(HKEY("COND:SERV:NEWU"), 2, conditional_serv_newuser_disabled, CTX_NONE);
766 RegisterConditional(HKEY("COND:SERV:FULLTEXT_ENABLED"), 2, conditional_serv_fulltext_enabled, CTX_NONE);
767 RegisterConditional(HKEY("COND:SERV:LDAP_ENABLED"), 2, conditional_serv_ldap_enabled, CTX_NONE);
768 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, NULL, CTX_NONE);
769 RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, NULL, CTX_NONE);
770 RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, NULL, CTX_NONE);
771 RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, NULL, CTX_NONE);
772 RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, NULL, CTX_NONE);
773 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, NULL, CTX_NONE);
774 RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, NULL, CTX_NONE);
775 RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, NULL, CTX_NONE);
781 SessionDestroyModule_SERVFUNC
784 DeleteServInfo(&sess->serv_info);