9 char serv_sock_name[PATH_MAX] = "";
11 void DeleteServInfo(ServInfo **FreeMe)
15 FreeStrBuf(&(*FreeMe)->serv_nodename);
16 FreeStrBuf(&(*FreeMe)->serv_humannode);
17 FreeStrBuf(&(*FreeMe)->serv_fqdn);
18 FreeStrBuf(&(*FreeMe)->serv_software);
19 FreeStrBuf(&(*FreeMe)->serv_bbs_city);
20 FreeStrBuf(&(*FreeMe)->serv_sysadm);
21 FreeStrBuf(&(*FreeMe)->serv_moreprompt);
22 FreeStrBuf(&(*FreeMe)->serv_default_cal_zone);
23 FreeStrBuf(&(*FreeMe)->serv_svn_revision);
29 * get info about the server we've connected to
31 * browser_host the citadell we want to connect to
32 * user_agent which browser uses our client?
34 ServInfo *get_serv_info(StrBuf *browser_host, StrBuf *user_agent)
41 /** Tell the server what kind of client is connecting */
42 serv_printf("IDEN %d|%d|%d|%s|%s",
49 StrBuf_ServGetln(Buf);
51 /** Tell the server what kind of richtext we prefer */
52 serv_puts("MSGP text/calendar|text/vnote|text/html|text/plain");
53 StrBuf_ServGetln(Buf);
56 * Tell the server that when we save a calendar event, we
57 * want invitations to be generated by the Citadel server
58 * instead of by the client.
60 serv_puts("ICAL sgi|1");
61 StrBuf_ServGetln(Buf);
63 /** Now ask the server to tell us a little bit about itself... */
65 StrBuf_ServGetln(Buf);
66 if (GetServerStatus(Buf, NULL) != 1) {
71 info = (ServInfo*)malloc(sizeof(ServInfo));
72 memset(info, 0, sizeof(ServInfo));
74 while (StrBuf_ServGetln(Buf), (strcmp(ChrPtr(Buf), "000")!= 0)) {
75 /* lprintf (1, "a: %d [%s]", a, ChrPtr(Buf));*/
78 info->serv_pid = StrToi(Buf);
79 WC->ctdl_pid = info->serv_pid;
82 info->serv_nodename = NewStrBufDup(Buf);
85 info->serv_humannode = NewStrBufDup(Buf);
88 info->serv_fqdn = NewStrBufDup(Buf);
91 info->serv_software = NewStrBufDup(Buf);
94 info->serv_rev_level = StrToi(Buf);
97 info->serv_bbs_city = NewStrBufDup(Buf);
100 info->serv_sysadm = NewStrBufDup(Buf);
103 info->serv_moreprompt = NewStrBufDup(Buf);
106 info->serv_supports_ldap = StrToi(Buf);
109 info->serv_newuser_disabled = StrToi(Buf);
112 info->serv_default_cal_zone = NewStrBufDup(Buf);
115 info->serv_supports_sieve = StrToi(Buf);
118 info->serv_fulltext_enabled = StrToi(Buf);
121 info->serv_svn_revision = NewStrBufDup(Buf);
124 info->serv_supports_openid = StrToi(Buf);
133 int GetConnected (void)
138 if (WCC->ReadBuf == NULL)
139 WCC->ReadBuf = NewStrBuf();
140 if (is_uds) /* unix domain socket */
141 WCC->serv_sock = uds_connectsock(serv_sock_name);
142 else /* tcp socket */
143 WCC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
145 if (WCC->serv_sock < 0) {
147 FreeStrBuf(&WCC->ReadBuf);
154 StrBuf_ServGetln(Buf); /* get the server greeting */
155 GetServerStatus(Buf, &Status);
157 /* Are there too many users already logged in? */
159 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."));
161 end_webcit_session();
166 * From what host is our user connecting? Go with
167 * the host at the other end of the HTTP socket,
168 * unless we are following X-Forwarded-For: headers
169 * and such a header has already turned up something.
171 if ( (!follow_xff) || (StrLength(WCC->Hdr->browser_host) == 0) ) {
172 if (WCC->Hdr->browser_host == NULL) {
173 WCC->Hdr->browser_host = NewStrBuf();
174 Put(WCC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"),
175 WCC->Hdr->browser_host, HFreeStrBuf);
177 locate_host(WCC->Hdr->browser_host, WCC->Hdr->http_sock);
179 if (WCC->serv_info == NULL)
180 WCC->serv_info = get_serv_info(WCC->Hdr->browser_host, WCC->Hdr->user_agent);
181 if (WCC->serv_info == NULL){
183 wprintf(_("Received unexpected answer from Citadel "
184 "server; bailing out."));
185 hprintf("HTTP/1.1 200 OK\r\n");
186 hprintf("Content-type: text/plain; charset=utf-8\r\n");
188 end_webcit_session();
191 if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
193 wprintf(_("You are connected to a Citadel "
194 "server running Citadel %d.%02d. \n"
195 "In order to run this version of WebCit "
196 "you must also have Citadel %d.%02d or"
198 WCC->serv_info->serv_rev_level / 100,
199 WCC->serv_info->serv_rev_level % 100,
200 MINIMUM_CIT_VERSION / 100,
201 MINIMUM_CIT_VERSION % 100
203 hprintf("HTTP/1.1 200 OK\r\n");
204 hprintf("Content-type: text/plain; charset=utf-8\r\n");
206 end_webcit_session();
210 if (WCC->ReadBuf == NULL)
211 WCC->ReadBuf = NewStrBuf();
212 if (is_uds)/* unix domain socket */
213 WCC->serv_sock = uds_connectsock(serv_sock_name);
214 else /* tcp socket */
215 WCC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
217 if (WCC->serv_sock < 0) {
219 FreeStrBuf(&WCC->ReadBuf);
228 StrBuf_ServGetln(Buf);
229 GetServerStatus(Buf,&Status);
230 /* get the server greeting */
232 /* Are there too many users already logged in? */
234 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."));
236 end_webcit_session();
241 * From what host is our user connecting? Go with
242 * the host at the other end of the HTTP socket,
243 * unless we are following X-Forwarded-For: headers
244 * and such a header has already turned up something.
246 if ( (!follow_xff) || (StrLength(WCC->Hdr->browser_host) == 0) ) {
247 if (WCC->Hdr->browser_host == NULL) {
248 WCC->Hdr->browser_host = NewStrBuf();
249 Put(WCC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"),
250 WCC->Hdr->browser_host, HFreeStrBuf);
252 locate_host(WCC->Hdr->browser_host, WCC->Hdr->http_sock);
254 if (WCC->serv_info == NULL)
255 WCC->serv_info = get_serv_info(WCC->Hdr->browser_host, WCC->Hdr->user_agent);
256 if (WCC->serv_info == NULL){
258 wprintf(_("Received unexpected answer from Citadel "
259 "server; bailing out."));
260 hprintf("HTTP/1.1 200 OK\r\n");
261 hprintf("Content-type: text/plain; charset=utf-8\r\n");
263 end_webcit_session();
266 if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
268 wprintf(_("You are connected to a Citadel "
269 "server running Citadel %d.%02d. \n"
270 "In order to run this version of WebCit "
271 "you must also have Citadel %d.%02d or"
273 WCC->serv_info->serv_rev_level / 100,
274 WCC->serv_info->serv_rev_level % 100,
275 MINIMUM_CIT_VERSION / 100,
276 MINIMUM_CIT_VERSION % 100
278 hprintf("HTTP/1.1 200 OK\r\n");
279 hprintf("Content-type: text/plain; charset=utf-8\r\n");
281 end_webcit_session();
289 * Read Citadel variformat text and spit it out as HTML.
290 * align html align string
292 inline void fmout(char *align)
294 _fmout(WC->WBuf, align);
297 void _fmout(StrBuf *Target, char *align)
303 StrBufAppendPrintf(Target, "<div align=%s>\n", align);
304 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
306 if ((intext == 1) && (isspace(buf[0]))) {
312 * Quoted text should be displayed in italics and in a
313 * different colour. This code understands Citadel-style
314 * " >" quotes and will convert to <BLOCKQUOTE> tags.
316 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
317 StrBufAppendBufPlain(Target, HKEY("<BLOCKQUOTE>"), 0);
319 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
320 StrBufAppendBufPlain(Target, HKEY("</BLOCKQUOTE>"), 0);
323 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
324 strcpy(buf, &buf[2]);
326 /** Activate embedded URL's */
327 url(buf, sizeof(buf));
330 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
335 wprintf("</div><br />\n");
338 void FmOut(StrBuf *Target, char *align, StrBuf *Source)
340 const char *ptr, *pte;
341 const char *BufPtr = NULL;
342 StrBuf *Line = NewStrBuf();
343 StrBuf *Line1 = NewStrBuf();
344 StrBuf *Line2 = NewStrBuf();
351 StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
352 while ((n = StrBufSipLine(Line, Source, &BufPtr), n >= 0) && !done)
358 len = StrLength(Line);
361 if ((intext == 1) && (isspace(*ptr))) {
362 StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
365 if (isspace(*ptr)) while ((ptr < pte) &&
376 * Quoted text should be displayed in italics and in a
377 * different colour. This code understands Citadel-style
378 * " >" quotes and will convert to <BLOCKQUOTE> tags.
380 if (i > 0) StrBufCutLeft(Line, i);
383 for (i = bn; i < bq; i++)
384 StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
385 for (i = bq; i < bn; i++)
386 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
389 if (StrLength(Line) == 0)
391 /** Activate embedded URL's */
392 UrlizeText(Line1, Line, Line2);
394 StrEscAppend(Target, Line1, NULL, 0, 0);
396 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
398 for (i = 0; i < bn; i++)
399 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
400 StrBufAppendBufPlain(Target, HKEY("</div><br>\n"), 0);
410 * Read Citadel variformat text and spit it out as HTML in a form
411 * suitable for embedding in another message (forward/quote).
412 * (NO LINEBREAKS ALLOWED HERE!)
414 void pullquote_fmout(void) {
419 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
421 if ((intext == 1) && (isspace(buf[0]))) {
427 * Quoted text should be displayed in italics and in a
428 * different colour. This code understands Citadel-style
429 * " >" quotes and will convert to <BLOCKQUOTE> tags.
431 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
432 wprintf("<BLOCKQUOTE>");
434 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
435 wprintf("</BLOCKQUOTE>");
438 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
439 strcpy(buf, &buf[2]);
453 * Transmit message text (in memory) to the server.
455 * ptr Pointer to the message being transmitted
457 void text_to_server(char *ptr)
465 while (ptr[pos] != 0) {
469 while ( (isspace(buf[len - 1]))
471 && (buf[1] != '\0') )
475 if (ptr[pos] != 0) strcat(buf, " ");
480 if ((ch == 32) && (strlen(buf) > 200)) {
485 if (strlen(buf) > 250) {
496 * Transmit message text (in memory) to the server,
497 * converting to Quoted-Printable encoding as we go.
499 * ptr Pointer to the message being transmitted
501 void text_to_server_qp(char *ptr)
503 unsigned char ch, buf[256];
511 while (ptr[pos] != 0) {
512 ch = (unsigned char)(ptr[pos++]);
515 /* ignore carriage returns */
518 /* hard line break */
519 if (output_len > 0) {
520 if (isspace(buf[output_len-1])) {
521 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
525 buf[output_len++] = 0;
526 serv_puts((char *)buf);
530 buf[output_len++] = ch;
532 else if ( (ch >= 32) && (ch <= 60) ) {
533 buf[output_len++] = ch;
535 else if ( (ch >= 62) && (ch <= 126) ) {
536 buf[output_len++] = ch;
539 sprintf((char *)&buf[output_len], "=%02X", ch);
543 if (output_len > 72) {
544 /* soft line break */
545 if (isspace(buf[output_len-1])) {
546 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
549 buf[output_len++] = '=';
550 buf[output_len++] = 0;
551 serv_puts((char *)buf);
556 /* end of data - transmit anything that's left */
557 if (output_len > 0) {
558 if (isspace(buf[output_len-1])) {
559 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
562 buf[output_len++] = 0;
563 serv_puts((char *)buf);
572 * translate server message output to text
573 * (used for editing room info files and such)
575 void server_to_text()
581 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
582 if ((buf[0] == 32) && (count > 0)) {
593 * Read binary data from server into memory using a series of
594 * server READ commands.
595 * \return the read content as StrBuf
597 int read_server_binary(StrBuf *Ret, size_t total_len, StrBuf *Buf)
601 size_t thisblock = 0;
606 while (bytes < total_len) {
608 if ((total_len - bytes) < thisblock) {
609 thisblock = total_len - bytes;
610 if (thisblock == 0) {
616 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
617 if (StrBuf_ServGetln(Buf) > 0)
619 if (GetServerStatus(Buf, NULL) == 6)
621 StrBufCutLeft(Buf, 4);
622 thisblock = StrTol(Buf);
623 if (!WC->connected) {
628 StrBuf_ServGetBLOBBuffered(Ret, thisblock);
633 lprintf(3, "Error: %s\n", &buf[4]);
638 return StrLength(Ret);
643 * Read text from server, appending to a string buffer until the
644 * usual 000 terminator is found. Caller is responsible for freeing
645 * the returned pointer.
647 int read_server_text(StrBuf *Buf, long *nLines)
655 while ((WCC->serv_sock!=-1) &&
656 (nRead = StrBuf_ServGetln(Buf), (nRead >= 0) ))
658 if (strcmp(ChrPtr(Buf) + nTotal, "000") != 0) {
659 StrBufCutRight(Buf, nRead);
675 int GetServerStatus(StrBuf *Line, long* FullState)
677 if (FullState != NULL)
678 *FullState = StrTol(Line);
679 return ChrPtr(Line)[0] - 48;
683 void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP)
685 StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
688 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP)
691 if (WCC->serv_info == NULL)
693 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_nodename, 0);
696 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP)
699 if (WCC->serv_info == NULL)
701 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_humannode, 0);
704 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP)
707 if (WCC->serv_info == NULL)
709 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_fqdn, 0);
712 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP)
715 if (WCC->serv_info == NULL)
717 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_software, 0);
720 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP)
723 if (WCC->serv_info == NULL)
725 StrBufAppendPrintf(Target, "%d.%02d",
726 WCC->serv_info->serv_rev_level / 100,
727 WCC->serv_info->serv_rev_level % 100);
729 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP)
732 if (WCC->serv_info == NULL)
734 return WCC->serv_info->serv_newuser_disabled != 0;
736 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP)
739 if (WCC->serv_info == NULL)
741 return WCC->serv_info->serv_supports_openid != 0;
744 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP)
747 if (WCC->serv_info == NULL)
749 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
753 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP)
762 serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
764 StrBuf_ServGetln(Line);
765 if (GetServerStatus(Line, NULL) == 1) {
766 while (!Done && (StrBuf_ServGetln(Line)>=0)) {
767 if ( (StrLength(Line)==3) &&
768 !strcmp(ChrPtr(Line), "000"))
773 StrBufAppendBufPlain(Buf, "\n", 1, 0);
774 StrBufAppendBuf(Buf, Line, 0);
780 FmOut(Line, "center", Buf);
781 StrBufAppendTemplate(Target, TP, Line, 1);
791 is_uds = strcasecmp(ctdlhost, "uds") == 0;
793 snprintf(serv_sock_name, PATH_MAX, "%s/citadel.socket", ctdlport);
795 RegisterConditional(HKEY("COND:SERV:OPENID"), 2, conditional_serv_supports_openid, CTX_NONE);
796 RegisterConditional(HKEY("COND:SERV:NEWU"), 2, conditional_serv_newuser_disabled, CTX_NONE);
797 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, CTX_NONE);
798 RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, CTX_NONE);
799 RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, CTX_NONE);
800 RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, CTX_NONE);
801 RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, CTX_NONE);
802 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, CTX_NONE);
803 RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, CTX_NONE);
804 RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, CTX_NONE);
805 /*TODO // RegisterNamespace("SERV:LDAP_SUPP", 0, 0, tmplput_serv_ldap_enabled, 0); */
811 SessionDestroyModule_SERVFUNC
814 DeleteServInfo(&sess->serv_info);