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) {
170 FreeStrBuf(&WCC->ReadBuf);
178 StrBuf_ServGetln(Buf); /* get the server greeting */
179 short_status = GetServerStatus(Buf, &Status);
182 /* Server isn't ready for us? */
183 if (short_status != 2) {
185 hprintf("HTTP/1.1 503 Service Unavailable\r\n");
186 hprintf("Content-type: text/plain; charset=utf-8\r\n");
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.")
194 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
195 hprintf("Content-type: text/plain; charset=utf-8\r\n");
198 end_webcit_session();
202 /* 2010jun03: every now and then the connection to Citadel dies before this point. why? */
205 * From what host is our user connecting? Go with
206 * the host at the other end of the HTTP socket,
207 * unless we are following X-Forwarded-For: headers
208 * and such a header has already turned up something.
210 if ( (!follow_xff) || (StrLength(WCC->Hdr->HR.browser_host) == 0) ) {
211 if (WCC->Hdr->HR.browser_host == NULL) {
212 WCC->Hdr->HR.browser_host = NewStrBuf();
213 Put(WCC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"),
214 WCC->Hdr->HR.browser_host, HFreeStrBuf);
216 locate_host(WCC->Hdr->HR.browser_host, WCC->Hdr->http_sock);
218 if (WCC->serv_info == NULL) {
219 WCC->serv_info = get_serv_info(WCC->Hdr->HR.browser_host, WCC->Hdr->HR.user_agent);
221 if (WCC->serv_info == NULL){
223 wc_printf(_("Received unexpected answer from Citadel server; bailing out."));
224 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
225 hprintf("Content-type: text/plain; charset=utf-8\r\n");
227 end_webcit_session();
230 if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
232 wc_printf(_("You are connected to a Citadel "
233 "server running Citadel %d.%02d. \n"
234 "In order to run this version of WebCit "
235 "you must also have Citadel %d.%02d or"
237 WCC->serv_info->serv_rev_level / 100,
238 WCC->serv_info->serv_rev_level % 100,
239 MINIMUM_CIT_VERSION / 100,
240 MINIMUM_CIT_VERSION % 100
242 hprintf("HTTP/1.1 200 OK\r\n");
243 hprintf("Content-type: text/plain; charset=utf-8\r\n");
245 end_webcit_session();
248 SetInlinMimeRenderers();
254 * Read Citadel variformat text and spit it out as HTML.
255 * align html align string
257 inline void fmout(char *align)
259 _fmout(WC->WBuf, align);
262 void _fmout(StrBuf *Target, char *align)
268 StrBufAppendPrintf(Target, "<div align=%s>\n", align);
269 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
271 if ((intext == 1) && (isspace(buf[0]))) {
277 * Quoted text should be displayed in italics and in a
278 * different colour. This code understands Citadel-style
279 * " >" quotes and will convert to <BLOCKQUOTE> tags.
281 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
282 StrBufAppendBufPlain(Target, HKEY("<BLOCKQUOTE>"), 0);
284 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
285 StrBufAppendBufPlain(Target, HKEY("</BLOCKQUOTE>"), 0);
288 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
289 strcpy(buf, &buf[2]);
291 /* Activate embedded URL's */
292 url(buf, sizeof(buf));
295 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
300 wc_printf("</div><br>\n");
303 void FmOut(StrBuf *Target, char *align, StrBuf *Source)
305 const char *ptr, *pte;
306 const char *BufPtr = NULL;
307 StrBuf *Line = NewStrBufPlain(NULL, SIZ);
308 StrBuf *Line1 = NewStrBufPlain(NULL, SIZ);
309 StrBuf *Line2 = NewStrBufPlain(NULL, SIZ);
316 StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
318 if (StrLength(Source) > 0)
321 StrBufSipLine(Line, Source, &BufPtr);
325 len = StrLength(Line);
328 if ((intext == 1) && (isspace(*ptr))) {
329 StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
332 if (isspace(*ptr)) while ((ptr < pte) &&
343 * Quoted text should be displayed in italics and in a
344 * different colour. This code understands Citadel-style
345 * " >" quotes and will convert to <BLOCKQUOTE> tags.
347 if (i > 0) StrBufCutLeft(Line, i);
350 for (i = bn; i < bq; i++)
351 StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
352 for (i = bq; i < bn; i++)
353 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
356 if (StrLength(Line) == 0)
359 /* Activate embedded URL's */
360 UrlizeText(Line1, Line, Line2);
362 StrEscAppend(Target, Line1, NULL, 0, 0);
364 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
366 while ((BufPtr != StrBufNOTNULL) &&
369 for (i = 0; i < bn; i++) {
370 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
372 StrBufAppendBufPlain(Target, HKEY("</div><br>\n"), 0);
380 * Read Citadel variformat text and spit it out as HTML in a form
381 * suitable for embedding in another message (forward/quote).
382 * (NO LINEBREAKS ALLOWED HERE!)
384 void pullquote_fmout(void) {
389 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
391 if ((intext == 1) && (isspace(buf[0]))) {
397 * Quoted text should be displayed in italics and in a
398 * different colour. This code understands Citadel-style
399 * " >" quotes and will convert to <BLOCKQUOTE> tags.
401 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
402 wc_printf("<BLOCKQUOTE>");
404 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
405 wc_printf("</BLOCKQUOTE>");
408 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
409 strcpy(buf, &buf[2]);
423 * Transmit message text (in memory) to the server.
425 void text_to_server(char *ptr)
433 while (ptr[pos] != 0) {
437 while ( (isspace(buf[len - 1]))
439 && (buf[1] != '\0') )
443 if (ptr[pos] != 0) strcat(buf, " ");
448 if ((ch == 32) && (strlen(buf) > 200)) {
453 if (strlen(buf) > 250) {
464 * Transmit message text (in memory) to the server, converting to Quoted-Printable encoding as we go.
466 void text_to_server_qp(char *ptr)
468 unsigned char ch, buf[256];
476 while (ptr[pos] != 0) {
477 ch = (unsigned char)(ptr[pos++]);
480 /* ignore carriage returns */
483 /* hard line break */
484 if (output_len > 0) {
485 if (isspace(buf[output_len-1])) {
486 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
490 buf[output_len++] = 0;
491 serv_puts((char *)buf);
495 buf[output_len++] = ch;
497 else if ( (ch >= 32) && (ch <= 60) ) {
498 buf[output_len++] = ch;
500 else if ( (ch >= 62) && (ch <= 126) ) {
501 buf[output_len++] = ch;
504 sprintf((char *)&buf[output_len], "=%02X", ch);
508 if (output_len > 72) {
509 /* soft line break */
510 if (isspace(buf[output_len-1])) {
511 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
514 buf[output_len++] = '=';
515 buf[output_len++] = 0;
516 serv_puts((char *)buf);
521 /* end of data - transmit anything that's left */
522 if (output_len > 0) {
523 if (isspace(buf[output_len-1])) {
524 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
527 buf[output_len++] = 0;
528 serv_puts((char *)buf);
537 * translate server message output to text (used for editing room info files and such)
539 void server_to_text()
545 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
546 if ((buf[0] == 32) && (count > 0)) {
549 wc_printf("%s", buf);
558 * Read text from server, appending to a string buffer until the
559 * usual 000 terminator is found. Caller is responsible for freeing
560 * the returned pointer.
562 int read_server_text(StrBuf *Buf, long *nLines)
570 while ((WCC->serv_sock!=-1) &&
571 (nRead = StrBuf_ServGetln(Buf), (nRead >= 0) ))
573 if (strcmp(ChrPtr(Buf) + nTotal, "000") != 0) {
574 StrBufCutRight(Buf, nRead);
586 int GetServerStatus(StrBuf *Line, long* FullState)
588 if (FullState != NULL)
589 *FullState = StrTol(Line);
590 return ChrPtr(Line)[0] - 48;
594 void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP)
596 StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
599 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP)
602 if (WCC->serv_info == NULL)
604 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_nodename, 0);
607 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP)
610 if (WCC->serv_info == NULL)
612 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_humannode, 0);
615 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP)
618 if (WCC->serv_info == NULL)
620 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_fqdn, 0);
623 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP)
626 if (WCC->serv_info == NULL)
628 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_software, 0);
631 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP)
634 if (WCC->serv_info == NULL)
636 StrBufAppendPrintf(Target, "%d.%02d",
637 WCC->serv_info->serv_rev_level / 100,
638 WCC->serv_info->serv_rev_level % 100);
640 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP)
643 if (WCC->serv_info == NULL)
645 return WCC->serv_info->serv_newuser_disabled != 0;
648 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP)
651 if (WCC->serv_info == NULL)
653 return WCC->serv_info->serv_supports_openid != 0;
656 int conditional_serv_fulltext_enabled(StrBuf *Target, WCTemplputParams *TP)
659 if (WCC->serv_info == NULL)
661 return WCC->serv_info->serv_fulltext_enabled != 0;
664 int conditional_serv_ldap_enabled(StrBuf *Target, WCTemplputParams *TP)
667 if (WCC->serv_info == NULL)
669 return WCC->serv_info->serv_supports_ldap != 0;
672 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP)
675 if (WCC->serv_info == NULL)
677 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
680 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP)
689 serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
691 StrBuf_ServGetln(Line);
692 if (GetServerStatus(Line, NULL) == 1) {
693 while (!Done && (StrBuf_ServGetln(Line)>=0)) {
694 if ( (StrLength(Line)==3) &&
695 !strcmp(ChrPtr(Line), "000"))
700 StrBufAppendBufPlain(Buf, "\n", 1, 0);
701 StrBufAppendBuf(Buf, Line, 0);
707 FmOut(Line, "center", Buf);
708 StrBufAppendTemplate(Target, TP, Line, 1);
715 void RegisterEmbeddableMimeType(const char *MimeType, long MTLen, int Priority)
718 MT = NewStrBufPlain(MimeType, MTLen);
719 Put(EmbeddableMimes, IKEY(Priority), MT, HFreeStrBuf);
722 void CreateMimeStr(void)
729 it = GetNewHashPos(EmbeddableMimes, 0);
730 while (GetNextHashPos(EmbeddableMimes, it, &len, &Key, &vMime) &&
732 if (StrLength(EmbeddableMimeStrs) > 0)
733 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("|"), 0);
735 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("MSGP "), 0);
736 StrBufAppendBuf(EmbeddableMimeStrs, (StrBuf*) vMime, 0);
742 ServerStartModule_SERV_FUNC
745 EmbeddableMimes = NewHash(1, Flathash);
746 EmbeddableMimeStrs = NewStrBuf();
751 ServerShutdownModule_SERV_FUNC
754 FreeStrBuf(&EmbeddableMimeStrs);
755 DeleteHash(&EmbeddableMimes);
762 is_uds = strcasecmp(ctdlhost, "uds") == 0;
764 snprintf(serv_sock_name, PATH_MAX, "%s/citadel.socket", ctdlport);
766 RegisterConditional(HKEY("COND:SERV:OPENID"), 2, conditional_serv_supports_openid, CTX_NONE);
767 RegisterConditional(HKEY("COND:SERV:NEWU"), 2, conditional_serv_newuser_disabled, CTX_NONE);
768 RegisterConditional(HKEY("COND:SERV:FULLTEXT_ENABLED"), 2, conditional_serv_fulltext_enabled, CTX_NONE);
769 RegisterConditional(HKEY("COND:SERV:LDAP_ENABLED"), 2, conditional_serv_ldap_enabled, CTX_NONE);
770 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, NULL, CTX_NONE);
771 RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, NULL, CTX_NONE);
772 RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, NULL, CTX_NONE);
773 RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, NULL, CTX_NONE);
774 RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, NULL, CTX_NONE);
775 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, NULL, CTX_NONE);
776 RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, NULL, CTX_NONE);
777 RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, NULL, CTX_NONE);
783 SessionDestroyModule_SERVFUNC
786 DeleteServInfo(&sess->serv_info);