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