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