]> code.citadel.org Git - citadel.git/blobdiff - webcit/subst.c
* put filename reference into token, so we can put in errormessages where we wouldn...
[citadel.git] / webcit / subst.c
index 37c7b32d96114aea08b6fcaafd43bf3f5e106c9c..c764ce6527b6a06b951484a5e94fa3e96a517f72 100644 (file)
@@ -38,6 +38,7 @@ int LoadTemplates = 0;
 #define SV_NEG_CONDITIONAL 3
 #define SV_CUST_STR_CONDITIONAL 4
 #define SV_SUBTEMPL 5
+#define SV_PREEVALUATED 6
 
 typedef struct _WCTemplate {
        StrBuf *Data;
@@ -50,12 +51,18 @@ typedef struct _WCTemplate {
 typedef struct _HashHandler {
        int nMinArgs;
        int nMaxArgs;
+       int ContextRequired;
        WCHandlerFunc HandlerFunc;
 }HashHandler;
 
 void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere);
 
-void RegisterNS(const char *NSName, long len, int nMinArgs, int nMaxArgs, WCHandlerFunc HandlerFunc)
+void RegisterNS(const char *NSName, 
+               long len, 
+               int nMinArgs, 
+               int nMaxArgs, 
+               WCHandlerFunc HandlerFunc, 
+               int ContextRequired)
 {
        HashHandler *NewHandler;
        
@@ -63,9 +70,39 @@ void RegisterNS(const char *NSName, long len, int nMinArgs, int nMaxArgs, WCHand
        NewHandler->nMinArgs = nMinArgs;
        NewHandler->nMaxArgs = nMaxArgs;
        NewHandler->HandlerFunc = HandlerFunc;  
+       NewHandler->ContextRequired = ContextRequired;
        Put(GlobalNS, NSName, len, NewHandler, NULL);
 }
 
+void FreeToken(WCTemplateToken **Token)
+{
+       int i; 
+       FreeStrBuf(&(*Token)->FlatToken);
+       if ((*Token)->HaveParameters) 
+               for (i = 0; i < (*Token)->nParameters; i++)
+                       free((*Token)->Params[i]);
+       free(*Token);
+       *Token = NULL;
+}
+
+
+
+void FreeWCTemplate(void *vFreeMe)
+{
+       int i;
+       WCTemplate *FreeMe = (WCTemplate*)vFreeMe;
+
+       if (FreeMe->TokenSpace > 0) {
+               for (i = 0; i < FreeMe->nTokensUsed; i ++) {
+                       FreeToken(&FreeMe->Tokens[i]);
+               }
+               free(FreeMe->Tokens);
+       }
+       FreeStrBuf(&FreeMe->FileName);
+       FreeStrBuf(&FreeMe->Data);
+       free(FreeMe);
+}
+
 
 /**
  * \brief debugging function to print array to log
@@ -346,7 +383,7 @@ void SVPutLong(char *keyname, size_t keylen, long Data)
  * \param keyname the keystring to substitute
  * \param fcn_ptr the function callback to give the substitution string
  */
-void SVCallback(char *keyname, size_t keylen, var_callback_fptr fcn_ptr)
+void SVCallback(char *keyname, size_t keylen, WCHandlerFunc fcn_ptr)
 {
        wcsubst *ptr;
        void *vPtr;
@@ -369,14 +406,14 @@ void SVCallback(char *keyname, size_t keylen, var_callback_fptr fcn_ptr)
 
        ptr->wcs_function = fcn_ptr;
 }
-inline void SVCALLBACK(char *keyname, var_callback_fptr fcn_ptr)
+inline void SVCALLBACK(char *keyname, WCHandlerFunc fcn_ptr)
 {
        SVCallback(keyname, strlen(keyname), fcn_ptr);
 }
 
 
 
-void SVPUTBuf(const char *keyname, int keylen, StrBuf *Buf, int ref)
+void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref)
 {
        wcsubst *ptr;
        void *vPtr;
@@ -396,7 +433,7 @@ void SVPUTBuf(const char *keyname, int keylen, StrBuf *Buf, int ref)
        {
                ptr = NewSubstVar(keyname, keylen, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
        }
-       ptr->wcs_value = Buf;
+       ptr->wcs_value = (StrBuf*)Buf;
 }
 
 /**
@@ -430,21 +467,21 @@ void pvo_do_cmd(StrBuf *Target, StrBuf *servcmd) {
  * \brief Print the value of a variable
  * \param keyname get a key to print
  */
-void print_value_of(StrBuf *Target, const char *keyname, size_t keylen) {
+void print_value_of(StrBuf *Target, WCTemplateToken *Token, void *Context, int ContextType) {
        struct wcsession *WCC = WC;
        wcsubst *ptr;
        void *vVar;
 
        /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
        /// TODO: debricated!
-       if (keyname[0] == '=') {
-               DoTemplate(keyname+1, keylen - 1, NULL, NULL);
+       if (Token->pName[0] == '=') {
+               DoTemplate(Token->pName+1, Token->NameEnd - 1, NULL, NULL, 0);
        }
 
 //////TODO: if param[1] == "U" -> urlescape
 /// X -> escputs
        /** Page-local variables */
-       if ((WCC->vars!= NULL) && GetHash(WCC->vars, keyname, keylen, &vVar)) {
+       if ((WCC->vars!= NULL) && GetHash(WCC->vars, Token->pName, Token->NameEnd, &vVar)) {
                ptr = (wcsubst*) vVar;
                switch(ptr->wcs_type) {
                case WCS_STRING:
@@ -454,7 +491,7 @@ void print_value_of(StrBuf *Target, const char *keyname, size_t keylen) {
                        pvo_do_cmd(Target, ptr->wcs_value);
                        break;
                case WCS_FUNCTION:
-                       (*ptr->wcs_function) ();
+                       (*ptr->wcs_function) (Target, Token->nParameters, Token, Context, ContextType);
                        break;
                case WCS_STRBUF:
                case WCS_STRBUF_REF:
@@ -464,7 +501,8 @@ void print_value_of(StrBuf *Target, const char *keyname, size_t keylen) {
                        StrBufAppendPrintf(Target, "%ld", ptr->lvalue);
                        break;
                default:
-                       lprintf(1,"WARNING: invalid value in SV-Hash at %s!", keyname);
+                       lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n", Token->pName);
+                       StrBufAppendPrintf(Target, "<pre>WARNING: \ninvalid value in SV-Hash at %s!\n</pre>", Token->pName);
                }
        }
 }
@@ -500,7 +538,7 @@ int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLoo
                                return ParamToCompare->lvalue == ptr->lvalue;
                        break;
                default:
-                       lprintf(1,"WARNING: invalid value in SV-Hash at %s!", 
+                       lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n", 
                                ParamToLookup->Start);
                }
        }
@@ -530,7 +568,7 @@ int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup)
                case WCS_LONG:
                        return StrTol(Compare) == ptr->lvalue;
                default:
-                       lprintf(1,"WARNING: invalid value in SV-Hash at %s!", 
+                       lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n", 
                                ParamToLookup->Start);
                }
        }
@@ -658,6 +696,7 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
        TemplateParam *Param;
        WCTemplateToken *NewToken = (WCTemplateToken*)malloc(sizeof(WCTemplateToken));
 
+       NewToken->FileName = pTmpl->FileName; /* to print meaningfull log messages... */
        NewToken->Flags = 0;
        NewToken->Line = Line + 1;
        NewToken->pTokenStart = pTmplStart;
@@ -665,7 +704,8 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
        NewToken->TokenEnd =  (pTmplEnd - pStart) - NewToken->TokenStart;
        NewToken->pTokenEnd = pTmplEnd;
        NewToken->NameEnd = NewToken->TokenEnd - 2;
-       NewToken->FlatToken = NewStrBufPlain(pTmplStart, pTmplEnd - pTmplStart);
+       NewToken->PreEval = NULL;
+       NewToken->FlatToken = NewStrBufPlain(pTmplStart + 2, pTmplEnd - pTmplStart - 2);
        
        StrBufPeek(Buf, pTmplStart, + 1, '\0');
        StrBufPeek(Buf, pTmplEnd, -1, '\0');
@@ -684,8 +724,14 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
                                if (Param != NULL) {
                                        NewToken->HaveParameters = 1;
                                        if (NewToken->nParameters > MAXPARAM) {
-                                               lprintf(1, "Only %ld Tokens supported!\n", MAXPARAM);
+                                               lprintf(1, "Error (in '%s' line %ld); "
+                                                       "only [%ld] Params allowed in Tokens [%s]\n",
+                                                       ChrPtr(pTmpl->FileName),
+                                                       NewToken->Line,
+                                                       MAXPARAM,
+                                                       ChrPtr(NewToken->FlatToken));
                                                free(Param);
+                                               FreeToken(&NewToken);
                                                return NULL;
                                        }
                                        NewToken->Params[NewToken->nParameters++] = Param;
@@ -715,42 +761,38 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
                }
                else pch ++;            
        }
-       return NewToken;
-}
+       
+       if (NewToken->Flags == 0) {
+               /* If we're able to find out more about the token, do it now while its fresh. */
+               void *vVar;
+               if (GetHash(GlobalNS, NewToken->pName, NewToken->NameEnd, &vVar)) {
+                       HashHandler *Handler;
+                       Handler = (HashHandler*) vVar;
+                       if ((NewToken->nParameters < Handler->nMinArgs) || 
+                           (NewToken->nParameters > Handler->nMaxArgs)) {
+                               lprintf(1, "Handler [%s] (in '%s' line %ld); "
+                                       "doesn't work with %ld params [%s]\n", 
+                                       NewToken->pName,
+                                       ChrPtr(pTmpl->FileName),
+                                       NewToken->Line,
+                                       NewToken->nParameters, 
+                                       ChrPtr(NewToken->FlatToken));
+                       }
+                       else {
+                               NewToken->PreEval = Handler;
+                               NewToken->Flags = SV_PREEVALUATED;              
+                       }
+               }               
+       }
 
-void FreeToken(WCTemplateToken **Token)
-{
-       int i; 
-       FreeStrBuf(&(*Token)->FlatToken);
-       if ((*Token)->HaveParameters) 
-               for (i = 0; i < (*Token)->nParameters; i++)
-                       free((*Token)->Params[i]);
-       free(*Token);
-       *Token = NULL;
+       return NewToken;
 }
 
 
 
-void FreeWCTemplate(void *vFreeMe)
+int EvaluateConditional(StrBuf *Target, WCTemplateToken *Token, WCTemplate *pTmpl, void *Context, int Neg, int state, int ContextType)
 {
-       int i;
-       WCTemplate *FreeMe = (WCTemplate*)vFreeMe;
-
-       if (FreeMe->TokenSpace > 0) {
-               for (i = 0; i < FreeMe->nTokensUsed; i ++) {
-                       FreeToken(&FreeMe->Tokens[i]);
-               }
-               free(FreeMe->Tokens);
-       }
-       FreeStrBuf(&FreeMe->FileName);
-       FreeStrBuf(&FreeMe->Data);
-       free(FreeMe);
-}
-
-
-int EvaluateConditional(WCTemplateToken *Token, WCTemplate *pTmpl, void *Context, int Neg, int state)
-{
-       void *vConditional;
+       void *vConditional = NULL;
        ConditionalStruct *Cond;
 
        if ((Token->Params[0]->len == 1) &&
@@ -760,24 +802,25 @@ int EvaluateConditional(WCTemplateToken *Token, WCTemplate *pTmpl, void *Context
        if (!GetHash(Contitionals, 
                 Token->Params[0]->Start,
                 Token->Params[0]->len,
-                &vConditional)) {
+                &vConditional) || 
+           (vConditional == NULL)) {
                lprintf(1, "Conditional [%s] (in '%s' line %ld); Not found![%s]\n", 
                        Token->Params[0]->Start,
                        ChrPtr(pTmpl->FileName),
                        Token->Line,
                        ChrPtr(Token->FlatToken));
-       }
-           
-       Cond = (ConditionalStruct *) vConditional;
-
-       if (Cond == NULL) {
-               lprintf(1, "Conditional [%s] (in '%s' line %ld); Not found![%s]\n", 
+               StrBufAppendPrintf(
+                       Target, 
+                       "<pre>\nConditional [%s] (in '%s' line %ld); Not found!\n[%s]\n</pre>\n", 
                        Token->Params[0]->Start,
                        ChrPtr(pTmpl->FileName),
                        Token->Line,
                        ChrPtr(Token->FlatToken));
                return 0;
        }
+           
+       Cond = (ConditionalStruct *) vConditional;
+
        if (Token->nParameters < Cond->nParams) {
                lprintf(1, "Conditional [%s] (in '%s' line %ld); needs %ld Params![%s]\n", 
                        Token->Params[0]->Start,
@@ -785,31 +828,41 @@ int EvaluateConditional(WCTemplateToken *Token, WCTemplate *pTmpl, void *Context
                        Token->Line,
                        Cond->nParams,
                        ChrPtr(Token->FlatToken));
+               StrBufAppendPrintf(
+                       Target, 
+                       "<pre>\nConditional [%s] (in '%s' line %ld); needs %ld Params!\n[%s]\n</pre>\n", 
+                       Token->Params[0]->Start,
+                       ChrPtr(pTmpl->FileName),
+                       Token->Line,
+                       Cond->nParams,
+                       ChrPtr(Token->FlatToken));
                return 0;
        }
-       if (Cond->CondF(Token, Context) == Neg)
+       if (Cond->CondF(Token, Context, ContextType) == Neg)
                return Token->Params[1]->lvalue;
        return 0;
 }
 
-int EvaluateToken(StrBuf *Target, WCTemplateToken *Token, WCTemplate *pTmpl, void *Context, int state)
+int EvaluateToken(StrBuf *Target, WCTemplateToken *Token, WCTemplate *pTmpl, void *Context, int state, int ContextType)
 {
+       HashHandler *Handler;
        void *vVar;
 // much output, since pName is not terminated...
 //     lprintf(1,"Doing token: %s\n",Token->pName);
+
        switch (Token->Flags) {
        case SV_GETTEXT:
                TmplGettext(Target, Token->nParameters, Token);
                break;
        case SV_CONDITIONAL: /** Forward conditional evaluation */
-               return EvaluateConditional(Token, pTmpl, Context, 1, state);
+               return EvaluateConditional(Target, Token, pTmpl, Context, 1, state, ContextType);
                break;
        case SV_NEG_CONDITIONAL: /** Reverse conditional evaluation */
-               return EvaluateConditional(Token, pTmpl, Context, 0, state);
+               return EvaluateConditional(Target, Token, pTmpl, Context, 0, state, ContextType);
                break;
        case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
                if (Token->nParameters >= 6) {
-                       if (EvaluateConditional(Token, pTmpl, Context, 0, state))
+                       if (EvaluateConditional(Target, Token, pTmpl, Context, 0, state, ContextType))
                                StrBufAppendBufPlain(Target, 
                                                     Token->Params[5]->Start,
                                                     Token->Params[5]->len,
@@ -823,39 +876,100 @@ int EvaluateToken(StrBuf *Target, WCTemplateToken *Token, WCTemplate *pTmpl, voi
                break;
        case SV_SUBTEMPL:
                if (Token->nParameters == 1)
-                       DoTemplate(Token->Params[0]->Start, Token->Params[0]->len, NULL, NULL);
+                       DoTemplate(Token->Params[0]->Start, Token->Params[0]->len, NULL, NULL, ContextType);
                break;
+       case SV_PREEVALUATED:
+               Handler = (HashHandler*) Token->PreEval;
+               if ((Handler->ContextRequired != CTX_NONE) &&
+                   (Handler->ContextRequired != ContextType)) {
+                       lprintf(1, "Handler [%s] (in '%s' line %ld); "
+                               "requires context of type %ld, have %ld [%s]\n", 
+                               Token->pName,
+                               ChrPtr(pTmpl->FileName),
+                               Token->Line,
+                               Handler->ContextRequired, 
+                               ContextType,
+                               ChrPtr(Token->FlatToken));
+                       StrBufAppendPrintf(
+                               Target, 
+                               "<pre>\nHandler [%s] (in '%s' line %ld);"
+                               " requires context of type %ld, have %ld!\n[%s]\n</pre>\n", 
+                               Token->pName,
+                               ChrPtr(pTmpl->FileName),
+                               Token->Line,
+                               Handler->ContextRequired, 
+                               ContextType,
+                               ChrPtr(Token->FlatToken));
+                       return -1;
+
+               }
+               Handler->HandlerFunc(Target, 
+                                    Token->nParameters,
+                                    Token,
+                                    Context, 
+                                    ContextType); 
+               break;          
        default:
                if (GetHash(GlobalNS, Token->pName, Token->NameEnd, &vVar)) {
-                       HashHandler *Handler;
                        Handler = (HashHandler*) vVar;
-                       if ((Token->nParameters < Handler->nMinArgs) || 
-                           (Token->nParameters > Handler->nMaxArgs)) {
+                       if ((Handler->ContextRequired != CTX_NONE) &&
+                           (Handler->ContextRequired != ContextType)) {
+                               lprintf(1, "Handler [%s] (in '%s' line %ld); "
+                                       "requires context of type %ld, have %ld [%s]\n", 
+                                       Token->pName,
+                                       ChrPtr(pTmpl->FileName),
+                                       Token->Line,
+                                       Handler->ContextRequired, 
+                                       ContextType,
+                                       ChrPtr(Token->FlatToken));
+                               StrBufAppendPrintf(
+                                       Target, 
+                                       "<pre>\nHandler [%s] (in '%s' line %ld);"
+                                       " requires context of type %ld, have %ld!\n[%s]\n</pre>\n", 
+                                       Token->pName,
+                                       ChrPtr(pTmpl->FileName),
+                                       Token->Line,
+                                       Handler->ContextRequired, 
+                                       ContextType,
+                                       ChrPtr(Token->FlatToken));
+                               return -1;
+                       }
+                       else if ((Token->nParameters < Handler->nMinArgs) || 
+                                (Token->nParameters > Handler->nMaxArgs)) {
                                lprintf(1, "Handler [%s] (in '%s' line %ld); "
-                                       "doesn't work with %ld params [%s]", 
+                                       "doesn't work with %ld params [%s]\n", 
                                        Token->pName,
                                        ChrPtr(pTmpl->FileName),
                                        Token->Line,
                                        Token->nParameters, 
                                        ChrPtr(Token->FlatToken));
+                               StrBufAppendPrintf(
+                                       Target, 
+                                       "<pre>\nHandler [%s] (in '%s' line %ld);"
+                                       " doesn't work with %ld params!\n[%s]\n</pre>\n", 
+                                       Token->pName,
+                                       ChrPtr(pTmpl->FileName),
+                                       Token->Line,
+                                       Token->nParameters,
+                                       ChrPtr(Token->FlatToken));
                        }
                        else {
                                Handler->HandlerFunc(Target, 
                                                     Token->nParameters,
                                                     Token,
-                                                    Context); /*TODO: subset of that */
-                               
+                                                    Context
+                                                    ContextType); /*TODO: subset of that */
                                
                        }
                }
                else {
-                       print_value_of(Target, Token->pName, Token->NameEnd);
+                       print_value_of(Target, Token, Context, ContextType);
                }
        }
        return 0;
 }
 
-void ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, void *Context)
+void ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, void *Context, int ContextType)
 {
        WCTemplate *pTmpl = Tmpl;
        int done = 0;
@@ -868,6 +982,14 @@ void ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, void *Context)
                        lprintf(1, "DBG: ----- loading:  [%s] ------ \n", 
                                ChrPtr(Tmpl->FileName));
                pTmpl = load_template(Tmpl->FileName, NULL, NULL);
+               if(pTmpl == NULL) {
+                       StrBufAppendPrintf(
+                               Target, 
+                               "<pre>\nError loading Template [%s]\n See Logfile for details\n</pre>\n", 
+                               ChrPtr(Tmpl->FileName));
+                       return;
+               }
+
        }
 
        pS = pData = ChrPtr(pTmpl->Data);
@@ -885,17 +1007,20 @@ void ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, void *Context)
                        StrBufAppendBufPlain(
                                Target, pData, 
                                pTmpl->Tokens[i]->pTokenStart - pData, 0);
-                       state = EvaluateToken(Target, pTmpl->Tokens[i], pTmpl, Context, state);
+                       state = EvaluateToken(Target, pTmpl->Tokens[i], pTmpl, Context, state, ContextType);
                        while ((state != 0) && (i+1 < pTmpl->nTokensUsed)) {
                        /* condition told us to skip till its end condition */
                                i++;
                                if ((pTmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
                                    (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
-                                       if (state == EvaluateConditional(pTmpl->Tokens[i], 
-                                                                        pTmpl,
-                                                                        Context, 
-                                                                        pTmpl->Tokens[i]->Flags,
-                                                                        state))
+                                       if (state == EvaluateConditional(
+                                                   Target,
+                                                   pTmpl->Tokens[i], 
+                                                   pTmpl,
+                                                   Context, 
+                                                   pTmpl->Tokens[i]->Flags,
+                                                   state, 
+                                                   ContextType))
                                                state = 0;
                                }
                        }
@@ -976,6 +1101,8 @@ void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
                const char *pts, *pte;
                int InQuotes = 0;
                int InDoubleQuotes = 0;
+
+               /** Find one <? > */
                pos = (-1);
                for (; pch < pE; pch ++) {
                        if ((*pch=='<')&&(*(pch + 1)=='?'))
@@ -985,6 +1112,8 @@ void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
                if (pch >= pE)
                        continue;
                pts = pch;
+
+               /** Found one? parse it. */
                for (; pch < pE - 1; pch ++) {
                        if (*pch == '"')
                                InDoubleQuotes = ! InDoubleQuotes;
@@ -1023,7 +1152,7 @@ const char* PrintTemplate(void *vSubst)
  * \brief Display a variable-substituted template
  * \param templatename template file to load
  */
-void DoTemplate(const char *templatename, long len, void *Context, StrBuf *Target
+void DoTemplate(const char *templatename, long len, StrBuf *Target, void *Context, int ContextType
 {
        HashList *Static;
        HashList *StaticLocal;
@@ -1040,16 +1169,26 @@ void DoTemplate(const char *templatename, long len, void *Context, StrBuf *Targe
                StaticLocal = LocalTemplateCache;
        }
 
+       if (len == 0)
+       {
+               lprintf (1, "Can't to load a template with empty name!\n");
+               StrBufAppendPrintf(Target, "<pre>\nCan't to load a template with empty name!\n</pre>");
+               return;
+       }
+
        if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
            !GetHash(Static, templatename, len, &vTmpl)) {
-               printf ("didn't find %s %ld %ld\n", templatename, len , (long)strlen(templatename));
+               lprintf (1, "didn't find Template [%s] %ld %ld\n", templatename, len , (long)strlen(templatename));
+               StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>", 
+                                  templatename, len, 
+                                  (long)strlen(templatename));
 ///            dbg_PrintHash(Static, PrintTemplate, NULL);
 //             PrintHash(Static, VarPrintTransition, PrintTemplate);
                return;
        }
        if (vTmpl == NULL) 
                return;
-       ProcessTemplate(vTmpl, Target, Context);        
+       ProcessTemplate(vTmpl, Target, Context, ContextType);
 }
 
 int LoadTemplateDir(const char *DirName, HashList *wireless, HashList *big)
@@ -1087,6 +1226,8 @@ int LoadTemplateDir(const char *DirName, HashList *wireless, HashList *big)
                        d_without_ext --;
                if ((d_without_ext == 0) || (d_namelen < 3))
                        continue;
+               if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
+                       continue; /* Ignore backup files... */
 
                IsMobile = (strstr(filedir_entry->d_name, ".m.html")!= NULL);
                PStart = filedir_entry->d_name;
@@ -1097,7 +1238,7 @@ int LoadTemplateDir(const char *DirName, HashList *wireless, HashList *big)
                StrBufPlain(Tag, filedir_entry->d_name, MinorPtr - filedir_entry->d_name);
 
 
-               printf("%s %d %s\n",ChrPtr(FileName), IsMobile, ChrPtr(Tag));
+               lprintf(1, "%s %d %s\n",ChrPtr(FileName), IsMobile, ChrPtr(Tag));
                if (LoadTemplates == 0)
                        load_template(FileName, Tag, (IsMobile)?wireless:big);
                else
@@ -1120,49 +1261,49 @@ void InitTemplateCache(void)
                        LocalTemplateCache);
 }
 
-void tmplput_serv_ip(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+void tmplput_serv_ip(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
 }
 
-void tmplput_serv_nodename(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+void tmplput_serv_nodename(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        StrEscAppend(Target, NULL, serv_info.serv_nodename, 0, 0);
 }
 
-void tmplput_serv_humannode(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+void tmplput_serv_humannode(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        StrEscAppend(Target, NULL, serv_info.serv_humannode, 0, 0);
 }
 
-void tmplput_serv_fqdn(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+void tmplput_serv_fqdn(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        StrEscAppend(Target, NULL, serv_info.serv_fqdn, 0, 0);
 }
 
-void tmmplput_serv_software(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+void tmmplput_serv_software(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        StrEscAppend(Target, NULL, serv_info.serv_software, 0, 0);
 }
 
-void tmplput_serv_rev_level(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+void tmplput_serv_rev_level(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        StrBufAppendPrintf(Target, "%d.%02d",
                            serv_info.serv_rev_level / 100,
                            serv_info.serv_rev_level % 100);
 }
 
-void tmmplput_serv_bbs_city(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+void tmmplput_serv_bbs_city(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        StrEscAppend(Target, NULL, serv_info.serv_bbs_city, 0, 0);
 }
 
-void tmplput_current_user(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+void tmplput_current_user(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        StrEscAppend(Target, NULL, WC->wc_fullname, 0, 0);
 }
 
-void tmplput_current_room(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+void tmplput_current_room(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        StrEscAppend(Target, NULL, WC->wc_roomname, 0, 0);
 }
@@ -1170,12 +1311,15 @@ void tmplput_current_room(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, vo
 
 typedef struct _HashIterator {
        HashList *StaticList;
+       int AdditionalParams;
+       int ContextType;
+       int XPectContextType;
        RetrieveHashlistFunc GetHash;
        HashDestructorFunc Destructor;
        SubTemplFunc DoSubTemplate;
 } HashIterator;
 
-void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        void *vIt;
        HashIterator *It;
@@ -1190,22 +1334,91 @@ void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, vo
        if (!GetHash(Iterators, 
                     Tokens->Params[0]->Start,
                     Tokens->Params[0]->len,
-                    &vIt))
+                    &vIt)) {
+               lprintf(1, "unknown Iterator [%s] (in '%s' line %ld); "
+                       " [%s]\n", 
+                       Tokens->Params[0]->Start,
+                       ChrPtr(Tokens->FileName),
+                       Tokens->Line,
+                       ChrPtr(Tokens->FlatToken));
+               StrBufAppendPrintf(
+                       Target,
+                       "<pre>\nunknown Iterator [%s] (in '%s' line %ld); \n"
+                       " [%s]\n</pre>", 
+                       Tokens->Params[0]->Start,
+                       ChrPtr(Tokens->FileName),
+                       Tokens->Line,
+                       ChrPtr(Tokens->FlatToken));
                return;
+       }
+
        It = (HashIterator*) vIt;
+
+       if (Tokens->nParameters < It->AdditionalParams + 2) {
+               lprintf(1, "Iterator [%s] (in '%s' line %ld); "
+                       "doesn't work with %ld params [%s]\n", 
+                       Tokens->Params[0]->Start,
+                       ChrPtr(Tokens->FileName),
+                       Tokens->Line,
+                       Tokens->nParameters, 
+                       ChrPtr(Tokens->FlatToken));
+               StrBufAppendPrintf(
+                       Target,
+                       "<pre>Iterator [%s] \n(in '%s' line %ld);\n"
+                       "doesn't work with %ld params \n[%s]\n</pre>", 
+                       Tokens->Params[0]->Start,
+                       ChrPtr(Tokens->FileName),
+                       Tokens->Line,
+                       Tokens->nParameters, 
+                       ChrPtr(Tokens->FlatToken));
+               return;
+       }
+
+       if ((It->XPectContextType != CTX_NONE) &&
+           (It->XPectContextType != ContextType)) {
+               lprintf(1, "Iterator [%s] (in '%s' line %ld); "
+                       "requires context of type %ld, have %ld [%s]\n", 
+                       Tokens->pName,
+                       ChrPtr(Tokens->FileName),
+                       Tokens->Line,
+                       It->XPectContextType, 
+                       ContextType,
+                       ChrPtr(Tokens->FlatToken));
+               StrBufAppendPrintf(
+                       Target, 
+                       "<pre>\nIterator [%s] (in '%s' line %ld);"
+                       " requires context of type %ld, have %ld!\n[%s]\n</pre>\n", 
+                       Tokens->pName,
+                       ChrPtr(Tokens->FileName),
+                       Tokens->Line,
+                       It->XPectContextType, 
+                       ContextType,
+                       ChrPtr(Tokens->FlatToken));
+               return ;
+               
+       }
+
+
+
+
        if (It->StaticList == NULL)
-               List = It->GetHash();
+               List = It->GetHash(Target, nArgs, Tokens, Context, ContextType);
        else
                List = It->StaticList;
 
        SubBuf = NewStrBuf();
        it = GetNewHashPos();
        while (GetNextHashPos(List, it, &len, &Key, &vContext)) {
-               svprintf(HKEY("ITERATE:ODDEVEN"), WCS_STRING, "%s", (oddeven)?"odd":"even");
-               It->DoSubTemplate(SubBuf, vContext);
+               svprintf(HKEY("ITERATE:ODDEVEN"), WCS_STRING, "%s", 
+                        (oddeven) ? "odd" : "even");
+               svprintf(HKEY("ITERATE:KEY"), WCS_STRING, "%s", Key);
+
+               if (It->DoSubTemplate != NULL)
+                       It->DoSubTemplate(SubBuf, vContext, Tokens);
                DoTemplate(Tokens->Params[1]->Start,
                           Tokens->Params[1]->len,
-                          vContext, SubBuf);
+                          SubBuf, vContext,
+                          It->ContextType);
                        
                StrBufAppendBuf(Target, SubBuf, 0);
                FlushStrBuf(SubBuf);
@@ -1214,10 +1427,10 @@ void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, vo
        FreeStrBuf(&SubBuf);
        DeleteHashPos(&it);
        if (It->Destructor != NULL)
-               It->Destructor(List);
+               It->Destructor(&List);
 }
 
-int ConditionalVar(WCTemplateToken *Tokens, void *Context)
+int ConditionalVar(WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        void *vsubst;
        wcsubst *subst;
@@ -1228,6 +1441,12 @@ int ConditionalVar(WCTemplateToken *Tokens, void *Context)
                     &vsubst))
                return 0;
        subst = (wcsubst*) vsubst;
+       if ((subst->ContextRequired != CTX_NONE) &&
+           (subst->ContextRequired != ContextType)) {
+               lprintf(1,"  WARNING: Conditional requires Context: [%ld]!\n", Tokens->Params[2]->Start);
+               return -1;
+       }
+
        switch(subst->wcs_type) {
        case WCS_FUNCTION:
                return (subst->wcs_function!=NULL);
@@ -1246,54 +1465,65 @@ int ConditionalVar(WCTemplateToken *Tokens, void *Context)
                return (subst->lvalue == Tokens->Params[3]->lvalue);
        default:
                lprintf(1,"  WARNING: invalid type: [%ld]!\n", subst->wcs_type);
+               return -1;
        }
        return 0;
 }
 
 void RegisterITERATOR(const char *Name, long len, 
+                     int AdditionalParams, 
                      HashList *StaticList, 
                      RetrieveHashlistFunc GetHash, 
                      SubTemplFunc DoSubTempl,
-                     HashDestructorFunc Destructor)
+                     HashDestructorFunc Destructor,
+                     int ContextType, 
+                     int XPectContextType)
 {
        HashIterator *It = (HashIterator*)malloc(sizeof(HashIterator));
        It->StaticList = StaticList;
+       It->AdditionalParams = AdditionalParams;
        It->GetHash = GetHash;
        It->DoSubTemplate = DoSubTempl;
        It->Destructor = Destructor;
+       It->ContextType = ContextType;
+       It->XPectContextType = XPectContextType;
        Put(Iterators, Name, len, It, NULL);
 }
 
 void RegisterConditional(const char *Name, long len, 
                         int nParams,
-                        WCConditionalFunc CondF)
+                        WCConditionalFunc CondF, 
+                        int ContextRequired)
 {
        ConditionalStruct *Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
        Cond->nParams = nParams;
        Cond->CondF = CondF;
+       Cond->ContextRequired = ContextRequired;
        Put(Contitionals, Name, len, Cond, NULL);
 }
 
-void tmpl_do_boxed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+void tmpl_do_boxed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        if (nArgs == 2) {
                StrBuf *Headline = NewStrBuf();
                DoTemplate(Tokens->Params[1]->Start, 
                           Tokens->Params[1]->len,
+                          Headline, 
                           Context, 
-                          Headline);
+                          ContextType);
                SVPutBuf("BOXTITLE", Headline, 0);
        }
-
-       DoTemplate(HKEY("beginbox"), Context, Target);
+       
+       DoTemplate(HKEY("beginbox"), Target, Context, ContextType);
        DoTemplate(Tokens->Params[0]->Start, 
                   Tokens->Params[0]->len,
+                  Target, 
                   Context, 
-                  Target);
-       DoTemplate(HKEY("endbox"), Context, Target);
+                  ContextType);
+       DoTemplate(HKEY("endbox"), Target, Context, ContextType);
 }
 
-void tmpl_do_tabbed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+void tmpl_do_tabbed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        StrBuf **TabNames;
        int i, ntabs, nTabs;
@@ -1306,8 +1536,9 @@ void tmpl_do_tabbed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Co
                if (Tokens->Params[i * 2]->len > 0) {
                        DoTemplate(Tokens->Params[i * 2]->Start, 
                                   Tokens->Params[i * 2]->len,
+                                  TabNames[i],
                                   Context,
-                                  TabNames[i]);
+                                  ContextType);
                }
                else { 
                        /** A Tab without subject? we can't count that, add it as silent */
@@ -1321,8 +1552,9 @@ void tmpl_do_tabbed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Co
 
                DoTemplate(Tokens->Params[i * 2 + 1]->Start, 
                           Tokens->Params[i * 2 + 1]->len,
+                          Target,
                           Context, 
-                          Target);
+                          ContextType);
                StrEndTab(Target, i, nTabs);
        }
 }
@@ -1331,20 +1563,20 @@ void
 InitModule_SUBST
 (void)
 {
-       RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip);
-       RegisterNamespace("SERV:NODENAME", 0, 0, tmplput_serv_nodename);
-       RegisterNamespace("SERV:HUMANNODE", 0, 0, tmplput_serv_humannode);
-       RegisterNamespace("SERV:FQDN", 0, 0, tmplput_serv_fqdn);
-       RegisterNamespace("SERV:SOFTWARE", 0, 0, tmmplput_serv_software);
-       RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level);
-       RegisterNamespace("SERV:BBS_CITY", 0, 0, tmmplput_serv_bbs_city);
-///    RegisterNamespace("SERV:LDAP_SUPP", 0, 0, tmmplput_serv_ldap_enabled);
-       RegisterNamespace("CURRENT_USER", 0, 0, tmplput_current_user);
-       RegisterNamespace("CURRENT_ROOM", 0, 0, tmplput_current_room);
-       RegisterNamespace("ITERATE", 2, 4, tmpl_iterate_subtmpl);
-       RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed);
-       RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed);
-       RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar);
+       RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, CTX_NONE);
+       RegisterNamespace("SERV:NODENAME", 0, 0, tmplput_serv_nodename, CTX_NONE);
+       RegisterNamespace("SERV:HUMANNODE", 0, 0, tmplput_serv_humannode, CTX_NONE);
+       RegisterNamespace("SERV:FQDN", 0, 0, tmplput_serv_fqdn, CTX_NONE);
+       RegisterNamespace("SERV:SOFTWARE", 0, 0, tmmplput_serv_software, CTX_NONE);
+       RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, CTX_NONE);
+       RegisterNamespace("SERV:BBS_CITY", 0, 0, tmmplput_serv_bbs_city, CTX_NONE);
+///    RegisterNamespace("SERV:LDAP_SUPP", 0, 0, tmmplput_serv_ldap_enabled, 0);
+       RegisterNamespace("CURRENT_USER", 0, 0, tmplput_current_user, CTX_NONE);
+       RegisterNamespace("CURRENT_ROOM", 0, 0, tmplput_current_room, CTX_NONE);
+       RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl, CTX_NONE);
+       RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed, CTX_NONE);
+       RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed, CTX_NONE);
+       RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar, CTX_NONE);
 }
 
 /*@}*/