2 * @defgroup subst Template processing functions
5 extern HashList *Conditionals;
6 extern HashList *GlobalNS;
7 extern HashList *Iterators;
8 extern HashList *WirelessTemplateCache;
9 extern HashList *WirelessLocalTemplateCache;
10 extern HashList *TemplateCache;
11 extern HashList *LocalTemplateCache;
16 #define TYPE_PREFSTR 3
17 #define TYPE_PREFINT 4
18 #define TYPE_GETTEXT 5
20 #define TYPE_SUBTEMPLATE 7
21 #define TYPE_INTDEFINE 8
24 #define IS_NUMBER(a) ((a == TYPE_LONG) || (a == TYPE_PREFINT) || (a == TYPE_INTDEFINE))
27 * \brief Values for wcs_type
30 WCS_STRING, /* its a string */
31 WCS_FUNCTION, /* its a function callback */
32 WCS_SERVCMD, /* its a command to send to the citadel server */
33 WCS_STRBUF, /* its a strbuf we own */
34 WCS_STRBUF_REF, /* its a strbuf we mustn't free */
35 WCS_LONG /* its an integer */
38 #define CTX TP->Context
39 #define CCTX TP->ControlContext
48 #define CTX_NODECONF 7
49 #define CTX_USERLIST 8
51 #define CTX_MIME_ATACH 10
52 #define CTX_FILELIST 11
54 #define CTX_STRBUFARR 13
55 #define CTX_LONGVECTOR 14
58 #define CTX_ITERATE 17
63 #define CTX_SIEVELIST 22
64 #define CTX_SIEVESCRIPT 23
66 #define CTX_UNKNOWN 24
71 * ContextFilter resembles our RTTI information. With this structure
72 * we can make shure a tmplput function can live with the environment
74 * if not, we will log/print an error and refuse to call it.
76 typedef struct _contexts {
77 int ContextType; /* do we require a User Context ? */
78 int ControlContextType; /* are we inside of a control structure? */
79 int nMinArgs; /* How many arguments do we need at least? */
80 int nMaxArgs; /* up to how many arguments can we handle? */
84 /* Forward declarations... */
85 typedef struct WCTemplateToken WCTemplateToken;
86 typedef struct WCTemplputParams WCTemplputParams;
88 /* this is the signature of a tmplput function */
89 typedef void (*WCHandlerFunc)(StrBuf *Target, WCTemplputParams *TP);
91 /* if you want to pre-evaluate parts of your token, or do additional syntax, use this. */
92 typedef int (*WCPreevalFunc)(WCTemplateToken *Token);
94 /* make a template token a lookup key: */
95 #define TKEY(a) TP->Tokens->Params[a]->Start, TP->Tokens->Params[a]->len
99 * this is the signature of a conditional function
100 * Note: Target is just passed in for error messages; don't write onto it in regular cases.
102 typedef int (*WCConditionalFunc)(StrBuf *Target, WCTemplputParams *TP);
104 typedef enum _eBitMask {
110 typedef struct _TemplateParam {
111 /* are we a string or a number? */
116 /* if we're a number: */
124 * Representation of a token; everything thats inbetween <? and >
126 struct WCTemplateToken {
127 /* Reference to the filename we're in to print error messages; not to be freed */
128 const StrBuf *FileName;
129 /* Raw copy of our original token; for error printing */
131 /* Which line did the template parser pick us up in? For error printing */
134 /* our position in the template cache buffer */
135 const char *pTokenStart;
136 /* our token length */
140 const char *pTokenEnd;
141 /* just our token name: */
145 /* stuff the pre-evaluater finds out: */
147 /* pointer to our runntime evaluator; so we can cache this and save hash-lookups */
151 /* if we have parameters here we go: */
152 /* do we have parameters or not? */
154 /* How many of them? */
157 TemplateParam *Params[MAXPARAM];
162 struct WCTemplputParams {
163 ContextFilter Filter;
165 void *ControlContext;
167 WCTemplateToken *Tokens;
172 typedef struct _ConditionalStruct {
173 ContextFilter Filter;
174 const char *PlainName;
175 WCConditionalFunc CondF;
179 typedef void (*SubTemplFunc)(StrBuf *TemplBuffer, WCTemplputParams *TP);
180 typedef HashList *(*RetrieveHashlistFunc)(StrBuf *Target, WCTemplputParams *TP);
181 typedef void (*HashDestructorFunc) (HashList **KillMe);
184 extern WCTemplputParams NoCtx;
186 #define HAVE_PARAM(a) (TP->Tokens->nParameters > a)
194 * @brief log an error while evaluating a token; print it to the actual template
195 * @param Target your Target Buffer to print the error message next to the log
196 * @param Type What sort of thing are we talking about? Tokens? Conditionals?
197 * @param TP grab our set of default information here
198 * @param Format for the custom error message
200 void LogTemplateError (StrBuf *Target,
203 WCTemplputParams *TP,
204 const char *Format, ...)__attribute__((__format__(__printf__,5,6)));
209 * @brief log an error while in global context; print it to Wildfire / Target
210 * @param Target your Target Buffer to print the error message next to the log
211 * @param Type What sort of thing are we talking about? Tokens? Conditionals?
212 * @param Format for the custom error message
214 void LogError (StrBuf *Target, const char *Type, const char *Format, ...);
218 * @brief get the actual value of a token parameter
219 * in your tmplputs or conditionals use this function to access parameters that can also be
220 * retrieved from dynamic facilities:
221 * _ -> Gettext; retrieve this token from the i18n facilities
222 * : -> lookup a setting of that name
223 * B -> bstr; an URL-Parameter
224 * = -> subtemplate; parse a template by this name, and treat its content as this tokens value
226 * @param N which token do you want to lookup?
227 * @param Value reference to the string of the token; don't free me.
228 * @param len the length of Value
230 void GetTemplateTokenString(StrBuf *Target,
231 WCTemplputParams *TP,
237 * @return whether @ref GetTemplateTokenString would be able to give you a string
239 int HaveTemplateTokenString(StrBuf *Target,
240 WCTemplputParams *TP,
249 * @brief get the actual integer value of a token parameter
250 * in your tmplputs or conditionals use this function to access parameters that can also be
251 * retrieved from dynamic facilities:
252 * _ -> Gettext; retrieve this token from the i18n facilities
253 * : -> lookup a setting of that name
254 * B -> bstr; an URL-Parameter
255 * = -> subtemplate; parse a template by this name, and treat its content as this tokens value
257 * @param N which token do you want to lookup?
258 * @param dflt default value to be retrieved if not found in preferences
259 * \returns the long value
261 long GetTemplateTokenNumber(StrBuf *Target,
262 WCTemplputParams *TP,
266 * @brief put a token value into the template
267 * use this function to append your strings into a Template.
268 * it can escape your string according to the token at FormattypeIndex:
269 * H: de-QP and utf8-ify
270 * X: escapize for HTML
272 * @param Target the destination buffer
273 * @param TP the template token information
274 * @param Source string to append
275 * @param FormatTypeIndex which parameter contains the escaping functionality?
276 * if this token doesn't have as much parameters, plain append is done.
278 void StrBufAppendTemplate(StrBuf *Target,
279 WCTemplputParams *TP,
280 const StrBuf *Source,
281 int FormatTypeIndex);
284 #define RegisterNamespace(a, b, c, d, e, f) RegisterNS(a, sizeof(a)-1, b, c, d, e, f)
287 * @brief register a template token handler
288 * call this function in your InitModule_MODULENAME which will be called at the server start
289 * @param nMinArgs how much parameters does your token require at least?
290 * @param nMaxArgs how many parameters does your token accept?
291 * @param HandlerFunc your callback when the template is rendered and your token is there
292 * @param PreEvalFunc is called when the template is parsed; you can do additional
293 * syntax checks here or pre-evaluate stuff for better performance
294 * @param ContextRequired if your token requires a specific context, else say CTX_NONE here.
296 void RegisterNS(const char *NSName, long len,
299 WCHandlerFunc HandlerFunc,
300 WCPreevalFunc PreEvalFunc,
301 int ContextRequired);
305 * @brief register a conditional token <pair> handler
306 * call this function in your InitModule_MODULENAME which will be called at the server start
307 * conditionals can be ? or ! with a pair or % similar to an implicit if
308 * @param Name whats the name of your conditional? should start with COND:
309 * @param len the token length so we don't have to measure it.
310 * @param nParams how many parameters does your conditional need on top of the default conditional parameters
311 * @param CondF your Callback to be called when the template is evaluated at runtime; return 0 or 1 to us please.
312 * @param ContextRequired if your token requires a specific context, else say CTX_NONE here.
314 void RegisterConditional(const char *Name, long len,
316 WCConditionalFunc CondF,
317 int ContextRequired);
321 * @brief register a string that will represent a long value
322 * this will allow to resolve <?...(#"Name")> to Value; that way
323 * plain strings can be used an lexed in templates without having the
324 * lookup overhead at runtime.
325 * @param Name The name of the define
326 * @param len length of Name
327 * @param Value the value to associate with Name
329 void RegisterTokenParamDefine(const char *Name, long len,
332 * teh r0x0r! forward your favourite define from C to the templates with one easy call!
334 #define REGISTERTokenParamDefine(a) RegisterTokenParamDefine(#a, sizeof(#a) - 1, a);
338 * @brief retrieve the long value of a registered string define
339 * @param Name The name of the define
340 * @param len length of Name
341 * @param Value the value to return if not found
343 long GetTokenDefine(const char *Name,
349 #define IT_FLAG_DETECT_GROUPCHANGE (1<<0)
350 #define RegisterIterator(a, b, c, d, e, f, g, h, i) RegisterITERATOR(a, sizeof(a)-1, b, c, d, e, f, g, h, i)
351 void RegisterITERATOR(const char *Name, long len, /* Our identifier */
352 int AdditionalParams, /* doe we use more parameters? */
353 HashList *StaticList, /* pointer to webcit lifetime hashlists */
354 RetrieveHashlistFunc GetHash, /* else retrieve the hashlist by calling this function */
355 SubTemplFunc DoSubTempl, /* call this function on each iteration for svput & friends */
356 HashDestructorFunc Destructor, /* use this function to shut down the hash; NULL if its a reference */
357 int ContextType, /* which context do we provide to the subtemplate? */
358 int XPectContextType, /* which context do we expct to be called in? */
366 CompareFunc RetrieveSort(WCTemplputParams *TP,
367 const char *OtherPrefix, long OtherPrefixLen,
368 const char *Default, long ldefault,
369 long DefaultDirection);
370 void RegisterSortFunc(const char *name, long len,
371 const char *prepend, long preplen,
374 CompareFunc GroupChange,
377 void dbg_print_longvector(long *LongVector);
379 #define do_template(a) DoTemplate(a, sizeof(a) -1, NULL, &NoCtx)
380 const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP);
381 void url_do_template(void);
384 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup);
385 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup);