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