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);
69 * Tell the server that when we save a calendar event, we
70 * want invitations to be generated by the Citadel server
71 * instead of by the client.
73 serv_puts("ICAL sgi|1");
74 StrBuf_ServGetln(Buf);
76 /** Now ask the server to tell us a little bit about itself... */
78 StrBuf_ServGetln(Buf);
79 if (GetServerStatus(Buf, NULL) != 1) {
84 info = (ServInfo*)malloc(sizeof(ServInfo));
85 memset(info, 0, sizeof(ServInfo));
87 while (StrBuf_ServGetln(Buf), (strcmp(ChrPtr(Buf), "000")!= 0)) {
88 /* lprintf (1, "a: %d [%s]", a, ChrPtr(Buf));*/
91 info->serv_pid = StrToi(Buf);
92 WC->ctdl_pid = info->serv_pid;
95 info->serv_nodename = NewStrBufDup(Buf);
98 info->serv_humannode = NewStrBufDup(Buf);
101 info->serv_fqdn = NewStrBufDup(Buf);
104 info->serv_software = NewStrBufDup(Buf);
107 info->serv_rev_level = StrToi(Buf);
110 info->serv_bbs_city = NewStrBufDup(Buf);
113 info->serv_sysadm = NewStrBufDup(Buf);
116 info->serv_moreprompt = NewStrBufDup(Buf);
119 info->serv_supports_ldap = StrToi(Buf);
122 info->serv_newuser_disabled = StrToi(Buf);
125 info->serv_default_cal_zone = NewStrBufDup(Buf);
128 info->serv_supports_sieve = StrToi(Buf);
131 info->serv_fulltext_enabled = StrToi(Buf);
134 info->serv_svn_revision = NewStrBufDup(Buf);
137 info->serv_supports_openid = StrToi(Buf);
146 int GetConnected (void)
151 if (WCC->ReadBuf == NULL)
152 WCC->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
153 if (is_uds) /* unix domain socket */
154 WCC->serv_sock = uds_connectsock(serv_sock_name);
155 else /* tcp socket */
156 WCC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
158 if (WCC->serv_sock < 0) {
160 FreeStrBuf(&WCC->ReadBuf);
167 StrBuf_ServGetln(Buf); /* get the server greeting */
168 GetServerStatus(Buf, &Status);
171 /* Are there too many users already logged in? */
173 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."));
175 end_webcit_session();
180 * From what host is our user connecting? Go with
181 * the host at the other end of the HTTP socket,
182 * unless we are following X-Forwarded-For: headers
183 * and such a header has already turned up something.
185 if ( (!follow_xff) || (StrLength(WCC->Hdr->HR.browser_host) == 0) ) {
186 if (WCC->Hdr->HR.browser_host == NULL) {
187 WCC->Hdr->HR.browser_host = NewStrBuf();
188 Put(WCC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"),
189 WCC->Hdr->HR.browser_host, HFreeStrBuf);
191 locate_host(WCC->Hdr->HR.browser_host, WCC->Hdr->http_sock);
193 if (WCC->serv_info == NULL)
194 WCC->serv_info = get_serv_info(WCC->Hdr->HR.browser_host, WCC->Hdr->HR.user_agent);
195 if (WCC->serv_info == NULL){
197 wc_printf(_("Received unexpected answer from Citadel "
198 "server; bailing out."));
199 hprintf("HTTP/1.1 200 OK\r\n");
200 hprintf("Content-type: text/plain; charset=utf-8\r\n");
202 end_webcit_session();
205 if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
207 wc_printf(_("You are connected to a Citadel "
208 "server running Citadel %d.%02d. \n"
209 "In order to run this version of WebCit "
210 "you must also have Citadel %d.%02d or"
212 WCC->serv_info->serv_rev_level / 100,
213 WCC->serv_info->serv_rev_level % 100,
214 MINIMUM_CIT_VERSION / 100,
215 MINIMUM_CIT_VERSION % 100
217 hprintf("HTTP/1.1 200 OK\r\n");
218 hprintf("Content-type: text/plain; charset=utf-8\r\n");
220 end_webcit_session();
223 SetInlinMimeRenderers();
229 * Read Citadel variformat text and spit it out as HTML.
230 * align html align string
232 inline void fmout(char *align)
234 _fmout(WC->WBuf, align);
237 void _fmout(StrBuf *Target, char *align)
243 StrBufAppendPrintf(Target, "<div align=%s>\n", align);
244 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
246 if ((intext == 1) && (isspace(buf[0]))) {
252 * Quoted text should be displayed in italics and in a
253 * different colour. This code understands Citadel-style
254 * " >" quotes and will convert to <BLOCKQUOTE> tags.
256 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
257 StrBufAppendBufPlain(Target, HKEY("<BLOCKQUOTE>"), 0);
259 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
260 StrBufAppendBufPlain(Target, HKEY("</BLOCKQUOTE>"), 0);
263 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
264 strcpy(buf, &buf[2]);
266 /* Activate embedded URL's */
267 url(buf, sizeof(buf));
270 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
275 wc_printf("</div><br />\n");
278 void FmOut(StrBuf *Target, char *align, StrBuf *Source)
280 const char *ptr, *pte;
281 const char *BufPtr = NULL;
282 StrBuf *Line = NewStrBufPlain(NULL, SIZ);
283 StrBuf *Line1 = NewStrBufPlain(NULL, SIZ);
284 StrBuf *Line2 = NewStrBufPlain(NULL, SIZ);
291 StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
293 if (StrLength(Source) > 0)
296 StrBufSipLine(Line, Source, &BufPtr);
300 len = StrLength(Line);
303 if ((intext == 1) && (isspace(*ptr))) {
304 StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
307 if (isspace(*ptr)) while ((ptr < pte) &&
318 * Quoted text should be displayed in italics and in a
319 * different colour. This code understands Citadel-style
320 * " >" quotes and will convert to <BLOCKQUOTE> tags.
322 if (i > 0) StrBufCutLeft(Line, i);
325 for (i = bn; i < bq; i++)
326 StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
327 for (i = bq; i < bn; i++)
328 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
331 if (StrLength(Line) == 0)
333 /** Activate embedded URL's */
334 UrlizeText(Line1, Line, Line2);
336 StrEscAppend(Target, Line1, NULL, 0, 0);
338 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
340 while ((BufPtr != StrBufNOTNULL) &&
343 for (i = 0; i < bn; i++) {
344 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
346 StrBufAppendBufPlain(Target, HKEY("</div><br>\n"), 0);
354 * Read Citadel variformat text and spit it out as HTML in a form
355 * suitable for embedding in another message (forward/quote).
356 * (NO LINEBREAKS ALLOWED HERE!)
358 void pullquote_fmout(void) {
363 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
365 if ((intext == 1) && (isspace(buf[0]))) {
371 * Quoted text should be displayed in italics and in a
372 * different colour. This code understands Citadel-style
373 * " >" quotes and will convert to <BLOCKQUOTE> tags.
375 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
376 wc_printf("<BLOCKQUOTE>");
378 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
379 wc_printf("</BLOCKQUOTE>");
382 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
383 strcpy(buf, &buf[2]);
397 * Transmit message text (in memory) to the server.
399 void text_to_server(char *ptr)
407 while (ptr[pos] != 0) {
411 while ( (isspace(buf[len - 1]))
413 && (buf[1] != '\0') )
417 if (ptr[pos] != 0) strcat(buf, " ");
422 if ((ch == 32) && (strlen(buf) > 200)) {
427 if (strlen(buf) > 250) {
438 * Transmit message text (in memory) to the server, converting to Quoted-Printable encoding as we go.
440 void text_to_server_qp(char *ptr)
442 unsigned char ch, buf[256];
450 while (ptr[pos] != 0) {
451 ch = (unsigned char)(ptr[pos++]);
454 /* ignore carriage returns */
457 /* hard line break */
458 if (output_len > 0) {
459 if (isspace(buf[output_len-1])) {
460 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
464 buf[output_len++] = 0;
465 serv_puts((char *)buf);
469 buf[output_len++] = ch;
471 else if ( (ch >= 32) && (ch <= 60) ) {
472 buf[output_len++] = ch;
474 else if ( (ch >= 62) && (ch <= 126) ) {
475 buf[output_len++] = ch;
478 sprintf((char *)&buf[output_len], "=%02X", ch);
482 if (output_len > 72) {
483 /* soft line break */
484 if (isspace(buf[output_len-1])) {
485 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
488 buf[output_len++] = '=';
489 buf[output_len++] = 0;
490 serv_puts((char *)buf);
495 /* end of data - transmit anything that's left */
496 if (output_len > 0) {
497 if (isspace(buf[output_len-1])) {
498 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
501 buf[output_len++] = 0;
502 serv_puts((char *)buf);
511 * translate server message output to text (used for editing room info files and such)
513 void server_to_text()
519 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
520 if ((buf[0] == 32) && (count > 0)) {
523 wc_printf("%s", buf);
532 * Read text from server, appending to a string buffer until the
533 * usual 000 terminator is found. Caller is responsible for freeing
534 * the returned pointer.
536 int read_server_text(StrBuf *Buf, long *nLines)
544 while ((WCC->serv_sock!=-1) &&
545 (nRead = StrBuf_ServGetln(Buf), (nRead >= 0) ))
547 if (strcmp(ChrPtr(Buf) + nTotal, "000") != 0) {
548 StrBufCutRight(Buf, nRead);
560 int GetServerStatus(StrBuf *Line, long* FullState)
562 if (FullState != NULL)
563 *FullState = StrTol(Line);
564 return ChrPtr(Line)[0] - 48;
568 void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP)
570 StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
573 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP)
576 if (WCC->serv_info == NULL)
578 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_nodename, 0);
581 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP)
584 if (WCC->serv_info == NULL)
586 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_humannode, 0);
589 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP)
592 if (WCC->serv_info == NULL)
594 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_fqdn, 0);
597 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP)
600 if (WCC->serv_info == NULL)
602 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_software, 0);
605 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP)
608 if (WCC->serv_info == NULL)
610 StrBufAppendPrintf(Target, "%d.%02d",
611 WCC->serv_info->serv_rev_level / 100,
612 WCC->serv_info->serv_rev_level % 100);
614 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP)
617 if (WCC->serv_info == NULL)
619 return WCC->serv_info->serv_newuser_disabled != 0;
622 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP)
625 if (WCC->serv_info == NULL)
627 return WCC->serv_info->serv_supports_openid != 0;
630 int conditional_serv_fulltext_enabled(StrBuf *Target, WCTemplputParams *TP)
633 if (WCC->serv_info == NULL)
635 return WCC->serv_info->serv_fulltext_enabled != 0;
638 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP)
641 if (WCC->serv_info == NULL)
643 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
646 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP)
655 serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
657 StrBuf_ServGetln(Line);
658 if (GetServerStatus(Line, NULL) == 1) {
659 while (!Done && (StrBuf_ServGetln(Line)>=0)) {
660 if ( (StrLength(Line)==3) &&
661 !strcmp(ChrPtr(Line), "000"))
666 StrBufAppendBufPlain(Buf, "\n", 1, 0);
667 StrBufAppendBuf(Buf, Line, 0);
673 FmOut(Line, "center", Buf);
674 StrBufAppendTemplate(Target, TP, Line, 1);
681 void RegisterEmbeddableMimeType(const char *MimeType, long MTLen, int Priority)
684 MT = NewStrBufPlain(MimeType, MTLen);
685 Put(EmbeddableMimes, IKEY(Priority), MT, HFreeStrBuf);
688 void CreateMimeStr(void)
695 it = GetNewHashPos(EmbeddableMimes, 0);
696 while (GetNextHashPos(EmbeddableMimes, it, &len, &Key, &vMime) &&
698 if (StrLength(EmbeddableMimeStrs) > 0)
699 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("|"), 0);
701 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("MSGP "), 0);
702 StrBufAppendBuf(EmbeddableMimeStrs, (StrBuf*) vMime, 0);
708 ServerStartModule_SERV_FUNC
711 EmbeddableMimes = NewHash(1, Flathash);
712 EmbeddableMimeStrs = NewStrBuf();
717 ServerShutdownModule_SERV_FUNC
720 FreeStrBuf(&EmbeddableMimeStrs);
721 DeleteHash(&EmbeddableMimes);
728 is_uds = strcasecmp(ctdlhost, "uds") == 0;
730 snprintf(serv_sock_name, PATH_MAX, "%s/citadel.socket", ctdlport);
732 RegisterConditional(HKEY("COND:SERV:OPENID"), 2, conditional_serv_supports_openid, CTX_NONE);
733 RegisterConditional(HKEY("COND:SERV:NEWU"), 2, conditional_serv_newuser_disabled, CTX_NONE);
734 RegisterConditional(HKEY("COND:SERV:HAVEFULLTEXT"), 2, conditional_serv_fulltext_enabled, CTX_NONE);
735 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, NULL, CTX_NONE);
736 RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, NULL, CTX_NONE);
737 RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, NULL, CTX_NONE);
738 RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, NULL, CTX_NONE);
739 RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, NULL, CTX_NONE);
740 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, NULL, CTX_NONE);
741 RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, NULL, CTX_NONE);
742 RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, NULL, CTX_NONE);
743 /*TODO // RegisterNamespace("SERV:LDAP_SUPP", 0, 0, tmplput_serv_ldap_enabled, 0); */
749 SessionDestroyModule_SERVFUNC
752 DeleteServInfo(&sess->serv_info);