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