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, atoi(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);
187 /* Server isn't ready for us? */
188 if (short_status != 2) {
190 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."));
193 wc_printf("%ld %s\n",
195 _("Received unexpected answer from Citadel server; bailing out.")
199 end_webcit_session();
203 /* 2010jun03: every now and then the connection to Citadel dies before this point. why? */
206 * From what host is our user connecting? Go with
207 * the host at the other end of the HTTP socket,
208 * unless we are following X-Forwarded-For: headers
209 * and such a header has already turned up something.
211 if ( (!follow_xff) || (StrLength(WCC->Hdr->HR.browser_host) == 0) ) {
212 if (WCC->Hdr->HR.browser_host == NULL) {
213 WCC->Hdr->HR.browser_host = NewStrBuf();
214 Put(WCC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"),
215 WCC->Hdr->HR.browser_host, HFreeStrBuf);
217 locate_host(WCC->Hdr->HR.browser_host, WCC->Hdr->http_sock);
219 if (WCC->serv_info == NULL) {
220 WCC->serv_info = get_serv_info(WCC->Hdr->HR.browser_host, WCC->Hdr->HR.user_agent);
222 if (WCC->serv_info == NULL){
224 wc_printf(_("Received unexpected answer from Citadel server; bailing out."));
225 hprintf("HTTP/1.1 200 OK\r\n");
226 hprintf("Content-type: text/plain; charset=utf-8\r\n");
228 end_webcit_session();
231 if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
233 wc_printf(_("You are connected to a Citadel "
234 "server running Citadel %d.%02d. \n"
235 "In order to run this version of WebCit "
236 "you must also have Citadel %d.%02d or"
238 WCC->serv_info->serv_rev_level / 100,
239 WCC->serv_info->serv_rev_level % 100,
240 MINIMUM_CIT_VERSION / 100,
241 MINIMUM_CIT_VERSION % 100
243 hprintf("HTTP/1.1 200 OK\r\n");
244 hprintf("Content-type: text/plain; charset=utf-8\r\n");
246 end_webcit_session();
249 SetInlinMimeRenderers();
255 * Read Citadel variformat text and spit it out as HTML.
256 * align html align string
258 inline void fmout(char *align)
260 _fmout(WC->WBuf, align);
263 void _fmout(StrBuf *Target, char *align)
269 StrBufAppendPrintf(Target, "<div align=%s>\n", align);
270 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
272 if ((intext == 1) && (isspace(buf[0]))) {
278 * Quoted text should be displayed in italics and in a
279 * different colour. This code understands Citadel-style
280 * " >" quotes and will convert to <BLOCKQUOTE> tags.
282 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
283 StrBufAppendBufPlain(Target, HKEY("<BLOCKQUOTE>"), 0);
285 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
286 StrBufAppendBufPlain(Target, HKEY("</BLOCKQUOTE>"), 0);
289 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
290 strcpy(buf, &buf[2]);
292 /* Activate embedded URL's */
293 url(buf, sizeof(buf));
296 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
301 wc_printf("</div><br />\n");
304 void FmOut(StrBuf *Target, char *align, StrBuf *Source)
306 const char *ptr, *pte;
307 const char *BufPtr = NULL;
308 StrBuf *Line = NewStrBufPlain(NULL, SIZ);
309 StrBuf *Line1 = NewStrBufPlain(NULL, SIZ);
310 StrBuf *Line2 = NewStrBufPlain(NULL, SIZ);
317 StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
319 if (StrLength(Source) > 0)
322 StrBufSipLine(Line, Source, &BufPtr);
326 len = StrLength(Line);
329 if ((intext == 1) && (isspace(*ptr))) {
330 StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
333 if (isspace(*ptr)) while ((ptr < pte) &&
344 * Quoted text should be displayed in italics and in a
345 * different colour. This code understands Citadel-style
346 * " >" quotes and will convert to <BLOCKQUOTE> tags.
348 if (i > 0) StrBufCutLeft(Line, i);
351 for (i = bn; i < bq; i++)
352 StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
353 for (i = bq; i < bn; i++)
354 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
357 if (StrLength(Line) == 0)
360 /* Activate embedded URL's */
361 UrlizeText(Line1, Line, Line2);
363 StrEscAppend(Target, Line1, NULL, 0, 0);
365 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
367 while ((BufPtr != StrBufNOTNULL) &&
370 for (i = 0; i < bn; i++) {
371 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
373 StrBufAppendBufPlain(Target, HKEY("</div><br>\n"), 0);
381 * Read Citadel variformat text and spit it out as HTML in a form
382 * suitable for embedding in another message (forward/quote).
383 * (NO LINEBREAKS ALLOWED HERE!)
385 void pullquote_fmout(void) {
390 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
392 if ((intext == 1) && (isspace(buf[0]))) {
398 * Quoted text should be displayed in italics and in a
399 * different colour. This code understands Citadel-style
400 * " >" quotes and will convert to <BLOCKQUOTE> tags.
402 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
403 wc_printf("<BLOCKQUOTE>");
405 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
406 wc_printf("</BLOCKQUOTE>");
409 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
410 strcpy(buf, &buf[2]);
424 * Transmit message text (in memory) to the server.
426 void text_to_server(char *ptr)
434 while (ptr[pos] != 0) {
438 while ( (isspace(buf[len - 1]))
440 && (buf[1] != '\0') )
444 if (ptr[pos] != 0) strcat(buf, " ");
449 if ((ch == 32) && (strlen(buf) > 200)) {
454 if (strlen(buf) > 250) {
465 * Transmit message text (in memory) to the server, converting to Quoted-Printable encoding as we go.
467 void text_to_server_qp(char *ptr)
469 unsigned char ch, buf[256];
477 while (ptr[pos] != 0) {
478 ch = (unsigned char)(ptr[pos++]);
481 /* ignore carriage returns */
484 /* hard line break */
485 if (output_len > 0) {
486 if (isspace(buf[output_len-1])) {
487 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
491 buf[output_len++] = 0;
492 serv_puts((char *)buf);
496 buf[output_len++] = ch;
498 else if ( (ch >= 32) && (ch <= 60) ) {
499 buf[output_len++] = ch;
501 else if ( (ch >= 62) && (ch <= 126) ) {
502 buf[output_len++] = ch;
505 sprintf((char *)&buf[output_len], "=%02X", ch);
509 if (output_len > 72) {
510 /* soft line break */
511 if (isspace(buf[output_len-1])) {
512 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
515 buf[output_len++] = '=';
516 buf[output_len++] = 0;
517 serv_puts((char *)buf);
522 /* end of data - transmit anything that's left */
523 if (output_len > 0) {
524 if (isspace(buf[output_len-1])) {
525 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
528 buf[output_len++] = 0;
529 serv_puts((char *)buf);
538 * translate server message output to text (used for editing room info files and such)
540 void server_to_text()
546 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
547 if ((buf[0] == 32) && (count > 0)) {
550 wc_printf("%s", buf);
559 * Read text from server, appending to a string buffer until the
560 * usual 000 terminator is found. Caller is responsible for freeing
561 * the returned pointer.
563 int read_server_text(StrBuf *Buf, long *nLines)
571 while ((WCC->serv_sock!=-1) &&
572 (nRead = StrBuf_ServGetln(Buf), (nRead >= 0) ))
574 if (strcmp(ChrPtr(Buf) + nTotal, "000") != 0) {
575 StrBufCutRight(Buf, nRead);
587 int GetServerStatus(StrBuf *Line, long* FullState)
589 if (FullState != NULL)
590 *FullState = StrTol(Line);
591 return ChrPtr(Line)[0] - 48;
595 void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP)
597 StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
600 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP)
603 if (WCC->serv_info == NULL)
605 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_nodename, 0);
608 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP)
611 if (WCC->serv_info == NULL)
613 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_humannode, 0);
616 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP)
619 if (WCC->serv_info == NULL)
621 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_fqdn, 0);
624 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP)
627 if (WCC->serv_info == NULL)
629 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_software, 0);
632 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP)
635 if (WCC->serv_info == NULL)
637 StrBufAppendPrintf(Target, "%d.%02d",
638 WCC->serv_info->serv_rev_level / 100,
639 WCC->serv_info->serv_rev_level % 100);
641 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP)
644 if (WCC->serv_info == NULL)
646 return WCC->serv_info->serv_newuser_disabled != 0;
649 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP)
652 if (WCC->serv_info == NULL)
654 return WCC->serv_info->serv_supports_openid != 0;
657 int conditional_serv_fulltext_enabled(StrBuf *Target, WCTemplputParams *TP)
660 if (WCC->serv_info == NULL)
662 return WCC->serv_info->serv_fulltext_enabled != 0;
665 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP)
668 if (WCC->serv_info == NULL)
670 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
673 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP)
682 serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
684 StrBuf_ServGetln(Line);
685 if (GetServerStatus(Line, NULL) == 1) {
686 while (!Done && (StrBuf_ServGetln(Line)>=0)) {
687 if ( (StrLength(Line)==3) &&
688 !strcmp(ChrPtr(Line), "000"))
693 StrBufAppendBufPlain(Buf, "\n", 1, 0);
694 StrBufAppendBuf(Buf, Line, 0);
700 FmOut(Line, "center", Buf);
701 StrBufAppendTemplate(Target, TP, Line, 1);
708 void RegisterEmbeddableMimeType(const char *MimeType, long MTLen, int Priority)
711 MT = NewStrBufPlain(MimeType, MTLen);
712 Put(EmbeddableMimes, IKEY(Priority), MT, HFreeStrBuf);
715 void CreateMimeStr(void)
722 it = GetNewHashPos(EmbeddableMimes, 0);
723 while (GetNextHashPos(EmbeddableMimes, it, &len, &Key, &vMime) &&
725 if (StrLength(EmbeddableMimeStrs) > 0)
726 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("|"), 0);
728 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("MSGP "), 0);
729 StrBufAppendBuf(EmbeddableMimeStrs, (StrBuf*) vMime, 0);
735 ServerStartModule_SERV_FUNC
738 EmbeddableMimes = NewHash(1, Flathash);
739 EmbeddableMimeStrs = NewStrBuf();
744 ServerShutdownModule_SERV_FUNC
747 FreeStrBuf(&EmbeddableMimeStrs);
748 DeleteHash(&EmbeddableMimes);
755 is_uds = strcasecmp(ctdlhost, "uds") == 0;
757 snprintf(serv_sock_name, PATH_MAX, "%s/citadel.socket", ctdlport);
759 RegisterConditional(HKEY("COND:SERV:OPENID"), 2, conditional_serv_supports_openid, CTX_NONE);
760 RegisterConditional(HKEY("COND:SERV:NEWU"), 2, conditional_serv_newuser_disabled, CTX_NONE);
761 RegisterConditional(HKEY("COND:SERV:HAVEFULLTEXT"), 2, conditional_serv_fulltext_enabled, CTX_NONE);
762 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, NULL, CTX_NONE);
763 RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, NULL, CTX_NONE);
764 RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, NULL, CTX_NONE);
765 RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, NULL, CTX_NONE);
766 RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, NULL, CTX_NONE);
767 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, NULL, CTX_NONE);
768 RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, NULL, CTX_NONE);
769 RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, NULL, CTX_NONE);
775 SessionDestroyModule_SERVFUNC
778 DeleteServInfo(&sess->serv_info);