final touches on dkim test harness
[citadel.git] / webcit / subst.h
index 644b55950ad2a815d29abedd5af43b241b9ee4c4..063f46d211e0883fe2be28a773943f4fd83b661a 100644 (file)
@@ -1,3 +1,16 @@
+/*
+ * Copyright (c) 1996-2013 by the citadel.org team
+ *
+ * This program is open source software.  You can redistribute it and/or
+ * modify it under the terms of the GNU General Public License, version 3.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * subst template processing functions
+ */
 
 extern HashList *Conditionals;
 extern HashList *GlobalNS;
@@ -11,11 +24,12 @@ extern HashList *LocalTemplateCache;
 #define TYPE_STR   1
 #define TYPE_LONG  2
 #define TYPE_PREFSTR 3
-#define TYPE_PREFINT 4
-#define TYPE_GETTEXT 5
-#define TYPE_BSTR 6
-#define TYPE_SUBTEMPLATE 7
-#define TYPE_INTDEFINE 8
+#define TYPE_ROOMPREFSTR 4
+#define TYPE_PREFINT 5
+#define TYPE_GETTEXT 6
+#define TYPE_BSTR 7
+#define TYPE_SUBTEMPLATE 8
+#define TYPE_INTDEFINE 9
 #define MAXPARAM  25
 
 #define IS_NUMBER(a) ((a == TYPE_LONG) || (a == TYPE_PREFINT) || (a == TYPE_INTDEFINE))
@@ -32,46 +46,31 @@ enum {
        WCS_LONG          /* its an integer */
 };
 
-#define CTX TP->Context
-#define CCTX TP->ControlContext
-
 #define CTX_NONE 0
-#define CTX_SITECFG 1
-#define CTX_SESSION 2
-#define CTX_INETCFG 3
-#define CTX_VNOTE 4
-#define CTX_WHO 5
-#define CTX_PREF 6
-#define CTX_NODECONF 7
-#define CTX_USERLIST 8
-#define CTX_MAILSUM 9
-#define CTX_MIME_ATACH 10
-#define CTX_FILELIST 11
-#define CTX_STRBUF 12
-#define CTX_STRBUFARR 13
-#define CTX_LONGVECTOR 14
-#define CTX_ROOMS 15
-#define CTX_FLOORS 16
-#define CTX_ITERATE 17
-#define CTX_ICAL 18
-#define CTX_DAVNS 19
-#define CTX_TAB 20
-#define CTX_VCARD 21
-#define CTX_SIEVELIST 22
-#define CTX_SIEVESCRIPT 23
-
-#define CTX_UNKNOWN 24
 
+typedef int CtxType;
+typedef struct __CtxTypeStruct {
+       CtxType Type;
+       StrBuf *Name;
+} CtxTypeStruct;
+
+CtxTypeStruct *GetContextType(CtxType Type);
+void RegisterContextType(const char *name, long len, CtxType *TheCtx);
+#define RegisterCTX(a) RegisterContextType(#a, sizeof(#a) - 1, &a)
+
+extern CtxType CTX_STRBUF;
+extern CtxType CTX_STRBUFARR;
+extern CtxType CTX_LONGVECTOR;
 
 /**
+ * @ingroup subst
  * ContextFilter resembles our RTTI information. With this structure
  * we can make shure a tmplput function can live with the environment
  * we call it in.
  * if not, we will log/print an error and refuse to call it.
  */
 typedef struct _contexts {
-       int ContextType;                /* do we require a User Context ? */
-       int ControlContextType;         /* are we inside of a control structure? */
+       CtxType ContextType;                /* do we require a User Context ? */
        int nMinArgs;                   /* How many arguments do we need at least? */
        int nMaxArgs;                   /* up to how many arguments can we handle? */
 } ContextFilter;
@@ -90,7 +89,11 @@ typedef int (*WCPreevalFunc)(WCTemplateToken *Token);
 /* make a template token a lookup key: */
 #define TKEY(a) TP->Tokens->Params[a]->Start, TP->Tokens->Params[a]->len
 
+void *GetContextPayload(WCTemplputParams *TP, CtxType ContextType);
+#define CTX(a) GetContextPayload(TP, a)
+
 /**
+ * @ingroup subst
  * this is the signature of a conditional function 
  * Note: Target is just passed in for error messages; don't write onto it in regular cases.
  */
@@ -115,6 +118,7 @@ typedef struct _TemplateParam {
 
 
 /**
+ * @ingroup subst
  * Representation of a token; everything thats inbetween <? and >
  */ 
 struct WCTemplateToken {
@@ -156,9 +160,11 @@ struct WCTemplateToken {
 struct WCTemplputParams {
        ContextFilter Filter;
        void *Context;
-       void *ControlContext;
        int nArgs;
        WCTemplateToken *Tokens;
+       WCTemplputParams *Sub, *Super;
+       WCConditionalFunc ExitCtx;
+        long ExitCTXID;
 };
 
 
@@ -167,13 +173,14 @@ typedef struct _ConditionalStruct {
        ContextFilter Filter;
        const char *PlainName;
        WCConditionalFunc CondF;
+       WCConditionalFunc CondExitCtx;
 } ConditionalStruct;
 
 
 typedef void (*SubTemplFunc)(StrBuf *TemplBuffer, WCTemplputParams *TP);
 typedef HashList *(*RetrieveHashlistFunc)(StrBuf *Target, WCTemplputParams *TP);
 typedef void (*HashDestructorFunc) (HashList **KillMe);
-
+typedef int (*FilterByParamFunc)(const char* key, long len, void *Context, StrBuf *Target, WCTemplputParams *TP);
 
 extern WCTemplputParams NoCtx;
 
@@ -184,7 +191,8 @@ extern WCTemplputParams NoCtx;
 #define ERR_PARM1 1
 #define ERR_PARM2 2
 /**
- * @Brief log an error while evaluating a token; print it to the actual template 
+ * @ingroup subst
+ * @brief log an error while evaluating a token; print it to the actual template 
  * @param Target your Target Buffer to print the error message next to the log
  * @param Type What sort of thing are we talking about? Tokens? Conditionals?
  * @param TP grab our set of default information here
@@ -198,7 +206,8 @@ void LogTemplateError (StrBuf *Target,
 
 
 /**
- * @Brief log an error while in global context; print it to Wildfire / Target
+ * @ingroup subst
+ * @brief log an error while in global context; print it to Wildfire / Target
  * @param Target your Target Buffer to print the error message next to the log
  * @param Type What sort of thing are we talking about? Tokens? Conditionals?
  * @param Format for the custom error message
@@ -206,7 +215,8 @@ void LogTemplateError (StrBuf *Target,
 void LogError (StrBuf *Target, const char *Type, const char *Format, ...);
 
 /**
- * @Brief get the actual value of a token parameter
+ * @ingroup subst
+ * @brief get the actual 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
@@ -224,6 +234,7 @@ void GetTemplateTokenString(StrBuf *Target,
                            const char **Value, 
                            long *len);
 /**
+ * @ingroup subst
  * @return whether @ref GetTemplateTokenString would be able to give you a string
  */
 int HaveTemplateTokenString(StrBuf *Target, 
@@ -235,7 +246,8 @@ int HaveTemplateTokenString(StrBuf *Target,
 
 
 /**
- * @Brief get the actual integer value of a token parameter
+ * @ingroup subst
+ * @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
@@ -252,7 +264,7 @@ long GetTemplateTokenNumber(StrBuf *Target,
                            int N, long dflt);
 
 /**
- * @Brief put a token value into the template
+ * @brief put a token value into the template
  * use this function to append your strings into a Template. 
  * it can escape your string according to the token at FormattypeIndex:
  *  H: de-QP and utf8-ify
@@ -269,10 +281,16 @@ void StrBufAppendTemplate(StrBuf *Target,
                          const StrBuf *Source, 
                          int FormatTypeIndex);
 
+void StrBufAppendTemplateStr(StrBuf *Target, 
+                            WCTemplputParams *TP,
+                            const char *Source, 
+                            int FormatTypeIndex);
+
 
 #define RegisterNamespace(a, b, c, d, e, f) RegisterNS(a, sizeof(a)-1, b, c, d, e, f)
 /**
- * @Brief register a template token handler
+ * @ingroup subst
+ * @brief register a template token handler
  * call this function in your InitModule_MODULENAME which will be called at the server start
  * @param nMinArgs how much parameters does your token require at least?
  * @param nMaxArgs how many parameters does your token accept?
@@ -289,21 +307,32 @@ void RegisterNS(const char *NSName, long len,
                int ContextRequired);
 
 /**
- * @Brief register a conditional token <pair> handler
+ * @ingroup subst
+ * @brief register a conditional token <pair> handler
  * call this function in your InitModule_MODULENAME which will be called at the server start
  * conditionals can be ? or ! with a pair or % similar to an implicit if
  * @param Name whats the name of your conditional? should start with COND:
  * @param len the token length so we don't have to measure it.
  * @param nParams how many parameters does your conditional need on top of the default conditional parameters
  * @param CondF your Callback to be called when the template is evaluated at runtime; return 0 or 1 to us please.
+ * @param ExitCtxCond if non-NULL, will be called after the area of the conditional is left behind.
  * @param ContextRequired if your token requires a specific context, else say CTX_NONE here.
  */
-void RegisterConditional(const char *Name, long len, 
-                        int nParams,
-                        WCConditionalFunc CondF, 
-                        int ContextRequired);
+void RegisterContextConditional(const char *Name, long len, 
+                               int nParams,
+                               WCConditionalFunc CondF, 
+                               WCConditionalFunc ExitCtxCond,
+                               int ContextRequired);
+
+#define RegisterCtxConditional(Name, nParams, CondF, ExitCtxCond, ContextRequired) \
+       RegisterContextConditional(Name, sizeof(Name) -1, nParams, CondF, ExitCtxCond, ContextRequired)
+
+#define RegisterConditional(Name, nParams, CondF, ContextRequired) \
+       RegisterContextConditional(Name, sizeof(Name) -1, nParams, CondF, NULL, ContextRequired)
+
 
 /**
+ * @ingroup subst
  * @brief register a string that will represent a long value
  * this will allow to resolve <?...(#"Name")> to Value; that way 
  * plain strings can be used an lexed in templates without having the 
@@ -320,6 +349,7 @@ void RegisterTokenParamDefine(const char *Name, long len,
 #define REGISTERTokenParamDefine(a) RegisterTokenParamDefine(#a, sizeof(#a) - 1, a);
 
 /**
+ * @ingroup subst
  * @brief retrieve the long value of a registered string define
  * @param Name The name of the define
  * @param len length of Name
@@ -332,19 +362,36 @@ long GetTokenDefine(const char *Name,
 
 #define IT_NOFLAG 0
 #define IT_FLAG_DETECT_GROUPCHANGE (1<<0)
-#define RegisterIterator(a, b, c, d, e, f, g, h, i) RegisterITERATOR(a, sizeof(a)-1, b, c, d, e, f, g, h, i)
+#define IT_ADDT_PARAM(n) 5 + n /* If you have AdditionalParams, use this macro to fetch them. */
+#define RegisterIterator(a, b, c, d, e, f, g, h, i) RegisterITERATOR(a, sizeof(a)-1, b, c, d, e, f, NULL, g, h, i)
+#define RegisterFilteredIterator(a, b, c, d, e, f, g, h, i, j) RegisterITERATOR(a, sizeof(a)-1, b, c, d, e, f, g, h, i, j)
 void RegisterITERATOR(const char *Name, long len, /* Our identifier */
-                     int AdditionalParams,       /* doe we use more parameters? */
+                     int AdditionalParams,       /* do we use more parameters? */
                      HashList *StaticList,       /* pointer to webcit lifetime hashlists */
                      RetrieveHashlistFunc GetHash, /* else retrieve the hashlist by calling this function */
                      SubTemplFunc DoSubTempl,       /* call this function on each iteration for svput & friends */
                      HashDestructorFunc Destructor, /* use this function to shut down the hash; NULL if its a reference */
-                     int ContextType,               /* which context do we provide to the subtemplate? */
-                     int XPectContextType,          /* which context do we expct to be called in? */
+                     FilterByParamFunc Filter,      /* use this function if you want to skip items */
+                     CtxType ContextType,               /* which context do we provide to the subtemplate? */
+                     CtxType XPectContextType,          /* which context do we expct to be called in? */
                      int Flags);
 
 
 
+void StackDynamicContext(WCTemplputParams *Super, 
+                        WCTemplputParams *Sub, 
+                        void *Context,
+                        CtxType ContextType,
+                        int nArgs,
+                        WCTemplateToken *Tokens, 
+                        WCConditionalFunc ExitCtx,
+                        long ExitCTXID);
+
+#define StackContext(Super, Sub, Context, ContextType, nArgs, Tokens) \
+       StackDynamicContext(Super, Sub, Context, ContextType, nArgs, Tokens, NULL, 0)
+
+
+void UnStackContext(WCTemplputParams *Sub);
 
 
 
@@ -357,7 +404,7 @@ void RegisterSortFunc(const char *name, long len,
                      CompareFunc Forward, 
                      CompareFunc Reverse, 
                      CompareFunc GroupChange, 
-                     long ContextType);
+                     CtxType ContextType);
 
 void dbg_print_longvector(long *LongVector);