36c68a2d2c47c4f4521a3e5a99cdc06b090377ce
[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 /* make a template token a lookup key: */
81 #define TKEY(a) TP->Tokens->Params[a]->Start, TP->Tokens->Params[a]->len
82
83 /**
84  * this is the signature of a conditional function 
85  * Note: Target is just passed in for error messages; don't write onto it in regular cases.
86  */
87 typedef int (*WCConditionalFunc)(StrBuf *Target, WCTemplputParams *TP);
88
89
90 typedef struct _TemplateParam {
91         /* are we a string or a number? */
92         int Type;
93         /* string data: */
94         const char *Start;
95         long len;
96         /* if we're a number: */
97         long lvalue;
98 } TemplateParam;
99
100
101 /**
102  * Representation of a token; everything thats inbetween <? and >
103  */ 
104 struct WCTemplateToken {
105         /* Reference to the filename we're in to print error messages; not to be freed */
106         const StrBuf *FileName; 
107         /* Raw copy of our original token; for error printing */
108         StrBuf *FlatToken;
109         /* Which line did the template parser pick us up in? For error printing */
110         long Line;
111
112         /* our position in the template cache buffer */
113         const char *pTokenStart;
114         /* our token length */
115         size_t TokenStart;
116         size_t TokenEnd;
117         /* point after us */
118         const char *pTokenEnd;
119         /* just our token name: */
120         const char *pName;
121         size_t NameEnd;
122
123         /* stuff the pre-evaluater finds out: */
124         int Flags;
125         /* pointer to our runntime evaluator; so we can cache this and save hash-lookups */
126         void *PreEval;
127
128         /* if we have parameters here we go: */
129         /* do we have parameters or not? */
130         int HaveParameters;
131         /* How many of them? */
132         int nParameters;
133         /* the parameters */
134         TemplateParam *Params[MAXPARAM];
135 };
136
137
138
139 struct WCTemplputParams {
140         ContextFilter Filter;
141         void *Context;
142         void *ControlContext;
143         int nArgs;
144         WCTemplateToken *Tokens;
145 };
146
147
148
149 typedef struct _ConditionalStruct {
150         ContextFilter Filter;
151         const char *PlainName;
152         WCConditionalFunc CondF;
153 } ConditionalStruct;
154
155
156 typedef void (*SubTemplFunc)(StrBuf *TemplBuffer, WCTemplputParams *TP);
157 typedef HashList *(*RetrieveHashlistFunc)(StrBuf *Target, WCTemplputParams *TP);
158 typedef void (*HashDestructorFunc) (HashList **KillMe);
159
160
161 extern WCTemplputParams NoCtx;
162
163 #define HAVE_PARAM(a) (TP->Tokens->nParameters > a)
164
165
166 #define ERR_NAME 0
167 #define ERR_PARM1 1
168 #define ERR_PARM2 2
169 /**
170  * \Brief log an error while evaluating a token; print it to the actual template 
171  * \param Target your Target Buffer to print the error message next to the log
172  * \param Type What sort of thing are we talking about? Tokens? Conditionals?
173  * \param TP grab our set of default information here
174  * \param Format for the custom error message
175  */ 
176 void LogTemplateError (StrBuf *Target, 
177                        const char *Type, 
178                        int ErrorPos, 
179                        WCTemplputParams *TP, 
180                        const char *Format, ...)__attribute__((__format__(__printf__,5,6)));
181
182
183 /**
184  * \Brief log an error while in global context; print it to Wildfire / Target
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 Format for the custom error message
188  */ 
189 void LogError (StrBuf *Target, const char *Type, const char *Format, ...);
190
191 /**
192  * \Brief get the actual value of a token parameter
193  * in your tmplputs or conditionals use this function to access parameters that can also be 
194  * retrieved from dynamic facilities:
195  *  _ -> Gettext; retrieve this token from the i18n facilities
196  *  : -> lookup a setting of that name
197  *  B -> bstr; an URL-Parameter
198  *  = -> subtemplate; parse a template by this name, and treat its content as this tokens value 
199  * 
200  * \param N which token do you want to lookup?
201  * \param Value reference to the string of the token; don't free me.
202  * \param len the length of Value
203  */
204 void GetTemplateTokenString(StrBuf *Target, 
205                             WCTemplputParams *TP,
206                             int N,
207                             const char **Value, 
208                             long *len);
209
210
211
212 /**
213  * \Brief get the actual integer value of a token parameter
214  * in your tmplputs or conditionals use this function to access parameters that can also be 
215  * retrieved from dynamic facilities:
216  *  _ -> Gettext; retrieve this token from the i18n facilities
217  *  : -> lookup a setting of that name
218  *  B -> bstr; an URL-Parameter
219  *  = -> subtemplate; parse a template by this name, and treat its content as this tokens value 
220  * 
221  * \param N which token do you want to lookup?
222  * \param dflt default value to be retrieved if not found in preferences
223  * \returns the long value
224  */
225 long GetTemplateTokenNumber(StrBuf *Target, 
226                             WCTemplputParams *TP, 
227                             int N, long dflt);
228
229 /**
230  * \Brief put a token value into the template
231  * use this function to append your strings into a Template. 
232  * it can escape your string according to the token at FormattypeIndex:
233  *  H: de-QP and utf8-ify
234  *  X: escapize for HTML
235  *  J: JSON Escapize
236  * \param Target the destination buffer
237  * \param TP the template token information
238  * \param Source string to append
239  * \param FormatTypeIndex which parameter contains the escaping functionality?
240  *        if this token doesn't have as much parameters, plain append is done.
241  */
242 void StrBufAppendTemplate(StrBuf *Target, 
243                           WCTemplputParams *TP,
244                           const StrBuf *Source, 
245                           int FormatTypeIndex);
246
247
248 #define RegisterNamespace(a, b, c, d, e) RegisterNS(a, sizeof(a)-1, b, c, d, e)
249 void RegisterNS(const char *NSName, long len, 
250                 int nMinArgs, 
251                 int nMaxArgs, 
252                 WCHandlerFunc HandlerFunc,
253                 int ContextRequired);
254
255 void RegisterConditional(const char *Name, long len, 
256                          int nParams,
257                          WCConditionalFunc CondF, 
258                          int ContextRequired);
259
260
261
262 #define IT_NOFLAG 0
263 #define IT_FLAG_DETECT_GROUPCHANGE (1<<0)
264 #define RegisterIterator(a, b, c, d, e, f, g, h, i) RegisterITERATOR(a, sizeof(a)-1, b, c, d, e, f, g, h, i)
265 void RegisterITERATOR(const char *Name, long len, /* Our identifier */
266                       int AdditionalParams,       /* doe we use more parameters? */
267                       HashList *StaticList,       /* pointer to webcit lifetime hashlists */
268                       RetrieveHashlistFunc GetHash, /* else retrieve the hashlist by calling this function */
269                       SubTemplFunc DoSubTempl,       /* call this function on each iteration for svput & friends */
270                       HashDestructorFunc Destructor, /* use this function to shut down the hash; NULL if its a reference */
271                       int ContextType,               /* which context do we provide to the subtemplate? */
272                       int XPectContextType,          /* which context do we expct to be called in? */
273                       int Flags);
274
275
276
277
278
279
280 CompareFunc RetrieveSort(WCTemplputParams *TP, 
281                          const char *OtherPrefix, long OtherPrefixLen,  
282                          const char *Default, long ldefault, 
283                          long DefaultDirection);
284 void RegisterSortFunc(const char *name, long len, 
285                       const char *prepend, long preplen,
286                       CompareFunc Forward, 
287                       CompareFunc Reverse, 
288                       CompareFunc GroupChange, 
289                       long ContextType);
290
291
292
293
294 void dbg_print_longvector(long *LongVector);
295
296
297
298
299
300 #define do_template(a, b) DoTemplate(a, sizeof(a) -1, NULL, &NoCtx);
301 const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP);
302 void url_do_template(void);
303
304
305
306
307
308 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup);
309 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup);
310
311 void SVPut(char *keyname, size_t keylen, int keytype, char *Data);
312 #define svput(a, b, c) SVPut(a, sizeof(a) - 1, b, c)
313 void SVPutLong(char *keyname, size_t keylen, long Data);
314 #define svputlong(a, b) SVPutLong(a, sizeof(a) - 1, b)
315 void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...) __attribute__((__format__(__printf__,4,5)));
316 void SVPRINTF(char *keyname, int keytype, const char *format,...) __attribute__((__format__(__printf__,3,4)));
317 void SVCALLBACK(char *keyname, WCHandlerFunc fcn_ptr);
318 void SVCallback(char *keyname, size_t keylen,  WCHandlerFunc fcn_ptr);
319 #define svcallback(a, b) SVCallback(a, sizeof(a) - 1, b)
320
321 void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref);
322 #define SVPutBuf(a, b, c); SVPUTBuf(a, sizeof(a) - 1, b, c)