+ new way to retrieve integers from templates (bstr, pref...)
authorWilfried Göesgens <willi@citadel.org>
Sun, 1 Feb 2009 21:43:04 +0000 (21:43 +0000)
committerWilfried Göesgens <willi@citadel.org>
Sun, 1 Feb 2009 21:43:04 +0000 (21:43 +0000)
+ add logging to GetTemplateTokenNumber and GetTemplateTokenString
+ iterate now has three optional parameters: StartAt, StepWidth, StopAt; defaults: 0, 0, -1
* move inetconf parameters so they don't interfere with the new iterator params

webcit/inetconf.c
webcit/static/t/aide_inet_aliases.html
webcit/static/t/aide_inet_clamav.html
webcit/static/t/aide_inet_dirnames.html
webcit/static/t/aide_inet_masqdomains.html
webcit/static/t/aide_inet_rbldns.html
webcit/static/t/aide_inet_smarthosts.html
webcit/static/t/aide_inet_spamass.html
webcit/subst.c
webcit/subst.h

index 5e0bd5b11403f45604577734de56451748c308ce..1d3c68beb3918c3c75963664a170cf5f349321e7 100644 (file)
@@ -197,8 +197,8 @@ HashList *GetInetConfHash(StrBuf *Target, WCTemplputParams *TP)
 
        if (WCC->InetCfg == NULL)
                load_inetconf();
-       GetHash(WCC->InetCfg, TKEY(2), &vHash);
-       svprintf(HKEY("SERVCFG:INET:TYPE"), WCS_STRING, TP->Tokens->Params[2]->Start);
+       GetHash(WCC->InetCfg, TKEY(5), &vHash);
+       svprintf(HKEY("SERVCFG:INET:TYPE"), WCS_STRING, TP->Tokens->Params[5]->Start);
        return vHash;
 }
 
index 3028cca30bef3f244c5aaef08079a02f97ded5e6..76a2be25c6dcc3a308f59ce696e40d010a76e87b 100644 (file)
@@ -2,7 +2,7 @@
 <?_("(domains for which this host receives mail)")>
 </span><br />
 <table border=0 cellspacing="2" cellpadding="2" width=94% class="altern" >
-<?ITERATE("SERVCFG:INET", "section_aide_inetconf_entry", "localhost")>
+<?ITERATE("SERVCFG:INET", "section_aide_inetconf_entry", 0, 0, -1, "localhost")>
 <form method="post" action="save_inetconf">
 <input type="hidden" name="nonce" value='<?NONCE>'>
 <tr><td>
index dceee25b97884e85d8e6e9daa3cb5639b0f15c47..2dc2aceed260112e770e3e90f3ba60ac4354c97f 100644 (file)
@@ -2,7 +2,7 @@
 <?_("(hosts running the ClamAV clamd service)")>
 </span><br />
 <table border=0 cellspacing="2px" cellpadding="2px" width=94%% class="altern" >
-<?ITERATE("SERVCFG:INET", "section_aide_inetconf_entry", "clamav")>
+<?ITERATE("SERVCFG:INET", "section_aide_inetconf_entry", 0, 0, -1, "clamav")>
 <form method="post" action="save_inetconf">
 <input type="hidden" name="nonce" value='<?NONCE>'>
 <tr><td>
index 28470c9c8ac79c87a7e2ba15de288f8c674babf4..04d28b917f46ab8d19fa863573127cc00ad4991d 100644 (file)
@@ -2,7 +2,7 @@
 <?_("(domains mapped with the Global Address Book)")>
 </span><br />
 <table border=0 cellspacing="2" cellpadding="2" width=94% class="altern" >
-<?ITERATE("SERVCFG:INET", "section_aide_inetconf_entry", "directory")>
+<?ITERATE("SERVCFG:INET", "section_aide_inetconf_entry", 0, 0, -1, "directory")>
 <form method="post" action="save_inetconf">
 <input type="hidden" name="nonce" value='<?NONCE>'>
 <tr><td>
index b98f697d32a35bb043bad6b1f8d707a344ac52de..08390f4b26d1b4be3b75717494d14070bd69cbc3 100644 (file)
@@ -2,7 +2,7 @@
 <?_("(Domains as which users are allowed to masquerade)")>
 </span><br />
 <table border=0 cellspacing="2" cellpadding="2" width=94% class="altern" >
-<?ITERATE("SERVCFG:INET", "section_aide_inetconf_entry", "masqdomain")>
+<?ITERATE("SERVCFG:INET", "section_aide_inetconf_entry", 0, 0, -1, "masqdomain")>
 <form method="post" action="save_inetconf">
 <input type="hidden" name="nonce" value='<?NONCE>'>
 <tr><td>
index 447dccdd9b2e3db8b487037fa79ebcb8c7bcfc2c..b1f5b5fc3e920d18bb1808f60fe74fa70f1d26fb 100644 (file)
@@ -2,7 +2,7 @@
 <?_("(hosts running a Realtime Blackhole List)")>
 </span><br />
 <table border=0 cellspacing="2" cellpadding="2" width=94% class="altern" >
-<?ITERATE("SERVCFG:INET", "section_aide_inetconf_entry", "rbl")>
+<?ITERATE("SERVCFG:INET", "section_aide_inetconf_entry", 0, 0, -1, "rbl")>
 <form method="post" action="save_inetconf">
 <input type="hidden" name="nonce" value='<?NONCE>'>
 <tr><td>
index 8980675307d2dd2f4ae2e2c968c05a496d43ce6e..786d334591ed5ee34854cd23f2d1ffe86f0c4919 100644 (file)
@@ -2,7 +2,7 @@
 <?_("(if present, forward all outbound mail to one of these hosts)")>
 </span><br />
 <table border=0 cellspacing="2" cellpadding="2" width=94% class="altern" >
-<?ITERATE("SERVCFG:INET", "section_aide_inetconf_entry", "smarthost")>
+<?ITERATE("SERVCFG:INET", "section_aide_inetconf_entry", 0, 0, -1, "smarthost")>
 <form method="post" action="save_inetconf">
 <input type="hidden" name="nonce" value='<?NONCE>'>
 <tr><td>
index e4dc5bbab6847748c6b1d696a8564ba2da0c54b7..eecf250813dab24b5a8fd08f013a3f1fc0499360 100644 (file)
@@ -2,7 +2,7 @@
 <?_("(hosts running the SpamAssassin service)")>
 </span><br />
 <table border=0 cellspacing="2" cellpadding="2" width=94% class="altern" >
-<?ITERATE("SERVCFG:INET", "section_aide_inetconf_entry", "spamassassin")>
+<?ITERATE("SERVCFG:INET", "section_aide_inetconf_entry", 0, 0, -1, "spamassassin")>
 <form method="post" action="save_inetconf">
 <input type="hidden" name="nonce" value='<?NONCE>'>
 <tr><td>
index ff4afb52176a523049e4127123c8a95b115615cf..394af9ab8dc56f59244ad50ca4c188ac01a6bd9b 100644 (file)
@@ -690,7 +690,8 @@ void pvo_do_cmd(StrBuf *Target, StrBuf *servcmd) {
        }
 }
 
-void GetTemplateTokenString(WCTemplputParams *TP,
+void GetTemplateTokenString(StrBuf *Target, 
+                           WCTemplputParams *TP,
                            int N,
                            const char **Value, 
                            long *len)
@@ -699,8 +700,9 @@ void GetTemplateTokenString(WCTemplputParams *TP,
        WCTemplputParams SubTP;
 
        if (TP->Tokens->nParameters < N) {
-               lprintf(1, "invalid token. this shouldn't have come till here.\n");
-               wc_backtrace(); 
+               LogTemplateError(Target, 
+                                "TokenParameter", N, TP, 
+                                "invalid token %d. this shouldn't have come till here.\n", N);
                *Value = "";
                *len = 0;
                return;
@@ -723,8 +725,15 @@ void GetTemplateTokenString(WCTemplputParams *TP,
                *len = StrLength(Buf);
                break;
        case TYPE_LONG:
+               LogTemplateError(Target, 
+                                "TokenParameter", N, TP, 
+                                "Requesting parameter %d; of type LONG, want string.", N);
+               break;
        case TYPE_PREFINT:
-               break; /* todo: string to text? */
+               LogTemplateError(Target, 
+                                "TokenParameter", N, TP, 
+                                "Requesting parameter %d; of type PREFINT, want string.", N);
+               break;
        case TYPE_GETTEXT:
                *Value = _(TP->Tokens->Params[N]->Start);
                *len = strlen(*Value);
@@ -742,8 +751,60 @@ void GetTemplateTokenString(WCTemplputParams *TP,
                break;
 
        default:
+               LogTemplateError(Target, 
+                                "TokenParameter", N, TP, 
+                                "unknown param type %d; [%d]", N, TP->Tokens->Params[N]->Type);
                break;
-/*/todo log error */
+       }
+}
+
+long GetTemplateTokenNumber(StrBuf *Target, WCTemplputParams *TP, int N, long dflt)
+{
+       long Ret;
+       if (TP->Tokens->nParameters < N) {
+               LogTemplateError(Target, 
+                                "TokenParameter", N, TP, 
+                                "invalid token %d. this shouldn't have come till here.\n", N);
+               wc_backtrace(); 
+               return 0;
+       }
+
+       switch (TP->Tokens->Params[N]->Type) {
+
+       case TYPE_STR:
+               return atol(TP->Tokens->Params[N]->Start);
+               break;
+       case TYPE_BSTR:
+               return  LBstr(TKEY(N));
+               break;
+       case TYPE_PREFSTR:
+               LogTemplateError(Target, 
+                                "TokenParameter", N, TP, 
+                                "requesting a prefstring in param %d want a number", N);
+               if (get_PREF_LONG(TKEY(N), &Ret, dflt))
+                       return Ret;
+               return 0;               
+       case TYPE_LONG:
+               return TP->Tokens->Params[N]->lvalue;
+       case TYPE_PREFINT:
+               if (get_PREF_LONG(TKEY(N), &Ret, dflt))
+                       return Ret;
+               return 0;               
+       case TYPE_GETTEXT:
+               LogTemplateError(Target, 
+                                "TokenParameter", N, TP, 
+                                "requesting a I18N string in param %d; want a number", N);
+               return 0;
+       case TYPE_SUBTEMPLATE:
+               LogTemplateError(Target, 
+                                "TokenParameter", N, TP, 
+                                "requesting a subtemplate in param %d; not supported for numbers", N);
+               return 0;
+       default:
+               LogTemplateError(Target, 
+                                "TokenParameter", N, TP, 
+                                "unknown param type %d; [%d]", N, TP->Tokens->Params[N]->Type);
+               return 0;
        }
 }
 
@@ -1461,14 +1522,14 @@ int EvaluateToken(StrBuf *Target, int state, WCTemplputParams *TP)
        case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
                if (TP->Tokens->nParameters >= 6) {
                        if (EvaluateConditional(Target, 0, state, TP)) {
-                               GetTemplateTokenString(TP, 5, &AppendMe, &AppendMeLen);
+                               GetTemplateTokenString(Target, TP, 5, &AppendMe, &AppendMeLen);
                                StrBufAppendBufPlain(Target, 
                                                     AppendMe, 
                                                     AppendMeLen,
                                                     0);
                        }
                        else{
-                               GetTemplateTokenString(TP, 4, &AppendMe, &AppendMeLen);
+                               GetTemplateTokenString(Target, TP, 4, &AppendMe, &AppendMeLen);
                                StrBufAppendBufPlain(Target, 
                                                     AppendMe, 
                                                     AppendMeLen,
@@ -1701,6 +1762,10 @@ void tmpl_iterate_subtmpl(StrBuf *Target, WCTemplputParams *TP)
        WCTemplputParams SubTP;
        IterateStruct Status;
 
+       long StartAt = 0;
+       long StepWidth = 0;
+       long StopAt = -1;
+
        memset(&Status, 0, sizeof(IterateStruct));
        memcpy (&SubTP, &TP, sizeof(WCTemplputParams));
        
@@ -1760,21 +1825,37 @@ void tmpl_iterate_subtmpl(StrBuf *Target, WCTemplputParams *TP)
        SubTP.Filter.ContextType = It->ContextType;
        SubTP.Filter.ControlContextType = CTX_ITERATE;
        SubTP.ControlContext = &Status;
-       it = GetNewHashPos(List, 0);
+       
+       if (HAVE_PARAM(3)) {
+               StartAt = GetTemplateTokenNumber(Target, TP, 3, 0);
+       }
+       if (HAVE_PARAM(4)) {
+               StepWidth = GetTemplateTokenNumber(Target, TP, 4, 0);
+       }
+       if (HAVE_PARAM(5)) {
+               StopAt = GetTemplateTokenNumber(Target, TP, 5, -1);
+       }
+       if (StopAt < 0) {
+               StopAt = GetCount(List)  + 1;
+       }
+       it = GetNewHashPos(List, StepWidth);
        while (GetNextHashPos(List, it, &Status.KeyLen, &Status.Key, &vContext)) {
-               if (DetectGroupChange && Status.n > 0) {
-                       Status.GroupChange = (SortBy->GroupChange(vContext, vLastContext))? 1:0;
-               }
-               Status.LastN = ++Status.LastN == nMembersUsed;
-               SubTP.Context = vContext;
-               if (It->DoSubTemplate != NULL)
-                       It->DoSubTemplate(SubBuf, &SubTP);
-               DoTemplate(TKEY(1), SubBuf, &SubTP);
+               if ((Status.n > StartAt) && (Status.n < StopAt)) {
+                       if (DetectGroupChange && Status.n > 0) {
+                               Status.GroupChange = (SortBy->GroupChange(vContext, vLastContext))? 1:0;
+                       }
+                       Status.LastN = (Status.n + 1) == nMembersUsed;
+                       SubTP.Context = vContext;
+                       if (It->DoSubTemplate != NULL)
+                               It->DoSubTemplate(SubBuf, &SubTP);
+                       DoTemplate(TKEY(1), SubBuf, &SubTP);
                        
-               StrBufAppendBuf(Target, SubBuf, 0);
-               FlushStrBuf(SubBuf);
-               Status.oddeven = ! Status.oddeven;
-               vLastContext = vContext;
+                       StrBufAppendBuf(Target, SubBuf, 0);
+                       FlushStrBuf(SubBuf);
+                       Status.oddeven = ! Status.oddeven;
+                       vLastContext = vContext;
+               }
+               Status.n++;
        }
        FreeStrBuf(&SubBuf);
        DeleteHashPos(&it);
@@ -1924,7 +2005,7 @@ int ConditionalContextStr(StrBuf *Target, WCTemplputParams *TP)
        const char *CompareToken;
        long len;
 
-       GetTemplateTokenString(TP, 2, &CompareToken, &len);
+       GetTemplateTokenString(Target, TP, 2, &CompareToken, &len);
        return strcmp(ChrPtr(TokenText), CompareToken) == 0;
 }
 
@@ -1945,7 +2026,8 @@ void tmpl_do_boxed(StrBuf *Target, WCTemplputParams *TP)
                else {
                        const char *Ch;
                        long len;
-                       GetTemplateTokenString(TP, 
+                       GetTemplateTokenString(Target, 
+                                              TP, 
                                               1,
                                               &Ch,
                                               &len);
@@ -1982,7 +2064,8 @@ void tmpl_do_tabbed(StrBuf *Target, WCTemplputParams *TP)
                else if (TP->Tokens->Params[i * 2]->Type == TYPE_GETTEXT) {
                        const char *Ch;
                        long len;
-                       GetTemplateTokenString(TP, 
+                       GetTemplateTokenString(Target, 
+                                              TP, 
                                               i * 2,
                                               &Ch,
                                               &len);
index 4f53cc3726cdf5accc4e689a0ee95fbc2bfdda94..cc620e14fb8e35d54501d0a7622f8d9961574067 100644 (file)
@@ -182,7 +182,7 @@ typedef void (*HashDestructorFunc) (HashList **KillMe);
 
 extern WCTemplputParams NoCtx;
 
-
+#define HAVE_PARAM(a) (TP->Tokens->nParameters > a)
 
 
 #define ERR_NAME 0
@@ -214,11 +214,31 @@ void LogTemplateError (StrBuf *Target,
  * \param Value reference to the string of the token; don't free me.
  * \param len the length of Value
  */
-void GetTemplateTokenString(WCTemplputParams *TP,
+void GetTemplateTokenString(StrBuf *Target, 
+                           WCTemplputParams *TP,
                            int N,
                            const char **Value, 
                            long *len);
 
+
+
+/**
+ * \Brief get the actual integer value of a token parameter
+ * in your tmplputs or conditionals use this function to access parameters that can also be 
+ * retrieved from dynamic facilities:
+ *  _ -> Gettext; retrieve this token from the i18n facilities
+ *  : -> lookup a setting of that name
+ *  B -> bstr; an URL-Parameter
+ *  = -> subtemplate; parse a template by this name, and treat its content as this tokens value 
+ * 
+ * \param N which token do you want to lookup?
+ * \param dflt default value to be retrieved if not found in preferences
+ * \returns the long value
+ */
+long GetTemplateTokenNumber(StrBuf *Target, 
+                           WCTemplputParams *TP, 
+                           int N, long dflt);
+
 /**
  * \Brief put a token value into the template
  * use this function to append your strings into a Template.