* BIG rewrite of header handling and slimming of context_loop and session_loop; shuff...
[citadel.git] / webcit / serv_func.c
1 /*
2  * $Id$
3  */
4
5 #include "webcit.h"
6 #include "webserver.h"
7
8 int is_uds = 0;
9 char serv_sock_name[PATH_MAX] = "";
10
11 void DeleteServInfo(ServInfo **FreeMe)
12 {
13         if (*FreeMe == NULL)
14                 return;
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);
24         free(*FreeMe);
25         *FreeMe = NULL;
26 }
27
28 /*
29  * get info about the server we've connected to
30  *
31  * browser_host         the citadell we want to connect to
32  * user_agent           which browser uses our client?
33  */
34 ServInfo *get_serv_info(StrBuf *browser_host, StrBuf *user_agent)
35 {
36         ServInfo *info;
37         StrBuf *Buf;
38         int a;
39
40         Buf = NewStrBuf();
41         /** Tell the server what kind of client is connecting */
42         serv_printf("IDEN %d|%d|%d|%s|%s",
43                     DEVELOPER_ID,
44                     CLIENT_ID,
45                     CLIENT_VERSION,
46                     ChrPtr(user_agent),
47                     ChrPtr(browser_host)
48         );
49         StrBuf_ServGetln(Buf);
50
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);
54
55         /*
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.
59          */
60         serv_puts("ICAL sgi|1");
61         StrBuf_ServGetln(Buf);
62
63         /** Now ask the server to tell us a little bit about itself... */
64         serv_puts("INFO");
65         StrBuf_ServGetln(Buf);
66         if (GetServerStatus(Buf, NULL) != 1) {
67                 FreeStrBuf(&Buf);
68                 return NULL;
69         }
70
71         info = (ServInfo*)malloc(sizeof(ServInfo));
72         memset(info, 0, sizeof(ServInfo));
73         a = 0;
74         while (StrBuf_ServGetln(Buf), (strcmp(ChrPtr(Buf), "000")!= 0)) {
75 /*              lprintf (1, "a: %d [%s]", a, ChrPtr(Buf));*/
76                 switch (a) {
77                 case 0:
78                         info->serv_pid = StrToi(Buf);
79                         WC->ctdl_pid = info->serv_pid;
80                         break;
81                 case 1:
82                         info->serv_nodename = NewStrBufDup(Buf);
83                         break;
84                 case 2:
85                         info->serv_humannode = NewStrBufDup(Buf);
86                         break;
87                 case 3:
88                         info->serv_fqdn = NewStrBufDup(Buf);
89                         break;
90                 case 4:
91                         info->serv_software = NewStrBufDup(Buf);
92                         break;
93                 case 5:
94                         info->serv_rev_level = StrToi(Buf);
95                         break;
96                 case 6:
97                         info->serv_bbs_city = NewStrBufDup(Buf);
98                         break;
99                 case 7:
100                         info->serv_sysadm = NewStrBufDup(Buf);
101                         break;
102                 case 9:
103                         info->serv_moreprompt = NewStrBufDup(Buf);
104                         break;
105                 case 14:
106                         info->serv_supports_ldap = StrToi(Buf);
107                         break;
108                 case 15:
109                         info->serv_newuser_disabled = StrToi(Buf);
110                         break;
111                 case 16:
112                         info->serv_default_cal_zone = NewStrBufDup(Buf);
113                         break;
114                 case 20:
115                         info->serv_supports_sieve = StrToi(Buf);
116                         break;
117                 case 21:
118                         info->serv_fulltext_enabled = StrToi(Buf);
119                         break;
120                 case 22:
121                         info->serv_svn_revision = NewStrBufDup(Buf);
122                         break;
123                 case 23:
124                         info->serv_supports_openid = StrToi(Buf);
125                         break;
126                 }
127                 ++a;
128         }
129         FreeStrBuf(&Buf);
130         return info;
131 }
132
133 int GetConnected (void)
134 {
135         StrBuf *Buf;
136         wcsession *WCC = WC;
137
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);
144         
145         if (WCC->serv_sock < 0) {
146                 do_logout();
147                 FreeStrBuf(&WCC->ReadBuf);
148                 return 1;
149         }
150         else {
151                 long Status;
152                 Buf = NewStrBuf();
153                 WCC->connected = 1;
154                 StrBuf_ServGetln(Buf);  /* get the server greeting */
155                 GetServerStatus(Buf, &Status);
156                 FreeStrBuf(&Buf);
157                 /* Are there too many users already logged in? */
158                 if (Status == 571) {
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."));
160                         end_burst();
161                         end_webcit_session();
162                         return 1;
163                 }
164
165                 /*
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.
170                  */
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);
176                         }
177                         locate_host(WCC->Hdr->browser_host, WCC->Hdr->http_sock);
178                 }
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){
182                         begin_burst();
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");
187                         end_burst();
188                         end_webcit_session();
189                         return 1;
190                 }
191                 if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
192                         begin_burst();
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"
197                                   " newer.\n\n\n"),
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
202                                 );
203                         hprintf("HTTP/1.1 200 OK\r\n");
204                         hprintf("Content-type: text/plain; charset=utf-8\r\n");
205                         end_burst();
206                         end_webcit_session();
207                         return 1;
208                 }
209         }
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);
216         
217         if (WCC->serv_sock < 0) {
218                 do_logout();
219                 FreeStrBuf(&WCC->ReadBuf);
220                 return 1;
221         }
222         else {
223                 long Status;
224                 StrBuf *Buf;
225
226                 Buf = NewStrBuf();
227                 WCC->connected = 1;
228                 StrBuf_ServGetln(Buf);
229                 GetServerStatus(Buf,&Status);
230                 /* get the server greeting */
231                 
232                 /* Are there too many users already logged in? */
233                 if (Status == 571) {
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."));
235                         end_burst();
236                         end_webcit_session();
237                         return 1;
238                 }
239
240                 /*
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.
245                  */
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);
251                         }
252                         locate_host(WCC->Hdr->browser_host, WCC->Hdr->http_sock);
253                 }
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){
257                         begin_burst();
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");
262                         end_burst();
263                         end_webcit_session();
264                         return 1;
265                 }
266                 if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
267                         begin_burst();
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"
272                                   " newer.\n\n\n"),
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
277                                 );
278                         hprintf("HTTP/1.1 200 OK\r\n");
279                         hprintf("Content-type: text/plain; charset=utf-8\r\n");
280                         end_burst();
281                         end_webcit_session();
282                         return 1;
283                 }
284         }
285         return 0;
286 }
287
288 /**
289  *  Read Citadel variformat text and spit it out as HTML.
290  *  align html align string
291  */
292 inline void fmout(char *align)
293 {
294         _fmout(WC->WBuf, align);
295 }
296
297 void _fmout(StrBuf *Target, char *align)
298 {
299         int intext = 0;
300         int bq = 0;
301         char buf[SIZ];
302
303         StrBufAppendPrintf(Target, "<div align=%s>\n", align);
304         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
305
306                 if ((intext == 1) && (isspace(buf[0]))) {
307                         wprintf("<br />");
308                 }
309                 intext = 1;
310
311                 /**
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.
315                  */
316                 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
317                         StrBufAppendBufPlain(Target, HKEY("<BLOCKQUOTE>"), 0);
318                         bq = 1;
319                 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
320                         StrBufAppendBufPlain(Target, HKEY("</BLOCKQUOTE>"), 0);
321                         bq = 0;
322                 }
323                 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
324                         strcpy(buf, &buf[2]);
325                 }
326                 /** Activate embedded URL's */
327                 url(buf, sizeof(buf));
328
329                 escputs(buf);
330                 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
331         }
332         if (bq == 1) {
333                 wprintf("</I>");
334         }
335         wprintf("</div><br />\n");
336 }
337
338 void FmOut(StrBuf *Target, char *align, StrBuf *Source)
339 {
340         const char *ptr, *pte;
341         const char *BufPtr = NULL;
342         StrBuf *Line = NewStrBuf();
343         StrBuf *Line1 = NewStrBuf();
344         StrBuf *Line2 = NewStrBuf();
345         int bn = 0;
346         int bq = 0;
347         int i, n, done = 0;
348         long len;
349         int intext = 0;
350
351         StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
352         while ((n = StrBufSipLine(Line, Source, &BufPtr), n >= 0) && !done)
353         {
354                 done = n == 0;
355                 bq = 0;
356                 i = 0;
357                 ptr = ChrPtr(Line);
358                 len = StrLength(Line);
359                 pte = ptr + len;
360
361                 if ((intext == 1) && (isspace(*ptr))) {
362                         StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
363                 }
364                 intext = 1;
365                 if (isspace(*ptr)) while ((ptr < pte) &&
366                        ((*ptr == '>') ||
367                         isspace(*ptr)))
368                 {
369                         if (*ptr == '>')
370                                 bq++;
371                         ptr ++;
372                         i++;
373                 }
374
375                 /**
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.
379                  */
380                 if (i > 0) StrBufCutLeft(Line, i);
381                 
382
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);
387                 bn = bq;
388
389                 if (StrLength(Line) == 0)
390                         continue;
391                 /** Activate embedded URL's */
392                 UrlizeText(Line1, Line, Line2);
393
394                 StrEscAppend(Target, Line1, NULL, 0, 0);
395
396                 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
397         }
398         for (i = 0; i < bn; i++)                                
399                 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
400         StrBufAppendBufPlain(Target, HKEY("</div><br>\n"), 0);
401         FreeStrBuf(&Line);
402         FreeStrBuf(&Line1);
403         FreeStrBuf(&Line2);
404 }
405
406
407
408
409 /**
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!)
413  */
414 void pullquote_fmout(void) {
415         int intext = 0;
416         int bq = 0;
417         char buf[SIZ];
418
419         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
420
421                 if ((intext == 1) && (isspace(buf[0]))) {
422                         wprintf("<br />");
423                 }
424                 intext = 1;
425
426                 /**
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.
430                  */
431                 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
432                         wprintf("<BLOCKQUOTE>");
433                         bq = 1;
434                 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
435                         wprintf("</BLOCKQUOTE>");
436                         bq = 0;
437                 }
438                 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
439                         strcpy(buf, &buf[2]);
440                 }
441
442                 msgescputs(buf);
443         }
444         if (bq == 1) {
445                 wprintf("</I>");
446         }
447 }
448
449
450
451
452 /**
453  *  Transmit message text (in memory) to the server.
454  *
455  *  ptr Pointer to the message being transmitted
456  */
457 void text_to_server(char *ptr)
458 {
459         char buf[256];
460         int ch, a, pos, len;
461
462         pos = 0;
463         buf[0] = 0;
464
465         while (ptr[pos] != 0) {
466                 ch = ptr[pos++];
467                 if (ch == 10) {
468                         len = strlen(buf);
469                         while ( (isspace(buf[len - 1]))
470                                 && (buf[0] !=  '\0') 
471                                 && (buf[1] !=  '\0') )
472                                 buf[--len] = 0;
473                         serv_puts(buf);
474                         buf[0] = 0;
475                         if (ptr[pos] != 0) strcat(buf, " ");
476                 } else {
477                         a = strlen(buf);
478                         buf[a + 1] = 0;
479                         buf[a] = ch;
480                         if ((ch == 32) && (strlen(buf) > 200)) {
481                                 buf[a] = 0;
482                                 serv_puts(buf);
483                                 buf[0] = 0;
484                         }
485                         if (strlen(buf) > 250) {
486                                 serv_puts(buf);
487                                 buf[0] = 0;
488                         }
489                 }
490         }
491         serv_puts(buf);
492 }
493
494
495 /**
496  *  Transmit message text (in memory) to the server,
497  *        converting to Quoted-Printable encoding as we go.
498  *
499  *  ptr Pointer to the message being transmitted
500  */
501 void text_to_server_qp(char *ptr)
502 {
503         unsigned char ch, buf[256];
504         int pos;
505         int output_len = 0;
506
507         pos = 0;
508         buf[0] = 0;
509         output_len = 0;
510
511         while (ptr[pos] != 0) {
512                 ch = (unsigned char)(ptr[pos++]);
513
514                 if (ch == 13) {
515                         /* ignore carriage returns */
516                 }
517                 else if (ch == 10) {
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]);
522                                         output_len += 2;
523                                 }
524                         }
525                         buf[output_len++] = 0;
526                         serv_puts((char *)buf);
527                         output_len = 0;
528                 }
529                 else if (ch == 9) {
530                         buf[output_len++] = ch;
531                 }
532                 else if ( (ch >= 32) && (ch <= 60) ) {
533                         buf[output_len++] = ch;
534                 }
535                 else if ( (ch >= 62) && (ch <= 126) ) {
536                         buf[output_len++] = ch;
537                 }
538                 else {
539                         sprintf((char *)&buf[output_len], "=%02X", ch);
540                         output_len += 3;
541                 }
542                 
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]);
547                                 output_len += 2;
548                         }
549                         buf[output_len++] = '=';
550                         buf[output_len++] = 0;
551                         serv_puts((char *)buf);
552                         output_len = 0;
553                 }
554         }
555
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]);
560                         output_len += 2;
561                 }
562                 buf[output_len++] = 0;
563                 serv_puts((char *)buf);
564                 output_len = 0;
565         }
566 }
567
568
569
570
571 /**
572  *  translate server message output to text
573  * (used for editing room info files and such)
574  */
575 void server_to_text()
576 {
577         char buf[SIZ];
578
579         int count = 0;
580
581         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
582                 if ((buf[0] == 32) && (count > 0)) {
583                         wprintf("\n");
584                 }
585                 wprintf("%s", buf);
586                 ++count;
587         }
588 }
589
590
591
592 /**
593  * Read binary data from server into memory using a series of
594  * server READ commands.
595  * \return the read content as StrBuf
596  */
597 int read_server_binary(StrBuf *Ret, size_t total_len, StrBuf *Buf) 
598 {
599         char buf[SIZ];
600         size_t bytes = 0;
601         size_t thisblock = 0;
602         
603         if (Ret == NULL)
604             return -1;
605
606         while (bytes < total_len) {
607                 thisblock = 4095;
608                 if ((total_len - bytes) < thisblock) {
609                         thisblock = total_len - bytes;
610                         if (thisblock == 0) {
611                                 FlushStrBuf(Ret); 
612                                 FreeStrBuf(&Buf);
613                                 return -1; 
614                         }
615                 }
616                 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
617                 if (StrBuf_ServGetln(Buf) > 0)
618                 {
619                         if (GetServerStatus(Buf, NULL) == 6)
620                         {
621                             StrBufCutLeft(Buf, 4);
622                             thisblock = StrTol(Buf);
623                             if (!WC->connected) {
624                                     FlushStrBuf(Ret); 
625                                     FreeStrBuf(&Buf); 
626                                     return -1; 
627                             }
628                             StrBuf_ServGetBLOBBuffered(Ret, thisblock);
629                             bytes += thisblock;
630                     }
631                     else {
632                             FreeStrBuf(&Buf);
633                             lprintf(3, "Error: %s\n", &buf[4]);
634                             return -1;
635                     }
636                 }
637         }
638         return StrLength(Ret);
639 }
640
641
642 /**
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.
646  */
647 int read_server_text(StrBuf *Buf, long *nLines)
648 {
649         wcsession *WCC = WC;
650         long nRead;
651         long nTotal = 0;
652         long nlines;
653         
654         nlines = 0;
655         while ((WCC->serv_sock!=-1) &&
656                (nRead = StrBuf_ServGetln(Buf), (nRead >= 0) ))
657         {
658                 if (strcmp(ChrPtr(Buf) + nTotal, "000") != 0) {
659                         StrBufCutRight(Buf, nRead);
660                         break;
661                 }
662                 nTotal += nRead;
663                 nlines ++;
664         }
665
666         *nLines = nlines;
667         return nTotal;
668 }
669
670
671
672
673
674
675 int GetServerStatus(StrBuf *Line, long* FullState)
676 {
677         if (FullState != NULL)
678                 *FullState = StrTol(Line);
679         return ChrPtr(Line)[0] - 48;
680 }
681
682
683 void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP)
684 {
685         StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
686 }
687
688 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP)
689 {
690         wcsession *WCC = WC;
691         if (WCC->serv_info == NULL)
692                 return;
693         StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_nodename, 0);
694 }
695
696 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP)
697 {
698         wcsession *WCC = WC;
699         if (WCC->serv_info == NULL)
700                 return;
701         StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_humannode, 0);
702 }
703
704 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP)
705 {
706         wcsession *WCC = WC;
707         if (WCC->serv_info == NULL)
708                 return;
709         StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_fqdn, 0);
710 }
711
712 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP)
713 {
714         wcsession *WCC = WC;
715         if (WCC->serv_info == NULL)
716                 return;
717         StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_software, 0);
718 }
719
720 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP)
721 {
722         wcsession *WCC = WC;
723         if (WCC->serv_info == NULL)
724                 return;
725         StrBufAppendPrintf(Target, "%d.%02d",
726                             WCC->serv_info->serv_rev_level / 100,
727                             WCC->serv_info->serv_rev_level % 100);
728 }
729 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP)
730 {
731         wcsession *WCC = WC;
732         if (WCC->serv_info == NULL)
733                 return 0;
734         return WCC->serv_info->serv_newuser_disabled != 0;
735 }
736 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP)
737 {
738         wcsession *WCC = WC;
739         if (WCC->serv_info == NULL)
740                 return 0;
741         return WCC->serv_info->serv_supports_openid != 0;
742 }
743
744 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP)
745 {
746         wcsession *WCC = WC;
747         if (WCC->serv_info == NULL)
748                 return;
749         StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
750 }
751
752
753 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP)
754 {
755         int n = 0;
756         int Done = 0;
757         StrBuf *Line;
758         StrBuf *Buf;
759
760         Buf = NewStrBuf();
761         Line = NewStrBuf();
762         serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
763
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")) 
769                                 Done = 1;
770                         else
771                         {
772                                 if (n > 0)
773                                         StrBufAppendBufPlain(Buf, "\n", 1, 0);
774                                 StrBufAppendBuf(Buf, Line, 0);
775                         }
776                         n++;
777                 }
778         
779                 FlushStrBuf(Line);
780                 FmOut(Line, "center", Buf);
781                 StrBufAppendTemplate(Target, TP, Line, 1);
782         }
783         FreeStrBuf(&Buf);
784         FreeStrBuf(&Line);
785 }
786
787 void 
788 InitModule_SERVFUNC
789 (void)
790 {
791         is_uds = strcasecmp(ctdlhost, "uds") == 0;
792         if (is_uds)
793                 snprintf(serv_sock_name, PATH_MAX, "%s/citadel.socket", ctdlport);
794
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); */
806 }
807
808
809
810 void 
811 SessionDestroyModule_SERVFUNC
812 (wcsession *sess)
813 {
814         DeleteServInfo(&sess->serv_info);
815 }
816 /*@}*/