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