9 char serv_sock_name[PATH_MAX] = "";
11 HashList *EmbeddableMimes = NULL;
12 StrBuf *EmbeddableMimeStrs = NULL;
15 void SetInlinMimeRenderers(void)
20 /** Tell the server what kind of richtext we prefer */
21 serv_putbuf(EmbeddableMimeStrs);
22 StrBuf_ServGetln(Buf);
28 void DeleteServInfo(ServInfo **FreeMe)
32 FreeStrBuf(&(*FreeMe)->serv_nodename);
33 FreeStrBuf(&(*FreeMe)->serv_humannode);
34 FreeStrBuf(&(*FreeMe)->serv_fqdn);
35 FreeStrBuf(&(*FreeMe)->serv_software);
36 FreeStrBuf(&(*FreeMe)->serv_bbs_city);
37 FreeStrBuf(&(*FreeMe)->serv_sysadm);
38 FreeStrBuf(&(*FreeMe)->serv_moreprompt);
39 FreeStrBuf(&(*FreeMe)->serv_default_cal_zone);
40 FreeStrBuf(&(*FreeMe)->serv_svn_revision);
46 * get info about the server we've connected to
48 * browser_host the citadel we want to connect to
49 * user_agent which browser uses our client?
51 ServInfo *get_serv_info(StrBuf *browser_host, StrBuf *user_agent)
58 /** Tell the server what kind of client is connecting */
59 serv_printf("IDEN %d|%d|%d|%s|%s",
66 StrBuf_ServGetln(Buf);
67 if (GetServerStatus(Buf, NULL) != 2) {
68 lprintf(0, "get_serv_info(IDEN): unexpected answer [%s]\n",
75 * Tell the server that when we save a calendar event, we
76 * want invitations to be generated by the Citadel server
77 * instead of by the client.
79 serv_puts("ICAL sgi|1");
80 StrBuf_ServGetln(Buf);
81 if (GetServerStatus(Buf, NULL) != 2) {
82 lprintf(0, "get_serv_info(ICAL sgi|1): unexpected answer [%s]\n",
88 /** Now ask the server to tell us a little bit about itself... */
90 StrBuf_ServGetln(Buf);
91 if (GetServerStatus(Buf, NULL) != 1) {
92 lprintf(0, "get_serv_info(INFO sgi|1): unexpected answer [%s]\n",
98 info = (ServInfo*)malloc(sizeof(ServInfo));
99 memset(info, 0, sizeof(ServInfo));
101 while (StrBuf_ServGetln(Buf), (strcmp(ChrPtr(Buf), "000")!= 0)) {
102 /* lprintf (1, "a: %d [%s]", a, ChrPtr(Buf));*/
105 info->serv_pid = StrToi(Buf);
106 WC->ctdl_pid = info->serv_pid;
109 info->serv_nodename = NewStrBufDup(Buf);
112 info->serv_humannode = NewStrBufDup(Buf);
115 info->serv_fqdn = NewStrBufDup(Buf);
118 info->serv_software = NewStrBufDup(Buf);
121 info->serv_rev_level = StrToi(Buf);
124 info->serv_bbs_city = NewStrBufDup(Buf);
127 info->serv_sysadm = NewStrBufDup(Buf);
130 info->serv_moreprompt = NewStrBufDup(Buf);
133 info->serv_supports_ldap = StrToi(Buf);
136 info->serv_newuser_disabled = StrToi(Buf);
139 info->serv_default_cal_zone = NewStrBufDup(Buf);
142 info->serv_supports_sieve = StrToi(Buf);
145 info->serv_fulltext_enabled = StrToi(Buf);
148 info->serv_svn_revision = NewStrBufDup(Buf);
151 info->serv_supports_openid = StrToi(Buf);
160 int GetConnected (void)
165 if (WCC->ReadBuf == NULL)
166 WCC->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
167 if (is_uds) /* unix domain socket */
168 WCC->serv_sock = uds_connectsock(serv_sock_name);
169 else /* tcp socket */
170 WCC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
172 if (WCC->serv_sock < 0) {
174 FreeStrBuf(&WCC->ReadBuf);
181 StrBuf_ServGetln(Buf); /* get the server greeting */
182 GetServerStatus(Buf, &Status);
185 /* Are there too many users already logged in? */
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."));
189 end_webcit_session();
194 * From what host is our user connecting? Go with
195 * the host at the other end of the HTTP socket,
196 * unless we are following X-Forwarded-For: headers
197 * and such a header has already turned up something.
199 if ( (!follow_xff) || (StrLength(WCC->Hdr->HR.browser_host) == 0) ) {
200 if (WCC->Hdr->HR.browser_host == NULL) {
201 WCC->Hdr->HR.browser_host = NewStrBuf();
202 Put(WCC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"),
203 WCC->Hdr->HR.browser_host, HFreeStrBuf);
205 locate_host(WCC->Hdr->HR.browser_host, WCC->Hdr->http_sock);
207 if (WCC->serv_info == NULL)
208 WCC->serv_info = get_serv_info(WCC->Hdr->HR.browser_host, WCC->Hdr->HR.user_agent);
209 if (WCC->serv_info == NULL){
211 wc_printf(_("Received unexpected answer from Citadel "
212 "server; bailing out."));
213 hprintf("HTTP/1.1 200 OK\r\n");
214 hprintf("Content-type: text/plain; charset=utf-8\r\n");
216 end_webcit_session();
219 if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
221 wc_printf(_("You are connected to a Citadel "
222 "server running Citadel %d.%02d. \n"
223 "In order to run this version of WebCit "
224 "you must also have Citadel %d.%02d or"
226 WCC->serv_info->serv_rev_level / 100,
227 WCC->serv_info->serv_rev_level % 100,
228 MINIMUM_CIT_VERSION / 100,
229 MINIMUM_CIT_VERSION % 100
231 hprintf("HTTP/1.1 200 OK\r\n");
232 hprintf("Content-type: text/plain; charset=utf-8\r\n");
234 end_webcit_session();
237 SetInlinMimeRenderers();
243 * Read Citadel variformat text and spit it out as HTML.
244 * align html align string
246 inline void fmout(char *align)
248 _fmout(WC->WBuf, align);
251 void _fmout(StrBuf *Target, char *align)
257 StrBufAppendPrintf(Target, "<div align=%s>\n", align);
258 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
260 if ((intext == 1) && (isspace(buf[0]))) {
266 * Quoted text should be displayed in italics and in a
267 * different colour. This code understands Citadel-style
268 * " >" quotes and will convert to <BLOCKQUOTE> tags.
270 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
271 StrBufAppendBufPlain(Target, HKEY("<BLOCKQUOTE>"), 0);
273 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
274 StrBufAppendBufPlain(Target, HKEY("</BLOCKQUOTE>"), 0);
277 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
278 strcpy(buf, &buf[2]);
280 /* Activate embedded URL's */
281 url(buf, sizeof(buf));
284 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
289 wc_printf("</div><br />\n");
292 void FmOut(StrBuf *Target, char *align, StrBuf *Source)
294 const char *ptr, *pte;
295 const char *BufPtr = NULL;
296 StrBuf *Line = NewStrBufPlain(NULL, SIZ);
297 StrBuf *Line1 = NewStrBufPlain(NULL, SIZ);
298 StrBuf *Line2 = NewStrBufPlain(NULL, SIZ);
305 StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
307 if (StrLength(Source) > 0)
310 StrBufSipLine(Line, Source, &BufPtr);
314 len = StrLength(Line);
317 if ((intext == 1) && (isspace(*ptr))) {
318 StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
321 if (isspace(*ptr)) while ((ptr < pte) &&
332 * Quoted text should be displayed in italics and in a
333 * different colour. This code understands Citadel-style
334 * " >" quotes and will convert to <BLOCKQUOTE> tags.
336 if (i > 0) StrBufCutLeft(Line, i);
339 for (i = bn; i < bq; i++)
340 StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
341 for (i = bq; i < bn; i++)
342 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
345 if (StrLength(Line) == 0)
347 /** Activate embedded URL's */
348 UrlizeText(Line1, Line, Line2);
350 StrEscAppend(Target, Line1, NULL, 0, 0);
352 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
354 while ((BufPtr != StrBufNOTNULL) &&
357 for (i = 0; i < bn; i++) {
358 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
360 StrBufAppendBufPlain(Target, HKEY("</div><br>\n"), 0);
368 * Read Citadel variformat text and spit it out as HTML in a form
369 * suitable for embedding in another message (forward/quote).
370 * (NO LINEBREAKS ALLOWED HERE!)
372 void pullquote_fmout(void) {
377 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
379 if ((intext == 1) && (isspace(buf[0]))) {
385 * Quoted text should be displayed in italics and in a
386 * different colour. This code understands Citadel-style
387 * " >" quotes and will convert to <BLOCKQUOTE> tags.
389 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
390 wc_printf("<BLOCKQUOTE>");
392 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
393 wc_printf("</BLOCKQUOTE>");
396 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
397 strcpy(buf, &buf[2]);
411 * Transmit message text (in memory) to the server.
413 void text_to_server(char *ptr)
421 while (ptr[pos] != 0) {
425 while ( (isspace(buf[len - 1]))
427 && (buf[1] != '\0') )
431 if (ptr[pos] != 0) strcat(buf, " ");
436 if ((ch == 32) && (strlen(buf) > 200)) {
441 if (strlen(buf) > 250) {
452 * Transmit message text (in memory) to the server, converting to Quoted-Printable encoding as we go.
454 void text_to_server_qp(char *ptr)
456 unsigned char ch, buf[256];
464 while (ptr[pos] != 0) {
465 ch = (unsigned char)(ptr[pos++]);
468 /* ignore carriage returns */
471 /* hard line break */
472 if (output_len > 0) {
473 if (isspace(buf[output_len-1])) {
474 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
478 buf[output_len++] = 0;
479 serv_puts((char *)buf);
483 buf[output_len++] = ch;
485 else if ( (ch >= 32) && (ch <= 60) ) {
486 buf[output_len++] = ch;
488 else if ( (ch >= 62) && (ch <= 126) ) {
489 buf[output_len++] = ch;
492 sprintf((char *)&buf[output_len], "=%02X", ch);
496 if (output_len > 72) {
497 /* soft line break */
498 if (isspace(buf[output_len-1])) {
499 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
502 buf[output_len++] = '=';
503 buf[output_len++] = 0;
504 serv_puts((char *)buf);
509 /* end of data - transmit anything that's left */
510 if (output_len > 0) {
511 if (isspace(buf[output_len-1])) {
512 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
515 buf[output_len++] = 0;
516 serv_puts((char *)buf);
525 * translate server message output to text (used for editing room info files and such)
527 void server_to_text()
533 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
534 if ((buf[0] == 32) && (count > 0)) {
537 wc_printf("%s", buf);
546 * Read text from server, appending to a string buffer until the
547 * usual 000 terminator is found. Caller is responsible for freeing
548 * the returned pointer.
550 int read_server_text(StrBuf *Buf, long *nLines)
558 while ((WCC->serv_sock!=-1) &&
559 (nRead = StrBuf_ServGetln(Buf), (nRead >= 0) ))
561 if (strcmp(ChrPtr(Buf) + nTotal, "000") != 0) {
562 StrBufCutRight(Buf, nRead);
574 int GetServerStatus(StrBuf *Line, long* FullState)
576 if (FullState != NULL)
577 *FullState = StrTol(Line);
578 return ChrPtr(Line)[0] - 48;
582 void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP)
584 StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
587 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP)
590 if (WCC->serv_info == NULL)
592 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_nodename, 0);
595 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP)
598 if (WCC->serv_info == NULL)
600 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_humannode, 0);
603 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP)
606 if (WCC->serv_info == NULL)
608 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_fqdn, 0);
611 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP)
614 if (WCC->serv_info == NULL)
616 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_software, 0);
619 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP)
622 if (WCC->serv_info == NULL)
624 StrBufAppendPrintf(Target, "%d.%02d",
625 WCC->serv_info->serv_rev_level / 100,
626 WCC->serv_info->serv_rev_level % 100);
628 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP)
631 if (WCC->serv_info == NULL)
633 return WCC->serv_info->serv_newuser_disabled != 0;
636 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP)
639 if (WCC->serv_info == NULL)
641 return WCC->serv_info->serv_supports_openid != 0;
644 int conditional_serv_fulltext_enabled(StrBuf *Target, WCTemplputParams *TP)
647 if (WCC->serv_info == NULL)
649 return WCC->serv_info->serv_fulltext_enabled != 0;
652 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP)
655 if (WCC->serv_info == NULL)
657 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
660 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP)
669 serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
671 StrBuf_ServGetln(Line);
672 if (GetServerStatus(Line, NULL) == 1) {
673 while (!Done && (StrBuf_ServGetln(Line)>=0)) {
674 if ( (StrLength(Line)==3) &&
675 !strcmp(ChrPtr(Line), "000"))
680 StrBufAppendBufPlain(Buf, "\n", 1, 0);
681 StrBufAppendBuf(Buf, Line, 0);
687 FmOut(Line, "center", Buf);
688 StrBufAppendTemplate(Target, TP, Line, 1);
695 void RegisterEmbeddableMimeType(const char *MimeType, long MTLen, int Priority)
698 MT = NewStrBufPlain(MimeType, MTLen);
699 Put(EmbeddableMimes, IKEY(Priority), MT, HFreeStrBuf);
702 void CreateMimeStr(void)
709 it = GetNewHashPos(EmbeddableMimes, 0);
710 while (GetNextHashPos(EmbeddableMimes, it, &len, &Key, &vMime) &&
712 if (StrLength(EmbeddableMimeStrs) > 0)
713 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("|"), 0);
715 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("MSGP "), 0);
716 StrBufAppendBuf(EmbeddableMimeStrs, (StrBuf*) vMime, 0);
722 ServerStartModule_SERV_FUNC
725 EmbeddableMimes = NewHash(1, Flathash);
726 EmbeddableMimeStrs = NewStrBuf();
731 ServerShutdownModule_SERV_FUNC
734 FreeStrBuf(&EmbeddableMimeStrs);
735 DeleteHash(&EmbeddableMimes);
742 is_uds = strcasecmp(ctdlhost, "uds") == 0;
744 snprintf(serv_sock_name, PATH_MAX, "%s/citadel.socket", ctdlport);
746 RegisterConditional(HKEY("COND:SERV:OPENID"), 2, conditional_serv_supports_openid, CTX_NONE);
747 RegisterConditional(HKEY("COND:SERV:NEWU"), 2, conditional_serv_newuser_disabled, CTX_NONE);
748 RegisterConditional(HKEY("COND:SERV:HAVEFULLTEXT"), 2, conditional_serv_fulltext_enabled, CTX_NONE);
749 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, NULL, CTX_NONE);
750 RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, NULL, CTX_NONE);
751 RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, NULL, CTX_NONE);
752 RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, NULL, CTX_NONE);
753 RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, NULL, CTX_NONE);
754 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, NULL, CTX_NONE);
755 RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, NULL, CTX_NONE);
756 RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, NULL, CTX_NONE);
757 /*TODO // RegisterNamespace("SERV:LDAP_SUPP", 0, 0, tmplput_serv_ldap_enabled, 0); */
763 SessionDestroyModule_SERVFUNC
766 DeleteServInfo(&sess->serv_info);