* BIG rewrite of header handling and slimming of context_loop and session_loop; shuff...
[citadel.git] / webcit / serv_func.c
index c0614aa2c10a2c286ff709470f499a2c86840204..88289bde65adabc9c242d14b94dac00a303fef1a 100644 (file)
@@ -5,32 +5,52 @@
 #include "webcit.h"
 #include "webserver.h"
 
-struct serv_info serv_info; /**< our connection data to the server */
+int is_uds = 0;
+char serv_sock_name[PATH_MAX] = "";
+
+void DeleteServInfo(ServInfo **FreeMe)
+{
+       if (*FreeMe == NULL)
+               return;
+       FreeStrBuf(&(*FreeMe)->serv_nodename);
+       FreeStrBuf(&(*FreeMe)->serv_humannode);
+       FreeStrBuf(&(*FreeMe)->serv_fqdn);
+       FreeStrBuf(&(*FreeMe)->serv_software);
+       FreeStrBuf(&(*FreeMe)->serv_bbs_city);
+       FreeStrBuf(&(*FreeMe)->serv_sysadm);
+       FreeStrBuf(&(*FreeMe)->serv_moreprompt);
+       FreeStrBuf(&(*FreeMe)->serv_default_cal_zone);
+       FreeStrBuf(&(*FreeMe)->serv_svn_revision);
+       free(*FreeMe);
+       *FreeMe = NULL;
+}
+
 /*
  * get info about the server we've connected to
  *
  * browser_host                the citadell we want to connect to
  * user_agent          which browser uses our client?
  */
-void get_serv_info(char *browser_host, char *user_agent)
+ServInfo *get_serv_info(StrBuf *browser_host, StrBuf *user_agent)
 {
+       ServInfo *info;
        StrBuf *Buf;
-       char buf[SIZ];
        int a;
 
+       Buf = NewStrBuf();
        /** Tell the server what kind of client is connecting */
        serv_printf("IDEN %d|%d|%d|%s|%s",
-               DEVELOPER_ID,
-               CLIENT_ID,
-               CLIENT_VERSION,
-               user_agent,
-               browser_host
+                   DEVELOPER_ID,
+                   CLIENT_ID,
+                   CLIENT_VERSION,
+                   ChrPtr(user_agent),
+                   ChrPtr(browser_host)
        );
-       serv_getln(buf, sizeof buf);
+       StrBuf_ServGetln(Buf);
 
        /** Tell the server what kind of richtext we prefer */
-       serv_puts("MSGP text/calendar|text/html|text/plain");
-       serv_getln(buf, sizeof buf);
+       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
@@ -38,74 +58,232 @@ void get_serv_info(char *browser_host, char *user_agent)
         * instead of by the client.
         */
        serv_puts("ICAL sgi|1");
-       serv_getln(buf, sizeof buf);
+       StrBuf_ServGetln(Buf);
 
        /** Now ask the server to tell us a little bit about itself... */
        serv_puts("INFO");
-       serv_getln(buf, sizeof buf);
-       if (buf[0] != '1')
-               return;
+       StrBuf_ServGetln(Buf);
+       if (GetServerStatus(Buf, NULL) != 1) {
+               FreeStrBuf(&Buf);
+               return NULL;
+       }
 
+       info = (ServInfo*)malloc(sizeof(ServInfo));
+       memset(info, 0, sizeof(ServInfo));
        a = 0;
-       Buf = NewStrBuf();
        while (StrBuf_ServGetln(Buf), (strcmp(ChrPtr(Buf), "000")!= 0)) {
 /*             lprintf (1, "a: %d [%s]", a, ChrPtr(Buf));*/
                switch (a) {
                case 0:
-                       serv_info.serv_pid = StrToi(Buf);
-                       WC->ctdl_pid = serv_info.serv_pid;
+                       info->serv_pid = StrToi(Buf);
+                       WC->ctdl_pid = info->serv_pid;
                        break;
                case 1:
-                       serv_info.serv_nodename = NewStrBufDup(Buf);
+                       info->serv_nodename = NewStrBufDup(Buf);
                        break;
                case 2:
-                       serv_info.serv_humannode = NewStrBufDup(Buf);
+                       info->serv_humannode = NewStrBufDup(Buf);
                        break;
                case 3:
-                       serv_info.serv_fqdn = NewStrBufDup(Buf);
+                       info->serv_fqdn = NewStrBufDup(Buf);
                        break;
                case 4:
-                       serv_info.serv_software = NewStrBufDup(Buf);
+                       info->serv_software = NewStrBufDup(Buf);
                        break;
                case 5:
-                       serv_info.serv_rev_level = StrToi(Buf);
+                       info->serv_rev_level = StrToi(Buf);
                        break;
                case 6:
-                       serv_info.serv_bbs_city = NewStrBufDup(Buf);
+                       info->serv_bbs_city = NewStrBufDup(Buf);
                        break;
                case 7:
-                       serv_info.serv_sysadm = NewStrBufDup(Buf);
+                       info->serv_sysadm = NewStrBufDup(Buf);
                        break;
                case 9:
-                       serv_info.serv_moreprompt = NewStrBufDup(Buf);
+                       info->serv_moreprompt = NewStrBufDup(Buf);
                        break;
                case 14:
-                       serv_info.serv_supports_ldap = StrToi(Buf);
+                       info->serv_supports_ldap = StrToi(Buf);
                        break;
                case 15:
-                       serv_info.serv_newuser_disabled = StrToi(Buf);
+                       info->serv_newuser_disabled = StrToi(Buf);
                        break;
                case 16:
-                       serv_info.serv_default_cal_zone = NewStrBufDup(Buf);
+                       info->serv_default_cal_zone = NewStrBufDup(Buf);
                        break;
                case 20:
-                       serv_info.serv_supports_sieve = StrToi(Buf);
+                       info->serv_supports_sieve = StrToi(Buf);
                        break;
                case 21:
-                       serv_info.serv_fulltext_enabled = StrToi(Buf);
+                       info->serv_fulltext_enabled = StrToi(Buf);
                        break;
                case 22:
-                       serv_info.serv_svn_revision = NewStrBufDup(Buf);
+                       info->serv_svn_revision = NewStrBufDup(Buf);
                        break;
                case 23:
-                       serv_info.serv_supports_openid = StrToi(Buf);
+                       info->serv_supports_openid = StrToi(Buf);
                        break;
                }
                ++a;
        }
+       FreeStrBuf(&Buf);
+       return info;
 }
 
+int GetConnected (void)
+{
+       StrBuf *Buf;
+       wcsession *WCC = WC;
+
+       if (WCC->ReadBuf == NULL)
+               WCC->ReadBuf = NewStrBuf();
+       if (is_uds) /* unix domain socket */
+               WCC->serv_sock = uds_connectsock(serv_sock_name);
+       else        /* tcp socket */
+               WCC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
+       
+       if (WCC->serv_sock < 0) {
+               do_logout();
+               FreeStrBuf(&WCC->ReadBuf);
+               return 1;
+       }
+       else {
+               long Status;
+               Buf = NewStrBuf();
+               WCC->connected = 1;
+               StrBuf_ServGetln(Buf);  /* get the server greeting */
+               GetServerStatus(Buf, &Status);
+               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."));
+                       end_burst();
+                       end_webcit_session();
+                       return 1;
+               }
 
+               /*
+                * From what host is our user connecting?  Go with
+                * the host at the other end of the HTTP socket,
+                * 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();
+                               Put(WCC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"), 
+                                   WCC->Hdr->browser_host, HFreeStrBuf);
+                       }
+                       locate_host(WCC->Hdr->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);
+               if (WCC->serv_info == NULL){
+                       begin_burst();
+                       wprintf(_("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");
+                       end_burst();
+                       end_webcit_session();
+                       return 1;
+               }
+               if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
+                       begin_burst();
+                       wprintf(_("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"
+                                 " newer.\n\n\n"),
+                               WCC->serv_info->serv_rev_level / 100,
+                               WCC->serv_info->serv_rev_level % 100,
+                               MINIMUM_CIT_VERSION / 100,
+                               MINIMUM_CIT_VERSION % 100
+                               );
+                       hprintf("HTTP/1.1 200 OK\r\n");
+                       hprintf("Content-type: text/plain; charset=utf-8\r\n");
+                       end_burst();
+                       end_webcit_session();
+                       return 1;
+               }
+       }
+       if (WCC->ReadBuf == NULL)
+               WCC->ReadBuf = NewStrBuf();
+       if (is_uds)/* unix domain socket */
+               WCC->serv_sock = uds_connectsock(serv_sock_name);
+       else /* tcp socket */
+               WCC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
+       
+       if (WCC->serv_sock < 0) {
+               do_logout();
+               FreeStrBuf(&WCC->ReadBuf);
+               return 1;
+       }
+       else {
+               long Status;
+               StrBuf *Buf;
+
+               Buf = NewStrBuf();
+               WCC->connected = 1;
+               StrBuf_ServGetln(Buf);
+               GetServerStatus(Buf,&Status);
+               /* get the server greeting */
+               
+               /* 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."));
+                       end_burst();
+                       end_webcit_session();
+                       return 1;
+               }
+
+               /*
+                * From what host is our user connecting?  Go with
+                * the host at the other end of the HTTP socket,
+                * 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();
+                               Put(WCC->Hdr->HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"), 
+                                   WCC->Hdr->browser_host, HFreeStrBuf);
+                       }
+                       locate_host(WCC->Hdr->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);
+               if (WCC->serv_info == NULL){
+                       begin_burst();
+                       wprintf(_("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");
+                       end_burst();
+                       end_webcit_session();
+                       return 1;
+               }
+               if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
+                       begin_burst();
+                       wprintf(_("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"
+                                 " newer.\n\n\n"),
+                               WCC->serv_info->serv_rev_level / 100,
+                               WCC->serv_info->serv_rev_level % 100,
+                               MINIMUM_CIT_VERSION / 100,
+                               MINIMUM_CIT_VERSION % 100
+                               );
+                       hprintf("HTTP/1.1 200 OK\r\n");
+                       hprintf("Content-type: text/plain; charset=utf-8\r\n");
+                       end_burst();
+                       end_webcit_session();
+                       return 1;
+               }
+       }
+       return 0;
+}
 
 /**
  *  Read Citadel variformat text and spit it out as HTML.
@@ -148,7 +326,7 @@ void _fmout(StrBuf *Target, char *align)
                /** Activate embedded URL's */
                url(buf, sizeof(buf));
 
-               escputs(buf);/*/ TODO: Target */
+               escputs(buf);
                StrBufAppendBufPlain(Target, HKEY("\n"), 0);
        }
        if (bq == 1) {
@@ -170,7 +348,7 @@ void FmOut(StrBuf *Target, char *align, StrBuf *Source)
        long len;
        int intext = 0;
 
-       StrBufAppendPrintf(Target, "<div align=\"%s\">\n", align);
+       StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
        while ((n = StrBufSipLine(Line, Source, &BufPtr), n >= 0) && !done)
        {
                done = n == 0;
@@ -181,7 +359,7 @@ void FmOut(StrBuf *Target, char *align, StrBuf *Source)
                pte = ptr + len;
 
                if ((intext == 1) && (isspace(*ptr))) {
-                       StrBufAppendBufPlain(Target, HKEY("<br />"), 0);
+                       StrBufAppendBufPlain(Target, HKEY("<br>"), 0);
                }
                intext = 1;
                if (isspace(*ptr)) while ((ptr < pte) &&
@@ -219,7 +397,7 @@ void FmOut(StrBuf *Target, char *align, StrBuf *Source)
        }
        for (i = 0; i < bn; i++)                                
                StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
-       StrBufAppendBufPlain(Target, HKEY("</div><br />\n"), 0);
+       StrBufAppendBufPlain(Target, HKEY("</div><br>\n"), 0);
        FreeStrBuf(&Line);
        FreeStrBuf(&Line1);
        FreeStrBuf(&Line2);
@@ -416,14 +594,12 @@ void server_to_text()
  * server READ commands.
  * \return the read content as StrBuf
  */
-int read_server_binary(StrBuf *Ret, size_t total_len) 
+int read_server_binary(StrBuf *Ret, size_t total_len, StrBuf *Buf
 {
        char buf[SIZ];
        size_t bytes = 0;
        size_t thisblock = 0;
-       StrBuf *Buf;
        
-       Buf = NewStrBuf();
        if (Ret == NULL)
            return -1;
 
@@ -442,14 +618,14 @@ int read_server_binary(StrBuf *Ret, size_t total_len)
                {
                        if (GetServerStatus(Buf, NULL) == 6)
                        {
-                           StrBufCutLeft(Buf, 4); /*/ TODO : thisblock = (size_t)atoi(&buf[4]); */
+                           StrBufCutLeft(Buf, 4);
                            thisblock = StrTol(Buf);
                            if (!WC->connected) {
                                    FlushStrBuf(Ret); 
                                    FreeStrBuf(&Buf); 
                                    return -1; 
                            }
-                           StrBuf_ServGetBLOB(Ret, thisblock);
+                           StrBuf_ServGetBLOBBuffered(Ret, thisblock);
                            bytes += thisblock;
                    }
                    else {
@@ -459,7 +635,6 @@ int read_server_binary(StrBuf *Ret, size_t total_len)
                    }
                }
        }
-       FreeStrBuf(&Buf);
        return StrLength(Ret);
 }
 
@@ -512,42 +687,66 @@ void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP)
 
 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP)
 {
-       StrBufAppendTemplate(Target, TP, serv_info.serv_nodename, 0);
+       wcsession *WCC = WC;
+       if (WCC->serv_info == NULL)
+               return;
+       StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_nodename, 0);
 }
 
 void tmplput_serv_humannode(StrBuf *Target, WCTemplputParams *TP)
 {
-       StrBufAppendTemplate(Target, TP, serv_info.serv_humannode, 0);
+       wcsession *WCC = WC;
+       if (WCC->serv_info == NULL)
+               return;
+       StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_humannode, 0);
 }
 
 void tmplput_serv_fqdn(StrBuf *Target, WCTemplputParams *TP)
 {
-       StrBufAppendTemplate(Target, TP, serv_info.serv_fqdn, 0);
+       wcsession *WCC = WC;
+       if (WCC->serv_info == NULL)
+               return;
+       StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_fqdn, 0);
 }
 
 void tmplput_serv_software(StrBuf *Target, WCTemplputParams *TP)
 {
-       StrBufAppendTemplate(Target, TP, serv_info.serv_software, 0);
+       wcsession *WCC = WC;
+       if (WCC->serv_info == NULL)
+               return;
+       StrBufAppendTemplate(Target, TP, WCC->serv_info->serv_software, 0);
 }
 
 void tmplput_serv_rev_level(StrBuf *Target, WCTemplputParams *TP)
 {
+       wcsession *WCC = WC;
+       if (WCC->serv_info == NULL)
+               return;
        StrBufAppendPrintf(Target, "%d.%02d",
-                           serv_info.serv_rev_level / 100,
-                           serv_info.serv_rev_level % 100);
+                           WCC->serv_info->serv_rev_level / 100,
+                           WCC->serv_info->serv_rev_level % 100);
 }
 int conditional_serv_newuser_disabled(StrBuf *Target, WCTemplputParams *TP)
 {
-       return serv_info.serv_newuser_disabled != 0;
+       wcsession *WCC = WC;
+       if (WCC->serv_info == NULL)
+               return 0;
+       return WCC->serv_info->serv_newuser_disabled != 0;
 }
 int conditional_serv_supports_openid(StrBuf *Target, WCTemplputParams *TP)
 {
-       return serv_info.serv_supports_openid != 0;
+       wcsession *WCC = WC;
+       if (WCC->serv_info == NULL)
+               return 0;
+       return WCC->serv_info->serv_supports_openid != 0;
 }
 
 void tmplput_serv_bbs_city(StrBuf *Target, WCTemplputParams *TP)
 {
-       StrBufAppendTemplate(Target, TP, serv_info.serv_bbs_city, 0);
+       wcsession *WCC = WC;
+       if (WCC->serv_info == NULL)
+               return;
+       StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
 }
 
 
@@ -561,22 +760,26 @@ void tmplput_mesg(StrBuf *Target, WCTemplputParams *TP)
        Buf = NewStrBuf();
        Line = NewStrBuf();
        serv_printf("MESG %s", TP->Tokens->Params[0]->Start);
+
        StrBuf_ServGetln(Line);
-       while (!Done &&  (StrBuf_ServGetln(Line)>=0)) {
-               if ( (StrLength(Line)==3) && 
-                   !strcmp(ChrPtr(Line), "000")) 
-                       Done = 1;
-               else
-               {
-                       if (n > 0)
-                               StrBufAppendBufPlain(Buf, "\n", 1, 0);
-                       StrBufAppendBuf(Buf, Line, 0);
+       if (GetServerStatus(Line, NULL) == 1) {
+               while (!Done &&  (StrBuf_ServGetln(Line)>=0)) {
+                       if ( (StrLength(Line)==3) && 
+                            !strcmp(ChrPtr(Line), "000")) 
+                               Done = 1;
+                       else
+                       {
+                               if (n > 0)
+                                       StrBufAppendBufPlain(Buf, "\n", 1, 0);
+                               StrBufAppendBuf(Buf, Line, 0);
+                       }
+                       n++;
                }
-               n++;
+       
+               FlushStrBuf(Line);
+               FmOut(Line, "center", Buf);
+               StrBufAppendTemplate(Target, TP, Line, 1);
        }
-       FlushStrBuf(Line);
-       FmOut(Line, "center", Buf);
-       StrBufAppendTemplate(Target, TP, Line, 1);
        FreeStrBuf(&Buf);
        FreeStrBuf(&Line);
 }
@@ -585,6 +788,9 @@ void
 InitModule_SERVFUNC
 (void)
 {
+       is_uds = strcasecmp(ctdlhost, "uds") == 0;
+       if (is_uds)
+               snprintf(serv_sock_name, PATH_MAX, "%s/citadel.socket", ctdlport);
 
        RegisterConditional(HKEY("COND:SERV:OPENID"), 2, conditional_serv_supports_openid, CTX_NONE);
        RegisterConditional(HKEY("COND:SERV:NEWU"), 2, conditional_serv_newuser_disabled, CTX_NONE);
@@ -599,4 +805,12 @@ InitModule_SERVFUNC
 /*TODO //      RegisterNamespace("SERV:LDAP_SUPP", 0, 0, tmplput_serv_ldap_enabled, 0); */
 }
 
+
+
+void 
+SessionDestroyModule_SERVFUNC
+(wcsession *sess)
+{
+       DeleteServInfo(&sess->serv_info);
+}
 /*@}*/