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
65 #define CTX_MAILQITEM 24
66 #define CTX_MAILQ_RCPT 25
69 #define CTX_UNKNOWN 27
74 * ContextFilter resembles our RTTI information. With this structure
75 * we can make shure a tmplput function can live with the environment
77 * if not, we will log/print an error and refuse to call it.
79 typedef struct _contexts {
80 int ContextType; /* do we require a User Context ? */
81 int ControlContextType; /* are we inside of a control structure? */
82 int nMinArgs; /* How many arguments do we need at least? */
83 int nMaxArgs; /* up to how many arguments can we handle? */
87 /* Forward declarations... */
88 typedef struct WCTemplateToken WCTemplateToken;
89 typedef struct WCTemplputParams WCTemplputParams;
91 /* this is the signature of a tmplput function */
92 typedef void (*WCHandlerFunc)(StrBuf *Target, WCTemplputParams *TP);
94 /* if you want to pre-evaluate parts of your token, or do additional syntax, use this. */
95 typedef int (*WCPreevalFunc)(WCTemplateToken *Token);
97 /* make a template token a lookup key: */
98 #define TKEY(a) TP->Tokens->Params[a]->Start, TP->Tokens->Params[a]->len
102 * this is the signature of a conditional function
103 * Note: Target is just passed in for error messages; don't write onto it in regular cases.
105 typedef int (*WCConditionalFunc)(StrBuf *Target, WCTemplputParams *TP);
107 typedef enum _eBitMask {
113 typedef struct _TemplateParam {
114 /* are we a string or a number? */
119 /* if we're a number: */
127 * Representation of a token; everything thats inbetween <? and >
129 struct WCTemplateToken {
130 /* Reference to the filename we're in to print error messages; not to be freed */
131 const StrBuf *FileName;
132 /* Raw copy of our original token; for error printing */
134 /* Which line did the template parser pick us up in? For error printing */
137 /* our position in the template cache buffer */
138 const char *pTokenStart;
139 /* our token length */
143 const char *pTokenEnd;
144 /* just our token name: */
148 /* stuff the pre-evaluater finds out: */
150 /* pointer to our runntime evaluator; so we can cache this and save hash-lookups */
154 /* if we have parameters here we go: */
155 /* do we have parameters or not? */
157 /* How many of them? */
160 TemplateParam *Params[MAXPARAM];
165 struct WCTemplputParams {
166 ContextFilter Filter;
168 void *ControlContext;
170 WCTemplateToken *Tokens;
175 typedef struct _ConditionalStruct {
176 ContextFilter Filter;
177 const char *PlainName;
178 WCConditionalFunc CondF;
182 typedef void (*SubTemplFunc)(StrBuf *TemplBuffer, WCTemplputParams *TP);
183 typedef HashList *(*RetrieveHashlistFunc)(StrBuf *Target, WCTemplputParams *TP);
184 typedef void (*HashDestructorFunc) (HashList **KillMe);
187 extern WCTemplputParams NoCtx;
189 #define HAVE_PARAM(a) (TP->Tokens->nParameters > a)
197 * @brief log an error while evaluating a token; print it to the actual template
198 * @param Target your Target Buffer to print the error message next to the log
199 * @param Type What sort of thing are we talking about? Tokens? Conditionals?
200 * @param TP grab our set of default information here
201 * @param Format for the custom error message
203 void LogTemplateError (StrBuf *Target,
206 WCTemplputParams *TP,
207 const char *Format, ...)__attribute__((__format__(__printf__,5,6)));
212 * @brief log an error while in global context; print it to Wildfire / Target
213 * @param Target your Target Buffer to print the error message next to the log
214 * @param Type What sort of thing are we talking about? Tokens? Conditionals?
215 * @param Format for the custom error message
217 void LogError (StrBuf *Target, const char *Type, const char *Format, ...);
221 * @brief get the actual value of a token parameter
222 * in your tmplputs or conditionals use this function to access parameters that can also be
223 * retrieved from dynamic facilities:
224 * _ -> Gettext; retrieve this token from the i18n facilities
225 * : -> lookup a setting of that name
226 * B -> bstr; an URL-Parameter
227 * = -> subtemplate; parse a template by this name, and treat its content as this tokens value
229 * @param N which token do you want to lookup?
230 * @param Value reference to the string of the token; don't free me.
231 * @param len the length of Value
233 void GetTemplateTokenString(StrBuf *Target,
234 WCTemplputParams *TP,
240 * @return whether @ref GetTemplateTokenString would be able to give you a string
242 int HaveTemplateTokenString(StrBuf *Target,
243 WCTemplputParams *TP,
252 * @brief get the actual integer value of a token parameter
253 * in your tmplputs or conditionals use this function to access parameters that can also be
254 * retrieved from dynamic facilities:
255 * _ -> Gettext; retrieve this token from the i18n facilities
256 * : -> lookup a setting of that name
257 * B -> bstr; an URL-Parameter
258 * = -> subtemplate; parse a template by this name, and treat its content as this tokens value
260 * @param N which token do you want to lookup?
261 * @param dflt default value to be retrieved if not found in preferences
262 * \returns the long value
264 long GetTemplateTokenNumber(StrBuf *Target,
265 WCTemplputParams *TP,
269 * @brief put a token value into the template
270 * use this function to append your strings into a Template.
271 * it can escape your string according to the token at FormattypeIndex:
272 * H: de-QP and utf8-ify
273 * X: escapize for HTML
275 * @param Target the destination buffer
276 * @param TP the template token information
277 * @param Source string to append
278 * @param FormatTypeIndex which parameter contains the escaping functionality?
279 * if this token doesn't have as much parameters, plain append is done.
281 void StrBufAppendTemplate(StrBuf *Target,
282 WCTemplputParams *TP,
283 const StrBuf *Source,
284 int FormatTypeIndex);
287 #define RegisterNamespace(a, b, c, d, e, f) RegisterNS(a, sizeof(a)-1, b, c, d, e, f)
290 * @brief register a template token handler
291 * call this function in your InitModule_MODULENAME which will be called at the server start
292 * @param nMinArgs how much parameters does your token require at least?
293 * @param nMaxArgs how many parameters does your token accept?
294 * @param HandlerFunc your callback when the template is rendered and your token is there
295 * @param PreEvalFunc is called when the template is parsed; you can do additional
296 * syntax checks here or pre-evaluate stuff for better performance
297 * @param ContextRequired if your token requires a specific context, else say CTX_NONE here.
299 void RegisterNS(const char *NSName, long len,
302 WCHandlerFunc HandlerFunc,
303 WCPreevalFunc PreEvalFunc,
304 int ContextRequired);
308 * @brief register a conditional token <pair> handler
309 * call this function in your InitModule_MODULENAME which will be called at the server start
310 * conditionals can be ? or ! with a pair or % similar to an implicit if
311 * @param Name whats the name of your conditional? should start with COND:
312 * @param len the token length so we don't have to measure it.
313 * @param nParams how many parameters does your conditional need on top of the default conditional parameters
314 * @param CondF your Callback to be called when the template is evaluated at runtime; return 0 or 1 to us please.
315 * @param ContextRequired if your token requires a specific context, else say CTX_NONE here.
317 void RegisterConditional(const char *Name, long len,
319 WCConditionalFunc CondF,
320 int ContextRequired);
324 * @brief register a string that will represent a long value
325 * this will allow to resolve <?...(#"Name")> to Value; that way
326 * plain strings can be used an lexed in templates without having the
327 * lookup overhead at runtime.
328 * @param Name The name of the define
329 * @param len length of Name
330 * @param Value the value to associate with Name
332 void RegisterTokenParamDefine(const char *Name, long len,
335 * teh r0x0r! forward your favourite define from C to the templates with one easy call!
337 #define REGISTERTokenParamDefine(a) RegisterTokenParamDefine(#a, sizeof(#a) - 1, a);
341 * @brief retrieve the long value of a registered string define
342 * @param Name The name of the define
343 * @param len length of Name
344 * @param Value the value to return if not found
346 long GetTokenDefine(const char *Name,
352 #define IT_FLAG_DETECT_GROUPCHANGE (1<<0)
353 #define RegisterIterator(a, b, c, d, e, f, g, h, i) RegisterITERATOR(a, sizeof(a)-1, b, c, d, e, f, g, h, i)
354 void RegisterITERATOR(const char *Name, long len, /* Our identifier */
355 int AdditionalParams, /* doe we use more parameters? */
356 HashList *StaticList, /* pointer to webcit lifetime hashlists */
357 RetrieveHashlistFunc GetHash, /* else retrieve the hashlist by calling this function */
358 SubTemplFunc DoSubTempl, /* call this function on each iteration for svput & friends */
359 HashDestructorFunc Destructor, /* use this function to shut down the hash; NULL if its a reference */
360 int ContextType, /* which context do we provide to the subtemplate? */
361 int XPectContextType, /* which context do we expct to be called in? */
369 CompareFunc RetrieveSort(WCTemplputParams *TP,
370 const char *OtherPrefix, long OtherPrefixLen,
371 const char *Default, long ldefault,
372 long DefaultDirection);
373 void RegisterSortFunc(const char *name, long len,
374 const char *prepend, long preplen,
377 CompareFunc GroupChange,
380 void dbg_print_longvector(long *LongVector);
382 #define do_template(a) DoTemplate(a, sizeof(a) -1, NULL, &NoCtx)
383 const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP);
384 void url_do_template(void);
387 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup);
388 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup);