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