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
68 #define CTX_UNKNOWN 25
73 * ContextFilter resembles our RTTI information. With this structure
74 * we can make shure a tmplput function can live with the environment
76 * if not, we will log/print an error and refuse to call it.
78 typedef struct _contexts {
79 int ContextType; /* do we require a User Context ? */
80 int ControlContextType; /* are we inside of a control structure? */
81 int nMinArgs; /* How many arguments do we need at least? */
82 int nMaxArgs; /* up to how many arguments can we handle? */
86 /* Forward declarations... */
87 typedef struct WCTemplateToken WCTemplateToken;
88 typedef struct WCTemplputParams WCTemplputParams;
90 /* this is the signature of a tmplput function */
91 typedef void (*WCHandlerFunc)(StrBuf *Target, WCTemplputParams *TP);
93 /* if you want to pre-evaluate parts of your token, or do additional syntax, use this. */
94 typedef int (*WCPreevalFunc)(WCTemplateToken *Token);
96 /* make a template token a lookup key: */
97 #define TKEY(a) TP->Tokens->Params[a]->Start, TP->Tokens->Params[a]->len
101 * this is the signature of a conditional function
102 * Note: Target is just passed in for error messages; don't write onto it in regular cases.
104 typedef int (*WCConditionalFunc)(StrBuf *Target, WCTemplputParams *TP);
106 typedef enum _eBitMask {
112 typedef struct _TemplateParam {
113 /* are we a string or a number? */
118 /* if we're a number: */
126 * Representation of a token; everything thats inbetween <? and >
128 struct WCTemplateToken {
129 /* Reference to the filename we're in to print error messages; not to be freed */
130 const StrBuf *FileName;
131 /* Raw copy of our original token; for error printing */
133 /* Which line did the template parser pick us up in? For error printing */
136 /* our position in the template cache buffer */
137 const char *pTokenStart;
138 /* our token length */
142 const char *pTokenEnd;
143 /* just our token name: */
147 /* stuff the pre-evaluater finds out: */
149 /* pointer to our runntime evaluator; so we can cache this and save hash-lookups */
153 /* if we have parameters here we go: */
154 /* do we have parameters or not? */
156 /* How many of them? */
159 TemplateParam *Params[MAXPARAM];
164 struct WCTemplputParams {
165 ContextFilter Filter;
167 void *ControlContext;
169 WCTemplateToken *Tokens;
174 typedef struct _ConditionalStruct {
175 ContextFilter Filter;
176 const char *PlainName;
177 WCConditionalFunc CondF;
181 typedef void (*SubTemplFunc)(StrBuf *TemplBuffer, WCTemplputParams *TP);
182 typedef HashList *(*RetrieveHashlistFunc)(StrBuf *Target, WCTemplputParams *TP);
183 typedef void (*HashDestructorFunc) (HashList **KillMe);
186 extern WCTemplputParams NoCtx;
188 #define HAVE_PARAM(a) (TP->Tokens->nParameters > a)
196 * @brief log an error while evaluating a token; print it to the actual template
197 * @param Target your Target Buffer to print the error message next to the log
198 * @param Type What sort of thing are we talking about? Tokens? Conditionals?
199 * @param TP grab our set of default information here
200 * @param Format for the custom error message
202 void LogTemplateError (StrBuf *Target,
205 WCTemplputParams *TP,
206 const char *Format, ...)__attribute__((__format__(__printf__,5,6)));
211 * @brief log an error while in global context; print it to Wildfire / Target
212 * @param Target your Target Buffer to print the error message next to the log
213 * @param Type What sort of thing are we talking about? Tokens? Conditionals?
214 * @param Format for the custom error message
216 void LogError (StrBuf *Target, const char *Type, const char *Format, ...);
220 * @brief get the actual value of a token parameter
221 * in your tmplputs or conditionals use this function to access parameters that can also be
222 * retrieved from dynamic facilities:
223 * _ -> Gettext; retrieve this token from the i18n facilities
224 * : -> lookup a setting of that name
225 * B -> bstr; an URL-Parameter
226 * = -> subtemplate; parse a template by this name, and treat its content as this tokens value
228 * @param N which token do you want to lookup?
229 * @param Value reference to the string of the token; don't free me.
230 * @param len the length of Value
232 void GetTemplateTokenString(StrBuf *Target,
233 WCTemplputParams *TP,
239 * @return whether @ref GetTemplateTokenString would be able to give you a string
241 int HaveTemplateTokenString(StrBuf *Target,
242 WCTemplputParams *TP,
251 * @brief get the actual integer value of a token parameter
252 * in your tmplputs or conditionals use this function to access parameters that can also be
253 * retrieved from dynamic facilities:
254 * _ -> Gettext; retrieve this token from the i18n facilities
255 * : -> lookup a setting of that name
256 * B -> bstr; an URL-Parameter
257 * = -> subtemplate; parse a template by this name, and treat its content as this tokens value
259 * @param N which token do you want to lookup?
260 * @param dflt default value to be retrieved if not found in preferences
261 * \returns the long value
263 long GetTemplateTokenNumber(StrBuf *Target,
264 WCTemplputParams *TP,
268 * @brief put a token value into the template
269 * use this function to append your strings into a Template.
270 * it can escape your string according to the token at FormattypeIndex:
271 * H: de-QP and utf8-ify
272 * X: escapize for HTML
274 * @param Target the destination buffer
275 * @param TP the template token information
276 * @param Source string to append
277 * @param FormatTypeIndex which parameter contains the escaping functionality?
278 * if this token doesn't have as much parameters, plain append is done.
280 void StrBufAppendTemplate(StrBuf *Target,
281 WCTemplputParams *TP,
282 const StrBuf *Source,
283 int FormatTypeIndex);
286 #define RegisterNamespace(a, b, c, d, e, f) RegisterNS(a, sizeof(a)-1, b, c, d, e, f)
289 * @brief register a template token handler
290 * call this function in your InitModule_MODULENAME which will be called at the server start
291 * @param nMinArgs how much parameters does your token require at least?
292 * @param nMaxArgs how many parameters does your token accept?
293 * @param HandlerFunc your callback when the template is rendered and your token is there
294 * @param PreEvalFunc is called when the template is parsed; you can do additional
295 * syntax checks here or pre-evaluate stuff for better performance
296 * @param ContextRequired if your token requires a specific context, else say CTX_NONE here.
298 void RegisterNS(const char *NSName, long len,
301 WCHandlerFunc HandlerFunc,
302 WCPreevalFunc PreEvalFunc,
303 int ContextRequired);
307 * @brief register a conditional token <pair> handler
308 * call this function in your InitModule_MODULENAME which will be called at the server start
309 * conditionals can be ? or ! with a pair or % similar to an implicit if
310 * @param Name whats the name of your conditional? should start with COND:
311 * @param len the token length so we don't have to measure it.
312 * @param nParams how many parameters does your conditional need on top of the default conditional parameters
313 * @param CondF your Callback to be called when the template is evaluated at runtime; return 0 or 1 to us please.
314 * @param ContextRequired if your token requires a specific context, else say CTX_NONE here.
316 void RegisterConditional(const char *Name, long len,
318 WCConditionalFunc CondF,
319 int ContextRequired);
323 * @brief register a string that will represent a long value
324 * this will allow to resolve <?...(#"Name")> to Value; that way
325 * plain strings can be used an lexed in templates without having the
326 * lookup overhead at runtime.
327 * @param Name The name of the define
328 * @param len length of Name
329 * @param Value the value to associate with Name
331 void RegisterTokenParamDefine(const char *Name, long len,
334 * teh r0x0r! forward your favourite define from C to the templates with one easy call!
336 #define REGISTERTokenParamDefine(a) RegisterTokenParamDefine(#a, sizeof(#a) - 1, a);
340 * @brief retrieve the long value of a registered string define
341 * @param Name The name of the define
342 * @param len length of Name
343 * @param Value the value to return if not found
345 long GetTokenDefine(const char *Name,
351 #define IT_FLAG_DETECT_GROUPCHANGE (1<<0)
352 #define RegisterIterator(a, b, c, d, e, f, g, h, i) RegisterITERATOR(a, sizeof(a)-1, b, c, d, e, f, g, h, i)
353 void RegisterITERATOR(const char *Name, long len, /* Our identifier */
354 int AdditionalParams, /* doe we use more parameters? */
355 HashList *StaticList, /* pointer to webcit lifetime hashlists */
356 RetrieveHashlistFunc GetHash, /* else retrieve the hashlist by calling this function */
357 SubTemplFunc DoSubTempl, /* call this function on each iteration for svput & friends */
358 HashDestructorFunc Destructor, /* use this function to shut down the hash; NULL if its a reference */
359 int ContextType, /* which context do we provide to the subtemplate? */
360 int XPectContextType, /* which context do we expct to be called in? */
368 CompareFunc RetrieveSort(WCTemplputParams *TP,
369 const char *OtherPrefix, long OtherPrefixLen,
370 const char *Default, long ldefault,
371 long DefaultDirection);
372 void RegisterSortFunc(const char *name, long len,
373 const char *prepend, long preplen,
376 CompareFunc GroupChange,
379 void dbg_print_longvector(long *LongVector);
381 #define do_template(a) DoTemplate(a, sizeof(a) -1, NULL, &NoCtx)
382 const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP);
383 void url_do_template(void);
386 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup);
387 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup);