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 citadell 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);
170 /* Are there too many users already logged in? */
172 wprintf(_("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."));
174 end_webcit_session();
179 * From what host is our user connecting? Go with
180 * the host at the other end of the HTTP socket,
181 * unless we are following X-Forwarded-For: headers
182 * and such a header has already turned up something.
184 if ( (!follow_xff) || (StrLength(WCC->Hdr->HR.browser_host) == 0) ) {
185 if (WCC->Hdr->HR.browser_host == NULL) {
186 WCC->Hdr->HR.browser_host = NewStrBuf();
187 Put(WCC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"),
188 WCC->Hdr->HR.browser_host, HFreeStrBuf);
190 locate_host(WCC->Hdr->HR.browser_host, WCC->Hdr->http_sock);
192 if (WCC->serv_info == NULL)
193 WCC->serv_info = get_serv_info(WCC->Hdr->HR.browser_host, WCC->Hdr->HR.user_agent);
194 if (WCC->serv_info == NULL){
196 wprintf(_("Received unexpected answer from Citadel "
197 "server; bailing out."));
198 hprintf("HTTP/1.1 200 OK\r\n");
199 hprintf("Content-type: text/plain; charset=utf-8\r\n");
201 end_webcit_session();
204 if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
206 wprintf(_("You are connected to a Citadel "
207 "server running Citadel %d.%02d. \n"
208 "In order to run this version of WebCit "
209 "you must also have Citadel %d.%02d or"
211 WCC->serv_info->serv_rev_level / 100,
212 WCC->serv_info->serv_rev_level % 100,
213 MINIMUM_CIT_VERSION / 100,
214 MINIMUM_CIT_VERSION % 100
216 hprintf("HTTP/1.1 200 OK\r\n");
217 hprintf("Content-type: text/plain; charset=utf-8\r\n");
219 end_webcit_session();
222 SetInlinMimeRenderers();
228 * Read Citadel variformat text and spit it out as HTML.
229 * align html align string
231 inline void fmout(char *align)
233 _fmout(WC->WBuf, align);
236 void _fmout(StrBuf *Target, char *align)
242 StrBufAppendPrintf(Target, "<div align=%s>\n", align);
243 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
245 if ((intext == 1) && (isspace(buf[0]))) {
251 * Quoted text should be displayed in italics and in a
252 * different colour. This code understands Citadel-style
253 * " >" quotes and will convert to <BLOCKQUOTE> tags.
255 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
256 StrBufAppendBufPlain(Target, HKEY("<BLOCKQUOTE>"), 0);
258 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
259 StrBufAppendBufPlain(Target, HKEY("</BLOCKQUOTE>"), 0);
262 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
263 strcpy(buf, &buf[2]);
265 /** Activate embedded URL's */
266 url(buf, sizeof(buf));
269 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
274 wprintf("</div><br />\n");
277 void FmOut(StrBuf *Target, char *align, StrBuf *Source)
279 const char *ptr, *pte;
280 const char *BufPtr = NULL;
281 StrBuf *Line = NewStrBufPlain(NULL, SIZ);
282 StrBuf *Line1 = NewStrBufPlain(NULL, SIZ);
283 StrBuf *Line2 = NewStrBufPlain(NULL, SIZ);
290 StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
292 if (StrLength(Source) > 0)
295 StrBufSipLine(Line, Source, &BufPtr);
299 len = StrLength(Line);
302 if ((intext == 1) && (isspace(*ptr))) {
303 StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
306 if (isspace(*ptr)) while ((ptr < pte) &&
317 * Quoted text should be displayed in italics and in a
318 * different colour. This code understands Citadel-style
319 * " >" quotes and will convert to <BLOCKQUOTE> tags.
321 if (i > 0) StrBufCutLeft(Line, i);
324 for (i = bn; i < bq; i++)
325 StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
326 for (i = bq; i < bn; i++)
327 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
330 if (StrLength(Line) == 0)
332 /** Activate embedded URL's */
333 UrlizeText(Line1, Line, Line2);
335 StrEscAppend(Target, Line1, NULL, 0, 0);
337 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
339 while ((BufPtr != StrBufNOTNULL) &&
342 for (i = 0; i < bn; i++)
343 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
344 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 wprintf("<BLOCKQUOTE>");
378 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
379 wprintf("</BLOCKQUOTE>");
382 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
383 strcpy(buf, &buf[2]);
397 * Transmit message text (in memory) to the server.
399 * ptr Pointer to the message being transmitted
401 void text_to_server(char *ptr)
409 while (ptr[pos] != 0) {
413 while ( (isspace(buf[len - 1]))
415 && (buf[1] != '\0') )
419 if (ptr[pos] != 0) strcat(buf, " ");
424 if ((ch == 32) && (strlen(buf) > 200)) {
429 if (strlen(buf) > 250) {
440 * Transmit message text (in memory) to the server,
441 * converting to Quoted-Printable encoding as we go.
443 * ptr Pointer to the message being transmitted
445 void text_to_server_qp(char *ptr)
447 unsigned char ch, buf[256];
455 while (ptr[pos] != 0) {
456 ch = (unsigned char)(ptr[pos++]);
459 /* ignore carriage returns */
462 /* hard line break */
463 if (output_len > 0) {
464 if (isspace(buf[output_len-1])) {
465 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
469 buf[output_len++] = 0;
470 serv_puts((char *)buf);
474 buf[output_len++] = ch;
476 else if ( (ch >= 32) && (ch <= 60) ) {
477 buf[output_len++] = ch;
479 else if ( (ch >= 62) && (ch <= 126) ) {
480 buf[output_len++] = ch;
483 sprintf((char *)&buf[output_len], "=%02X", ch);
487 if (output_len > 72) {
488 /* soft line break */
489 if (isspace(buf[output_len-1])) {
490 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
493 buf[output_len++] = '=';
494 buf[output_len++] = 0;
495 serv_puts((char *)buf);
500 /* end of data - transmit anything that's left */
501 if (output_len > 0) {
502 if (isspace(buf[output_len-1])) {
503 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
506 buf[output_len++] = 0;
507 serv_puts((char *)buf);
516 * translate server message output to text
517 * (used for editing room info files and such)
519 void server_to_text()
525 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
526 if ((buf[0] == 32) && (count > 0)) {
537 * Read binary data from server into memory using a series of
538 * server READ commands.
539 * \return the read content as StrBuf
541 int read_server_binary(StrBuf *Ret, size_t total_len, StrBuf *Buf)
545 size_t thisblock = 0;
550 while (bytes < total_len) {
552 if ((total_len - bytes) < thisblock) {
553 thisblock = total_len - bytes;
554 if (thisblock == 0) {
560 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
561 if (StrBuf_ServGetln(Buf) > 0)
563 if (GetServerStatus(Buf, NULL) == 6)
565 StrBufCutLeft(Buf, 4);
566 thisblock = StrTol(Buf);
567 if (!WC->connected) {
572 StrBuf_ServGetBLOBBuffered(Ret, thisblock);
577 lprintf(3, "Error: %s\n", &buf[4]);
582 return StrLength(Ret);
587 * Read text from server, appending to a string buffer until the
588 * usual 000 terminator is found. Caller is responsible for freeing
589 * the returned pointer.
591 int read_server_text(StrBuf *Buf, long *nLines)
599 while ((WCC->serv_sock!=-1) &&
600 (nRead = StrBuf_ServGetln(Buf), (nRead >= 0) ))
602 if (strcmp(ChrPtr(Buf) + nTotal, "000") != 0) {
603 StrBufCutRight(Buf, nRead);
619 int GetServerStatus(StrBuf *Line, long* FullState)
621 if (FullState != NULL)
622 *FullState = StrTol(Line);
623 return ChrPtr(Line)[0] - 48;
627 void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP)
629 StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
632 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP)
635 if (WCC->serv_info == NULL)
637 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_nodename, 0);
640 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP)
643 if (WCC->serv_info == NULL)
645 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_humannode, 0);
648 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP)
651 if (WCC->serv_info == NULL)
653 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_fqdn, 0);
656 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP)
659 if (WCC->serv_info == NULL)
661 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_software, 0);
664 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP)
667 if (WCC->serv_info == NULL)
669 StrBufAppendPrintf(Target, "%d.%02d",
670 WCC->serv_info->serv_rev_level / 100,
671 WCC->serv_info->serv_rev_level % 100);
673 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP)
676 if (WCC->serv_info == NULL)
678 return WCC->serv_info->serv_newuser_disabled != 0;
680 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP)
683 if (WCC->serv_info == NULL)
685 return WCC->serv_info->serv_supports_openid != 0;
687 int conditional_serv_fulltext_enabled(StrBuf *Target, WCTemplputParams *TP)
690 if (WCC->serv_info == NULL)
692 return WCC->serv_info->serv_fulltext_enabled != 0;
697 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP)
700 if (WCC->serv_info == NULL)
702 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
706 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP)
715 serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
717 StrBuf_ServGetln(Line);
718 if (GetServerStatus(Line, NULL) == 1) {
719 while (!Done && (StrBuf_ServGetln(Line)>=0)) {
720 if ( (StrLength(Line)==3) &&
721 !strcmp(ChrPtr(Line), "000"))
726 StrBufAppendBufPlain(Buf, "\n", 1, 0);
727 StrBufAppendBuf(Buf, Line, 0);
733 FmOut(Line, "center", Buf);
734 StrBufAppendTemplate(Target, TP, Line, 1);
741 void RegisterEmbeddableMimeType(const char *MimeType, long MTLen, int Priority)
744 MT = NewStrBufPlain(MimeType, MTLen);
745 Put(EmbeddableMimes, IKEY(Priority), MT, HFreeStrBuf);
748 void CreateMimeStr(void)
755 it = GetNewHashPos(EmbeddableMimes, 0);
756 while (GetNextHashPos(EmbeddableMimes, it, &len, &Key, &vMime) &&
758 if (StrLength(EmbeddableMimeStrs) > 0)
759 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("|"), 0);
761 StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("MSGP "), 0);
762 StrBufAppendBuf(EmbeddableMimeStrs, (StrBuf*) vMime, 0);
768 ServerStartModule_SERV_FUNC
771 EmbeddableMimes = NewHash(1, Flathash);
772 EmbeddableMimeStrs = NewStrBuf();
777 ServerShutdownModule_SERV_FUNC
780 FreeStrBuf(&EmbeddableMimeStrs);
781 DeleteHash(&EmbeddableMimes);
788 is_uds = strcasecmp(ctdlhost, "uds") == 0;
790 snprintf(serv_sock_name, PATH_MAX, "%s/citadel.socket", ctdlport);
792 RegisterConditional(HKEY("COND:SERV:OPENID"), 2, conditional_serv_supports_openid, CTX_NONE);
793 RegisterConditional(HKEY("COND:SERV:NEWU"), 2, conditional_serv_newuser_disabled, CTX_NONE);
794 RegisterConditional(HKEY("COND:SERV:HAVEFULLTEXT"), 2, conditional_serv_fulltext_enabled, CTX_NONE);
795 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, NULL, CTX_NONE);
796 RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, NULL, CTX_NONE);
797 RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, NULL, CTX_NONE);
798 RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, NULL, CTX_NONE);
799 RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, NULL, CTX_NONE);
800 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, NULL, CTX_NONE);
801 RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, NULL, CTX_NONE);
802 RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, NULL, CTX_NONE);
803 /*TODO // RegisterNamespace("SERV:LDAP_SUPP", 0, 0, tmplput_serv_ldap_enabled, 0); */
809 SessionDestroyModule_SERVFUNC
812 DeleteServInfo(&sess->serv_info);