* shrinked the rest of these shutdown stuff into the module-handler.
[citadel.git] / webcit / serv_func.c
index 6b36860f4cd520283c71e99896b0ec7c05f8f875..de6fb0538411cd4561f19b85ccb315f79e3be21d 100644 (file)
@@ -8,6 +8,8 @@
 
 void DeleteServInfo(ServInfo **FreeMe)
 {
+       if (*FreeMe == NULL)
+               return;
        FreeStrBuf(&(*FreeMe)->serv_nodename);
        FreeStrBuf(&(*FreeMe)->serv_humannode);
        FreeStrBuf(&(*FreeMe)->serv_fqdn);
@@ -31,9 +33,9 @@ ServInfo *get_serv_info(StrBuf *browser_host, char *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,
@@ -42,11 +44,11 @@ ServInfo *get_serv_info(StrBuf *browser_host, char *user_agent)
                    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);
+       StrBuf_ServGetln(Buf);
 
        /*
         * Tell the server that when we save a calendar event, we
@@ -54,18 +56,19 @@ ServInfo *get_serv_info(StrBuf *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')
+       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) {
@@ -168,7 +171,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) {
@@ -190,7 +193,7 @@ void FmOut(StrBuf *Target, char *align, StrBuf *Source)
        long len;
        int intext = 0;
 
-       StrBufAppendPrintf(Target, "<div class=\"fmout\">\n", align);
+       StrBufAppendPrintf(Target, "<div class=\"fmout-%s\">\n", align);
        while ((n = StrBufSipLine(Line, Source, &BufPtr), n >= 0) && !done)
        {
                done = n == 0;
@@ -436,14 +439,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;
 
@@ -462,14 +463,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 {
@@ -479,7 +480,6 @@ int read_server_binary(StrBuf *Ret, size_t total_len)
                    }
                }
        }
-       FreeStrBuf(&Buf);
        return StrLength(Ret);
 }
 
@@ -532,41 +532,65 @@ void tmplput_serv_ip(StrBuf *Target, WCTemplputParams *TP)
 
 void tmplput_serv_nodename(StrBuf *Target, WCTemplputParams *TP)
 {
-       StrBufAppendTemplate(Target, TP, WC->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, WC->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, WC->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, WC->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",
-                           WC->serv_info->serv_rev_level / 100,
-                           WC->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 WC->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 WC->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)
 {
+       wcsession *WCC = WC;
+       if (WCC->serv_info == NULL)
+               return;
        StrBufAppendTemplate(Target, TP, WC->serv_info->serv_bbs_city, 0);
 }
 
@@ -581,22 +605,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);
 }
@@ -619,4 +647,12 @@ InitModule_SERVFUNC
 /*TODO //      RegisterNamespace("SERV:LDAP_SUPP", 0, 0, tmplput_serv_ldap_enabled, 0); */
 }
 
+
+
+void 
+SessionDestroyModule_SERVFUNC
+(wcsession *sess)
+{
+       DeleteServInfo(&sess->serv_info);
+}
 /*@}*/