9 char serv_sock_name[PATH_MAX] = "";
11 HashList *EmbeddableMimes = NULL;
12 StrBuf *EmbeddableMimeStrs = NULL;
15 void SetInlinMimeRenderers(void)
21 /* Tell the server what kind of richtext we prefer */
22 serv_putbuf(EmbeddableMimeStrs);
23 StrBuf_ServGetln(Buf);
29 void DeleteServInfo(ServInfo **FreeMe)
33 FreeStrBuf(&(*FreeMe)->serv_nodename);
34 FreeStrBuf(&(*FreeMe)->serv_humannode);
35 FreeStrBuf(&(*FreeMe)->serv_fqdn);
36 FreeStrBuf(&(*FreeMe)->serv_software);
37 FreeStrBuf(&(*FreeMe)->serv_bbs_city);
38 FreeStrBuf(&(*FreeMe)->serv_sysadm);
39 FreeStrBuf(&(*FreeMe)->serv_moreprompt);
40 FreeStrBuf(&(*FreeMe)->serv_default_cal_zone);
41 FreeStrBuf(&(*FreeMe)->serv_svn_revision);
47 * get info about the server we've connected to
49 * browser_host the citadel we want to connect to
50 * user_agent which browser uses our client?
52 ServInfo *get_serv_info(StrBuf *browser_host, StrBuf *user_agent)
60 /* Tell the server what kind of client is connecting */
61 serv_printf("IDEN %d|%d|%d|%s|%s",
68 StrBuf_ServGetln(Buf);
69 if (GetServerStatus(Buf, NULL) != 2) {
70 lprintf(0, "get_serv_info(IDEN): unexpected answer [%s]\n",
77 * Tell the server that when we save a calendar event, we
78 * want invitations to be generated by the Citadel server
79 * instead of by the client.
81 serv_puts("ICAL sgi|1");
82 StrBuf_ServGetln(Buf);
83 if (GetServerStatus(Buf, NULL) != 2) {
84 lprintf(0, "get_serv_info(ICAL sgi|1): unexpected answer [%s]\n",
90 /* Now ask the server to tell us a little bit about itself... */
92 StrBuf_ServGetln(Buf);
93 if (GetServerStatus(Buf, NULL) != 1) {
94 lprintf(0, "get_serv_info(INFO sgi|1): unexpected answer [%s]\n",
100 info = (ServInfo*)malloc(sizeof(ServInfo));
101 memset(info, 0, sizeof(ServInfo));
103 while (StrBuf_ServGetln(Buf), (strcmp(ChrPtr(Buf), "000")!= 0)) {
106 info->serv_pid = StrToi(Buf);
107 WC->ctdl_pid = info->serv_pid;
110 info->serv_nodename = NewStrBufDup(Buf);
113 info->serv_humannode = NewStrBufDup(Buf);
116 info->serv_fqdn = NewStrBufDup(Buf);
119 info->serv_software = NewStrBufDup(Buf);
122 info->serv_rev_level = StrToi(Buf);
125 info->serv_bbs_city = NewStrBufDup(Buf);
128 info->serv_sysadm = NewStrBufDup(Buf);
131 info->serv_moreprompt = NewStrBufDup(Buf);
134 info->serv_supports_ldap = StrToi(Buf);
137 info->serv_newuser_disabled = StrToi(Buf);
140 info->serv_default_cal_zone = NewStrBufDup(Buf);
143 info->serv_supports_sieve = StrToi(Buf);
146 info->serv_fulltext_enabled = StrToi(Buf);
149 info->serv_svn_revision = NewStrBufDup(Buf);
152 info->serv_supports_openid = StrToi(Buf);
161 int GetConnected (void)
166 if (WCC->ReadBuf == NULL)
167 WCC->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
168 if (is_uds) /* unix domain socket */
169 WCC->serv_sock = uds_connectsock(serv_sock_name);
170 else /* tcp socket */
171 WCC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
173 if (WCC->serv_sock < 0) {
175 FreeStrBuf(&WCC->ReadBuf);
183 StrBuf_ServGetln(Buf); /* get the server greeting */
184 short_status = GetServerStatus(Buf, &Status);
185 lprintf(9, "\033[32mGREETING: %s\033[0m\n", ChrPtr(Buf));
188 /* Server isn't ready for us? */
189 if (short_status != 2) {
191 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."));
194 wc_printf("%ld %s\n",
196 _("Received unexpected answer from Citadel server; bailing out.")
200 end_webcit_session();
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 200 OK\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 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP)
667 if (WCC->serv_info == NULL)
669 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
672 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP)
681 serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
683 StrBuf_ServGetln(Line);
684 if (GetServerStatus(Line, NULL) == 1) {
685 while (!Done && (StrBuf_ServGetln(Line)>=0)) {
686 if ( (StrLength(Line)==3) &&
687 !strcmp(ChrPtr(Line), "000"))
692 StrBufAppendBufPlain(Buf, "\n", 1, 0);
693 StrBufAppendBuf(Buf, Line, 0);
699 FmOut(Line, "center", Buf);
700 StrBufAppendTemplate(Target, TP, Line, 1);
707 void RegisterEmbeddableMimeType(const char *MimeType, long MTLen, int Priority)
710 MT = NewStrBufPlain(MimeType, MTLen);
711 Put(EmbeddableMimes, IKEY(Priority), MT, HFreeStrBuf);
714 void CreateMimeStr(void)
721 it = GetNewHashPos(EmbeddableMimes, 0);
722 while (GetNextHashPos(EmbeddableMimes, it, &len, &Key, &vMime) &&
724 if (StrLength(EmbeddableMimeStrs) > 0)
725 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("|"), 0);
727 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("MSGP "), 0);
728 StrBufAppendBuf(EmbeddableMimeStrs, (StrBuf*) vMime, 0);
734 ServerStartModule_SERV_FUNC
737 EmbeddableMimes = NewHash(1, Flathash);
738 EmbeddableMimeStrs = NewStrBuf();
743 ServerShutdownModule_SERV_FUNC
746 FreeStrBuf(&EmbeddableMimeStrs);
747 DeleteHash(&EmbeddableMimes);
754 is_uds = strcasecmp(ctdlhost, "uds") == 0;
756 snprintf(serv_sock_name, PATH_MAX, "%s/citadel.socket", ctdlport);
758 RegisterConditional(HKEY("COND:SERV:OPENID"), 2, conditional_serv_supports_openid, CTX_NONE);
759 RegisterConditional(HKEY("COND:SERV:NEWU"), 2, conditional_serv_newuser_disabled, CTX_NONE);
760 RegisterConditional(HKEY("COND:SERV:HAVEFULLTEXT"), 2, conditional_serv_fulltext_enabled, CTX_NONE);
761 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, NULL, CTX_NONE);
762 RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, NULL, CTX_NONE);
763 RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, NULL, CTX_NONE);
764 RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, NULL, CTX_NONE);
765 RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, NULL, CTX_NONE);
766 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, NULL, CTX_NONE);
767 RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, NULL, CTX_NONE);
768 RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, NULL, CTX_NONE);
774 SessionDestroyModule_SERVFUNC
777 DeleteServInfo(&sess->serv_info);