* implement ITERATE:THISROOM:GNET
[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  * @return whether @ref GetTemplateTokenString would be able to give you a string
216  */
217 int HaveTemplateTokenString(StrBuf *Target, 
218                             WCTemplputParams *TP,
219                             int N,
220                             const char **Value, 
221                             long *len);
222
223
224
225 /**
226  * @Brief get the actual integer value of a token parameter
227  * in your tmplputs or conditionals use this function to access parameters that can also be 
228  * retrieved from dynamic facilities:
229  *  _ -> Gettext; retrieve this token from the i18n facilities
230  *  : -> lookup a setting of that name
231  *  B -> bstr; an URL-Parameter
232  *  = -> subtemplate; parse a template by this name, and treat its content as this tokens value 
233  * 
234  * @param N which token do you want to lookup?
235  * @param dflt default value to be retrieved if not found in preferences
236  * \returns the long value
237  */
238 long GetTemplateTokenNumber(StrBuf *Target, 
239                             WCTemplputParams *TP, 
240                             int N, long dflt);
241
242 /**
243  * @Brief put a token value into the template
244  * use this function to append your strings into a Template. 
245  * it can escape your string according to the token at FormattypeIndex:
246  *  H: de-QP and utf8-ify
247  *  X: escapize for HTML
248  *  J: JSON Escapize
249  * @param Target the destination buffer
250  * @param TP the template token information
251  * @param Source string to append
252  * @param FormatTypeIndex which parameter contains the escaping functionality?
253  *        if this token doesn't have as much parameters, plain append is done.
254  */
255 void StrBufAppendTemplate(StrBuf *Target, 
256                           WCTemplputParams *TP,
257                           const StrBuf *Source, 
258                           int FormatTypeIndex);
259
260
261 #define RegisterNamespace(a, b, c, d, e, f) RegisterNS(a, sizeof(a)-1, b, c, d, e, f)
262 /**
263  * @Brief register a template token handler
264  * call this function in your InitModule_MODULENAME which will be called at the server start
265  * @param nMinArgs how much parameters does your token require at least?
266  * @param nMaxArgs how many parameters does your token accept?
267  * @param HandlerFunc your callback when the template is rendered and your token is there 
268  * @param PreEvalFunc is called when the template is parsed; you can do additional 
269  *        syntax checks here or pre-evaluate stuff for better performance
270  * @param ContextRequired if your token requires a specific context, else say CTX_NONE here.
271  */
272 void RegisterNS(const char *NSName, long len, 
273                 int nMinArgs, 
274                 int nMaxArgs, 
275                 WCHandlerFunc HandlerFunc,
276                 WCPreevalFunc PreEvalFunc,
277                 int ContextRequired);
278
279 /**
280  * @Brief register a conditional token <pair> handler
281  * call this function in your InitModule_MODULENAME which will be called at the server start
282  * conditionals can be ? or ! with a pair or % similar to an implicit if
283  * @param Name whats the name of your conditional? should start with COND:
284  * @param len the token length so we don't have to measure it.
285  * @param nParams how many parameters does your conditional need on top of the default conditional parameters
286  * @param CondF your Callback to be called when the template is evaluated at runtime; return 0 or 1 to us please.
287  * @param ContextRequired if your token requires a specific context, else say CTX_NONE here.
288  */
289 void RegisterConditional(const char *Name, long len, 
290                          int nParams,
291                          WCConditionalFunc CondF, 
292                          int ContextRequired);
293
294 /**
295  * @brief register a string that will represent a long value
296  * this will allow to resolve <?...(#"Name")> to Value; that way 
297  * plain strings can be used an lexed in templates without having the 
298  * lookup overhead at runtime.
299  * @param Name The name of the define
300  * @param len length of Name
301  * @param Value the value to associate with Name
302  */
303 void RegisterTokenParamDefine(const char *Name, long len, 
304                               long Value);
305 /**
306  * teh r0x0r! forward your favourite define from C to the templates with one easy call!
307  */
308 #define REGISTERTokenParamDefine(a) RegisterTokenParamDefine(#a, sizeof(#a) - 1, a);
309
310 /**
311  * @brief retrieve the long value of a registered string define
312  * @param Name The name of the define
313  * @param len length of Name
314  * @param Value the value to return if not found
315  */
316 long GetTokenDefine(const char *Name, 
317                     long len, 
318                     long DefValue);
319
320
321 #define IT_NOFLAG 0
322 #define IT_FLAG_DETECT_GROUPCHANGE (1<<0)
323 #define RegisterIterator(a, b, c, d, e, f, g, h, i) RegisterITERATOR(a, sizeof(a)-1, b, c, d, e, f, g, h, i)
324 void RegisterITERATOR(const char *Name, long len, /* Our identifier */
325                       int AdditionalParams,       /* doe we use more parameters? */
326                       HashList *StaticList,       /* pointer to webcit lifetime hashlists */
327                       RetrieveHashlistFunc GetHash, /* else retrieve the hashlist by calling this function */
328                       SubTemplFunc DoSubTempl,       /* call this function on each iteration for svput & friends */
329                       HashDestructorFunc Destructor, /* use this function to shut down the hash; NULL if its a reference */
330                       int ContextType,               /* which context do we provide to the subtemplate? */
331                       int XPectContextType,          /* which context do we expct to be called in? */
332                       int Flags);
333
334
335
336
337
338
339 CompareFunc RetrieveSort(WCTemplputParams *TP, 
340                          const char *OtherPrefix, long OtherPrefixLen,  
341                          const char *Default, long ldefault, 
342                          long DefaultDirection);
343 void RegisterSortFunc(const char *name, long len, 
344                       const char *prepend, long preplen,
345                       CompareFunc Forward, 
346                       CompareFunc Reverse, 
347                       CompareFunc GroupChange, 
348                       long ContextType);
349
350
351
352
353 void dbg_print_longvector(long *LongVector);
354
355
356
357
358
359 #define do_template(a, b) DoTemplate(a, sizeof(a) -1, NULL, &NoCtx);
360 const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP);
361 void url_do_template(void);
362
363
364
365
366
367 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup);
368 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup);
369
370 void SVPut(char *keyname, size_t keylen, int keytype, char *Data);
371 #define svput(a, b, c) SVPut(a, sizeof(a) - 1, b, c)
372 void SVPutLong(char *keyname, size_t keylen, long Data);
373 #define svputlong(a, b) SVPutLong(a, sizeof(a) - 1, b)
374 void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...) __attribute__((__format__(__printf__,4,5)));
375 void SVPRINTF(char *keyname, int keytype, const char *format,...) __attribute__((__format__(__printf__,3,4)));
376 void SVCALLBACK(char *keyname, WCHandlerFunc fcn_ptr);
377 void SVCallback(char *keyname, size_t keylen,  WCHandlerFunc fcn_ptr);
378 #define svcallback(a, b) SVCallback(a, sizeof(a) - 1, b)
379
380 void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref);
381 #define SVPutBuf(a, b, c); SVPUTBuf(a, sizeof(a) - 1, b, c)