* fixups, leaks...
[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                         FreeStrBuf(&Buf);
238                         return 1;
239                 }
240
241                 /*
242                  * From what host is our user connecting?  Go with
243                  * the host at the other end of the HTTP socket,
244                  * unless we are following X-Forwarded-For: headers
245                  * and such a header has already turned up something.
246                  */
247                 if ( (!follow_xff) || (StrLength(WCC->Hdr->browser_host) == 0) ) {
248                         if (WCC->Hdr->browser_host == NULL) {
249                                 WCC->Hdr->browser_host = NewStrBuf();
250                                 Put(WCC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"), 
251                                     WCC->Hdr->browser_host, HFreeStrBuf);
252                         }
253                         locate_host(WCC->Hdr->browser_host, WCC->Hdr->http_sock);
254                 }
255                 if (WCC->serv_info == NULL)
256                         WCC->serv_info = get_serv_info(WCC->Hdr->browser_host, WCC->Hdr->user_agent);
257                 if (WCC->serv_info == NULL){
258                         begin_burst();
259                         wprintf(_("Received unexpected answer from Citadel "
260                                   "server; bailing out."));
261                         hprintf("HTTP/1.1 200 OK\r\n");
262                         hprintf("Content-type: text/plain; charset=utf-8\r\n");
263                         end_burst();
264                         end_webcit_session();
265                         FreeStrBuf(&Buf);
266                         return 1;
267                 }
268                 if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
269                         begin_burst();
270                         wprintf(_("You are connected to a Citadel "
271                                   "server running Citadel %d.%02d. \n"
272                                   "In order to run this version of WebCit "
273                                   "you must also have Citadel %d.%02d or"
274                                   " newer.\n\n\n"),
275                                 WCC->serv_info->serv_rev_level / 100,
276                                 WCC->serv_info->serv_rev_level % 100,
277                                 MINIMUM_CIT_VERSION / 100,
278                                 MINIMUM_CIT_VERSION % 100
279                                 );
280                         hprintf("HTTP/1.1 200 OK\r\n");
281                         hprintf("Content-type: text/plain; charset=utf-8\r\n");
282                         end_burst();
283                         end_webcit_session();
284                         FreeStrBuf(&Buf);
285                         return 1;
286                 }
287                 FreeStrBuf(&Buf);
288         }
289         return 0;
290 }
291
292 /**
293  *  Read Citadel variformat text and spit it out as HTML.
294  *  align html align string
295  */
296 inline void fmout(char *align)
297 {
298         _fmout(WC->WBuf, align);
299 }
300
301 void _fmout(StrBuf *Target, char *align)
302 {
303         int intext = 0;
304         int bq = 0;
305         char buf[SIZ];
306
307         StrBufAppendPrintf(Target, "<div align=%s>\n", align);
308         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
309
310                 if ((intext == 1) && (isspace(buf[0]))) {
311                         wprintf("<br />");
312                 }
313                 intext = 1;
314
315                 /**
316                  * Quoted text should be displayed in italics and in a
317                  * different colour.  This code understands Citadel-style
318                  * " >" quotes and will convert to <BLOCKQUOTE> tags.
319                  */
320                 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
321                         StrBufAppendBufPlain(Target, HKEY("<BLOCKQUOTE>"), 0);
322                         bq = 1;
323                 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
324                         StrBufAppendBufPlain(Target, HKEY("</BLOCKQUOTE>"), 0);
325                         bq = 0;
326                 }
327                 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
328                         strcpy(buf, &buf[2]);
329                 }
330                 /** Activate embedded URL's */
331                 url(buf, sizeof(buf));
332
333                 escputs(buf);
334                 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
335         }
336         if (bq == 1) {
337                 wprintf("</I>");
338         }
339         wprintf("</div><br />\n");
340 }
341
342 void FmOut(StrBuf *Target, char *align, StrBuf *Source)
343 {
344         const char *ptr, *pte;
345         const char *BufPtr = NULL;
346         StrBuf *Line = NewStrBuf();
347         StrBuf *Line1 = NewStrBuf();
348         StrBuf *Line2 = NewStrBuf();
349         int bn = 0;
350         int bq = 0;
351         int i, n, done = 0;
352         long len;
353         int intext = 0;
354
355         StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
356         while ((n = StrBufSipLine(Line, Source, &BufPtr), n >= 0) && !done)
357         {
358                 done = n == 0;
359                 bq = 0;
360                 i = 0;
361                 ptr = ChrPtr(Line);
362                 len = StrLength(Line);
363                 pte = ptr + len;
364
365                 if ((intext == 1) && (isspace(*ptr))) {
366                         StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
367                 }
368                 intext = 1;
369                 if (isspace(*ptr)) while ((ptr < pte) &&
370                        ((*ptr == '>') ||
371                         isspace(*ptr)))
372                 {
373                         if (*ptr == '>')
374                                 bq++;
375                         ptr ++;
376                         i++;
377                 }
378
379                 /**
380                  * Quoted text should be displayed in italics and in a
381                  * different colour.  This code understands Citadel-style
382                  * " >" quotes and will convert to <BLOCKQUOTE> tags.
383                  */
384                 if (i > 0) StrBufCutLeft(Line, i);
385                 
386
387                 for (i = bn; i < bq; i++)                               
388                         StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
389                 for (i = bq; i < bn; i++)                               
390                         StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
391                 bn = bq;
392
393                 if (StrLength(Line) == 0)
394                         continue;
395                 /** Activate embedded URL's */
396                 UrlizeText(Line1, Line, Line2);
397
398                 StrEscAppend(Target, Line1, NULL, 0, 0);
399
400                 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
401         }
402         for (i = 0; i < bn; i++)                                
403                 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
404         StrBufAppendBufPlain(Target, HKEY("</div><br>\n"), 0);
405         FreeStrBuf(&Line);
406         FreeStrBuf(&Line1);
407         FreeStrBuf(&Line2);
408 }
409
410
411
412
413 /**
414  *  Read Citadel variformat text and spit it out as HTML in a form
415  * suitable for embedding in another message (forward/quote).
416  * (NO LINEBREAKS ALLOWED HERE!)
417  */
418 void pullquote_fmout(void) {
419         int intext = 0;
420         int bq = 0;
421         char buf[SIZ];
422
423         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
424
425                 if ((intext == 1) && (isspace(buf[0]))) {
426                         wprintf("<br />");
427                 }
428                 intext = 1;
429
430                 /**
431                  * Quoted text should be displayed in italics and in a
432                  * different colour.  This code understands Citadel-style
433                  * " >" quotes and will convert to <BLOCKQUOTE> tags.
434                  */
435                 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
436                         wprintf("<BLOCKQUOTE>");
437                         bq = 1;
438                 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
439                         wprintf("</BLOCKQUOTE>");
440                         bq = 0;
441                 }
442                 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
443                         strcpy(buf, &buf[2]);
444                 }
445
446                 msgescputs(buf);
447         }
448         if (bq == 1) {
449                 wprintf("</I>");
450         }
451 }
452
453
454
455
456 /**
457  *  Transmit message text (in memory) to the server.
458  *
459  *  ptr Pointer to the message being transmitted
460  */
461 void text_to_server(char *ptr)
462 {
463         char buf[256];
464         int ch, a, pos, len;
465
466         pos = 0;
467         buf[0] = 0;
468
469         while (ptr[pos] != 0) {
470                 ch = ptr[pos++];
471                 if (ch == 10) {
472                         len = strlen(buf);
473                         while ( (isspace(buf[len - 1]))
474                                 && (buf[0] !=  '\0') 
475                                 && (buf[1] !=  '\0') )
476                                 buf[--len] = 0;
477                         serv_puts(buf);
478                         buf[0] = 0;
479                         if (ptr[pos] != 0) strcat(buf, " ");
480                 } else {
481                         a = strlen(buf);
482                         buf[a + 1] = 0;
483                         buf[a] = ch;
484                         if ((ch == 32) && (strlen(buf) > 200)) {
485                                 buf[a] = 0;
486                                 serv_puts(buf);
487                                 buf[0] = 0;
488                         }
489                         if (strlen(buf) > 250) {
490                                 serv_puts(buf);
491                                 buf[0] = 0;
492                         }
493                 }
494         }
495         serv_puts(buf);
496 }
497
498
499 /**
500  *  Transmit message text (in memory) to the server,
501  *        converting to Quoted-Printable encoding as we go.
502  *
503  *  ptr Pointer to the message being transmitted
504  */
505 void text_to_server_qp(char *ptr)
506 {
507         unsigned char ch, buf[256];
508         int pos;
509         int output_len = 0;
510
511         pos = 0;
512         buf[0] = 0;
513         output_len = 0;
514
515         while (ptr[pos] != 0) {
516                 ch = (unsigned char)(ptr[pos++]);
517
518                 if (ch == 13) {
519                         /* ignore carriage returns */
520                 }
521                 else if (ch == 10) {
522                         /* hard line break */
523                         if (output_len > 0) {
524                                 if (isspace(buf[output_len-1])) {
525                                         sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
526                                         output_len += 2;
527                                 }
528                         }
529                         buf[output_len++] = 0;
530                         serv_puts((char *)buf);
531                         output_len = 0;
532                 }
533                 else if (ch == 9) {
534                         buf[output_len++] = ch;
535                 }
536                 else if ( (ch >= 32) && (ch <= 60) ) {
537                         buf[output_len++] = ch;
538                 }
539                 else if ( (ch >= 62) && (ch <= 126) ) {
540                         buf[output_len++] = ch;
541                 }
542                 else {
543                         sprintf((char *)&buf[output_len], "=%02X", ch);
544                         output_len += 3;
545                 }
546                 
547                 if (output_len > 72) {
548                         /* soft line break */
549                         if (isspace(buf[output_len-1])) {
550                                 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
551                                 output_len += 2;
552                         }
553                         buf[output_len++] = '=';
554                         buf[output_len++] = 0;
555                         serv_puts((char *)buf);
556                         output_len = 0;
557                 }
558         }
559
560         /* end of data - transmit anything that's left */
561         if (output_len > 0) {
562                 if (isspace(buf[output_len-1])) {
563                         sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
564                         output_len += 2;
565                 }
566                 buf[output_len++] = 0;
567                 serv_puts((char *)buf);
568                 output_len = 0;
569         }
570 }
571
572
573
574
575 /**
576  *  translate server message output to text
577  * (used for editing room info files and such)
578  */
579 void server_to_text()
580 {
581         char buf[SIZ];
582
583         int count = 0;
584
585         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
586                 if ((buf[0] == 32) && (count > 0)) {
587                         wprintf("\n");
588                 }
589                 wprintf("%s", buf);
590                 ++count;
591         }
592 }
593
594
595
596 /**
597  * Read binary data from server into memory using a series of
598  * server READ commands.
599  * \return the read content as StrBuf
600  */
601 int read_server_binary(StrBuf *Ret, size_t total_len, StrBuf *Buf) 
602 {
603         char buf[SIZ];
604         size_t bytes = 0;
605         size_t thisblock = 0;
606         
607         if (Ret == NULL)
608             return -1;
609
610         while (bytes < total_len) {
611                 thisblock = 4095;
612                 if ((total_len - bytes) < thisblock) {
613                         thisblock = total_len - bytes;
614                         if (thisblock == 0) {
615                                 FlushStrBuf(Ret); 
616                                 FreeStrBuf(&Buf);
617                                 return -1; 
618                         }
619                 }
620                 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
621                 if (StrBuf_ServGetln(Buf) > 0)
622                 {
623                         if (GetServerStatus(Buf, NULL) == 6)
624                         {
625                             StrBufCutLeft(Buf, 4);
626                             thisblock = StrTol(Buf);
627                             if (!WC->connected) {
628                                     FlushStrBuf(Ret); 
629                                     FreeStrBuf(&Buf); 
630                                     return -1; 
631                             }
632                             StrBuf_ServGetBLOBBuffered(Ret, thisblock);
633                             bytes += thisblock;
634                     }
635                     else {
636                             FreeStrBuf(&Buf);
637                             lprintf(3, "Error: %s\n", &buf[4]);
638                             return -1;
639                     }
640                 }
641         }
642         return StrLength(Ret);
643 }
644
645
646 /**
647  *  Read text from server, appending to a string buffer until the
648  * usual 000 terminator is found.  Caller is responsible for freeing
649  * the returned pointer.
650  */
651 int read_server_text(StrBuf *Buf, long *nLines)
652 {
653         wcsession *WCC = WC;
654         long nRead;
655         long nTotal = 0;
656         long nlines;
657         
658         nlines = 0;
659         while ((WCC->serv_sock!=-1) &&
660                (nRead = StrBuf_ServGetln(Buf), (nRead >= 0) ))
661         {
662                 if (strcmp(ChrPtr(Buf) + nTotal, "000") != 0) {
663                         StrBufCutRight(Buf, nRead);
664                         break;
665                 }
666                 nTotal += nRead;
667                 nlines ++;
668         }
669
670         *nLines = nlines;
671         return nTotal;
672 }
673
674
675
676
677
678
679 int GetServerStatus(StrBuf *Line, long* FullState)
680 {
681         if (FullState != NULL)
682                 *FullState = StrTol(Line);
683         return ChrPtr(Line)[0] - 48;
684 }
685
686
687 void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP)
688 {
689         StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
690 }
691
692 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP)
693 {
694         wcsession *WCC = WC;
695         if (WCC->serv_info == NULL)
696                 return;
697         StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_nodename, 0);
698 }
699
700 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP)
701 {
702         wcsession *WCC = WC;
703         if (WCC->serv_info == NULL)
704                 return;
705         StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_humannode, 0);
706 }
707
708 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP)
709 {
710         wcsession *WCC = WC;
711         if (WCC->serv_info == NULL)
712                 return;
713         StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_fqdn, 0);
714 }
715
716 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP)
717 {
718         wcsession *WCC = WC;
719         if (WCC->serv_info == NULL)
720                 return;
721         StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_software, 0);
722 }
723
724 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP)
725 {
726         wcsession *WCC = WC;
727         if (WCC->serv_info == NULL)
728                 return;
729         StrBufAppendPrintf(Target, "%d.%02d",
730                             WCC->serv_info->serv_rev_level / 100,
731                             WCC->serv_info->serv_rev_level % 100);
732 }
733 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP)
734 {
735         wcsession *WCC = WC;
736         if (WCC->serv_info == NULL)
737                 return 0;
738         return WCC->serv_info->serv_newuser_disabled != 0;
739 }
740 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP)
741 {
742         wcsession *WCC = WC;
743         if (WCC->serv_info == NULL)
744                 return 0;
745         return WCC->serv_info->serv_supports_openid != 0;
746 }
747
748 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP)
749 {
750         wcsession *WCC = WC;
751         if (WCC->serv_info == NULL)
752                 return;
753         StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
754 }
755
756
757 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP)
758 {
759         int n = 0;
760         int Done = 0;
761         StrBuf *Line;
762         StrBuf *Buf;
763
764         Buf = NewStrBuf();
765         Line = NewStrBuf();
766         serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
767
768         StrBuf_ServGetln(Line);
769         if (GetServerStatus(Line, NULL) == 1) {
770                 while (!Done &&  (StrBuf_ServGetln(Line)>=0)) {
771                         if ( (StrLength(Line)==3) && 
772                              !strcmp(ChrPtr(Line), "000")) 
773                                 Done = 1;
774                         else
775                         {
776                                 if (n > 0)
777                                         StrBufAppendBufPlain(Buf, "\n", 1, 0);
778                                 StrBufAppendBuf(Buf, Line, 0);
779                         }
780                         n++;
781                 }
782         
783                 FlushStrBuf(Line);
784                 FmOut(Line, "center", Buf);
785                 StrBufAppendTemplate(Target, TP, Line, 1);
786         }
787         FreeStrBuf(&Buf);
788         FreeStrBuf(&Line);
789 }
790
791 void 
792 InitModule_SERVFUNC
793 (void)
794 {
795         is_uds = strcasecmp(ctdlhost, "uds") == 0;
796         if (is_uds)
797                 snprintf(serv_sock_name, PATH_MAX, "%s/citadel.socket", ctdlport);
798
799         RegisterConditional(HKEY("COND:SERV:OPENID"), 2, conditional_serv_supports_openid, CTX_NONE);
800         RegisterConditional(HKEY("COND:SERV:NEWU"), 2, conditional_serv_newuser_disabled, CTX_NONE);
801         RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, CTX_NONE);
802         RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, CTX_NONE);
803         RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, CTX_NONE);
804         RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, CTX_NONE);
805         RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, CTX_NONE);
806         RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, CTX_NONE);
807         RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, CTX_NONE);
808         RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, CTX_NONE);
809 /*TODO //       RegisterNamespace("SERV:LDAP_SUPP", 0, 0, tmplput_serv_ldap_enabled, 0); */
810 }
811
812
813
814 void 
815 SessionDestroyModule_SERVFUNC
816 (wcsession *sess)
817 {
818         DeleteServInfo(&sess->serv_info);
819 }
820 /*@}*/