f0aa90c2407ffcc96ad4870369492d5e9413dd55
[citadel.git] / webcit / subst.h
1 /**
2  * @defgroup subst Template processing functions
3  */
4
5 extern HashList *Conditionals;
6 extern HashList *GlobalNS;
7 extern HashList *Iterators;
8 extern HashList *WirelessTemplateCache;
9 extern HashList *WirelessLocalTemplateCache;
10 extern HashList *TemplateCache;
11 extern HashList *LocalTemplateCache;
12
13
14 #define TYPE_STR   1
15 #define TYPE_LONG  2
16 #define TYPE_PREFSTR 3
17 #define TYPE_PREFINT 4
18 #define TYPE_GETTEXT 5
19 #define TYPE_BSTR 6
20 #define TYPE_SUBTEMPLATE 7
21 #define TYPE_INTDEFINE 8
22 #define MAXPARAM  25
23
24 #define IS_NUMBER(a) ((a == TYPE_LONG) || (a == TYPE_PREFINT) || (a == TYPE_INTDEFINE))
25
26 /*
27  * \brief Values for wcs_type
28  */
29 enum {
30         WCS_STRING,       /* its a string */
31         WCS_FUNCTION,     /* its a function callback */
32         WCS_SERVCMD,      /* its a command to send to the citadel server */
33         WCS_STRBUF,       /* its a strbuf we own */
34         WCS_STRBUF_REF,   /* its a strbuf we mustn't free */
35         WCS_LONG          /* its an integer */
36 };
37
38 #define CTX_NONE 0
39 #define CTX_SITECFG 1
40 #define CTX_SESSION 2
41 #define CTX_INETCFG 3
42 #define CTX_VNOTE 4
43 #define CTX_WHO 5
44 #define CTX_PREF 6
45 #define CTX_NODECONF 7
46 #define CTX_USERLIST 8
47 #define CTX_MAILSUM 9
48 #define CTX_MIME_ATACH 10
49 #define CTX_FILELIST 11
50 #define CTX_STRBUF 12
51 #define CTX_STRBUFARR 13
52 #define CTX_LONGVECTOR 14
53 #define CTX_ROOMS 15
54 #define CTX_FLOORS 16
55 #define CTX_ITERATE 17
56 #define CTX_ICAL 18
57 #define CTX_DAVNS 19
58 #define CTX_TAB 20
59 #define CTX_VCARD 21
60 #define CTX_SIEVELIST 22
61 #define CTX_SIEVESCRIPT 23
62 #define CTX_MAILQITEM 24
63 #define CTX_MAILQ_RCPT 25
64 #define CTX_SRVLOG 26
65
66 #define CTX_UNKNOWN 27
67
68
69 /**
70  * @ingroup subst
71  * ContextFilter resembles our RTTI information. With this structure
72  * we can make shure a tmplput function can live with the environment
73  * we call it in.
74  * if not, we will log/print an error and refuse to call it.
75  */
76 typedef struct _contexts {
77         int ContextType;                /* do we require a User Context ? */
78         int nMinArgs;                   /* How many arguments do we need at least? */
79         int nMaxArgs;                   /* up to how many arguments can we handle? */
80 } ContextFilter;
81
82
83 /* Forward declarations... */
84 typedef struct WCTemplateToken WCTemplateToken;
85 typedef struct WCTemplputParams WCTemplputParams;
86
87 /* this is the signature of a tmplput function */
88 typedef void (*WCHandlerFunc)(StrBuf *Target, WCTemplputParams *TP);
89
90 /* if you want to pre-evaluate parts of your token, or do additional syntax, use this. */ 
91 typedef int (*WCPreevalFunc)(WCTemplateToken *Token);
92
93 /* make a template token a lookup key: */
94 #define TKEY(a) TP->Tokens->Params[a]->Start, TP->Tokens->Params[a]->len
95
96 void *GetContextPayload(WCTemplputParams *TP, int ContextType);
97 #define CTX(a) GetContextPayload(TP, a)
98
99 /**
100  * @ingroup subst
101  * this is the signature of a conditional function 
102  * Note: Target is just passed in for error messages; don't write onto it in regular cases.
103  */
104 typedef int (*WCConditionalFunc)(StrBuf *Target, WCTemplputParams *TP);
105
106 typedef enum _eBitMask {
107         eNO = 0,
108         eOR,
109         eAND
110 }eBitMask;
111
112 typedef struct _TemplateParam {
113         /* are we a string or a number? */
114         int Type;
115         /* string data: */
116         const char *Start;
117         long len;
118         /* if we're a number: */
119         long lvalue;
120         eBitMask MaskBy;
121 } TemplateParam;
122
123
124 /**
125  * @ingroup subst
126  * Representation of a token; everything thats inbetween <? and >
127  */ 
128 struct WCTemplateToken {
129         /* Reference to the filename we're in to print error messages; not to be freed */
130         const StrBuf *FileName; 
131         /* Raw copy of our original token; for error printing */
132         StrBuf *FlatToken;
133         /* Which line did the template parser pick us up in? For error printing */
134         long Line;
135
136         /* our position in the template cache buffer */
137         const char *pTokenStart;
138         /* our token length */
139         size_t TokenStart;
140         size_t TokenEnd;
141         /* point after us */
142         const char *pTokenEnd;
143         /* just our token name: */
144         const char *pName;
145         size_t NameEnd;
146
147         /* stuff the pre-evaluater finds out: */
148         int Flags;
149         /* pointer to our runntime evaluator; so we can cache this and save hash-lookups */
150         void *PreEval;
151         void *Preeval2;
152
153         /* if we have parameters here we go: */
154         /* do we have parameters or not? */
155         int HaveParameters;
156         /* How many of them? */
157         int nParameters;
158         /* the parameters */
159         TemplateParam *Params[MAXPARAM];
160 };
161
162
163
164 struct WCTemplputParams {
165         ContextFilter Filter;
166         void *Context;
167         int nArgs;
168         WCTemplateToken *Tokens;
169         WCTemplputParams *Sub, *Super;
170 };
171
172
173
174 typedef struct _ConditionalStruct {
175         ContextFilter Filter;
176         const char *PlainName;
177         WCConditionalFunc CondF;
178 } ConditionalStruct;
179
180
181 typedef void (*SubTemplFunc)(StrBuf *TemplBuffer, WCTemplputParams *TP);
182 typedef HashList *(*RetrieveHashlistFunc)(StrBuf *Target, WCTemplputParams *TP);
183 typedef void (*HashDestructorFunc) (HashList **KillMe);
184
185
186 extern WCTemplputParams NoCtx;
187
188 #define HAVE_PARAM(a) (TP->Tokens->nParameters > a)
189
190
191 #define ERR_NAME 0
192 #define ERR_PARM1 1
193 #define ERR_PARM2 2
194 /**
195  * @ingroup subst
196  * @brief log an error while evaluating a token; print it to the actual template 
197  * @param Target your Target Buffer to print the error message next to the log
198  * @param Type What sort of thing are we talking about? Tokens? Conditionals?
199  * @param TP grab our set of default information here
200  * @param Format for the custom error message
201  */ 
202 void LogTemplateError (StrBuf *Target, 
203                        const char *Type, 
204                        int ErrorPos, 
205                        WCTemplputParams *TP, 
206                        const char *Format, ...)__attribute__((__format__(__printf__,5,6)));
207
208
209 /**
210  * @ingroup subst
211  * @brief log an error while in global context; print it to Wildfire / Target
212  * @param Target your Target Buffer to print the error message next to the log
213  * @param Type What sort of thing are we talking about? Tokens? Conditionals?
214  * @param Format for the custom error message
215  */ 
216 void LogError (StrBuf *Target, const char *Type, const char *Format, ...);
217
218 /**
219  * @ingroup subst
220  * @brief get the actual value of a token parameter
221  * in your tmplputs or conditionals use this function to access parameters that can also be 
222  * retrieved from dynamic facilities:
223  *  _ -> Gettext; retrieve this token from the i18n facilities
224  *  : -> lookup a setting of that name
225  *  B -> bstr; an URL-Parameter
226  *  = -> subtemplate; parse a template by this name, and treat its content as this tokens value 
227  * 
228  * @param N which token do you want to lookup?
229  * @param Value reference to the string of the token; don't free me.
230  * @param len the length of Value
231  */
232 void GetTemplateTokenString(StrBuf *Target, 
233                             WCTemplputParams *TP,
234                             int N,
235                             const char **Value, 
236                             long *len);
237 /**
238  * @ingroup subst
239  * @return whether @ref GetTemplateTokenString would be able to give you a string
240  */
241 int HaveTemplateTokenString(StrBuf *Target, 
242                             WCTemplputParams *TP,
243                             int N,
244                             const char **Value, 
245                             long *len);
246
247
248
249 /**
250  * @ingroup subst
251  * @brief get the actual integer value of a token parameter
252  * in your tmplputs or conditionals use this function to access parameters that can also be 
253  * retrieved from dynamic facilities:
254  *  _ -> Gettext; retrieve this token from the i18n facilities
255  *  : -> lookup a setting of that name
256  *  B -> bstr; an URL-Parameter
257  *  = -> subtemplate; parse a template by this name, and treat its content as this tokens value 
258  * 
259  * @param N which token do you want to lookup?
260  * @param dflt default value to be retrieved if not found in preferences
261  * \returns the long value
262  */
263 long GetTemplateTokenNumber(StrBuf *Target, 
264                             WCTemplputParams *TP, 
265                             int N, long dflt);
266
267 /**
268  * @brief put a token value into the template
269  * use this function to append your strings into a Template. 
270  * it can escape your string according to the token at FormattypeIndex:
271  *  H: de-QP and utf8-ify
272  *  X: escapize for HTML
273  *  J: JSON Escapize
274  * @param Target the destination buffer
275  * @param TP the template token information
276  * @param Source string to append
277  * @param FormatTypeIndex which parameter contains the escaping functionality?
278  *        if this token doesn't have as much parameters, plain append is done.
279  */
280 void StrBufAppendTemplate(StrBuf *Target, 
281                           WCTemplputParams *TP,
282                           const StrBuf *Source, 
283                           int FormatTypeIndex);
284
285
286 #define RegisterNamespace(a, b, c, d, e, f) RegisterNS(a, sizeof(a)-1, b, c, d, e, f)
287 /**
288  * @ingroup subst
289  * @brief register a template token handler
290  * call this function in your InitModule_MODULENAME which will be called at the server start
291  * @param nMinArgs how much parameters does your token require at least?
292  * @param nMaxArgs how many parameters does your token accept?
293  * @param HandlerFunc your callback when the template is rendered and your token is there 
294  * @param PreEvalFunc is called when the template is parsed; you can do additional 
295  *        syntax checks here or pre-evaluate stuff for better performance
296  * @param ContextRequired if your token requires a specific context, else say CTX_NONE here.
297  */
298 void RegisterNS(const char *NSName, long len, 
299                 int nMinArgs, 
300                 int nMaxArgs, 
301                 WCHandlerFunc HandlerFunc,
302                 WCPreevalFunc PreEvalFunc,
303                 int ContextRequired);
304
305 /**
306  * @ingroup subst
307  * @brief register a conditional token <pair> handler
308  * call this function in your InitModule_MODULENAME which will be called at the server start
309  * conditionals can be ? or ! with a pair or % similar to an implicit if
310  * @param Name whats the name of your conditional? should start with COND:
311  * @param len the token length so we don't have to measure it.
312  * @param nParams how many parameters does your conditional need on top of the default conditional parameters
313  * @param CondF your Callback to be called when the template is evaluated at runtime; return 0 or 1 to us please.
314  * @param ContextRequired if your token requires a specific context, else say CTX_NONE here.
315  */
316 void RegisterConditional(const char *Name, long len, 
317                          int nParams,
318                          WCConditionalFunc CondF, 
319                          int ContextRequired);
320
321 /**
322  * @ingroup subst
323  * @brief register a string that will represent a long value
324  * this will allow to resolve <?...(#"Name")> to Value; that way 
325  * plain strings can be used an lexed in templates without having the 
326  * lookup overhead at runtime.
327  * @param Name The name of the define
328  * @param len length of Name
329  * @param Value the value to associate with Name
330  */
331 void RegisterTokenParamDefine(const char *Name, long len, 
332                               long Value);
333 /**
334  * teh r0x0r! forward your favourite define from C to the templates with one easy call!
335  */
336 #define REGISTERTokenParamDefine(a) RegisterTokenParamDefine(#a, sizeof(#a) - 1, a);
337
338 /**
339  * @ingroup subst
340  * @brief retrieve the long value of a registered string define
341  * @param Name The name of the define
342  * @param len length of Name
343  * @param Value the value to return if not found
344  */
345 long GetTokenDefine(const char *Name, 
346                     long len, 
347                     long DefValue);
348
349
350 #define IT_NOFLAG 0
351 #define IT_FLAG_DETECT_GROUPCHANGE (1<<0)
352 #define RegisterIterator(a, b, c, d, e, f, g, h, i) RegisterITERATOR(a, sizeof(a)-1, b, c, d, e, f, g, h, i)
353 void RegisterITERATOR(const char *Name, long len, /* Our identifier */
354                       int AdditionalParams,       /* doe we use more parameters? */
355                       HashList *StaticList,       /* pointer to webcit lifetime hashlists */
356                       RetrieveHashlistFunc GetHash, /* else retrieve the hashlist by calling this function */
357                       SubTemplFunc DoSubTempl,       /* call this function on each iteration for svput & friends */
358                       HashDestructorFunc Destructor, /* use this function to shut down the hash; NULL if its a reference */
359                       int ContextType,               /* which context do we provide to the subtemplate? */
360                       int XPectContextType,          /* which context do we expct to be called in? */
361                       int Flags);
362
363
364
365 void StackContext(WCTemplputParams *Super, 
366                   WCTemplputParams *Sub, 
367                   void *Context,
368                   int ContextType,
369                   int nArgs,
370                   WCTemplateToken *Tokens);
371
372 void UnStackContext(WCTemplputParams *Sub);
373
374
375
376 CompareFunc RetrieveSort(WCTemplputParams *TP, 
377                          const char *OtherPrefix, long OtherPrefixLen,  
378                          const char *Default, long ldefault, 
379                          long DefaultDirection);
380 void RegisterSortFunc(const char *name, long len, 
381                       const char *prepend, long preplen,
382                       CompareFunc Forward, 
383                       CompareFunc Reverse, 
384                       CompareFunc GroupChange, 
385                       long ContextType);
386
387 void dbg_print_longvector(long *LongVector);
388
389 #define do_template(a) DoTemplate(a, sizeof(a) -1, NULL, &NoCtx)
390 const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP);
391 void url_do_template(void);
392
393
394 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup);
395 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup);