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;
160 typedef struct _ConditionalStruct {
161 ContextFilter Filter;
162 const char *PlainName;
163 WCConditionalFunc CondF;
167 typedef void (*SubTemplFunc)(StrBuf *TemplBuffer, WCTemplputParams *TP);
168 typedef HashList *(*RetrieveHashlistFunc)(StrBuf *Target, WCTemplputParams *TP);
169 typedef void (*HashDestructorFunc) (HashList **KillMe);
172 extern WCTemplputParams NoCtx;
174 #define HAVE_PARAM(a) (TP->Tokens->nParameters > a)
182 * @brief log an error while evaluating a token; print it to the actual template
183 * @param Target your Target Buffer to print the error message next to the log
184 * @param Type What sort of thing are we talking about? Tokens? Conditionals?
185 * @param TP grab our set of default information here
186 * @param Format for the custom error message
188 void LogTemplateError (StrBuf *Target,
191 WCTemplputParams *TP,
192 const char *Format, ...)__attribute__((__format__(__printf__,5,6)));
197 * @brief log an error while in global context; print it to Wildfire / Target
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 Format for the custom error message
202 void LogError (StrBuf *Target, const char *Type, const char *Format, ...);
206 * @brief get the actual value of a token parameter
207 * in your tmplputs or conditionals use this function to access parameters that can also be
208 * retrieved from dynamic facilities:
209 * _ -> Gettext; retrieve this token from the i18n facilities
210 * : -> lookup a setting of that name
211 * B -> bstr; an URL-Parameter
212 * = -> subtemplate; parse a template by this name, and treat its content as this tokens value
214 * @param N which token do you want to lookup?
215 * @param Value reference to the string of the token; don't free me.
216 * @param len the length of Value
218 void GetTemplateTokenString(StrBuf *Target,
219 WCTemplputParams *TP,
225 * @return whether @ref GetTemplateTokenString would be able to give you a string
227 int HaveTemplateTokenString(StrBuf *Target,
228 WCTemplputParams *TP,
237 * @brief get the actual integer value of a token parameter
238 * in your tmplputs or conditionals use this function to access parameters that can also be
239 * retrieved from dynamic facilities:
240 * _ -> Gettext; retrieve this token from the i18n facilities
241 * : -> lookup a setting of that name
242 * B -> bstr; an URL-Parameter
243 * = -> subtemplate; parse a template by this name, and treat its content as this tokens value
245 * @param N which token do you want to lookup?
246 * @param dflt default value to be retrieved if not found in preferences
247 * \returns the long value
249 long GetTemplateTokenNumber(StrBuf *Target,
250 WCTemplputParams *TP,
254 * @brief put a token value into the template
255 * use this function to append your strings into a Template.
256 * it can escape your string according to the token at FormattypeIndex:
257 * H: de-QP and utf8-ify
258 * X: escapize for HTML
260 * @param Target the destination buffer
261 * @param TP the template token information
262 * @param Source string to append
263 * @param FormatTypeIndex which parameter contains the escaping functionality?
264 * if this token doesn't have as much parameters, plain append is done.
266 void StrBufAppendTemplate(StrBuf *Target,
267 WCTemplputParams *TP,
268 const StrBuf *Source,
269 int FormatTypeIndex);
272 #define RegisterNamespace(a, b, c, d, e, f) RegisterNS(a, sizeof(a)-1, b, c, d, e, f)
275 * @brief register a template token handler
276 * call this function in your InitModule_MODULENAME which will be called at the server start
277 * @param nMinArgs how much parameters does your token require at least?
278 * @param nMaxArgs how many parameters does your token accept?
279 * @param HandlerFunc your callback when the template is rendered and your token is there
280 * @param PreEvalFunc is called when the template is parsed; you can do additional
281 * syntax checks here or pre-evaluate stuff for better performance
282 * @param ContextRequired if your token requires a specific context, else say CTX_NONE here.
284 void RegisterNS(const char *NSName, long len,
287 WCHandlerFunc HandlerFunc,
288 WCPreevalFunc PreEvalFunc,
289 int ContextRequired);
293 * @brief register a conditional token <pair> handler
294 * call this function in your InitModule_MODULENAME which will be called at the server start
295 * conditionals can be ? or ! with a pair or % similar to an implicit if
296 * @param Name whats the name of your conditional? should start with COND:
297 * @param len the token length so we don't have to measure it.
298 * @param nParams how many parameters does your conditional need on top of the default conditional parameters
299 * @param CondF your Callback to be called when the template is evaluated at runtime; return 0 or 1 to us please.
300 * @param ContextRequired if your token requires a specific context, else say CTX_NONE here.
302 void RegisterConditional(const char *Name, long len,
304 WCConditionalFunc CondF,
305 int ContextRequired);
309 * @brief register a string that will represent a long value
310 * this will allow to resolve <?...(#"Name")> to Value; that way
311 * plain strings can be used an lexed in templates without having the
312 * lookup overhead at runtime.
313 * @param Name The name of the define
314 * @param len length of Name
315 * @param Value the value to associate with Name
317 void RegisterTokenParamDefine(const char *Name, long len,
320 * teh r0x0r! forward your favourite define from C to the templates with one easy call!
322 #define REGISTERTokenParamDefine(a) RegisterTokenParamDefine(#a, sizeof(#a) - 1, a);
326 * @brief retrieve the long value of a registered string define
327 * @param Name The name of the define
328 * @param len length of Name
329 * @param Value the value to return if not found
331 long GetTokenDefine(const char *Name,
337 #define IT_FLAG_DETECT_GROUPCHANGE (1<<0)
338 #define RegisterIterator(a, b, c, d, e, f, g, h, i) RegisterITERATOR(a, sizeof(a)-1, b, c, d, e, f, g, h, i)
339 void RegisterITERATOR(const char *Name, long len, /* Our identifier */
340 int AdditionalParams, /* doe we use more parameters? */
341 HashList *StaticList, /* pointer to webcit lifetime hashlists */
342 RetrieveHashlistFunc GetHash, /* else retrieve the hashlist by calling this function */
343 SubTemplFunc DoSubTempl, /* call this function on each iteration for svput & friends */
344 HashDestructorFunc Destructor, /* use this function to shut down the hash; NULL if its a reference */
345 CtxType ContextType, /* which context do we provide to the subtemplate? */
346 CtxType XPectContextType, /* which context do we expct to be called in? */
351 void StackContext(WCTemplputParams *Super,
352 WCTemplputParams *Sub,
356 WCTemplateToken *Tokens);
358 void UnStackContext(WCTemplputParams *Sub);
362 CompareFunc RetrieveSort(WCTemplputParams *TP,
363 const char *OtherPrefix, long OtherPrefixLen,
364 const char *Default, long ldefault,
365 long DefaultDirection);
366 void RegisterSortFunc(const char *name, long len,
367 const char *prepend, long preplen,
370 CompareFunc GroupChange,
371 CtxType ContextType);
373 void dbg_print_longvector(long *LongVector);
375 #define do_template(a) DoTemplate(a, sizeof(a) -1, NULL, &NoCtx)
376 const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP);
377 void url_do_template(void);
380 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup);
381 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup);