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