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