removed a bunch of blank comment lines
[citadel.git] / webcit / subst.h
index 4f53cc3726cdf5accc4e689a0ee95fbc2bfdda94..b44b33a5217061a3a01a8753d3d1a748a46d2070 100644 (file)
@@ -1,3 +1,6 @@
+/**
+ * @defgroup subst Template processing functions
+ */
 
 extern HashList *Conditionals;
 extern HashList *GlobalNS;
@@ -15,8 +18,10 @@ extern HashList *LocalTemplateCache;
 #define TYPE_GETTEXT 5
 #define TYPE_BSTR 6
 #define TYPE_SUBTEMPLATE 7
-#define MAXPARAM  20
+#define TYPE_INTDEFINE 8
+#define MAXPARAM  25
 
+#define IS_NUMBER(a) ((a == TYPE_LONG) || (a == TYPE_PREFINT) || (a == TYPE_INTDEFINE))
 
 /*
  * \brief Values for wcs_type
@@ -46,15 +51,26 @@ enum {
 #define CTX_MIME_ATACH 10
 #define CTX_FILELIST 11
 #define CTX_STRBUF 12
-#define CTX_LONGVECTOR 13
-#define CTX_ROOMS 14
-#define CTX_FLOORS 15
-#define CTX_ITERATE 16
-
-#define CTX_UNKNOWN 17
+#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_MAILQITEM 24
+#define CTX_MAILQ_RCPT 25
+#define CTX_SRVLOG 26
+
+#define CTX_UNKNOWN 27
 
 
 /**
+ * @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.
@@ -75,30 +91,24 @@ typedef struct WCTemplputParams WCTemplputParams;
 /* this is the signature of a tmplput function */
 typedef void (*WCHandlerFunc)(StrBuf *Target, WCTemplputParams *TP);
 
+/* if you want to pre-evaluate parts of your token, or do additional syntax, use this. */ 
+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
 
-/* TODO: wcsubst should be private! */
-
-/*
- * \brief Dynamic content for variable substitution in templates
- */
-typedef struct _wcsubst {
-       ContextFilter Filter;
-       int wcs_type;                       /* which type of Substitution are we */
-       char wcs_key[32];                   /* copy of our hashkey for debugging */
-       StrBuf *wcs_value;                  /* if we're a string, keep it here */
-       long lvalue;                        /* type long? keep data here */
-       WCHandlerFunc wcs_function; /* funcion hook ???*/
-} wcsubst;
-
-
 /**
+ * @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.
  */
 typedef int (*WCConditionalFunc)(StrBuf *Target, WCTemplputParams *TP);
 
+typedef enum _eBitMask {
+       eNO = 0,
+       eOR,
+       eAND
+}eBitMask;
 
 typedef struct _TemplateParam {
         /* are we a string or a number? */
@@ -108,10 +118,12 @@ typedef struct _TemplateParam {
        long len;
        /* if we're a number: */
        long lvalue;
+       eBitMask MaskBy;
 } TemplateParam;
 
 
 /**
+ * @ingroup subst
  * Representation of a token; everything thats inbetween <? and >
  */ 
 struct WCTemplateToken {
@@ -137,6 +149,7 @@ struct WCTemplateToken {
        int Flags;
        /* pointer to our runntime evaluator; so we can cache this and save hash-lookups */
        void *PreEval;
+       void *Preeval2;
 
        /* if we have parameters here we go: */
        /* do we have parameters or not? */
@@ -165,15 +178,6 @@ typedef struct _ConditionalStruct {
        WCConditionalFunc CondF;
 } ConditionalStruct;
 
-/** IterateStruct TODO: SHOULD BE PRIVATE */
-typedef struct _iteratestruct {
-        int GroupChange;
-        int oddeven;  
-        const char *Key;
-        long KeyLen;
-        int n;
-        int LastN;
-}IterateStruct;
 
 typedef void (*SubTemplFunc)(StrBuf *TemplBuffer, WCTemplputParams *TP);
 typedef HashList *(*RetrieveHashlistFunc)(StrBuf *Target, WCTemplputParams *TP);
@@ -182,18 +186,19 @@ typedef void (*HashDestructorFunc) (HashList **KillMe);
 
 extern WCTemplputParams NoCtx;
 
-
+#define HAVE_PARAM(a) (TP->Tokens->nParameters > a)
 
 
 #define ERR_NAME 0
 #define ERR_PARM1 1
 #define ERR_PARM2 2
 /**
- * \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
- * \param Format for the custom error message
+ * @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
+ * @param Format for the custom error message
  */ 
 void LogTemplateError (StrBuf *Target, 
                       const char *Type, 
@@ -201,8 +206,19 @@ void LogTemplateError (StrBuf *Target,
                       WCTemplputParams *TP, 
                       const char *Format, ...)__attribute__((__format__(__printf__,5,6)));
 
+
 /**
- * \Brief get the actual value of a token parameter
+ * @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
+ */ 
+void LogError (StrBuf *Target, const char *Type, const char *Format, ...);
+
+/**
+ * @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
@@ -210,26 +226,56 @@ void LogTemplateError (StrBuf *Target,
  *  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 Value reference to the string of the token; don't free me.
- * \param len the length of Value
+ * @param N which token do you want to lookup?
+ * @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);
+/**
+ * @ingroup subst
+ * @return whether @ref GetTemplateTokenString would be able to give you a string
+ */
+int HaveTemplateTokenString(StrBuf *Target, 
+                           WCTemplputParams *TP,
+                           int N,
+                           const char **Value, 
+                           long *len);
+
+
+
+/**
+ * @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
+ *  : -> 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
+ * @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
  *  X: escapize for HTML
  *  J: JSON Escapize
- * \param Target the destination buffer
- * \param TP the template token information
- * \param Source string to append
- * \param FormatTypeIndex which parameter contains the escaping functionality?
+ * @param Target the destination buffer
+ * @param TP the template token information
+ * @param Source string to append
+ * @param FormatTypeIndex which parameter contains the escaping functionality?
  *        if this token doesn't have as much parameters, plain append is done.
  */
 void StrBufAppendTemplate(StrBuf *Target, 
@@ -238,18 +284,68 @@ void StrBufAppendTemplate(StrBuf *Target,
                          int FormatTypeIndex);
 
 
-#define RegisterNamespace(a, b, c, d, e) RegisterNS(a, sizeof(a)-1, b, c, d, e)
+#define RegisterNamespace(a, b, c, d, e, f) RegisterNS(a, sizeof(a)-1, b, c, d, e, f)
+/**
+ * @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?
+ * @param HandlerFunc your callback when the template is rendered and your token is there 
+ * @param PreEvalFunc is called when the template is parsed; you can do additional 
+ *        syntax checks here or pre-evaluate stuff for better performance
+ * @param ContextRequired if your token requires a specific context, else say CTX_NONE here.
+ */
 void RegisterNS(const char *NSName, long len, 
                int nMinArgs, 
                int nMaxArgs, 
                WCHandlerFunc HandlerFunc,
+               WCPreevalFunc PreEvalFunc,
                int ContextRequired);
 
+/**
+ * @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 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);
 
+/**
+ * @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 
+ * lookup overhead at runtime.
+ * @param Name The name of the define
+ * @param len length of Name
+ * @param Value the value to associate with Name
+ */
+void RegisterTokenParamDefine(const char *Name, long len, 
+                             long Value);
+/**
+ * teh r0x0r! forward your favourite define from C to the templates with one easy call!
+ */
+#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
+ * @param Value the value to return if not found
+ */
+long GetTokenDefine(const char *Name, 
+                   long len, 
+                   long DefValue);
 
 
 #define IT_NOFLAG 0
@@ -281,35 +377,12 @@ void RegisterSortFunc(const char *name, long len,
                      CompareFunc GroupChange, 
                      long ContextType);
 
-
-
-
 void dbg_print_longvector(long *LongVector);
 
-
-
-
-
-#define do_template(a, b) DoTemplate(a, sizeof(a) -1, NULL, &NoCtx);
+#define do_template(a) DoTemplate(a, sizeof(a) -1, NULL, &NoCtx)
 const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP);
 void url_do_template(void);
 
 
-
-
-
 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup);
 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup);
-
-void SVPut(char *keyname, size_t keylen, int keytype, char *Data);
-#define svput(a, b, c) SVPut(a, sizeof(a) - 1, b, c)
-void SVPutLong(char *keyname, size_t keylen, long Data);
-#define svputlong(a, b) SVPutLong(a, sizeof(a) - 1, b)
-void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...) __attribute__((__format__(__printf__,4,5)));
-void SVPRINTF(char *keyname, int keytype, const char *format,...) __attribute__((__format__(__printf__,3,4)));
-void SVCALLBACK(char *keyname, WCHandlerFunc fcn_ptr);
-void SVCallback(char *keyname, size_t keylen,  WCHandlerFunc fcn_ptr);
-#define svcallback(a, b) SVCallback(a, sizeof(a) - 1, b)
-
-void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref);
-#define SVPutBuf(a, b, c); SVPUTBuf(a, sizeof(a) - 1, b, c)