moved whitespace around
[citadel.git] / webcit / serv_func.c
1 // Functions which handle communication with the Citadel server.
2 //
3 // Copyright (c) 1996-2024 by the citadel.org team
4 //
5 // This program is open source software.  You can redistribute it and/or
6 // modify it under the terms of the GNU General Public License, version 3.
7
8 #include "webcit.h"
9
10
11 HashList *EmbeddableMimes = NULL;
12 StrBuf *EmbeddableMimeStrs = NULL;
13
14 void SetInlinMimeRenderers(void) {
15         StrBuf *Buf;
16
17         Buf = NewStrBuf();
18
19         // Tell the server what kind of richtext we prefer
20         serv_putbuf(EmbeddableMimeStrs);
21         StrBuf_ServGetln(Buf);
22
23         FreeStrBuf(&Buf);
24 }
25
26
27 void DeleteServInfo(ServInfo **FreeMe) {
28         if (*FreeMe == NULL) {
29                 return;
30         }
31         FreeStrBuf(&(*FreeMe)->serv_nodename);
32         FreeStrBuf(&(*FreeMe)->serv_humannode);
33         FreeStrBuf(&(*FreeMe)->serv_fqdn);
34         FreeStrBuf(&(*FreeMe)->serv_software);
35         FreeStrBuf(&(*FreeMe)->serv_bbs_city);
36         FreeStrBuf(&(*FreeMe)->serv_sysadm);
37         FreeStrBuf(&(*FreeMe)->serv_default_cal_zone);
38         FreeStrBuf(&(*FreeMe)->serv_svn_revision);
39         free(*FreeMe);
40         *FreeMe = NULL;
41 }
42
43
44 // get info about the server we've connected to
45 //
46 // browser_host         the citadel we want to connect to
47 // user_agent           which browser uses our client?
48 ServInfo *get_serv_info(StrBuf *browser_host, StrBuf *user_agent) {
49         ServInfo *info;
50         StrBuf *Buf;
51         int a;
52         int rc;
53
54         Buf = NewStrBuf();
55
56         // Tell the server what kind of client is connecting
57         serv_printf("IDEN 0|0|0|%s|%s", ChrPtr(user_agent), ChrPtr(browser_host));
58         StrBuf_ServGetln(Buf);
59         if (GetServerStatus(Buf, NULL) != 2) {
60                 syslog(LOG_WARNING, "get_serv_info(IDEN): unexpected answer [%s]\n", ChrPtr(Buf));
61                 FreeStrBuf(&Buf);
62                 return NULL;
63         }
64
65         // Tell the server that when we save a calendar event, we
66         // want invitations to be generated by the Citadel server
67         // instead of by the client.
68         serv_puts("ICAL sgi|1");
69         StrBuf_ServGetln(Buf);
70         if (GetServerStatus(Buf, NULL) != 2) {
71                 syslog(LOG_WARNING, "get_serv_info(ICAL sgi|1): unexpected answer [%s]\n", ChrPtr(Buf));
72                 FreeStrBuf(&Buf);
73                 return NULL;
74         }
75
76         // Now ask the server to tell us a little bit about itself...
77         serv_puts("INFO");
78         StrBuf_ServGetln(Buf);
79         if (GetServerStatus(Buf, NULL) != 1) {
80                 syslog(LOG_WARNING, "get_serv_info(INFO sgi|1): unexpected answer [%s]\n",
81                         ChrPtr(Buf));
82                 FreeStrBuf(&Buf);
83                 return NULL;
84         }
85
86         info = (ServInfo*)malloc(sizeof(ServInfo));
87         memset(info, 0, sizeof(ServInfo));
88         a = 0;
89         while (rc = StrBuf_ServGetln(Buf),
90                (rc >= 0) &&
91                ((rc != 3) || 
92                 strcmp(ChrPtr(Buf), "000")))
93         {
94                 switch (a) {
95                 case 0:
96                         info->serv_pid = StrToi(Buf);
97                         WC->ctdl_pid = info->serv_pid;
98                         break;
99                 case 1:
100                         info->serv_nodename = NewStrBufDup(Buf);
101                         break;
102                 case 2:
103                         info->serv_humannode = NewStrBufDup(Buf);
104                         break;
105                 case 3:
106                         info->serv_fqdn = NewStrBufDup(Buf);
107                         break;
108                 case 4:
109                         info->serv_software = NewStrBufDup(Buf);
110                         break;
111                 case 5:
112                         info->serv_rev_level = StrToi(Buf);
113                         break;
114                 case 6:
115                         info->serv_bbs_city = NewStrBufDup(Buf);
116                         break;
117                 case 7:
118                         info->serv_sysadm = NewStrBufDup(Buf);
119                         break;
120                 case 14:
121                         info->serv_supports_ldap = StrToi(Buf);
122                         break;
123                 case 15:
124                         info->serv_newuser_disabled = StrToi(Buf);
125                         break;
126                 case 16:
127                         info->serv_default_cal_zone = NewStrBufDup(Buf);
128                         break;
129                 case 20:
130                         info->serv_supports_sieve = StrToi(Buf);
131                         break;
132                 case 21:
133                         info->serv_fulltext_enabled = StrToi(Buf);
134                         break;
135                 case 22:
136                         info->serv_svn_revision = NewStrBufDup(Buf);
137                         break;
138                 case 23:
139                         info->serv_supports_openid = StrToi(Buf);
140                         break;
141                 case 24:
142                         info->serv_supports_guest = StrToi(Buf);
143                         break;
144                 }
145                 ++a;
146         }
147         FreeStrBuf(&Buf);
148         return info;
149 }
150
151 int GetConnected (void) {
152         StrBuf *Buf;
153
154         if (WC->ReadBuf == NULL) {
155                 WC->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
156         }
157
158         static char serv_sock_name[PATH_MAX] = "";
159         if (IsEmptyStr(serv_sock_name)) {
160                 snprintf(serv_sock_name, sizeof serv_sock_name, "%s/citadel.socket", ctdl_dir);
161         }
162         WC->serv_sock = connect_to_citadel(serv_sock_name);
163         
164         if (WC->serv_sock < 0) {
165                 WC->connected = 0;
166                 FreeStrBuf(&WC->ReadBuf);
167                 return 1;
168         }
169         else {
170                 long Status;
171                 int short_status;
172                 Buf = NewStrBuf();
173                 WC->connected = 1;
174                 StrBuf_ServGetln(Buf);  // get the server greeting
175                 short_status = GetServerStatus(Buf, &Status);
176                 FreeStrBuf(&Buf);
177
178                 // Server isn't ready for us?
179                 if (short_status != 2) {
180                         if (Status == 551) {
181                                 hprintf("HTTP/1.1 503 Service Unavailable\r\n");
182                                 hprintf("Content-type: text/plain; charset=utf-8\r\n");
183                                 wc_printf(_("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."));
184                         }
185                         else {
186                                 wc_printf("%ld %s\n",
187                                         Status,
188                                         _("Received unexpected answer from Citadel server; bailing out.")
189                                 );
190                                 hprintf("HTTP/1.1 502 Bad Gateway\r\n");
191                                 hprintf("Content-type: text/plain; charset=utf-8\r\n");
192                         }
193                         end_burst();
194                         end_webcit_session();
195                         return 1;
196                 }
197
198                 // From what host is our user connecting?  Go with
199                 // the host at the other end of the HTTP socket,
200                 // unless we are following X-Forwarded-For: headers
201                 // and such a header has already turned up something.
202                 if ( (!follow_xff) || (StrLength(WC->Hdr->HR.browser_host) == 0) ) {
203                         if (WC->Hdr->HR.browser_host == NULL) {
204                                 WC->Hdr->HR.browser_host = NewStrBuf();
205                                 Put(WC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"), 
206                                     WC->Hdr->HR.browser_host, HFreeStrBuf);
207                         }
208                         locate_host(WC->Hdr->HR.browser_host, WC->Hdr->http_sock);
209                 }
210                 if (WC->serv_info == NULL) {
211                         WC->serv_info = get_serv_info(WC->Hdr->HR.browser_host, WC->Hdr->HR.user_agent);
212                 }
213                 if (WC->serv_info == NULL){
214                         begin_burst();
215                         wc_printf(_("Received unexpected answer from Citadel server; bailing out."));
216                         hprintf("HTTP/1.1 502 Bad Gateway\r\n");
217                         hprintf("Content-type: text/plain; charset=utf-8\r\n");
218                         end_burst();
219                         end_webcit_session();
220                         return 1;
221                 }
222                 if (WC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
223                         begin_burst();
224                         wc_printf(_("You are connected to a Citadel "
225                                   "server running Citadel %d.%02d. \n"
226                                   "In order to run this version of WebCit "
227                                   "you must also have Citadel %d.%02d or"
228                                   " newer.\n\n\n"),
229                                 WC->serv_info->serv_rev_level,
230                                 0,
231                                 MINIMUM_CIT_VERSION,
232                                 0
233                                 );
234                         hprintf("HTTP/1.1 200 OK\r\n");
235                         hprintf("Content-type: text/plain; charset=utf-8\r\n");
236                         end_burst();
237                         end_webcit_session();
238                         return 1;
239                 }
240                 SetInlinMimeRenderers();
241         }
242         return 0;
243 }
244
245
246 void FmOut(StrBuf *Target, const char *align, const StrBuf *Source) {
247         const char *ptr, *pte;
248         const char *BufPtr = NULL;
249         StrBuf *Line = NewStrBufPlain(NULL, SIZ);
250         StrBuf *Line1 = NewStrBufPlain(NULL, SIZ);
251         StrBuf *Line2 = NewStrBufPlain(NULL, SIZ);
252         int bn = 0;
253         int bq = 0;
254         int i;
255         long len;
256         int intext = 0;
257
258         StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
259
260         if (StrLength(Source) > 0) 
261                 do 
262                 {
263                         StrBufSipLine(Line, Source, &BufPtr);
264                         bq = 0;
265                         i = 0;
266                         ptr = ChrPtr(Line);
267                         len = StrLength(Line);
268                         pte = ptr + len;
269
270                         if ((intext == 1) && (isspace(*ptr))) {
271                                 StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
272                         }
273                         intext = 1;
274                         if (isspace(*ptr)) {
275                                 while ((ptr < pte) && ((*ptr == '>') || isspace(*ptr))) {
276                                         if (*ptr == '>') {
277                                                 bq++;
278                                         }
279                                         ptr ++;
280                                         i++;
281                                 }
282                         }
283
284                         // Quoted text should be displayed in italics and in a
285                         // different colour.  This code understands Citadel-style
286                         // " >" quotes and will convert to <BLOCKQUOTE> tags.
287                         if (i > 0) StrBufCutLeft(Line, i);
288                 
289
290                         for (i = bn; i < bq; i++)                               
291                                 StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
292                         for (i = bq; i < bn; i++)                               
293                                 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
294                         bn = bq;
295
296                         if (StrLength(Line) == 0)
297                                 continue;
298
299                         // Activate embedded URL's
300                         UrlizeText(Line1, Line, Line2);
301
302                         StrEscAppend(Target, Line1, NULL, 0, 0);
303
304                         StrBufAppendBufPlain(Target, HKEY("\n"), 0);
305                 }
306                 while ((BufPtr != StrBufNOTNULL) &&
307                        (BufPtr != NULL));
308
309         for (i = 0; i < bn; i++) {
310                 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
311         }
312         StrBufAppendBufPlain(Target, HKEY("</div><br>\n"), 0);
313         FreeStrBuf(&Line);
314         FreeStrBuf(&Line1);
315         FreeStrBuf(&Line2);
316 }
317
318
319 // Transmit message text (in memory) to the server.
320 void text_to_server(char *ptr) {
321         char buf[256];
322         int ch, a, pos, len;
323
324         pos = 0;
325         buf[0] = 0;
326
327         while (ptr[pos] != 0) {
328                 ch = ptr[pos++];
329                 if (ch == 13) {
330                         // ignore CR characters
331                 }
332                 else if (ch == 10) {
333                         len = strlen(buf);
334                         while ( (isspace(buf[len - 1]))
335                                 && (buf[0] !=  '\0') 
336                                 && (buf[1] !=  '\0') )
337                                 buf[--len] = 0;
338                         serv_puts(buf);
339                         buf[0] = 0;
340                         if (ptr[pos] != 0) strcat(buf, " ");
341                 } else {
342                         a = strlen(buf);
343                         buf[a + 1] = 0;
344                         buf[a] = ch;
345                         if ((ch == 32) && (strlen(buf) > 200)) {
346                                 buf[a] = 0;
347                                 serv_puts(buf);
348                                 buf[0] = 0;
349                         }
350                         if (strlen(buf) > 250) {
351                                 serv_puts(buf);
352                                 buf[0] = 0;
353                         }
354                 }
355         }
356         serv_puts(buf);
357 }
358
359
360 // Transmit message text (in memory) to the server, converting to Quoted-Printable encoding as we go.
361 void text_to_server_qp(const StrBuf *SendMeEncoded) {
362         StrBuf *ServBuf;
363
364         ServBuf = StrBufQuotedPrintableEncode(SendMeEncoded);
365         serv_putbuf(ServBuf);
366         FreeStrBuf(&ServBuf);
367 }
368
369
370 // translate server message output to text (used for editing room info files and such)
371 void server_to_text() {
372         char buf[SIZ];
373
374         int count = 0;
375
376         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
377                 if ((buf[0] == 32) && (count > 0)) {
378                         wc_printf("\n");
379                 }
380                 wc_printf("%s", buf);
381                 ++count;
382         }
383 }
384
385
386 // Read text from server, appending to a string buffer until the
387 // usual 000 terminator is found.  Caller is responsible for freeing
388 // the returned pointer.
389 int read_server_text(StrBuf *Buf, long *nLines) {
390         StrBuf *ReadBuf;
391         long nRead;
392         long nTotal = 0;
393         long nlines;
394         
395         nlines = 0;
396         ReadBuf = NewStrBuf();
397         while ((WC->serv_sock!=-1) &&
398                (nRead = StrBuf_ServGetln(ReadBuf), (nRead >= 0) &&
399                 ((nRead != 3)||(strcmp(ChrPtr(ReadBuf), "000") != 0))))
400         {
401                 StrBufAppendBuf(Buf, ReadBuf, 0);
402                 StrBufAppendBufPlain(Buf, HKEY("\n"), 0);
403                 nTotal += nRead;
404                 nlines ++;
405         }
406         FreeStrBuf(&ReadBuf);
407         *nLines = nlines;
408         return nTotal;
409 }
410
411
412 int GetServerStatusMsg(StrBuf *Line, long* FullState, int PutImportantMessage, int MajorOK) {
413         int rc;
414         if (FullState != NULL)
415                 *FullState = StrTol(Line);
416         rc = ChrPtr(Line)[0] - 48;
417         if ((!PutImportantMessage) || 
418             (MajorOK == rc)||
419             (StrLength(Line) <= 4))
420                 return rc;
421
422         AppendImportantMessage(ChrPtr(Line) + 4, StrLength(Line) - 4);
423         return rc;
424 }
425
426
427 void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP) {
428         StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
429 }
430
431 void tmplput_serv_admin(StrBuf *Target, WCTemplputParams *TP) {
432         if (WC->serv_info == NULL)
433                 return;
434         StrBufAppendTemplate(Target, TP, WC->serv_info->serv_sysadm, 0);
435 }
436
437 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP) {
438         if (WC->serv_info == NULL)
439                 return;
440         StrBufAppendTemplate(Target, TP, WC->serv_info->serv_nodename, 0);
441 }
442
443 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP) {
444         if (WC->serv_info == NULL)
445                 return;
446         StrBufAppendTemplate(Target, TP, WC->serv_info->serv_humannode, 0);
447 }
448
449 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP) {
450         if (WC->serv_info == NULL)
451                 return;
452         StrBufAppendTemplate(Target, TP, WC->serv_info->serv_fqdn, 0);
453 }
454
455 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP) {
456         if (WC->serv_info == NULL)
457                 return;
458         StrBufAppendTemplate(Target, TP, WC->serv_info->serv_software, 0);
459 }
460
461 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP) {
462         if (WC->serv_info == NULL) return;
463         StrBufAppendPrintf(Target, "%d", WC->serv_info->serv_rev_level);
464 }
465 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP) {
466         if (WC->serv_info == NULL)
467                 return 0;
468         return WC->serv_info->serv_newuser_disabled != 0;
469 }
470
471 int conditional_serv_supports_guest(StrBuf *Target, WCTemplputParams *TP) {
472         if (WC->serv_info == NULL)
473                 return 0;
474         return WC->serv_info->serv_supports_guest != 0;
475 }
476
477 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP) {
478         if (WC->serv_info == NULL)
479                 return 0;
480         return WC->serv_info->serv_supports_openid != 0;
481 }
482
483 int conditional_serv_fulltext_enabled(StrBuf *Target, WCTemplputParams *TP) {
484         if (WC->serv_info == NULL)
485                 return 0;
486         return WC->serv_info->serv_fulltext_enabled != 0;
487 }
488
489 int conditional_serv_ldap_enabled(StrBuf *Target, WCTemplputParams *TP) {
490         if (WC->serv_info == NULL)
491                 return 0;
492         return WC->serv_info->serv_supports_ldap != 0;
493 }
494
495 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP) {
496         if (WC->serv_info == NULL)
497                 return;
498         StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
499 }
500
501 void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP) {
502         int n = 0;
503         int Done = 0;
504         StrBuf *Line;
505         StrBuf *Buf;
506
507         Buf = NewStrBuf();
508         Line = NewStrBuf();
509         serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
510
511         StrBuf_ServGetln(Line);
512         if (GetServerStatus(Line, NULL) == 1) {
513                 while (!Done &&  (StrBuf_ServGetln(Line)>=0)) {
514                         if ( (StrLength(Line)==3) && 
515                              !strcmp(ChrPtr(Line), "000")) 
516                                 Done = 1;
517                         else
518                         {
519                                 if (n > 0)
520                                         StrBufAppendBufPlain(Buf, "\n", 1, 0);
521                                 StrBufAppendBuf(Buf, Line, 0);
522                         }
523                         n++;
524                 }
525         
526                 FlushStrBuf(Line);
527                 FmOut(Line, "center", Buf);
528                 StrBufAppendTemplate(Target, TP, Line, 1);
529         }
530         FreeStrBuf(&Buf);
531         FreeStrBuf(&Line);
532 }
533
534 void tmplput_site_prefix(StrBuf *Target, WCTemplputParams *TP) {
535         if ((WC != NULL) && (WC->Hdr->HostHeader != NULL)) {
536                 StrBufAppendTemplate(Target, TP, WC->Hdr->HostHeader, 0);
537         }
538 }
539
540 void RegisterEmbeddableMimeType(const char *MimeType, long MTLen, int Priority) {
541         StrBuf *MT;
542         MT = NewStrBufPlain(MimeType, MTLen);
543         Put(EmbeddableMimes, IKEY(Priority), MT, HFreeStrBuf);
544 }
545
546 void CreateMimeStr(void) {
547         HashPos  *it;
548         void *vMime;
549         long len = 0;
550         const char *Key;
551
552         it = GetNewHashPos(EmbeddableMimes, 0);
553         while (GetNextHashPos(EmbeddableMimes, it, &len, &Key, &vMime) &&
554                (vMime != NULL)) {
555                 if (StrLength(EmbeddableMimeStrs) > 0)
556                         StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("|"), 0);
557                 else 
558                         StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("MSGP "), 0);
559                 StrBufAppendBuf(EmbeddableMimeStrs, (StrBuf*) vMime, 0);
560         }
561         DeleteHashPos(&it);
562 }
563
564 void
565 ServerStartModule_SERV_FUNC
566 (void)
567 {
568         EmbeddableMimes = NewHash(1, Flathash);
569         EmbeddableMimeStrs = NewStrBuf();
570 }
571
572
573 void
574 ServerShutdownModule_SERV_FUNC
575 (void)
576 {
577         FreeStrBuf(&EmbeddableMimeStrs);
578         DeleteHash(&EmbeddableMimes);
579 }
580
581 void 
582 InitModule_SERVFUNC
583 (void)
584 {
585         RegisterConditional("COND:SERV:OPENID", 2, conditional_serv_supports_openid, CTX_NONE);
586         RegisterConditional("COND:SERV:NEWU", 2, conditional_serv_newuser_disabled, CTX_NONE);
587         RegisterConditional("COND:SERV:FULLTEXT_ENABLED", 2, conditional_serv_fulltext_enabled, CTX_NONE);
588         RegisterConditional("COND:SERV:LDAP_ENABLED", 2, conditional_serv_ldap_enabled, CTX_NONE);
589         RegisterConditional("COND:SERV:SUPPORTS_GUEST", 2, conditional_serv_supports_guest, CTX_NONE);
590         RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, NULL, CTX_NONE);
591         RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, NULL, CTX_NONE);
592         RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, NULL, CTX_NONE);
593         RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, NULL, CTX_NONE);
594         
595         RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, NULL, CTX_NONE);
596         RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, NULL, CTX_NONE);
597         RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, NULL, CTX_NONE);
598         RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, NULL, CTX_NONE);
599         RegisterNamespace("SERV:ADMIN", 0, 1, tmplput_serv_admin, NULL, CTX_NONE);
600
601         RegisterNamespace("SERV:SITE:PREFIX", 0, 1, tmplput_site_prefix, NULL, CTX_NONE);
602
603
604 }
605
606
607
608 void 
609 SessionDestroyModule_SERVFUNC
610 (wcsession *sess)
611 {
612         DeleteServInfo(&sess->serv_info);
613 }