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