* move serv_func.c:read_server_binary() to tcp_sockets.c: serv_read_binary()
[citadel.git] / webcit / serv_func.c
index 221aa84ce585c86e7ee7a14595c00628e3278fc8..654cb4f2dc0bad384290a9c36ab013eddefc02ec 100644 (file)
@@ -1,4 +1,4 @@
-/*
+  /*
  * $Id$
  */
 
@@ -8,6 +8,23 @@
 int is_uds = 0;
 char serv_sock_name[PATH_MAX] = "";
 
+HashList *EmbeddableMimes = NULL;
+StrBuf *EmbeddableMimeStrs = NULL;
+
+
+void SetInlinMimeRenderers(void)
+{
+       StrBuf *Buf;
+
+       Buf = NewStrBuf();
+       /** Tell the server what kind of richtext we prefer */
+       serv_putbuf(EmbeddableMimeStrs);
+       StrBuf_ServGetln(Buf);
+
+       FreeStrBuf(&Buf);
+}
+
+
 void DeleteServInfo(ServInfo **FreeMe)
 {
        if (*FreeMe == NULL)
@@ -48,10 +65,6 @@ ServInfo *get_serv_info(StrBuf *browser_host, StrBuf *user_agent)
        );
        StrBuf_ServGetln(Buf);
 
-       /** Tell the server what kind of richtext we prefer */
-       serv_puts("MSGP text/calendar|text/vnote|text/html|text/plain");
-       StrBuf_ServGetln(Buf);
-
        /*
         * Tell the server that when we save a calendar event, we
         * want invitations to be generated by the Citadel server
@@ -136,7 +149,7 @@ int GetConnected (void)
        wcsession *WCC = WC;
 
        if (WCC->ReadBuf == NULL)
-               WCC->ReadBuf = NewStrBuf();
+               WCC->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
        if (is_uds) /* unix domain socket */
                WCC->serv_sock = uds_connectsock(serv_sock_name);
        else        /* tcp socket */
@@ -156,7 +169,7 @@ int GetConnected (void)
                FreeStrBuf(&Buf);
                /* Are there too many users already logged in? */
                if (Status == 571) {
-                       wprintf(_("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."));
+                       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."));
                        end_burst();
                        end_webcit_session();
                        return 1;
@@ -168,19 +181,19 @@ int GetConnected (void)
                 * unless we are following X-Forwarded-For: headers
                 * and such a header has already turned up something.
                 */
-               if ( (!follow_xff) || (StrLength(WCC->Hdr->browser_host) == 0) ) {
-                       if (WCC->Hdr->browser_host == NULL) {
-                               WCC->Hdr->browser_host = NewStrBuf();
+               if ( (!follow_xff) || (StrLength(WCC->Hdr->HR.browser_host) == 0) ) {
+                       if (WCC->Hdr->HR.browser_host == NULL) {
+                               WCC->Hdr->HR.browser_host = NewStrBuf();
                                Put(WCC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"), 
-                                   WCC->Hdr->browser_host, HFreeStrBuf);
+                                   WCC->Hdr->HR.browser_host, HFreeStrBuf);
                        }
-                       locate_host(WCC->Hdr->browser_host, WCC->Hdr->http_sock);
+                       locate_host(WCC->Hdr->HR.browser_host, WCC->Hdr->http_sock);
                }
                if (WCC->serv_info == NULL)
-                       WCC->serv_info = get_serv_info(WCC->Hdr->browser_host, WCC->Hdr->user_agent);
+                       WCC->serv_info = get_serv_info(WCC->Hdr->HR.browser_host, WCC->Hdr->HR.user_agent);
                if (WCC->serv_info == NULL){
                        begin_burst();
-                       wprintf(_("Received unexpected answer from Citadel "
+                       wc_printf(_("Received unexpected answer from Citadel "
                                  "server; bailing out."));
                        hprintf("HTTP/1.1 200 OK\r\n");
                        hprintf("Content-type: text/plain; charset=utf-8\r\n");
@@ -190,7 +203,7 @@ int GetConnected (void)
                }
                if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
                        begin_burst();
-                       wprintf(_("You are connected to a Citadel "
+                       wc_printf(_("You are connected to a Citadel "
                                  "server running Citadel %d.%02d. \n"
                                  "In order to run this version of WebCit "
                                  "you must also have Citadel %d.%02d or"
@@ -206,6 +219,7 @@ int GetConnected (void)
                        end_webcit_session();
                        return 1;
                }
+               SetInlinMimeRenderers();
        }
        return 0;
 }
@@ -229,7 +243,7 @@ void _fmout(StrBuf *Target, char *align)
        while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
 
                if ((intext == 1) && (isspace(buf[0]))) {
-                       wprintf("<br />");
+                       wc_printf("<br />");
                }
                intext = 1;
 
@@ -255,71 +269,76 @@ void _fmout(StrBuf *Target, char *align)
                StrBufAppendBufPlain(Target, HKEY("\n"), 0);
        }
        if (bq == 1) {
-               wprintf("</I>");
+               wc_printf("</I>");
        }
-       wprintf("</div><br />\n");
+       wc_printf("</div><br />\n");
 }
 
 void FmOut(StrBuf *Target, char *align, StrBuf *Source)
 {
        const char *ptr, *pte;
        const char *BufPtr = NULL;
-       StrBuf *Line = NewStrBuf();
-       StrBuf *Line1 = NewStrBuf();
-       StrBuf *Line2 = NewStrBuf();
+       StrBuf *Line = NewStrBufPlain(NULL, SIZ);
+       StrBuf *Line1 = NewStrBufPlain(NULL, SIZ);
+       StrBuf *Line2 = NewStrBufPlain(NULL, SIZ);
        int bn = 0;
        int bq = 0;
-       int i, n, done = 0;
+       int i;
        long len;
        int intext = 0;
 
        StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
-       while ((n = StrBufSipLine(Line, Source, &BufPtr), n >= 0) && !done)
-       {
-               done = n == 0;
-               bq = 0;
-               i = 0;
-               ptr = ChrPtr(Line);
-               len = StrLength(Line);
-               pte = ptr + len;
-
-               if ((intext == 1) && (isspace(*ptr))) {
-                       StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
-               }
-               intext = 1;
-               if (isspace(*ptr)) while ((ptr < pte) &&
-                      ((*ptr == '>') ||
-                       isspace(*ptr)))
+
+       if (StrLength(Source) > 0) 
+               do 
                {
-                       if (*ptr == '>')
-                               bq++;
-                       ptr ++;
-                       i++;
-               }
+                       StrBufSipLine(Line, Source, &BufPtr);
+                       bq = 0;
+                       i = 0;
+                       ptr = ChrPtr(Line);
+                       len = StrLength(Line);
+                       pte = ptr + len;
 
-               /**
-                * Quoted text should be displayed in italics and in a
-                * different colour.  This code understands Citadel-style
-                * " >" quotes and will convert to <BLOCKQUOTE> tags.
-                */
-               if (i > 0) StrBufCutLeft(Line, i);
+                       if ((intext == 1) && (isspace(*ptr))) {
+                               StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
+                       }
+                       intext = 1;
+                       if (isspace(*ptr)) while ((ptr < pte) &&
+                                                 ((*ptr == '>') ||
+                                                  isspace(*ptr)))
+                                          {
+                                                  if (*ptr == '>')
+                                                          bq++;
+                                                  ptr ++;
+                                                  i++;
+                                          }
+
+                       /**
+                        * Quoted text should be displayed in italics and in a
+                        * different colour.  This code understands Citadel-style
+                        * " >" quotes and will convert to <BLOCKQUOTE> tags.
+                        */
+                       if (i > 0) StrBufCutLeft(Line, i);
                
 
-               for (i = bn; i < bq; i++)                               
-                       StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
-               for (i = bq; i < bn; i++)                               
-                       StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
-               bn = bq;
+                       for (i = bn; i < bq; i++)                               
+                               StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
+                       for (i = bq; i < bn; i++)                               
+                               StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
+                       bn = bq;
 
-               if (StrLength(Line) == 0)
-                       continue;
-               /** Activate embedded URL's */
-               UrlizeText(Line1, Line, Line2);
+                       if (StrLength(Line) == 0)
+                               continue;
+                       /** Activate embedded URL's */
+                       UrlizeText(Line1, Line, Line2);
 
-               StrEscAppend(Target, Line1, NULL, 0, 0);
+                       StrEscAppend(Target, Line1, NULL, 0, 0);
+
+                       StrBufAppendBufPlain(Target, HKEY("\n"), 0);
+               }
+               while ((BufPtr != StrBufNOTNULL) &&
+                      (BufPtr != NULL));
 
-               StrBufAppendBufPlain(Target, HKEY("\n"), 0);
-       }
        for (i = 0; i < bn; i++)                                
                StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
        StrBufAppendBufPlain(Target, HKEY("</div><br>\n"), 0);
@@ -344,7 +363,7 @@ void pullquote_fmout(void) {
        while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
 
                if ((intext == 1) && (isspace(buf[0]))) {
-                       wprintf("<br />");
+                       wc_printf("<br />");
                }
                intext = 1;
 
@@ -354,10 +373,10 @@ void pullquote_fmout(void) {
                 * " >" quotes and will convert to <BLOCKQUOTE> tags.
                 */
                if ((bq == 0) && (!strncmp(buf, " >", 2))) {
-                       wprintf("<BLOCKQUOTE>");
+                       wc_printf("<BLOCKQUOTE>");
                        bq = 1;
                } else if ((bq == 1) && (strncmp(buf, " >", 2))) {
-                       wprintf("</BLOCKQUOTE>");
+                       wc_printf("</BLOCKQUOTE>");
                        bq = 0;
                }
                if ((bq == 1) && (!strncmp(buf, " >", 2))) {
@@ -367,7 +386,7 @@ void pullquote_fmout(void) {
                msgescputs(buf);
        }
        if (bq == 1) {
-               wprintf("</I>");
+               wc_printf("</I>");
        }
 }
 
@@ -505,64 +524,15 @@ void server_to_text()
 
        while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
                if ((buf[0] == 32) && (count > 0)) {
-                       wprintf("\n");
+                       wc_printf("\n");
                }
-               wprintf("%s", buf);
+               wc_printf("%s", buf);
                ++count;
        }
 }
 
 
 
-/**
- * Read binary data from server into memory using a series of
- * server READ commands.
- * \return the read content as StrBuf
- */
-int read_server_binary(StrBuf *Ret, size_t total_len, StrBuf *Buf) 
-{
-       char buf[SIZ];
-       size_t bytes = 0;
-       size_t thisblock = 0;
-       
-       if (Ret == NULL)
-           return -1;
-
-       while (bytes < total_len) {
-               thisblock = 4095;
-               if ((total_len - bytes) < thisblock) {
-                       thisblock = total_len - bytes;
-                       if (thisblock == 0) {
-                               FlushStrBuf(Ret); 
-                               FreeStrBuf(&Buf);
-                               return -1; 
-                       }
-               }
-               serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
-               if (StrBuf_ServGetln(Buf) > 0)
-               {
-                       if (GetServerStatus(Buf, NULL) == 6)
-                       {
-                           StrBufCutLeft(Buf, 4);
-                           thisblock = StrTol(Buf);
-                           if (!WC->connected) {
-                                   FlushStrBuf(Ret); 
-                                   FreeStrBuf(&Buf); 
-                                   return -1; 
-                           }
-                           StrBuf_ServGetBLOBBuffered(Ret, thisblock);
-                           bytes += thisblock;
-                   }
-                   else {
-                           FreeStrBuf(&Buf);
-                           lprintf(3, "Error: %s\n", &buf[4]);
-                           return -1;
-                   }
-               }
-       }
-       return StrLength(Ret);
-}
-
 
 /**
  *  Read text from server, appending to a string buffer until the
@@ -665,6 +635,15 @@ int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP)
                return 0;
        return WCC->serv_info->serv_supports_openid != 0;
 }
+int conditional_serv_fulltext_enabled(StrBuf *Target, WCTemplputParams *TP)
+{
+       wcsession *WCC = WC;
+       if (WCC->serv_info == NULL)
+               return 0;
+       return WCC->serv_info->serv_fulltext_enabled != 0;
+}
+
+
 
 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP)
 {
@@ -709,6 +688,50 @@ void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP)
        FreeStrBuf(&Line);
 }
 
+
+void RegisterEmbeddableMimeType(const char *MimeType, long MTLen, int Priority)
+{
+       StrBuf *MT;
+       MT = NewStrBufPlain(MimeType, MTLen);
+       Put(EmbeddableMimes, IKEY(Priority), MT, HFreeStrBuf);
+}
+
+void CreateMimeStr(void)
+{
+       HashPos  *it;
+       void *vMime;
+       long len = 0;
+       const char *Key;
+
+       it = GetNewHashPos(EmbeddableMimes, 0);
+       while (GetNextHashPos(EmbeddableMimes, it, &len, &Key, &vMime) &&
+               (vMime != NULL)) {
+               if (StrLength(EmbeddableMimeStrs) > 0)
+                       StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("|"), 0);
+               else 
+                       StrBufAppendBufPlain(EmbeddableMimeStrs, HKEY("MSGP "), 0);
+               StrBufAppendBuf(EmbeddableMimeStrs, (StrBuf*) vMime, 0);
+       }
+       DeleteHashPos(&it);
+}
+
+void
+ServerStartModule_SERV_FUNC
+(void)
+{
+       EmbeddableMimes = NewHash(1, Flathash);
+       EmbeddableMimeStrs = NewStrBuf();
+}
+
+
+void
+ServerShutdownModule_SERV_FUNC
+(void)
+{
+       FreeStrBuf(&EmbeddableMimeStrs);
+       DeleteHash(&EmbeddableMimes);
+}
+
 void 
 InitModule_SERVFUNC
 (void)
@@ -719,14 +742,15 @@ InitModule_SERVFUNC
 
        RegisterConditional(HKEY("COND:SERV:OPENID"), 2, conditional_serv_supports_openid, CTX_NONE);
        RegisterConditional(HKEY("COND:SERV:NEWU"), 2, conditional_serv_newuser_disabled, CTX_NONE);
-       RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, CTX_NONE);
-       RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, CTX_NONE);
-       RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, CTX_NONE);
-       RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, CTX_NONE);
-       RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, CTX_NONE);
-       RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, CTX_NONE);
-       RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, CTX_NONE);
-       RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, CTX_NONE);
+       RegisterConditional(HKEY("COND:SERV:HAVEFULLTEXT"), 2, conditional_serv_fulltext_enabled, CTX_NONE);
+       RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, NULL, CTX_NONE);
+       RegisterNamespace("SERV:NODENAME", 0, 1, tmplput_serv_nodename, NULL, CTX_NONE);
+       RegisterNamespace("SERV:HUMANNODE", 0, 1, tmplput_serv_humannode, NULL, CTX_NONE);
+       RegisterNamespace("SERV:FQDN", 0, 1, tmplput_serv_fqdn, NULL, CTX_NONE);
+       RegisterNamespace("SERV:SOFTWARE", 0, 1, tmplput_serv_software, NULL, CTX_NONE);
+       RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, NULL, CTX_NONE);
+       RegisterNamespace("SERV:BBS_CITY", 0, 1, tmplput_serv_bbs_city, NULL, CTX_NONE);
+       RegisterNamespace("SERV:MESG", 1, 2, tmplput_mesg, NULL, CTX_NONE);
 /*TODO //      RegisterNamespace("SERV:LDAP_SUPP", 0, 0, tmplput_serv_ldap_enabled, 0); */
 }