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