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");//// TODO: register me...
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 = NewStrBufPlain(NULL, SIZ * 4);
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->HR.browser_host) == 0) ) {
172 if (WCC->Hdr->HR.browser_host == NULL) {
173 WCC->Hdr->HR.browser_host = NewStrBuf();
174 Put(WCC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"),
175 WCC->Hdr->HR.browser_host, HFreeStrBuf);
177 locate_host(WCC->Hdr->HR.browser_host, WCC->Hdr->http_sock);
179 if (WCC->serv_info == NULL)
180 WCC->serv_info = get_serv_info(WCC->Hdr->HR.browser_host, WCC->Hdr->HR.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();
214 * Read Citadel variformat text and spit it out as HTML.
215 * align html align string
217 inline void fmout(char *align)
219 _fmout(WC->WBuf, align);
222 void _fmout(StrBuf *Target, char *align)
228 StrBufAppendPrintf(Target, "<div align=%s>\n", align);
229 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
231 if ((intext == 1) && (isspace(buf[0]))) {
237 * Quoted text should be displayed in italics and in a
238 * different colour. This code understands Citadel-style
239 * " >" quotes and will convert to <BLOCKQUOTE> tags.
241 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
242 StrBufAppendBufPlain(Target, HKEY("<BLOCKQUOTE>"), 0);
244 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
245 StrBufAppendBufPlain(Target, HKEY("</BLOCKQUOTE>"), 0);
248 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
249 strcpy(buf, &buf[2]);
251 /** Activate embedded URL's */
252 url(buf, sizeof(buf));
255 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
260 wprintf("</div><br />\n");
263 void FmOut(StrBuf *Target, char *align, StrBuf *Source)
265 const char *ptr, *pte;
266 const char *BufPtr = NULL;
267 StrBuf *Line = NewStrBufPlain(NULL, SIZ);
268 StrBuf *Line1 = NewStrBufPlain(NULL, SIZ);
269 StrBuf *Line2 = NewStrBufPlain(NULL, SIZ);
276 StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
277 while ((n = StrBufSipLine(Line, Source, &BufPtr), n >= 0) && !done)
283 len = StrLength(Line);
286 if ((intext == 1) && (isspace(*ptr))) {
287 StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
290 if (isspace(*ptr)) while ((ptr < pte) &&
301 * Quoted text should be displayed in italics and in a
302 * different colour. This code understands Citadel-style
303 * " >" quotes and will convert to <BLOCKQUOTE> tags.
305 if (i > 0) StrBufCutLeft(Line, i);
308 for (i = bn; i < bq; i++)
309 StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
310 for (i = bq; i < bn; i++)
311 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
314 if (StrLength(Line) == 0)
316 /** Activate embedded URL's */
317 UrlizeText(Line1, Line, Line2);
319 StrEscAppend(Target, Line1, NULL, 0, 0);
321 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
323 for (i = 0; i < bn; i++)
324 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
325 StrBufAppendBufPlain(Target, HKEY("</div><br>\n"), 0);
335 * Read Citadel variformat text and spit it out as HTML in a form
336 * suitable for embedding in another message (forward/quote).
337 * (NO LINEBREAKS ALLOWED HERE!)
339 void pullquote_fmout(void) {
344 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
346 if ((intext == 1) && (isspace(buf[0]))) {
352 * Quoted text should be displayed in italics and in a
353 * different colour. This code understands Citadel-style
354 * " >" quotes and will convert to <BLOCKQUOTE> tags.
356 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
357 wprintf("<BLOCKQUOTE>");
359 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
360 wprintf("</BLOCKQUOTE>");
363 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
364 strcpy(buf, &buf[2]);
378 * Transmit message text (in memory) to the server.
380 * ptr Pointer to the message being transmitted
382 void text_to_server(char *ptr)
390 while (ptr[pos] != 0) {
394 while ( (isspace(buf[len - 1]))
396 && (buf[1] != '\0') )
400 if (ptr[pos] != 0) strcat(buf, " ");
405 if ((ch == 32) && (strlen(buf) > 200)) {
410 if (strlen(buf) > 250) {
421 * Transmit message text (in memory) to the server,
422 * converting to Quoted-Printable encoding as we go.
424 * ptr Pointer to the message being transmitted
426 void text_to_server_qp(char *ptr)
428 unsigned char ch, buf[256];
436 while (ptr[pos] != 0) {
437 ch = (unsigned char)(ptr[pos++]);
440 /* ignore carriage returns */
443 /* hard line break */
444 if (output_len > 0) {
445 if (isspace(buf[output_len-1])) {
446 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
450 buf[output_len++] = 0;
451 serv_puts((char *)buf);
455 buf[output_len++] = ch;
457 else if ( (ch >= 32) && (ch <= 60) ) {
458 buf[output_len++] = ch;
460 else if ( (ch >= 62) && (ch <= 126) ) {
461 buf[output_len++] = ch;
464 sprintf((char *)&buf[output_len], "=%02X", ch);
468 if (output_len > 72) {
469 /* soft line break */
470 if (isspace(buf[output_len-1])) {
471 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
474 buf[output_len++] = '=';
475 buf[output_len++] = 0;
476 serv_puts((char *)buf);
481 /* end of data - transmit anything that's left */
482 if (output_len > 0) {
483 if (isspace(buf[output_len-1])) {
484 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
487 buf[output_len++] = 0;
488 serv_puts((char *)buf);
497 * translate server message output to text
498 * (used for editing room info files and such)
500 void server_to_text()
506 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
507 if ((buf[0] == 32) && (count > 0)) {
518 * Read binary data from server into memory using a series of
519 * server READ commands.
520 * \return the read content as StrBuf
522 int read_server_binary(StrBuf *Ret, size_t total_len, StrBuf *Buf)
526 size_t thisblock = 0;
531 while (bytes < total_len) {
533 if ((total_len - bytes) < thisblock) {
534 thisblock = total_len - bytes;
535 if (thisblock == 0) {
541 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
542 if (StrBuf_ServGetln(Buf) > 0)
544 if (GetServerStatus(Buf, NULL) == 6)
546 StrBufCutLeft(Buf, 4);
547 thisblock = StrTol(Buf);
548 if (!WC->connected) {
553 StrBuf_ServGetBLOBBuffered(Ret, thisblock);
558 lprintf(3, "Error: %s\n", &buf[4]);
563 return StrLength(Ret);
568 * Read text from server, appending to a string buffer until the
569 * usual 000 terminator is found. Caller is responsible for freeing
570 * the returned pointer.
572 int read_server_text(StrBuf *Buf, long *nLines)
580 while ((WCC->serv_sock!=-1) &&
581 (nRead = StrBuf_ServGetln(Buf), (nRead >= 0) ))
583 if (strcmp(ChrPtr(Buf) + nTotal, "000") != 0) {
584 StrBufCutRight(Buf, nRead);
600 int GetServerStatus(StrBuf *Line, long* FullState)
602 if (FullState != NULL)
603 *FullState = StrTol(Line);
604 return ChrPtr(Line)[0] - 48;
608 void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP)
610 StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
613 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP)
616 if (WCC->serv_info == NULL)
618 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_nodename, 0);
621 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP)
624 if (WCC->serv_info == NULL)
626 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_humannode, 0);
629 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP)
632 if (WCC->serv_info == NULL)
634 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_fqdn, 0);
637 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP)
640 if (WCC->serv_info == NULL)
642 StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_software, 0);
645 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP)
648 if (WCC->serv_info == NULL)
650 StrBufAppendPrintf(Target, "%d.%02d",
651 WCC->serv_info->serv_rev_level / 100,
652 WCC->serv_info->serv_rev_level % 100);
654 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP)
657 if (WCC->serv_info == NULL)
659 return WCC->serv_info->serv_newuser_disabled != 0;
661 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP)
664 if (WCC->serv_info == NULL)
666 return WCC->serv_info->serv_supports_openid != 0;
669 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP)
672 if (WCC->serv_info == NULL)
674 StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
678 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP)
687 serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
689 StrBuf_ServGetln(Line);
690 if (GetServerStatus(Line, NULL) == 1) {
691 while (!Done && (StrBuf_ServGetln(Line)>=0)) {
692 if ( (StrLength(Line)==3) &&
693 !strcmp(ChrPtr(Line), "000"))
698 StrBufAppendBufPlain(Buf, "\n", 1, 0);
699 StrBufAppendBuf(Buf, Line, 0);
705 FmOut(Line, "center", Buf);
706 StrBufAppendTemplate(Target, TP, Line, 1);
716 is_uds = strcasecmp(ctdlhost, "uds") == 0;
718 snprintf(serv_sock_name, PATH_MAX, "%s/citadel.socket", ctdlport);
720 RegisterConditional(HKEY("COND:SERV:OPENID"), 2, conditional_serv_supports_openid, CTX_NONE);
721 RegisterConditional(HKEY("COND:SERV:NEWU"), 2, conditional_serv_newuser_disabled, CTX_NONE);
722 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, CTX_NONE);
723 RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, CTX_NONE);
724 RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, CTX_NONE);
725 RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, CTX_NONE);
726 RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, CTX_NONE);
727 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, CTX_NONE);
728 RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, CTX_NONE);
729 RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, CTX_NONE);
730 /*TODO // RegisterNamespace("SERV:LDAP_SUPP", 0, 0, tmplput_serv_ldap_enabled, 0); */
736 SessionDestroyModule_SERVFUNC
739 DeleteServInfo(&sess->serv_info);