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