* serv_getln now is a wrapper around existing functionality. a new temporary var...
[citadel.git] / webcit / serv_func.c
1 /*
2  * $Id$
3  */
4
5 #include "webcit.h"
6 #include "webserver.h"
7
8
9 void DeleteServInfo(ServInfo **FreeMe)
10 {
11         if (*FreeMe == NULL)
12                 return;
13         FreeStrBuf(&(*FreeMe)->serv_nodename);
14         FreeStrBuf(&(*FreeMe)->serv_humannode);
15         FreeStrBuf(&(*FreeMe)->serv_fqdn);
16         FreeStrBuf(&(*FreeMe)->serv_software);
17         FreeStrBuf(&(*FreeMe)->serv_bbs_city);
18         FreeStrBuf(&(*FreeMe)->serv_sysadm);
19         FreeStrBuf(&(*FreeMe)->serv_moreprompt);
20         FreeStrBuf(&(*FreeMe)->serv_default_cal_zone);
21         FreeStrBuf(&(*FreeMe)->serv_svn_revision);
22         free(*FreeMe);
23         *FreeMe = NULL;
24 }
25
26 /*
27  * get info about the server we've connected to
28  *
29  * browser_host         the citadell we want to connect to
30  * user_agent           which browser uses our client?
31  */
32 ServInfo *get_serv_info(StrBuf *browser_host, char *user_agent)
33 {
34         ServInfo *info;
35         StrBuf *Buf;
36         char buf[SIZ];
37         int a;
38
39         /** Tell the server what kind of client is connecting */
40         serv_printf("IDEN %d|%d|%d|%s|%s",
41                     DEVELOPER_ID,
42                     CLIENT_ID,
43                     CLIENT_VERSION,
44                     user_agent,
45                     ChrPtr(browser_host)
46         );
47         serv_getln(buf, sizeof buf);
48
49         /** Tell the server what kind of richtext we prefer */
50         serv_puts("MSGP text/calendar|text/html|text/plain");
51         serv_getln(buf, sizeof buf);
52
53         /*
54          * Tell the server that when we save a calendar event, we
55          * want invitations to be generated by the Citadel server
56          * instead of by the client.
57          */
58         serv_puts("ICAL sgi|1");
59         serv_getln(buf, sizeof buf);
60
61         /** Now ask the server to tell us a little bit about itself... */
62         serv_puts("INFO");
63         serv_getln(buf, sizeof buf);
64         if (buf[0] != '1')
65                 return NULL;
66
67         info = (ServInfo*)malloc(sizeof(ServInfo));
68         memset(info, 0, sizeof(ServInfo));
69         a = 0;
70         Buf = NewStrBuf();
71         while (StrBuf_ServGetln(Buf), (strcmp(ChrPtr(Buf), "000")!= 0)) {
72 /*              lprintf (1, "a: %d [%s]", a, ChrPtr(Buf));*/
73                 switch (a) {
74                 case 0:
75                         info->serv_pid = StrToi(Buf);
76                         WC->ctdl_pid = info->serv_pid;
77                         break;
78                 case 1:
79                         info->serv_nodename = NewStrBufDup(Buf);
80                         break;
81                 case 2:
82                         info->serv_humannode = NewStrBufDup(Buf);
83                         break;
84                 case 3:
85                         info->serv_fqdn = NewStrBufDup(Buf);
86                         break;
87                 case 4:
88                         info->serv_software = NewStrBufDup(Buf);
89                         break;
90                 case 5:
91                         info->serv_rev_level = StrToi(Buf);
92                         break;
93                 case 6:
94                         info->serv_bbs_city = NewStrBufDup(Buf);
95                         break;
96                 case 7:
97                         info->serv_sysadm = NewStrBufDup(Buf);
98                         break;
99                 case 9:
100                         info->serv_moreprompt = NewStrBufDup(Buf);
101                         break;
102                 case 14:
103                         info->serv_supports_ldap = StrToi(Buf);
104                         break;
105                 case 15:
106                         info->serv_newuser_disabled = StrToi(Buf);
107                         break;
108                 case 16:
109                         info->serv_default_cal_zone = NewStrBufDup(Buf);
110                         break;
111                 case 20:
112                         info->serv_supports_sieve = StrToi(Buf);
113                         break;
114                 case 21:
115                         info->serv_fulltext_enabled = StrToi(Buf);
116                         break;
117                 case 22:
118                         info->serv_svn_revision = NewStrBufDup(Buf);
119                         break;
120                 case 23:
121                         info->serv_supports_openid = StrToi(Buf);
122                         break;
123                 }
124                 ++a;
125         }
126         FreeStrBuf(&Buf);
127         return info;
128 }
129
130
131
132 /**
133  *  Read Citadel variformat text and spit it out as HTML.
134  *  align html align string
135  */
136 inline void fmout(char *align)
137 {
138         _fmout(WC->WBuf, align);
139 }
140
141 void _fmout(StrBuf *Target, char *align)
142 {
143         int intext = 0;
144         int bq = 0;
145         char buf[SIZ];
146
147         StrBufAppendPrintf(Target, "<div align=%s>\n", align);
148         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
149
150                 if ((intext == 1) && (isspace(buf[0]))) {
151                         wprintf("<br />");
152                 }
153                 intext = 1;
154
155                 /**
156                  * Quoted text should be displayed in italics and in a
157                  * different colour.  This code understands Citadel-style
158                  * " >" quotes and will convert to <BLOCKQUOTE> tags.
159                  */
160                 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
161                         StrBufAppendBufPlain(Target, HKEY("<BLOCKQUOTE>"), 0);
162                         bq = 1;
163                 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
164                         StrBufAppendBufPlain(Target, HKEY("</BLOCKQUOTE>"), 0);
165                         bq = 0;
166                 }
167                 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
168                         strcpy(buf, &buf[2]);
169                 }
170                 /** Activate embedded URL's */
171                 url(buf, sizeof(buf));
172
173                 escputs(buf);
174                 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
175         }
176         if (bq == 1) {
177                 wprintf("</I>");
178         }
179         wprintf("</div><br />\n");
180 }
181
182 void FmOut(StrBuf *Target, char *align, StrBuf *Source)
183 {
184         const char *ptr, *pte;
185         const char *BufPtr = NULL;
186         StrBuf *Line = NewStrBuf();
187         StrBuf *Line1 = NewStrBuf();
188         StrBuf *Line2 = NewStrBuf();
189         int bn = 0;
190         int bq = 0;
191         int i, n, done = 0;
192         long len;
193         int intext = 0;
194
195         StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
196         while ((n = StrBufSipLine(Line, Source, &BufPtr), n >= 0) && !done)
197         {
198                 done = n == 0;
199                 bq = 0;
200                 i = 0;
201                 ptr = ChrPtr(Line);
202                 len = StrLength(Line);
203                 pte = ptr + len;
204
205                 if ((intext == 1) && (isspace(*ptr))) {
206                         StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
207                 }
208                 intext = 1;
209                 if (isspace(*ptr)) while ((ptr < pte) &&
210                        ((*ptr == '>') ||
211                         isspace(*ptr)))
212                 {
213                         if (*ptr == '>')
214                                 bq++;
215                         ptr ++;
216                         i++;
217                 }
218
219                 /**
220                  * Quoted text should be displayed in italics and in a
221                  * different colour.  This code understands Citadel-style
222                  * " >" quotes and will convert to <BLOCKQUOTE> tags.
223                  */
224                 if (i > 0) StrBufCutLeft(Line, i);
225                 
226
227                 for (i = bn; i < bq; i++)                               
228                         StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
229                 for (i = bq; i < bn; i++)                               
230                         StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
231                 bn = bq;
232
233                 if (StrLength(Line) == 0)
234                         continue;
235                 /** Activate embedded URL's */
236                 UrlizeText(Line1, Line, Line2);
237
238                 StrEscAppend(Target, Line1, NULL, 0, 0);
239
240                 StrBufAppendBufPlain(Target, HKEY("\n"), 0);
241         }
242         for (i = 0; i < bn; i++)                                
243                 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
244         StrBufAppendBufPlain(Target, HKEY("</div><br>\n"), 0);
245         FreeStrBuf(&Line);
246         FreeStrBuf(&Line1);
247         FreeStrBuf(&Line2);
248 }
249
250
251
252
253 /**
254  *  Read Citadel variformat text and spit it out as HTML in a form
255  * suitable for embedding in another message (forward/quote).
256  * (NO LINEBREAKS ALLOWED HERE!)
257  */
258 void pullquote_fmout(void) {
259         int intext = 0;
260         int bq = 0;
261         char buf[SIZ];
262
263         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
264
265                 if ((intext == 1) && (isspace(buf[0]))) {
266                         wprintf("<br />");
267                 }
268                 intext = 1;
269
270                 /**
271                  * Quoted text should be displayed in italics and in a
272                  * different colour.  This code understands Citadel-style
273                  * " >" quotes and will convert to <BLOCKQUOTE> tags.
274                  */
275                 if ((bq == 0) && (!strncmp(buf, " >", 2))) {
276                         wprintf("<BLOCKQUOTE>");
277                         bq = 1;
278                 } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
279                         wprintf("</BLOCKQUOTE>");
280                         bq = 0;
281                 }
282                 if ((bq == 1) && (!strncmp(buf, " >", 2))) {
283                         strcpy(buf, &buf[2]);
284                 }
285
286                 msgescputs(buf);
287         }
288         if (bq == 1) {
289                 wprintf("</I>");
290         }
291 }
292
293
294
295
296 /**
297  *  Transmit message text (in memory) to the server.
298  *
299  *  ptr Pointer to the message being transmitted
300  */
301 void text_to_server(char *ptr)
302 {
303         char buf[256];
304         int ch, a, pos, len;
305
306         pos = 0;
307         buf[0] = 0;
308
309         while (ptr[pos] != 0) {
310                 ch = ptr[pos++];
311                 if (ch == 10) {
312                         len = strlen(buf);
313                         while ( (isspace(buf[len - 1]))
314                                 && (buf[0] !=  '\0') 
315                                 && (buf[1] !=  '\0') )
316                                 buf[--len] = 0;
317                         serv_puts(buf);
318                         buf[0] = 0;
319                         if (ptr[pos] != 0) strcat(buf, " ");
320                 } else {
321                         a = strlen(buf);
322                         buf[a + 1] = 0;
323                         buf[a] = ch;
324                         if ((ch == 32) && (strlen(buf) > 200)) {
325                                 buf[a] = 0;
326                                 serv_puts(buf);
327                                 buf[0] = 0;
328                         }
329                         if (strlen(buf) > 250) {
330                                 serv_puts(buf);
331                                 buf[0] = 0;
332                         }
333                 }
334         }
335         serv_puts(buf);
336 }
337
338
339 /**
340  *  Transmit message text (in memory) to the server,
341  *        converting to Quoted-Printable encoding as we go.
342  *
343  *  ptr Pointer to the message being transmitted
344  */
345 void text_to_server_qp(char *ptr)
346 {
347         unsigned char ch, buf[256];
348         int pos;
349         int output_len = 0;
350
351         pos = 0;
352         buf[0] = 0;
353         output_len = 0;
354
355         while (ptr[pos] != 0) {
356                 ch = (unsigned char)(ptr[pos++]);
357
358                 if (ch == 13) {
359                         /* ignore carriage returns */
360                 }
361                 else if (ch == 10) {
362                         /* hard line break */
363                         if (output_len > 0) {
364                                 if (isspace(buf[output_len-1])) {
365                                         sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
366                                         output_len += 2;
367                                 }
368                         }
369                         buf[output_len++] = 0;
370                         serv_puts((char *)buf);
371                         output_len = 0;
372                 }
373                 else if (ch == 9) {
374                         buf[output_len++] = ch;
375                 }
376                 else if ( (ch >= 32) && (ch <= 60) ) {
377                         buf[output_len++] = ch;
378                 }
379                 else if ( (ch >= 62) && (ch <= 126) ) {
380                         buf[output_len++] = ch;
381                 }
382                 else {
383                         sprintf((char *)&buf[output_len], "=%02X", ch);
384                         output_len += 3;
385                 }
386                 
387                 if (output_len > 72) {
388                         /* soft line break */
389                         if (isspace(buf[output_len-1])) {
390                                 sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
391                                 output_len += 2;
392                         }
393                         buf[output_len++] = '=';
394                         buf[output_len++] = 0;
395                         serv_puts((char *)buf);
396                         output_len = 0;
397                 }
398         }
399
400         /* end of data - transmit anything that's left */
401         if (output_len > 0) {
402                 if (isspace(buf[output_len-1])) {
403                         sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
404                         output_len += 2;
405                 }
406                 buf[output_len++] = 0;
407                 serv_puts((char *)buf);
408                 output_len = 0;
409         }
410 }
411
412
413
414
415 /**
416  *  translate server message output to text
417  * (used for editing room info files and such)
418  */
419 void server_to_text()
420 {
421         char buf[SIZ];
422
423         int count = 0;
424
425         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
426                 if ((buf[0] == 32) && (count > 0)) {
427                         wprintf("\n");
428                 }
429                 wprintf("%s", buf);
430                 ++count;
431         }
432 }
433
434
435
436 /**
437  * Read binary data from server into memory using a series of
438  * server READ commands.
439  * \return the read content as StrBuf
440  */
441 int read_server_binary(StrBuf *Ret, size_t total_len, StrBuf *Buf) 
442 {
443         char buf[SIZ];
444         size_t bytes = 0;
445         size_t thisblock = 0;
446         
447         if (Ret == NULL)
448             return -1;
449
450         while (bytes < total_len) {
451                 thisblock = 4095;
452                 if ((total_len - bytes) < thisblock) {
453                         thisblock = total_len - bytes;
454                         if (thisblock == 0) {
455                                 FlushStrBuf(Ret); 
456                                 FreeStrBuf(&Buf);
457                                 return -1; 
458                         }
459                 }
460                 serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
461                 if (StrBuf_ServGetln(Buf) > 0)
462                 {
463                         if (GetServerStatus(Buf, NULL) == 6)
464                         {
465                             StrBufCutLeft(Buf, 4);
466                             thisblock = StrTol(Buf);
467                             if (!WC->connected) {
468                                     FlushStrBuf(Ret); 
469                                     FreeStrBuf(&Buf); 
470                                     return -1; 
471                             }
472                             StrBuf_ServGetBLOBBuffered(Ret, thisblock);
473                             bytes += thisblock;
474                     }
475                     else {
476                             FreeStrBuf(&Buf);
477                             lprintf(3, "Error: %s\n", &buf[4]);
478                             return -1;
479                     }
480                 }
481         }
482         return StrLength(Ret);
483 }
484
485
486 /**
487  *  Read text from server, appending to a string buffer until the
488  * usual 000 terminator is found.  Caller is responsible for freeing
489  * the returned pointer.
490  */
491 int read_server_text(StrBuf *Buf, long *nLines)
492 {
493         wcsession *WCC = WC;
494         long nRead;
495         long nTotal = 0;
496         long nlines;
497         
498         nlines = 0;
499         while ((WCC->serv_sock!=-1) &&
500                (nRead = StrBuf_ServGetln(Buf), (nRead >= 0) ))
501         {
502                 if (strcmp(ChrPtr(Buf) + nTotal, "000") != 0) {
503                         StrBufCutRight(Buf, nRead);
504                         break;
505                 }
506                 nTotal += nRead;
507                 nlines ++;
508         }
509
510         *nLines = nlines;
511         return nTotal;
512 }
513
514
515
516
517
518
519 int GetServerStatus(StrBuf *Line, long* FullState)
520 {
521         if (FullState != NULL)
522                 *FullState = StrTol(Line);
523         return ChrPtr(Line)[0] - 48;
524 }
525
526
527 void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP)
528 {
529         StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
530 }
531
532 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP)
533 {
534         wcsession *WCC = WC;
535         if (WCC->serv_info == NULL)
536                 return;
537         StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_nodename, 0);
538 }
539
540 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP)
541 {
542         wcsession *WCC = WC;
543         if (WCC->serv_info == NULL)
544                 return;
545         StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_humannode, 0);
546 }
547
548 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP)
549 {
550         wcsession *WCC = WC;
551         if (WCC->serv_info == NULL)
552                 return;
553         StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_fqdn, 0);
554 }
555
556 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP)
557 {
558         wcsession *WCC = WC;
559         if (WCC->serv_info == NULL)
560                 return;
561         StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_software, 0);
562 }
563
564 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP)
565 {
566         wcsession *WCC = WC;
567         if (WCC->serv_info == NULL)
568                 return;
569         StrBufAppendPrintf(Target, "%d.%02d",
570                             WCC->serv_info->serv_rev_level / 100,
571                             WCC->serv_info->serv_rev_level % 100);
572 }
573 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP)
574 {
575         wcsession *WCC = WC;
576         if (WCC->serv_info == NULL)
577                 return 0;
578         return WCC->serv_info->serv_newuser_disabled != 0;
579 }
580 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP)
581 {
582         wcsession *WCC = WC;
583         if (WCC->serv_info == NULL)
584                 return 0;
585         return WCC->serv_info->serv_supports_openid != 0;
586 }
587
588 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP)
589 {
590         wcsession *WCC = WC;
591         if (WCC->serv_info == NULL)
592                 return;
593         StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
594 }
595
596
597 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP)
598 {
599         int n = 0;
600         int Done = 0;
601         StrBuf *Line;
602         StrBuf *Buf;
603
604         Buf = NewStrBuf();
605         Line = NewStrBuf();
606         serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
607
608         StrBuf_ServGetln(Line);
609         if (GetServerStatus(Line, NULL) == 1) {
610                 while (!Done &&  (StrBuf_ServGetln(Line)>=0)) {
611                         if ( (StrLength(Line)==3) && 
612                              !strcmp(ChrPtr(Line), "000")) 
613                                 Done = 1;
614                         else
615                         {
616                                 if (n > 0)
617                                         StrBufAppendBufPlain(Buf, "\n", 1, 0);
618                                 StrBufAppendBuf(Buf, Line, 0);
619                         }
620                         n++;
621                 }
622         
623                 FlushStrBuf(Line);
624                 FmOut(Line, "center", Buf);
625                 StrBufAppendTemplate(Target, TP, Line, 1);
626         }
627         FreeStrBuf(&Buf);
628         FreeStrBuf(&Line);
629 }
630
631 void 
632 InitModule_SERVFUNC
633 (void)
634 {
635
636         RegisterConditional(HKEY("COND:SERV:OPENID"), 2, conditional_serv_supports_openid, CTX_NONE);
637         RegisterConditional(HKEY("COND:SERV:NEWU"), 2, conditional_serv_newuser_disabled, CTX_NONE);
638         RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, CTX_NONE);
639         RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, CTX_NONE);
640         RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, CTX_NONE);
641         RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, CTX_NONE);
642         RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, CTX_NONE);
643         RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, CTX_NONE);
644         RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, CTX_NONE);
645         RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, CTX_NONE);
646 /*TODO //       RegisterNamespace("SERV:LDAP_SUPP", 0, 0, tmplput_serv_ldap_enabled, 0); */
647 }
648
649 /*@}*/