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