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