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