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_ROOMPREFSTR 4
18 #define TYPE_PREFINT 5
19 #define TYPE_GETTEXT 6
21 #define TYPE_SUBTEMPLATE 8
22 #define TYPE_INTDEFINE 9
25 #define IS_NUMBER(a) ((a == TYPE_LONG) || (a == TYPE_PREFINT) || (a == TYPE_INTDEFINE))
28 * \brief Values for wcs_type
31 WCS_STRING, /* its a string */
32 WCS_FUNCTION, /* its a function callback */
33 WCS_SERVCMD, /* its a command to send to the citadel server */
34 WCS_STRBUF, /* its a strbuf we own */
35 WCS_STRBUF_REF, /* its a strbuf we mustn't free */
36 WCS_LONG /* its an integer */
42 typedef struct __CtxTypeStruct {
47 CtxTypeStruct *GetContextType(CtxType Type);
48 void RegisterContextType(const char *name, long len, CtxType *TheCtx);
49 #define RegisterCTX(a) RegisterContextType(#a, sizeof(#a) - 1, &a)
51 extern CtxType CTX_STRBUF;
52 extern CtxType CTX_STRBUFARR;
53 extern CtxType CTX_LONGVECTOR;
57 * ContextFilter resembles our RTTI information. With this structure
58 * we can make shure a tmplput function can live with the environment
60 * if not, we will log/print an error and refuse to call it.
62 typedef struct _contexts {
63 CtxType ContextType; /* do we require a User Context ? */
64 int nMinArgs; /* How many arguments do we need at least? */
65 int nMaxArgs; /* up to how many arguments can we handle? */
69 /* Forward declarations... */
70 typedef struct WCTemplateToken WCTemplateToken;
71 typedef struct WCTemplputParams WCTemplputParams;
73 /* this is the signature of a tmplput function */
74 typedef void (*WCHandlerFunc)(StrBuf *Target, WCTemplputParams *TP);
76 /* if you want to pre-evaluate parts of your token, or do additional syntax, use this. */
77 typedef int (*WCPreevalFunc)(WCTemplateToken *Token);
79 /* make a template token a lookup key: */
80 #define TKEY(a) TP->Tokens->Params[a]->Start, TP->Tokens->Params[a]->len
82 void *GetContextPayload(WCTemplputParams *TP, CtxType ContextType);
83 #define CTX(a) GetContextPayload(TP, a)
87 * this is the signature of a conditional function
88 * Note: Target is just passed in for error messages; don't write onto it in regular cases.
90 typedef int (*WCConditionalFunc)(StrBuf *Target, WCTemplputParams *TP);
92 typedef enum _eBitMask {
98 typedef struct _TemplateParam {
99 /* are we a string or a number? */
104 /* if we're a number: */
112 * Representation of a token; everything thats inbetween <? and >
114 struct WCTemplateToken {
115 /* Reference to the filename we're in to print error messages; not to be freed */
116 const StrBuf *FileName;
117 /* Raw copy of our original token; for error printing */
119 /* Which line did the template parser pick us up in? For error printing */
122 /* our position in the template cache buffer */
123 const char *pTokenStart;
124 /* our token length */
128 const char *pTokenEnd;
129 /* just our token name: */
133 /* stuff the pre-evaluater finds out: */
135 /* pointer to our runntime evaluator; so we can cache this and save hash-lookups */
139 /* if we have parameters here we go: */
140 /* do we have parameters or not? */
142 /* How many of them? */
145 TemplateParam *Params[MAXPARAM];
150 struct WCTemplputParams {
151 ContextFilter Filter;
154 WCTemplateToken *Tokens;
155 WCTemplputParams *Sub, *Super;
156 WCConditionalFunc ExitCtx;
162 typedef struct _ConditionalStruct {
163 ContextFilter Filter;
164 const char *PlainName;
165 WCConditionalFunc CondF;
166 WCConditionalFunc CondExitCtx;
170 typedef void (*SubTemplFunc)(StrBuf *TemplBuffer, WCTemplputParams *TP);
171 typedef HashList *(*RetrieveHashlistFunc)(StrBuf *Target, WCTemplputParams *TP);
172 typedef void (*HashDestructorFunc) (HashList **KillMe);
175 extern WCTemplputParams NoCtx;
177 #define HAVE_PARAM(a) (TP->Tokens->nParameters > a)
185 * @brief log an error while evaluating a token; print it to the actual template
186 * @param Target your Target Buffer to print the error message next to the log
187 * @param Type What sort of thing are we talking about? Tokens? Conditionals?
188 * @param TP grab our set of default information here
189 * @param Format for the custom error message
191 void LogTemplateError (StrBuf *Target,
194 WCTemplputParams *TP,
195 const char *Format, ...)__attribute__((__format__(__printf__,5,6)));
200 * @brief log an error while in global context; print it to Wildfire / Target
201 * @param Target your Target Buffer to print the error message next to the log
202 * @param Type What sort of thing are we talking about? Tokens? Conditionals?
203 * @param Format for the custom error message
205 void LogError (StrBuf *Target, const char *Type, const char *Format, ...);
209 * @brief get the actual value of a token parameter
210 * in your tmplputs or conditionals use this function to access parameters that can also be
211 * retrieved from dynamic facilities:
212 * _ -> Gettext; retrieve this token from the i18n facilities
213 * : -> lookup a setting of that name
214 * B -> bstr; an URL-Parameter
215 * = -> subtemplate; parse a template by this name, and treat its content as this tokens value
217 * @param N which token do you want to lookup?
218 * @param Value reference to the string of the token; don't free me.
219 * @param len the length of Value
221 void GetTemplateTokenString(StrBuf *Target,
222 WCTemplputParams *TP,
228 * @return whether @ref GetTemplateTokenString would be able to give you a string
230 int HaveTemplateTokenString(StrBuf *Target,
231 WCTemplputParams *TP,
240 * @brief get the actual integer value of a token parameter
241 * in your tmplputs or conditionals use this function to access parameters that can also be
242 * retrieved from dynamic facilities:
243 * _ -> Gettext; retrieve this token from the i18n facilities
244 * : -> lookup a setting of that name
245 * B -> bstr; an URL-Parameter
246 * = -> subtemplate; parse a template by this name, and treat its content as this tokens value
248 * @param N which token do you want to lookup?
249 * @param dflt default value to be retrieved if not found in preferences
250 * \returns the long value
252 long GetTemplateTokenNumber(StrBuf *Target,
253 WCTemplputParams *TP,
257 * @brief put a token value into the template
258 * use this function to append your strings into a Template.
259 * it can escape your string according to the token at FormattypeIndex:
260 * H: de-QP and utf8-ify
261 * X: escapize for HTML
263 * @param Target the destination buffer
264 * @param TP the template token information
265 * @param Source string to append
266 * @param FormatTypeIndex which parameter contains the escaping functionality?
267 * if this token doesn't have as much parameters, plain append is done.
269 void StrBufAppendTemplate(StrBuf *Target,
270 WCTemplputParams *TP,
271 const StrBuf *Source,
272 int FormatTypeIndex);
274 void StrBufAppendTemplateStr(StrBuf *Target,
275 WCTemplputParams *TP,
277 int FormatTypeIndex);
280 #define RegisterNamespace(a, b, c, d, e, f) RegisterNS(a, sizeof(a)-1, b, c, d, e, f)
283 * @brief register a template token handler
284 * call this function in your InitModule_MODULENAME which will be called at the server start
285 * @param nMinArgs how much parameters does your token require at least?
286 * @param nMaxArgs how many parameters does your token accept?
287 * @param HandlerFunc your callback when the template is rendered and your token is there
288 * @param PreEvalFunc is called when the template is parsed; you can do additional
289 * syntax checks here or pre-evaluate stuff for better performance
290 * @param ContextRequired if your token requires a specific context, else say CTX_NONE here.
292 void RegisterNS(const char *NSName, long len,
295 WCHandlerFunc HandlerFunc,
296 WCPreevalFunc PreEvalFunc,
297 int ContextRequired);
301 * @brief register a conditional token <pair> handler
302 * call this function in your InitModule_MODULENAME which will be called at the server start
303 * conditionals can be ? or ! with a pair or % similar to an implicit if
304 * @param Name whats the name of your conditional? should start with COND:
305 * @param len the token length so we don't have to measure it.
306 * @param nParams how many parameters does your conditional need on top of the default conditional parameters
307 * @param CondF your Callback to be called when the template is evaluated at runtime; return 0 or 1 to us please.
308 * @param ExitCtxCond if non-NULL, will be called after the area of the conditional is left behind.
309 * @param ContextRequired if your token requires a specific context, else say CTX_NONE here.
311 void RegisterContextConditional(const char *Name, long len,
313 WCConditionalFunc CondF,
314 WCConditionalFunc ExitCtxCond,
315 int ContextRequired);
317 #define RegisterCtxConditional(Name, nParams, CondF, ExitCtxCond, ContextRequired) \
318 RegisterContextConditional(Name, sizeof(Name) -1, nParams, CondF, ExitCtxCond, ContextRequired)
320 #define RegisterConditional(Name, nParams, CondF, ContextRequired) \
321 RegisterContextConditional(Name, sizeof(Name) -1, nParams, CondF, NULL, ContextRequired)
326 * @brief register a string that will represent a long value
327 * this will allow to resolve <?...(#"Name")> to Value; that way
328 * plain strings can be used an lexed in templates without having the
329 * lookup overhead at runtime.
330 * @param Name The name of the define
331 * @param len length of Name
332 * @param Value the value to associate with Name
334 void RegisterTokenParamDefine(const char *Name, long len,
337 * teh r0x0r! forward your favourite define from C to the templates with one easy call!
339 #define REGISTERTokenParamDefine(a) RegisterTokenParamDefine(#a, sizeof(#a) - 1, a);
343 * @brief retrieve the long value of a registered string define
344 * @param Name The name of the define
345 * @param len length of Name
346 * @param Value the value to return if not found
348 long GetTokenDefine(const char *Name,
354 #define IT_FLAG_DETECT_GROUPCHANGE (1<<0)
355 #define RegisterIterator(a, b, c, d, e, f, g, h, i) RegisterITERATOR(a, sizeof(a)-1, b, c, d, e, f, g, h, i)
356 void RegisterITERATOR(const char *Name, long len, /* Our identifier */
357 int AdditionalParams, /* doe we use more parameters? */
358 HashList *StaticList, /* pointer to webcit lifetime hashlists */
359 RetrieveHashlistFunc GetHash, /* else retrieve the hashlist by calling this function */
360 SubTemplFunc DoSubTempl, /* call this function on each iteration for svput & friends */
361 HashDestructorFunc Destructor, /* use this function to shut down the hash; NULL if its a reference */
362 CtxType ContextType, /* which context do we provide to the subtemplate? */
363 CtxType XPectContextType, /* which context do we expct to be called in? */
368 void StackDynamicContext(WCTemplputParams *Super,
369 WCTemplputParams *Sub,
373 WCTemplateToken *Tokens,
374 WCConditionalFunc ExitCtx,
377 #define StackContext(Super, Sub, Context, ContextType, nArgs, Tokens) \
378 StackDynamicContext(Super, Sub, Context, ContextType, nArgs, Tokens, NULL, 0)
381 void UnStackContext(WCTemplputParams *Sub);
385 CompareFunc RetrieveSort(WCTemplputParams *TP,
386 const char *OtherPrefix, long OtherPrefixLen,
387 const char *Default, long ldefault,
388 long DefaultDirection);
389 void RegisterSortFunc(const char *name, long len,
390 const char *prepend, long preplen,
393 CompareFunc GroupChange,
394 CtxType ContextType);
396 void dbg_print_longvector(long *LongVector);
398 #define do_template(a) DoTemplate(a, sizeof(a) -1, NULL, &NoCtx)
399 const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP);
400 void url_do_template(void);
403 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup);
404 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup);