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