]> code.citadel.org Git - citadel.git/blobdiff - webcit/subst.c
* add transitional beginboxx template and move some places to the new syntax
[citadel.git] / webcit / subst.c
index 2d9b27086d3e65bf76524c6829cb150c95209789..a1879ba9d39628b7b070502b7a783e04b1dfb2f4 100644 (file)
@@ -29,7 +29,7 @@ HashList *LocalTemplateCache;
 
 HashList *GlobalNS;
 HashList *Iterators;
-HashList *Contitionals;
+HashList *Conditionals;
 
 int LoadTemplates = 0;
 
@@ -463,6 +463,97 @@ void pvo_do_cmd(StrBuf *Target, StrBuf *servcmd) {
        }
 }
 
+
+/**
+ * \brief puts string into the template and computes which escape methon we should use
+ * \param Source the string we should put into the template
+ * \param FormatTypeIndex where should we look for escape types if?
+ */
+void StrBufAppendTemplate(StrBuf *Target, 
+                         int nArgs, 
+                         WCTemplateToken *Tokens,
+                         void *Context, int ContextType,
+                         const StrBuf *Source, int FormatTypeIndex)
+{
+        struct wcsession *WCC;
+       StrBuf *Buf;
+       char EscapeAs = ' ';
+
+       if ((FormatTypeIndex < Tokens->nParameters) &&
+           (Tokens->Params[FormatTypeIndex]->Type == TYPE_STR) &&
+           (Tokens->Params[FormatTypeIndex]->len == 1)) {
+               EscapeAs = *Tokens->Params[FormatTypeIndex]->Start;
+       }
+
+       switch(EscapeAs)
+       {
+       case 'H':
+               WCC = WC;
+               Buf = NewStrBufPlain(NULL, StrLength(Buf));
+               StrBuf_RFC822_to_Utf8(Buf, 
+                                     Source, 
+                                     (WCC!=NULL)? WCC->DefaultCharset : NULL, 
+                                     NULL);
+               StrEscAppend(Target, Buf, NULL, 0, 0);
+               FreeStrBuf(&Buf);
+               break;
+       case 'X':
+               StrEscAppend(Target, Source, NULL, 0, 0);
+               break;
+       default:
+               StrBufAppendBuf(Target, Source, 0);
+       }
+}
+
+
+void GetTemplateTokenString(WCTemplateToken *Tokens,
+                           int N, 
+                           const char **Value, 
+                           long *len)
+{
+       StrBuf *Buf;
+
+       if (Tokens->nParameters < N) {
+               *Value = "";
+               *len = 0;
+               return;
+       }
+
+       switch (Tokens->Params[N]->Type) {
+
+       case TYPE_STR:
+               *Value = Tokens->Params[N]->Start;
+               *len = Tokens->Params[N]->len;
+               break;
+       case TYPE_BSTR:
+               Buf = (StrBuf*) SBstr(Tokens->Params[N]->Start, 
+                                     Tokens->Params[N]->len);
+               *Value = ChrPtr(Buf);
+               *len = StrLength(Buf);
+               break;
+       case TYPE_PREFSTR:
+               get_PREFERENCE(
+                       Tokens->Params[N]->Start, 
+                       Tokens->Params[N]->len, 
+                       &Buf);
+               *Value = ChrPtr(Buf);
+               *len = StrLength(Buf);
+               break;
+       case TYPE_LONG:
+       case TYPE_PREFINT:
+               break; ///todo: string to text?
+       case TYPE_GETTEXT:
+               *Value = _(Tokens->Params[N]->Start);
+               *len = strlen(*Value);
+               break;
+       default:
+               break;
+//todo log error
+       }
+}
+
+
+
 /**
  * \brief Print the value of a variable
  * \param keyname get a key to print
@@ -505,6 +596,15 @@ void print_value_of(StrBuf *Target, WCTemplateToken *Token, void *Context, int C
                        StrBufAppendPrintf(Target, "<pre>WARNING: \ninvalid value in SV-Hash at %s!\n</pre>", Token->pName);
                }
        }
+       else {
+               lprintf(1, "didn't find Handler [%s] (in '%s' line %ld); "
+                       " [%s]\n", 
+                       Token->pName,
+                       ChrPtr(Token->FileName),
+                       Token->Line,
+                       ChrPtr(Token->FlatToken));
+               wc_backtrace();
+       }
 }
 
 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup)
@@ -606,11 +706,33 @@ TemplateParam *GetNextParameter(StrBuf *Buf, const char **pCh, const char *pe, W
        TemplateParam *Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
        char quote = '\0';
        
+
+       Parm->Type = TYPE_STR;
+
        /* Skip leading whitespaces */
        while ((*pch == ' ' )||
               (*pch == '\t')||
               (*pch == '\r')||
               (*pch == '\n')) pch ++;
+
+       if (*pch == ':') {
+               Parm->Type = TYPE_PREFSTR;
+               pch ++;
+       }
+       else if (*pch == ';') {
+               Parm->Type = TYPE_PREFINT;
+               pch ++;
+       }
+       else if (*pch == '_') {
+               Parm->Type = TYPE_GETTEXT;
+               pch ++;
+       }
+       else if (*pch == 'B') {
+               Parm->Type = TYPE_BSTR;
+               pch ++;
+       }
+
+
        if (*pch == '"')
                quote = '"';
        else if (*pch == '\'')
@@ -618,7 +740,6 @@ TemplateParam *GetNextParameter(StrBuf *Buf, const char **pCh, const char *pe, W
        if (quote != '\0') {
                pch ++;
                pchs = pch;
-               Parm->Type = TYPE_STR;
                while (pch <= pe &&
                       ((*pch != quote) ||
                        ( (pch > pchs) && (*(pch - 1) == '\\'))
@@ -692,10 +813,12 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
                                       long Line,
                                       WCTemplate *pTmpl)
 {
+       void *vVar;
        const char *pch;
        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;
@@ -718,6 +841,13 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
                        StrBufPeek(Buf, pch, -1, '\0');
                        NewToken->NameEnd = pch - NewToken->pName;
                        pch ++;
+                       if (*(pTmplEnd - 1) != ')') {
+                               lprintf(1, "Warning, Non welformed Token; missing right parenthesis (in '%s' line %ld); "
+                                       "[%s]\n", 
+                                       ChrPtr(pTmpl->FileName),
+                                       NewToken->Line,
+                                       ChrPtr(NewToken->FlatToken));
+                       }
                        while (pch < pTmplEnd - 1) {
                                Param = GetNextParameter(Buf, &pch, pTmplEnd - 1, NewToken, pTmpl);
                                if (Param != NULL) {
@@ -741,29 +871,24 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
                           (NewToken->HaveParameters == 1))
                           
                        {
-                               if ((NewToken->nParameters == 1) &&
-                                   (*(NewToken->pName) == '_'))
+                               if (*(NewToken->pName) == '_')
                                        NewToken->Flags = SV_GETTEXT;
-                               else if ((NewToken->nParameters == 1) &&
-                                        (*(NewToken->pName) == '='))
+                               else if (*(NewToken->pName) == '=')
                                        NewToken->Flags = SV_SUBTEMPL;
-                               else if ((NewToken->nParameters >= 2) &&
-                                        (*(NewToken->pName) == '%'))
+                               else if (*(NewToken->pName) == '%')
                                        NewToken->Flags = SV_CUST_STR_CONDITIONAL;
-                               else if ((NewToken->nParameters >= 2) &&
-                                        (*(NewToken->pName) == '?'))
+                               else if (*(NewToken->pName) == '?')
                                        NewToken->Flags = SV_CONDITIONAL;
-                               else if ((NewToken->nParameters >=2) &&
-                                        (*(NewToken->pName) == '!'))
+                               else if (*(NewToken->pName) == '!')
                                        NewToken->Flags = SV_NEG_CONDITIONAL;
                        }
                }
                else pch ++;            
        }
        
-       if (NewToken->Flags == 0) {
+       switch (NewToken->Flags) {
+       case 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;
@@ -781,9 +906,73 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
                                NewToken->PreEval = Handler;
                                NewToken->Flags = SV_PREEVALUATED;              
                        }
-               }               
+               }
+               break;
+       case SV_GETTEXT:
+               if (NewToken->nParameters !=1) {
+                       lprintf(1, "Gettext (in '%s' line %ld); "
+                               "requires exactly 1 parameter, you gave %ld params [%s]\n", 
+                               ChrPtr(pTmpl->FileName),
+                               NewToken->Line,
+                               NewToken->nParameters, 
+                               ChrPtr(NewToken->FlatToken));
+                       NewToken->Flags = 0;
+                       break;
+               }
+               break;
+       case SV_SUBTEMPL:
+               if (NewToken->nParameters != 1) {
+                       lprintf(1, "Subtemplates (in '%s' line %ld); "
+                               "require exactly 1 parameter, you gave %ld params [%s]\n", 
+                               ChrPtr(pTmpl->FileName),
+                               NewToken->Line,
+                               NewToken->nParameters, 
+                               ChrPtr(NewToken->FlatToken));
+                       break;
+               }
+               break;
+       case SV_CUST_STR_CONDITIONAL:
+       case SV_CONDITIONAL:
+       case SV_NEG_CONDITIONAL:
+               if (NewToken->nParameters <2) {
+                       lprintf(1, "Conditional (in '%s' line %ld); "
+                               "require at least 2 parameters, you gave %ld params [%s]\n", 
+                               ChrPtr(pTmpl->FileName),
+                               NewToken->Line,
+                               NewToken->nParameters, 
+                               ChrPtr(NewToken->FlatToken));
+                       NewToken->Flags = 0;
+                       break;
+               }
+               if (!GetHash(Conditionals, 
+                            NewToken->Params[0]->Start,
+                            NewToken->Params[0]->len,
+                            &vVar) || 
+                   (vVar == NULL)) {
+                       if ((NewToken->Params[0]->len == 1) &&
+                           (NewToken->Params[0]->Start[0] == 'X'))
+                               break;
+                       lprintf(1, "Conditional [%s] (in '%s' line %ld); Not found![%s]\n", 
+                               NewToken->Params[0]->Start,
+                               ChrPtr(pTmpl->FileName),
+                               NewToken->Line,
+                               ChrPtr(NewToken->FlatToken));
+/*
+                       NewToken->Error = NewStrBuf();
+                       StrBufAppendPrintf(
+                               NewToken->Error, 
+                               "<pre>\nConditional [%s] (in '%s' line %ld); Not found!\n[%s]\n</pre>\n", 
+                               NewToken->Params[0]->Start,
+                               ChrPtr(pTmpl->FileName),
+                               NewToken->Line,
+                               ChrPtr(NewToken->FlatToken));
+*/
+               }
+               else {
+                       NewToken->PreEval = vVar;
+               }
+               break;
        }
-
        return NewToken;
 }
 
@@ -791,33 +980,21 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
 
 int EvaluateConditional(StrBuf *Target, WCTemplateToken *Token, WCTemplate *pTmpl, void *Context, int Neg, int state, int ContextType)
 {
-       void *vConditional = NULL;
        ConditionalStruct *Cond;
 
        if ((Token->Params[0]->len == 1) &&
            (Token->Params[0]->Start[0] == 'X'))
                return (state != 0)?Token->Params[1]->lvalue:0;
-
-       if (!GetHash(Contitionals, 
-                Token->Params[0]->Start,
-                Token->Params[0]->len,
-                &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));
-               StrBufAppendPrintf(
-                       Target, 
-                       "<pre>\nConditional [%s] (in '%s' line %ld); Not found!\n[%s]\n</pre>\n", 
+           
+       Cond = (ConditionalStruct *) Token->PreEval;
+       if (Cond == NULL) {
+               lprintf(1, "Conditional [%s] (in '%s' line %ld); unknown![%s]\n", 
                        Token->Params[0]->Start,
                        ChrPtr(pTmpl->FileName),
                        Token->Line,
                        ChrPtr(Token->FlatToken));
+               return 1;
        }
-           
-       Cond = (ConditionalStruct *) vConditional;
 
        if (Token->nParameters < Cond->nParams) {
                lprintf(1, "Conditional [%s] (in '%s' line %ld); needs %ld Params![%s]\n", 
@@ -841,6 +1018,15 @@ int EvaluateConditional(StrBuf *Target, WCTemplateToken *Token, WCTemplate *pTmp
        return 0;
 }
 
+/**
+ * \brief executes one token
+ * \param Target buffer to append to
+ * \param Token da to  process.
+ * \param Template we're iterating
+ * \param Context Contextpoointer to pass in
+ * \param state are we in conditional state?
+ * \param ContextType what type of information does context giv us?
+ */
 int EvaluateToken(StrBuf *Target, WCTemplateToken *Token, WCTemplate *pTmpl, void *Context, int state, int ContextType)
 {
        HashHandler *Handler;
@@ -871,6 +1057,20 @@ int EvaluateToken(StrBuf *Target, WCTemplateToken *Token, WCTemplate *pTmpl, voi
                                                     Token->Params[4]->len,
                                                     0);
                }
+               else  {
+                       lprintf(1, "Conditional [%s] (in '%s' line %ld); needs at least 6 Params![%s]\n", 
+                               Token->Params[0]->Start,
+                               ChrPtr(pTmpl->FileName),
+                               Token->Line,
+                               ChrPtr(Token->FlatToken));
+                       StrBufAppendPrintf(
+                               Target, 
+                               "<pre>\nConditional [%s] (in '%s' line %ld); needs 6 Params!\n[%s]\n</pre>\n", 
+                               Token->Params[0]->Start,
+                               ChrPtr(pTmpl->FileName),
+                               Token->Line,
+                               ChrPtr(Token->FlatToken));
+               }
                break;
        case SV_SUBTEMPL:
                if (Token->nParameters == 1)
@@ -979,6 +1179,7 @@ void ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, void *Context, int Contex
                if (LoadTemplates > 1)
                        lprintf(1, "DBG: ----- loading:  [%s] ------ \n", 
                                ChrPtr(Tmpl->FileName));
+
                pTmpl = load_template(Tmpl->FileName, NULL, NULL);
                if(pTmpl == NULL) {
                        StrBufAppendPrintf(
@@ -1150,7 +1351,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, int ContextType) 
+void DoTemplate(const char *templatename, long len, StrBuf *Target, void *Context, int ContextType) 
 {
        HashList *Static;
        HashList *StaticLocal;
@@ -1235,8 +1436,8 @@ int LoadTemplateDir(const char *DirName, HashList *wireless, HashList *big)
                        *MinorPtr = '\0';
                StrBufPlain(Tag, filedir_entry->d_name, MinorPtr - filedir_entry->d_name);
 
-
-               lprintf(1, "%s %d %s\n",ChrPtr(FileName), IsMobile, ChrPtr(Tag));
+               if (LoadTemplates > 1)
+                       lprintf(1, "%s %d %s\n",ChrPtr(FileName), IsMobile, ChrPtr(Tag));
                if (LoadTemplates == 0)
                        load_template(FileName, Tag, (IsMobile)?wireless:big);
                else
@@ -1259,58 +1460,13 @@ void InitTemplateCache(void)
                        LocalTemplateCache);
 }
 
-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, int ContextType)
-{
-       StrEscAppend(Target, NULL, serv_info.serv_nodename, 0, 0);
-}
-
-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, int ContextType)
-{
-       StrEscAppend(Target, NULL, serv_info.serv_fqdn, 0, 0);
-}
-
-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, 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, int ContextType)
-{
-       StrEscAppend(Target, NULL, serv_info.serv_bbs_city, 0, 0);
-}
-
-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, int ContextType)
-{
-       StrEscAppend(Target, NULL, WC->wc_roomname, 0, 0);
-}
 
 
 typedef struct _HashIterator {
        HashList *StaticList;
        int AdditionalParams;
        int ContextType;
+       int XPectContextType;
        RetrieveHashlistFunc GetHash;
        HashDestructorFunc Destructor;
        SubTemplFunc DoSubTemplate;
@@ -1332,16 +1488,18 @@ void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, vo
                     Tokens->Params[0]->Start,
                     Tokens->Params[0]->len,
                     &vIt)) {
-               lprintf(1, "unknown Iterator [%s] (in line %ld); "
+               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 line %ld); \n"
+                       "<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;
@@ -1350,25 +1508,54 @@ void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, vo
        It = (HashIterator*) vIt;
 
        if (Tokens->nParameters < It->AdditionalParams + 2) {
-               lprintf(1, "Iterator [%s] (in line %ld); "
+               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 line %ld);\n"
+                       "<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(Tokens);
+               List = It->GetHash(Target, nArgs, Tokens, Context, ContextType);
        else
                List = It->StaticList;
 
@@ -1379,10 +1566,11 @@ void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, vo
                         (oddeven) ? "odd" : "even");
                svprintf(HKEY("ITERATE:KEY"), WCS_STRING, "%s", Key);
 
-               It->DoSubTemplate(SubBuf, vContext, Tokens);
+               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);
@@ -1441,7 +1629,8 @@ void RegisterITERATOR(const char *Name, long len,
                      RetrieveHashlistFunc GetHash, 
                      SubTemplFunc DoSubTempl,
                      HashDestructorFunc Destructor,
-                     int ContextType)
+                     int ContextType, 
+                     int XPectContextType)
 {
        HashIterator *It = (HashIterator*)malloc(sizeof(HashIterator));
        It->StaticList = StaticList;
@@ -1450,6 +1639,7 @@ void RegisterITERATOR(const char *Name, long len,
        It->DoSubTemplate = DoSubTempl;
        It->Destructor = Destructor;
        It->ContextType = ContextType;
+       It->XPectContextType = XPectContextType;
        Put(Iterators, Name, len, It, NULL);
 }
 
@@ -1459,33 +1649,62 @@ void RegisterConditional(const char *Name, long len,
                         int ContextRequired)
 {
        ConditionalStruct *Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
+       Cond->PlainName = Name;
        Cond->nParams = nParams;
        Cond->CondF = CondF;
        Cond->ContextRequired = ContextRequired;
-       Put(Contitionals, Name, len, Cond, NULL);
+       Put(Conditionals, Name, len, Cond, NULL);
 }
 
+
+void tmplput_ContextString(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
+{
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, (StrBuf*)Context, 0);
+}
+int ConditionalContextStr(WCTemplateToken *Tokens, void *Context, int ContextType)
+{
+       StrBuf *TokenText = (StrBuf*) Context;
+       const char *CompareToken;
+       long len;
+
+       GetTemplateTokenString(Tokens, 2, &CompareToken, &len);
+       return strcmp(ChrPtr(TokenText), CompareToken) == 0;
+}
+
+
 void tmpl_do_boxed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
+       StrBuf *Headline;
        if (nArgs == 2) {
-               StrBuf *Headline = NewStrBuf();
-               DoTemplate(Tokens->Params[1]->Start, 
-                          Tokens->Params[1]->len,
-                          Context, 
-                          Headline, 
-                          ContextType);
-               SVPutBuf("BOXTITLE", Headline, 0);
+               if (Tokens->Params[1]->Type == TYPE_STR) {
+                       Headline = NewStrBuf();
+                       DoTemplate(Tokens->Params[1]->Start, 
+                                  Tokens->Params[1]->len,
+                                  Headline, 
+                                  Context, 
+                                  ContextType);
+               }
+               else {
+                       const char *Ch;
+                       long len;
+                       GetTemplateTokenString(Tokens, 
+                                              1,
+                                              &Ch,
+                                              &len);
+                       Headline = NewStrBufPlain(Ch, len);
+               }
        }
        
-       DoTemplate(HKEY("beginbox"), Context, Target, ContextType);
+       DoTemplate(HKEY("beginbox"), Target, Headline, CTX_STRBUF);
        DoTemplate(Tokens->Params[0]->Start, 
                   Tokens->Params[0]->len,
-                  Context, 
                   Target, 
+                  Context, 
                   ContextType);
-       DoTemplate(HKEY("endbox"), Context, Target, ContextType);
+       DoTemplate(HKEY("endbox"), Target, Context, ContextType);
 }
 
+
 void tmpl_do_tabbed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        StrBuf **TabNames;
@@ -1499,8 +1718,8 @@ 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,
-                                  Context,
                                   TabNames[i],
+                                  Context,
                                   ContextType);
                }
                else { 
@@ -1515,8 +1734,8 @@ 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,
-                          Context, 
                           Target,
+                          Context, 
                           ContextType);
                StrEndTab(Target, i, nTabs);
        }
@@ -1526,20 +1745,12 @@ void
 InitModule_SUBST
 (void)
 {
-       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("CONTEXTSTR", 0, 1, tmplput_ContextString, CTX_STRBUF);
        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);
+       RegisterConditional(HKEY("COND:CONTEXTSTR"), 3, ConditionalContextStr, CTX_STRBUF);
 }
 
 /*@}*/