* iteratorstruct private 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 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
54 #define CTX_UNKNOWN 17
55
56
57 /**
58  * ContextFilter resembles our RTTI information. With this structure
59  * we can make shure a tmplput function can live with the environment
60  * we call it in.
61  * if not, we will log/print an error and refuse to call it.
62  */
63 typedef struct _contexts {
64         int ContextType;                /* do we require a User Context ? */
65         int ControlContextType;         /* are we inside of a control structure? */
66         int nMinArgs;                   /* How many arguments do we need at least? */
67         int nMaxArgs;                   /* up to how many arguments can we handle? */
68 } ContextFilter;
69
70
71 /* Forward declarations... */
72 typedef struct WCTemplateToken WCTemplateToken;
73 typedef struct WCTemplputParams WCTemplputParams;
74
75 /* this is the signature of a tmplput function */
76 typedef void (*WCHandlerFunc)(StrBuf *Target, WCTemplputParams *TP);
77
78 /* make a template token a lookup key: */
79 #define TKEY(a) TP->Tokens->Params[a]->Start, TP->Tokens->Params[a]->len
80
81 /* TODO: wcsubst should be private! */
82
83 /*
84  * \brief Dynamic content for variable substitution in templates
85  */
86 typedef struct _wcsubst {
87         ContextFilter Filter;
88         int wcs_type;                       /* which type of Substitution are we */
89         char wcs_key[32];                   /* copy of our hashkey for debugging */
90         StrBuf *wcs_value;                  /* if we're a string, keep it here */
91         long lvalue;                        /* type long? keep data here */
92         WCHandlerFunc wcs_function; /* funcion hook ???*/
93 } wcsubst;
94
95
96 /**
97  * this is the signature of a conditional function 
98  * Note: Target is just passed in for error messages; don't write onto it in regular cases.
99  */
100 typedef int (*WCConditionalFunc)(StrBuf *Target, WCTemplputParams *TP);
101
102
103 typedef struct _TemplateParam {
104         /* are we a string or a number? */
105         int Type;
106         /* string data: */
107         const char *Start;
108         long len;
109         /* if we're a number: */
110         long lvalue;
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
141         /* if we have parameters here we go: */
142         /* do we have parameters or not? */
143         int HaveParameters;
144         /* How many of them? */
145         int nParameters;
146         /* the parameters */
147         TemplateParam *Params[MAXPARAM];
148 };
149
150
151
152 struct WCTemplputParams {
153         ContextFilter Filter;
154         void *Context;
155         void *ControlContext;
156         int nArgs;
157         WCTemplateToken *Tokens;
158 };
159
160
161
162 typedef struct _ConditionalStruct {
163         ContextFilter Filter;
164         const char *PlainName;
165         WCConditionalFunc CondF;
166 } ConditionalStruct;
167
168
169 typedef void (*SubTemplFunc)(StrBuf *TemplBuffer, WCTemplputParams *TP);
170 typedef HashList *(*RetrieveHashlistFunc)(StrBuf *Target, WCTemplputParams *TP);
171 typedef void (*HashDestructorFunc) (HashList **KillMe);
172
173
174 extern WCTemplputParams NoCtx;
175
176 #define HAVE_PARAM(a) (TP->Tokens->nParameters > a)
177
178
179 #define ERR_NAME 0
180 #define ERR_PARM1 1
181 #define ERR_PARM2 2
182 /**
183  * \Brief log an error while evaluating a token; print it to the actual template 
184  * \param Target your Target Buffer to print the error message next to the log
185  * \param Type What sort of thing are we talking about? Tokens? Conditionals?
186  * \param TP grab our set of default information here
187  * \param Format for the custom error message
188  */ 
189 void LogTemplateError (StrBuf *Target, 
190                        const char *Type, 
191                        int ErrorPos, 
192                        WCTemplputParams *TP, 
193                        const char *Format, ...)__attribute__((__format__(__printf__,5,6)));
194
195 /**
196  * \Brief get the actual value of a token parameter
197  * in your tmplputs or conditionals use this function to access parameters that can also be 
198  * retrieved from dynamic facilities:
199  *  _ -> Gettext; retrieve this token from the i18n facilities
200  *  : -> lookup a setting of that name
201  *  B -> bstr; an URL-Parameter
202  *  = -> subtemplate; parse a template by this name, and treat its content as this tokens value 
203  * 
204  * \param N which token do you want to lookup?
205  * \param Value reference to the string of the token; don't free me.
206  * \param len the length of Value
207  */
208 void GetTemplateTokenString(StrBuf *Target, 
209                             WCTemplputParams *TP,
210                             int N,
211                             const char **Value, 
212                             long *len);
213
214
215
216 /**
217  * \Brief get the actual integer value of a token parameter
218  * in your tmplputs or conditionals use this function to access parameters that can also be 
219  * retrieved from dynamic facilities:
220  *  _ -> Gettext; retrieve this token from the i18n facilities
221  *  : -> lookup a setting of that name
222  *  B -> bstr; an URL-Parameter
223  *  = -> subtemplate; parse a template by this name, and treat its content as this tokens value 
224  * 
225  * \param N which token do you want to lookup?
226  * \param dflt default value to be retrieved if not found in preferences
227  * \returns the long value
228  */
229 long GetTemplateTokenNumber(StrBuf *Target, 
230                             WCTemplputParams *TP, 
231                             int N, long dflt);
232
233 /**
234  * \Brief put a token value into the template
235  * use this function to append your strings into a Template. 
236  * it can escape your string according to the token at FormattypeIndex:
237  *  H: de-QP and utf8-ify
238  *  X: escapize for HTML
239  *  J: JSON Escapize
240  * \param Target the destination buffer
241  * \param TP the template token information
242  * \param Source string to append
243  * \param FormatTypeIndex which parameter contains the escaping functionality?
244  *        if this token doesn't have as much parameters, plain append is done.
245  */
246 void StrBufAppendTemplate(StrBuf *Target, 
247                           WCTemplputParams *TP,
248                           const StrBuf *Source, 
249                           int FormatTypeIndex);
250
251
252 #define RegisterNamespace(a, b, c, d, e) RegisterNS(a, sizeof(a)-1, b, c, d, e)
253 void RegisterNS(const char *NSName, long len, 
254                 int nMinArgs, 
255                 int nMaxArgs, 
256                 WCHandlerFunc HandlerFunc,
257                 int ContextRequired);
258
259 void RegisterConditional(const char *Name, long len, 
260                          int nParams,
261                          WCConditionalFunc CondF, 
262                          int ContextRequired);
263
264
265
266 #define IT_NOFLAG 0
267 #define IT_FLAG_DETECT_GROUPCHANGE (1<<0)
268 #define RegisterIterator(a, b, c, d, e, f, g, h, i) RegisterITERATOR(a, sizeof(a)-1, b, c, d, e, f, g, h, i)
269 void RegisterITERATOR(const char *Name, long len, /* Our identifier */
270                       int AdditionalParams,       /* doe we use more parameters? */
271                       HashList *StaticList,       /* pointer to webcit lifetime hashlists */
272                       RetrieveHashlistFunc GetHash, /* else retrieve the hashlist by calling this function */
273                       SubTemplFunc DoSubTempl,       /* call this function on each iteration for svput & friends */
274                       HashDestructorFunc Destructor, /* use this function to shut down the hash; NULL if its a reference */
275                       int ContextType,               /* which context do we provide to the subtemplate? */
276                       int XPectContextType,          /* which context do we expct to be called in? */
277                       int Flags);
278
279
280
281
282
283
284 CompareFunc RetrieveSort(WCTemplputParams *TP, 
285                          const char *OtherPrefix, long OtherPrefixLen,  
286                          const char *Default, long ldefault, 
287                          long DefaultDirection);
288 void RegisterSortFunc(const char *name, long len, 
289                       const char *prepend, long preplen,
290                       CompareFunc Forward, 
291                       CompareFunc Reverse, 
292                       CompareFunc GroupChange, 
293                       long ContextType);
294
295
296
297
298 void dbg_print_longvector(long *LongVector);
299
300
301
302
303
304 #define do_template(a, b) DoTemplate(a, sizeof(a) -1, NULL, &NoCtx);
305 const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP);
306 void url_do_template(void);
307
308
309
310
311
312 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup);
313 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup);
314
315 void SVPut(char *keyname, size_t keylen, int keytype, char *Data);
316 #define svput(a, b, c) SVPut(a, sizeof(a) - 1, b, c)
317 void SVPutLong(char *keyname, size_t keylen, long Data);
318 #define svputlong(a, b) SVPutLong(a, sizeof(a) - 1, b)
319 void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...) __attribute__((__format__(__printf__,4,5)));
320 void SVPRINTF(char *keyname, int keytype, const char *format,...) __attribute__((__format__(__printf__,3,4)));
321 void SVCALLBACK(char *keyname, WCHandlerFunc fcn_ptr);
322 void SVCallback(char *keyname, size_t keylen,  WCHandlerFunc fcn_ptr);
323 #define svcallback(a, b) SVCallback(a, sizeof(a) - 1, b)
324
325 void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref);
326 #define SVPutBuf(a, b, c); SVPUTBuf(a, sizeof(a) - 1, b, c)