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