17 #define SHOW_ME_VAPPEND_PRINTF
20 #include "webserver.h"
22 extern char *static_dirs[PATH_MAX]; /* Disk representation */
24 HashList *WirelessTemplateCache;
25 HashList *WirelessLocalTemplateCache;
26 HashList *TemplateCache;
27 HashList *LocalTemplateCache;
31 HashList *Conditionals;
35 int DumpTemplateI18NStrings = 0;
36 int LoadTemplates = 0;
37 int dbg_bactrace_template_errors = 0;
38 WCTemplputParams NoCtx;
39 StrBuf *I18nDump = NULL;
41 const char EmptyStr[]="";
44 #define SV_CONDITIONAL 2
45 #define SV_NEG_CONDITIONAL 3
46 #define SV_CUST_STR_CONDITIONAL 4
48 #define SV_PREEVALUATED 6
52 * Dynamic content for variable substitution in templates
54 typedef struct _wcsubst {
56 int wcs_type; /* which type of Substitution are we */
57 char wcs_key[32]; /* copy of our hashkey for debugging */
58 StrBuf *wcs_value; /* if we're a string, keep it here */
59 long lvalue; /* type long? keep data here */
60 WCHandlerFunc wcs_function; /* funcion hook ???*/
64 typedef struct _WCTemplate {
70 WCTemplateToken **Tokens;
73 typedef struct _HashHandler {
75 WCPreevalFunc PreEvalFunc;
76 WCHandlerFunc HandlerFunc;
79 void *load_template(WCTemplate *NewTemplate);
80 int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP);
84 typedef struct _SortStruct {
89 CompareFunc GroupChange;
94 const char *CtxNames[] = {
105 "Context MIME_ATACH",
109 "Context LONGVECTOR",
114 "Context DavNamespace",
120 void DestroySortStruct(void *vSort)
122 SortStruct *Sort = (SortStruct*) vSort;
123 FreeStrBuf(&Sort->Name);
124 FreeStrBuf(&Sort->PrefPrepend);
128 const char *ContextName(int ContextType)
130 if (ContextType < CTX_UNKNOWN)
131 return CtxNames[ContextType];
133 return CtxNames[CTX_UNKNOWN];
136 void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplputParams *TP, const char *Format, ...)
143 const char *Err = NULL;
148 va_start(arg_ptr, Format);
149 StrBufVAppendPrintf(Error, Format, arg_ptr);
154 case ERR_NAME: /* the main token name... */
155 Err = (TP->Tokens!= NULL)? TP->Tokens->pName:"";
158 Err = ((TP->Tokens!= NULL) &&
159 (TP->Tokens->nParameters > 0))?
160 TP->Tokens->Params[0]->Start : "";
163 Err = ((TP->Tokens!= NULL) &&
164 (TP->Tokens->nParameters > 1))?
165 TP->Tokens->Params[1]->Start : "";
168 if (TP->Tokens != NULL)
170 lprintf(1, "%s [%s] (in '%s' line %ld); %s; [%s]\n",
173 ChrPtr(TP->Tokens->FileName),
176 ChrPtr(TP->Tokens->FlatToken));
180 lprintf(1, "%s: %s;\n",
195 Header = NewStrBuf();
196 if (TP->Tokens != NULL)
201 "<pre>\n%s [%s] (in '%s' line %ld); %s\n[%s]\n</pre>\n",
204 ChrPtr(TP->Tokens->FileName),
207 ChrPtr(TP->Tokens->FlatToken));
209 StrBufPrintf(Info, "%s [%s] %s; [%s]",
213 ChrPtr(TP->Tokens->FlatToken));
216 SerializeJson(Header, WildFireException(SKEY(TP->Tokens->FileName),
221 SerializeJson(Header, WildFireMessage(SKEY(TP->Tokens->FileName),
226 WildFireSerializePayload(Header, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
233 "<pre>\n%s: %s\n</pre>\n",
237 StrBufPrintf(Info, "%s [%s] %s; [%s]",
241 ChrPtr(TP->Tokens->FlatToken));
242 SerializeJson(Header, WildFireException(HKEY(__FILE__), __LINE__, Info, 1), 1);
243 WildFireSerializePayload(Header, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
249 if (dbg_bactrace_template_errors)
257 void LogError (StrBuf *Target, const char *Type, const char *Format, ...)
268 va_start(arg_ptr, Format);
269 StrBufVAppendPrintf(Error, Format, arg_ptr);
272 lprintf(1, ChrPtr(Error));
275 Header = NewStrBuf();
278 SerializeJson(Header, WildFireException(Type, strlen(Type),
282 WildFireSerializePayload(Header, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
288 if (dbg_bactrace_template_errors)
294 void RegisterNS(const char *NSName,
298 WCHandlerFunc HandlerFunc,
299 WCPreevalFunc PreevalFunc,
302 HashHandler *NewHandler;
304 NewHandler = (HashHandler*) malloc(sizeof(HashHandler));
305 memset(NewHandler, 0, sizeof(HashHandler));
306 NewHandler->Filter.nMinArgs = nMinArgs;
307 NewHandler->Filter.nMaxArgs = nMaxArgs;
308 NewHandler->Filter.ContextType = ContextRequired;
309 NewHandler->Filter.ControlContextType = CTX_NONE;
311 NewHandler->PreEvalFunc = PreevalFunc;
312 NewHandler->HandlerFunc = HandlerFunc;
313 Put(GlobalNS, NSName, len, NewHandler, NULL);
316 void RegisterControlNS(const char *NSName,
320 WCHandlerFunc HandlerFunc,
321 int ControlContextRequired)
323 HashHandler *NewHandler;
325 NewHandler = (HashHandler*) malloc(sizeof(HashHandler));
326 memset(NewHandler, 0, sizeof(HashHandler));
327 NewHandler->Filter.nMinArgs = nMinArgs;
328 NewHandler->Filter.nMaxArgs = nMaxArgs;
329 NewHandler->Filter.ContextType = CTX_NONE;
330 NewHandler->Filter.ControlContextType = ControlContextRequired;
331 NewHandler->HandlerFunc = HandlerFunc;
332 Put(GlobalNS, NSName, len, NewHandler, NULL);
337 int CheckContext(StrBuf *Target, ContextFilter *Need, WCTemplputParams *TP, const char *ErrType)
339 if ((Need->ContextType != CTX_NONE) &&
340 (Need->ContextType != TP->Filter.ContextType)) {
342 Target, ErrType, ERR_PARM1, TP,
343 " WARNING: requires Context: [%s], have [%s]!",
344 ContextName(Need->ContextType),
345 ContextName(TP->Filter.ContextType));
349 if ((Need->ControlContextType != CTX_NONE) &&
350 (Need->ControlContextType != TP->Filter.ControlContextType)) {
352 Target, ErrType, ERR_PARM1, TP,
353 " WARNING: requires Control Context: [%s], have [%s]!",
354 ContextName(Need->ControlContextType),
355 ContextName(TP->Filter.ControlContextType));
359 if (TP->Tokens->nParameters < Need->nMinArgs) {
360 LogTemplateError(Target, ErrType, ERR_NAME, TP,
361 "needs at least %ld params, have %ld",
363 TP->Tokens->nParameters);
367 else if (TP->Tokens->nParameters > Need->nMaxArgs) {
368 LogTemplateError(Target, ErrType, ERR_NAME, TP,
369 "just needs %ld params, you gave %ld",
371 TP->Tokens->nParameters);
379 void FreeToken(WCTemplateToken **Token)
382 FreeStrBuf(&(*Token)->FlatToken);
383 if ((*Token)->HaveParameters)
384 for (i = 0; i < (*Token)->nParameters; i++)
385 free((*Token)->Params[i]);
392 void FreeWCTemplate(void *vFreeMe)
395 WCTemplate *FreeMe = (WCTemplate*)vFreeMe;
397 if (FreeMe->TokenSpace > 0) {
398 for (i = 0; i < FreeMe->nTokensUsed; i ++) {
399 FreeToken(&FreeMe->Tokens[i]);
401 free(FreeMe->Tokens);
403 FreeStrBuf(&FreeMe->FileName);
404 FreeStrBuf(&FreeMe->Data);
405 FreeStrBuf(&FreeMe->MimeType);
411 * debugging function to print array to log
413 void VarPrintTransition(void *vVar1, void *vVar2, int odd){}
416 * debugging function to print array to log
418 void VarPrintEntry(const char *Key, void *vSubst, int odd)
421 lprintf(1,"Subst[%s] : ", Key);
422 ptr = (wcsubst*) vSubst;
424 switch(ptr->wcs_type) {
426 lprintf(1, " -> %s\n", ChrPtr(ptr->wcs_value));
429 lprintf(1, " -> Server [%s]\n", ChrPtr(ptr->wcs_value));
432 lprintf(1, " -> function at [%0xd]\n", ptr->wcs_function);
435 lprintf(1," WARNING: invalid type: [%ld]!\n", ptr->wcs_type);
442 * Clear out the list of substitution variables local to this session
444 void clear_substs(wcsession *wc) {
446 if (wc->vars != NULL) {
447 DeleteHash(&wc->vars);
452 * Clear out the list of substitution variables local to this session
454 void clear_local_substs(void) {
473 void FlushPayload(wcsubst *ptr, int reusestrbuf, int type)
475 int NeedNew = NeedNewBuf(type);
476 switch(ptr->wcs_type) {
480 if (reusestrbuf && NeedNew) {
481 FlushStrBuf(ptr->wcs_value);
485 FreeStrBuf(&ptr->wcs_value);
486 ptr->wcs_value = NULL;
490 ptr->wcs_function = NULL;
491 if (reusestrbuf && NeedNew)
492 ptr->wcs_value = NewStrBuf();
495 ptr->wcs_value = NULL;
496 if (reusestrbuf && NeedNew)
497 ptr->wcs_value = NewStrBuf();
501 if (reusestrbuf && NeedNew)
502 ptr->wcs_value = NewStrBuf();
505 if (reusestrbuf && NeedNew)
506 ptr->wcs_value = NewStrBuf();
513 * destructor; kill one entry.
515 void deletevar(void *data)
517 wcsubst *ptr = (wcsubst*)data;
518 FlushPayload(ptr, 0, ptr->wcs_type);
523 wcsubst *NewSubstVar(const char *keyname, int keylen, int type)
528 ptr = (wcsubst *) malloc(sizeof(wcsubst));
529 memset(ptr, 0, sizeof(wcsubst));
531 ptr->wcs_type = type;
532 safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
533 Put(WCC->vars, keyname, keylen, ptr, deletevar);
535 switch(ptr->wcs_type) {
538 ptr->wcs_value = NewStrBuf();
552 * Add a substitution variable (local to this session) (strlen version...)
553 * keyname the replacementstring to substitute
554 * keytype the kind of the key
555 * format the format string ala printf
556 * ... the arguments to substitute in the formatstring
558 void SVPRINTF(char *keyname, int keytype, const char *format,...)
566 keylen = strlen(keyname);
568 * First look if we're doing a replacement of
571 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
572 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
573 ptr = (wcsubst*)vPtr;
574 FlushPayload(ptr, keytype, keytype);
575 ptr->wcs_type = keytype;
577 else /** Otherwise allocate a new one */
579 ptr = NewSubstVar(keyname, keylen, keytype);
582 /* Format the string */
583 va_start(arg_ptr, format);
584 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
589 * Add a substitution variable (local to this session)
590 * keyname the replacementstring to substitute
591 * keytype the kind of the key
592 * format the format string ala printf
593 * ... the arguments to substitute in the formatstring
595 void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...)
603 * First look if we're doing a replacement of
606 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
607 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
608 ptr = (wcsubst*)vPtr;
609 FlushPayload(ptr, 1, keytype);
610 ptr->wcs_type = keytype;
612 else /** Otherwise allocate a new one */
614 ptr = NewSubstVar(keyname, keylen, keytype);
617 /** Format the string and save it */
618 va_start(arg_ptr, format);
619 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
624 * Add a substitution variable (local to this session)
625 * keyname the replacementstring to substitute
626 * keytype the kind of the key
627 * format the format string ala printf
628 * ... the arguments to substitute in the formatstring
630 void SVPut(char *keyname, size_t keylen, int keytype, char *Data)
638 * First look if we're doing a replacement of
641 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
642 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
643 ptr = (wcsubst*)vPtr;
644 FlushPayload(ptr, 1, keytype);
645 ptr->wcs_type = keytype;
647 else /** Otherwise allocate a new one */
649 ptr = NewSubstVar(keyname, keylen, keytype);
651 StrBufAppendBufPlain(ptr->wcs_value, Data, -1, 0);
655 * \brief Add a substitution variable (local to this session)
656 * \param keyname the replacementstring to substitute
657 * \param keytype the kind of the key
658 * \param format the format string ala printf
659 * \param ... the arguments to substitute in the formatstring
661 void SVPutLong(char *keyname, size_t keylen, long Data)
669 * First look if we're doing a replacement of
672 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
673 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
674 ptr = (wcsubst*)vPtr;
675 FlushPayload(ptr, 1, WCS_LONG);
676 ptr->wcs_type = WCS_LONG;
678 else /** Otherwise allocate a new one */
680 ptr = NewSubstVar(keyname, keylen, WCS_LONG);
686 * \brief Add a substitution variable (local to this session) that does a callback
687 * \param keyname the keystring to substitute
688 * \param fcn_ptr the function callback to give the substitution string
690 void SVCallback(char *keyname, size_t keylen, WCHandlerFunc fcn_ptr)
697 * First look if we're doing a replacement of
700 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
701 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
702 ptr = (wcsubst*)vPtr;
703 FlushPayload(ptr, 1, WCS_FUNCTION);
704 ptr->wcs_type = WCS_FUNCTION;
706 else /** Otherwise allocate a new one */
708 ptr = NewSubstVar(keyname, keylen, WCS_FUNCTION);
711 ptr->wcs_function = fcn_ptr;
713 inline void SVCALLBACK(char *keyname, WCHandlerFunc fcn_ptr)
715 SVCallback(keyname, strlen(keyname), fcn_ptr);
720 void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref)
727 * First look if we're doing a replacement of
730 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
731 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
732 ptr = (wcsubst*)vPtr;
733 FlushPayload(ptr, 0, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
734 ptr->wcs_type = (ref)?WCS_STRBUF_REF:WCS_STRBUF;
736 else /** Otherwise allocate a new one */
738 ptr = NewSubstVar(keyname, keylen, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
740 ptr->wcs_value = (StrBuf*)Buf;
744 * \brief back end for print_value_of() ... does a server command
745 * \param servcmd server command to execute on the citadel server
747 void pvo_do_cmd(StrBuf *Target, StrBuf *servcmd) {
751 serv_puts(ChrPtr(servcmd));
752 len = serv_getln(buf, sizeof buf);
758 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
761 _fmout(Target, "CENTER");
764 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
770 int HaveTemplateTokenString(StrBuf *Target,
771 WCTemplputParams *TP,
776 if (N >= TP->Tokens->nParameters) {
780 switch (TP->Tokens->Params[N]->Type) {
786 case TYPE_SUBTEMPLATE:
795 void GetTemplateTokenString(StrBuf *Target,
796 WCTemplputParams *TP,
802 WCTemplputParams SubTP;
804 if (N >= TP->Tokens->nParameters) {
805 LogTemplateError(Target,
806 "TokenParameter", N, TP,
807 "invalid token %d. this shouldn't have come till here.\n", N);
813 switch (TP->Tokens->Params[N]->Type) {
817 *Value = TP->Tokens->Params[N]->Start;
818 *len = TP->Tokens->Params[N]->len;
821 if (TP->Tokens->Params[N]->len == 0) {
822 LogTemplateError(Target,
823 "TokenParameter", N, TP,
824 "Requesting parameter %d; of type BSTR, empty lookup string not admitted.", N);
829 Buf = (StrBuf*) SBstr(TKEY(N));
830 *Value = ChrPtr(Buf);
831 *len = StrLength(Buf);
834 if (TP->Tokens->Params[N]->len == 0) {
835 LogTemplateError(Target,
836 "TokenParameter", N, TP,
837 "Requesting parameter %d; of type PREFSTR, empty lookup string not admitted.", N);
842 get_PREFERENCE(TKEY(N), &Buf);
843 *Value = ChrPtr(Buf);
844 *len = StrLength(Buf);
847 LogTemplateError(Target,
848 "TokenParameter", N, TP,
849 "Requesting parameter %d; of type LONG, want string.", N);
852 LogTemplateError(Target,
853 "TokenParameter", N, TP,
854 "Requesting parameter %d; of type PREFINT, want string.", N);
857 *Value = _(TP->Tokens->Params[N]->Start);
858 *len = strlen(*Value);
860 case TYPE_SUBTEMPLATE:
861 if (TP->Tokens->Params[N]->len == 0) {
862 LogTemplateError(Target,
863 "TokenParameter", N, TP,
864 "Requesting parameter %d; of type SUBTEMPLATE, empty lookup string not admitted.", N);
870 memset(&SubTP, 0, sizeof(WCTemplputParams *));
871 SubTP.Context = TP->Context;
872 SubTP.Filter.ContextType = TP->Filter.ContextType;
874 DoTemplate(TKEY(N), Buf, &SubTP);
875 *Value = ChrPtr(Buf);
876 *len = StrLength(Buf);
877 /* we can't free it here, so we put it into the subst so its discarded later on. */
878 SVPUTBuf(TKEY(N), Buf, 0);
882 LogTemplateError(Target,
883 "TokenParameter", N, TP,
884 "unknown param type %d; [%d]", N, TP->Tokens->Params[N]->Type);
889 long GetTemplateTokenNumber(StrBuf *Target, WCTemplputParams *TP, int N, long dflt)
892 if (N >= TP->Tokens->nParameters) {
893 LogTemplateError(Target,
894 "TokenParameter", N, TP,
895 "invalid token %d. this shouldn't have come till here.\n", N);
900 switch (TP->Tokens->Params[N]->Type) {
903 return atol(TP->Tokens->Params[N]->Start);
906 if (TP->Tokens->Params[N]->len == 0) {
907 LogTemplateError(Target,
908 "TokenParameter", N, TP,
909 "Requesting parameter %d; of type BSTR, empty lookup string not admitted.", N);
912 return LBstr(TKEY(N));
915 LogTemplateError(Target,
916 "TokenParameter", N, TP,
917 "requesting a prefstring in param %d want a number", N);
918 if (TP->Tokens->Params[N]->len == 0) {
919 LogTemplateError(Target,
920 "TokenParameter", N, TP,
921 "Requesting parameter %d; of type PREFSTR, empty lookup string not admitted.", N);
924 if (get_PREF_LONG(TKEY(N), &Ret, dflt))
929 return TP->Tokens->Params[N]->lvalue;
931 if (TP->Tokens->Params[N]->len == 0) {
932 LogTemplateError(Target,
933 "TokenParameter", N, TP,
934 "Requesting parameter %d; of type PREFINT, empty lookup string not admitted.", N);
937 if (get_PREF_LONG(TKEY(N), &Ret, dflt))
941 LogTemplateError(Target,
942 "TokenParameter", N, TP,
943 "requesting a I18N string in param %d; want a number", N);
945 case TYPE_SUBTEMPLATE:
946 LogTemplateError(Target,
947 "TokenParameter", N, TP,
948 "requesting a subtemplate in param %d; not supported for numbers", N);
951 LogTemplateError(Target,
952 "TokenParameter", N, TP,
953 "unknown param type %d; [%d]", N, TP->Tokens->Params[N]->Type);
961 * \brief Print the value of a variable
962 * \param keyname get a key to print
964 void print_value_of(StrBuf *Target, WCTemplputParams *TP)
971 /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
972 /* TODO: depricated! */
973 if (TP->Tokens->pName[0] == '=') {
974 DoTemplate(TP->Tokens->pName+1, TP->Tokens->NameEnd - 1, NULL, &NoCtx);
976 /*/////TODO: if param[1] == "U" -> urlescape
978 /** Page-local variables */
979 if ((WCC->vars!= NULL) && GetHash(WCC->vars, TP->Tokens->pName, TP->Tokens->NameEnd, &vVar)) {
980 ptr = (wcsubst*) vVar;
981 switch(ptr->wcs_type) {
983 StrBufAppendBuf(Target, ptr->wcs_value, 0);
986 pvo_do_cmd(Target, ptr->wcs_value);
989 (*ptr->wcs_function) (Target, TP);
993 StrBufAppendBuf(Target, ptr->wcs_value, 0);
996 StrBufAppendPrintf(Target, "%ld", ptr->lvalue);
1000 Target, "Subst", ERR_NAME, TP,
1001 "WARNING: invalid value in SV-Hash at %s!", TP->Tokens->pName);
1006 Target, "Token", ERR_NAME, TP,
1007 "didn't find Handler \"%s\"", TP->Tokens->pName);
1012 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup)
1014 wcsession *WCC = WC;
1018 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
1019 ParamToLookup->len, &vVar)) {
1020 ptr = (wcsubst*) vVar;
1021 switch(ptr->wcs_type) {
1024 case WCS_STRBUF_REF:
1025 if (ParamToCompare->Type == TYPE_STR)
1026 return ((ParamToCompare->len == StrLength(ptr->wcs_value)) &&
1027 (strcmp(ParamToCompare->Start, ChrPtr(ptr->wcs_value)) == 0));
1029 return ParamToCompare->lvalue == StrTol(ptr->wcs_value);
1037 if (ParamToCompare->Type == TYPE_STR)
1040 return ParamToCompare->lvalue == ptr->lvalue;
1043 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
1044 ParamToLookup->Start);
1050 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup)
1052 wcsession *WCC = WC;
1056 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
1057 ParamToLookup->len, &vVar)) {
1058 ptr = (wcsubst*) vVar;
1059 switch(ptr->wcs_type) {
1062 case WCS_STRBUF_REF:
1063 return ((StrLength(Compare) == StrLength(ptr->wcs_value)) &&
1064 (strcmp(ChrPtr(Compare), ChrPtr(ptr->wcs_value)) == 0));
1071 return StrTol(Compare) == ptr->lvalue;
1073 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
1074 ParamToLookup->Start);
1083 * \brief puts string into the template and computes which escape methon we should use
1084 * \param Source the string we should put into the template
1085 * \param FormatTypeIndex where should we look for escape types if?
1087 void StrBufAppendTemplate(StrBuf *Target,
1088 WCTemplputParams *TP,
1089 const StrBuf *Source, int FormatTypeIndex)
1092 char EscapeAs = ' ';
1094 if ((FormatTypeIndex < TP->Tokens->nParameters) &&
1095 (TP->Tokens->Params[FormatTypeIndex]->Type == TYPE_STR) &&
1096 (TP->Tokens->Params[FormatTypeIndex]->len == 1)) {
1097 EscapeAs = *TP->Tokens->Params[FormatTypeIndex]->Start;
1104 StrEscAppend(Target, Source, NULL, 0, 2);
1107 StrEscAppend(Target, Source, NULL, 0, 0);
1110 StrECMAEscAppend(Target, Source, NULL);
1113 StrHtmlEcmaEscAppend(Target, Source, NULL, 0, 0);
1116 StrBufUrlescAppend(Target, Source, NULL);
1119 StrBufAppendBuf(Target, Source, 0);
1124 void PutNewToken(WCTemplate *Template, WCTemplateToken *NewToken)
1126 if (Template->nTokensUsed + 1 >= Template->TokenSpace) {
1127 if (Template->TokenSpace <= 0) {
1128 Template->Tokens = (WCTemplateToken**)malloc(
1129 sizeof(WCTemplateToken*) * 10);
1130 memset(Template->Tokens, 0, sizeof(WCTemplateToken*) * 10);
1131 Template->TokenSpace = 10;
1134 WCTemplateToken **NewTokens;
1136 NewTokens= (WCTemplateToken**) malloc(
1137 sizeof(WCTemplateToken*) * Template->TokenSpace * 2);
1140 0, sizeof(WCTemplateToken*) * Template->TokenSpace * 2);
1144 sizeof(WCTemplateToken*) * Template->nTokensUsed);
1146 free(Template->Tokens);
1147 Template->TokenSpace *= 2;
1148 Template->Tokens = NewTokens;
1151 Template->Tokens[(Template->nTokensUsed)++] = NewToken;
1154 TemplateParam *GetNextParameter(StrBuf *Buf,
1157 WCTemplateToken *Tokens,
1159 WCTemplputParams *TP)
1161 const char *pch = *pCh;
1162 const char *pchs, *pche;
1163 TemplateParam *Parm;
1167 Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
1168 memset(Parm, 0, sizeof(TemplateParam));
1169 Parm->Type = TYPE_STR;
1171 /* Skip leading whitespaces */
1172 while ((*pch == ' ' )||
1175 (*pch == '\n')) pch ++;
1178 Parm->Type = TYPE_PREFSTR;
1185 else if (*pch == ';') {
1186 Parm->Type = TYPE_PREFINT;
1193 else if (*pch == '#') {
1194 Parm->Type = TYPE_INTDEFINE;
1197 else if (*pch == '_') {
1198 Parm->Type = TYPE_GETTEXT;
1205 else if (*pch == 'B') {
1206 Parm->Type = TYPE_BSTR;
1213 else if (*pch == '=') {
1214 Parm->Type = TYPE_SUBTEMPLATE;
1225 else if (*pch == '\'')
1227 if (quote != '\0') {
1232 ( (pch > pchs) && (*(pch - 1) == '\\'))
1237 if (*pch != quote) {
1238 lprintf(1, "Error (in '%s' line %ld); "
1239 "evaluating template param [%s] in Token [%s]\n",
1240 ChrPtr(pTmpl->FileName),
1242 ChrPtr(Tokens->FlatToken),
1249 StrBufPeek(Buf, pch, -1, '\0');
1250 if (LoadTemplates > 1) {
1251 lprintf(1, "DBG: got param [%s] %ld %ld\n",
1252 pchs, pche - pchs, strlen(pchs));
1255 Parm->len = pche - pchs;
1256 pch ++; /* move after trailing quote */
1257 if (ParamBrace && (*pch == ')')) {
1264 Parm->Type = TYPE_LONG;
1266 while ((pch <= pe) &&
1272 if (pch - pchs > 1){
1273 StrBufPeek(Buf, pch, -1, '\0');
1274 Parm->lvalue = atol(pchs);
1281 lprintf(1, "Error (in '%s' line %ld); "
1282 "evaluating long template param [%s] in Token [%s]\n",
1283 ChrPtr(pTmpl->FileName),
1285 ChrPtr(Tokens->FlatToken),
1292 while ((*pch == ' ' )||
1296 (*pch == '\n')) pch ++;
1301 if (DumpTemplateI18NStrings) {
1302 StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", Parm->Start);
1305 case TYPE_INTDEFINE: {
1308 if (GetHash(Defines, Parm->Start, Parm->len, &vPVal) &&
1312 PVal = (long*) vPVal;
1314 Parm->lvalue = *PVal;
1318 LogTemplateError(NULL, "Define", ERR_PARM1, TP,
1323 case TYPE_SUBTEMPLATE:{
1325 /* well, we don't check the mobile stuff here... */
1326 if (!GetHash(LocalTemplateCache, Parm->Start, Parm->len, &vTmpl) &&
1327 !GetHash(TemplateCache, Parm->Start, Parm->len, &vTmpl)) {
1329 NULL, "SubTemplate", ERR_PARM1, TP,
1330 "referenced here doesn't exist");
1338 WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
1340 const char *pTokenStart,
1341 const char *pTokenEnd,
1347 TemplateParam *Param;
1348 WCTemplateToken *NewToken;
1349 WCTemplputParams TP;
1351 NewToken = (WCTemplateToken*)malloc(sizeof(WCTemplateToken));
1352 memset(NewToken, 0, sizeof(WCTemplateToken));
1353 TP.Tokens = NewToken;
1354 NewToken->FileName = pTmpl->FileName; /* to print meaningfull log messages... */
1355 NewToken->Flags = 0;
1356 NewToken->Line = Line + 1;
1357 NewToken->pTokenStart = pTokenStart;
1358 NewToken->TokenStart = pTokenStart - pStart;
1359 NewToken->TokenEnd = (pTokenEnd - pStart) - NewToken->TokenStart;
1360 NewToken->pTokenEnd = pTokenEnd;
1361 NewToken->NameEnd = NewToken->TokenEnd - 2;
1362 NewToken->PreEval = NULL;
1363 NewToken->FlatToken = NewStrBufPlain(pTokenStart + 2, pTokenEnd - pTokenStart - 2);
1364 StrBufShrinkToFit(NewToken->FlatToken, 1);
1366 StrBufPeek(Buf, pTokenStart, + 1, '\0');
1367 StrBufPeek(Buf, pTokenEnd, -1, '\0');
1368 pch = NewToken->pName = pTokenStart + 2;
1370 NewToken->HaveParameters = 0;;
1371 NewToken->nParameters = 0;
1373 while (pch < pTokenEnd - 1) {
1375 StrBufPeek(Buf, pch, -1, '\0');
1376 NewToken->NameEnd = pch - NewToken->pName;
1378 if (*(pTokenEnd - 1) != ')') {
1380 NULL, "Parseerror", ERR_NAME, &TP,
1381 "Warning, Non welformed Token; missing right parenthesis");
1383 while (pch < pTokenEnd - 1) {
1384 Param = GetNextParameter(Buf, &pch, pTokenEnd - 1, NewToken, pTmpl, &TP);
1385 if (Param != NULL) {
1386 NewToken->HaveParameters = 1;
1387 if (NewToken->nParameters > MAXPARAM) {
1389 NULL, "Parseerror", ERR_NAME, &TP,
1390 "only [%d] Params allowed in Tokens",
1394 FreeToken(&NewToken);
1397 NewToken->Params[NewToken->nParameters++] = Param;
1401 if((NewToken->NameEnd == 1) &&
1402 (NewToken->HaveParameters == 1))
1405 if (*(NewToken->pName) == '_')
1406 NewToken->Flags = SV_GETTEXT;
1407 else if (*(NewToken->pName) == '=')
1408 NewToken->Flags = SV_SUBTEMPL;
1409 else if (*(NewToken->pName) == '%')
1410 NewToken->Flags = SV_CUST_STR_CONDITIONAL;
1411 else if (*(NewToken->pName) == '?')
1412 NewToken->Flags = SV_CONDITIONAL;
1413 else if (*(NewToken->pName) == '!')
1414 NewToken->Flags = SV_NEG_CONDITIONAL;
1420 switch (NewToken->Flags) {
1422 /* If we're able to find out more about the token, do it now while its fresh. */
1423 if (GetHash(GlobalNS, NewToken->pName, NewToken->NameEnd, &vVar)) {
1424 HashHandler *Handler;
1425 Handler = (HashHandler*) vVar;
1426 if ((NewToken->nParameters < Handler->Filter.nMinArgs) ||
1427 (NewToken->nParameters > Handler->Filter.nMaxArgs)) {
1429 NULL, "Token", ERR_NAME, &TP,
1430 "doesn't work with %d params",
1431 NewToken->nParameters);
1435 NewToken->PreEval = Handler;
1436 NewToken->Flags = SV_PREEVALUATED;
1437 if (Handler->PreEvalFunc != NULL)
1438 Handler->PreEvalFunc(NewToken);
1443 if (NewToken->nParameters !=1) {
1445 NULL, "Gettext", ERR_NAME, &TP,
1446 "requires exactly 1 parameter, you gave %d params",
1447 NewToken->nParameters);
1448 NewToken->Flags = 0;
1451 if (DumpTemplateI18NStrings) {
1452 StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", NewToken->Params[0]->Start);
1456 if (NewToken->nParameters != 1) {
1458 NULL, "Subtemplates", ERR_NAME, &TP,
1459 "require exactly 1 parameter, you gave %d params",
1460 NewToken->nParameters);
1465 /* well, we don't check the mobile stuff here... */
1466 if (!GetHash(LocalTemplateCache,
1467 NewToken->Params[0]->Start,
1468 NewToken->Params[0]->len,
1470 !GetHash(TemplateCache,
1471 NewToken->Params[0]->Start,
1472 NewToken->Params[0]->len,
1475 NULL, "SubTemplate", ERR_PARM1, &TP,
1480 case SV_CUST_STR_CONDITIONAL:
1481 case SV_CONDITIONAL:
1482 case SV_NEG_CONDITIONAL:
1483 if (NewToken->nParameters <2) {
1485 NULL, "Conditional", ERR_PARM1, &TP,
1486 "require at least 2 parameters, you gave %d params",
1487 NewToken->nParameters);
1488 NewToken->Flags = 0;
1491 if (NewToken->Params[1]->lvalue == 0) {
1493 NULL, "Conditional", ERR_PARM1, &TP,
1494 "Conditional ID (Parameter 1) mustn't be 0!");
1495 NewToken->Flags = 0;
1498 if (!GetHash(Conditionals,
1499 NewToken->Params[0]->Start,
1500 NewToken->Params[0]->len,
1503 if ((NewToken->Params[0]->len == 1) &&
1504 (NewToken->Params[0]->Start[0] == 'X'))
1507 NULL, "Conditional", ERR_PARM1, &TP,
1510 NewToken->Error = NewStrBuf();
1513 "<pre>\nConditional [%s] (in '%s' line %ld); Not found!\n[%s]\n</pre>\n",
1514 NewToken->Params[0]->Start,
1515 ChrPtr(pTmpl->FileName),
1517 ChrPtr(NewToken->FlatToken));
1521 NewToken->PreEval = vVar;
1533 * \brief Display a variable-substituted template
1534 * \param templatename template file to load
1536 void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1538 WCTemplate *NewTemplate;
1540 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1541 memset(NewTemplate, 0, sizeof(WCTemplate));
1542 NewTemplate->Data = NULL;
1543 NewTemplate->FileName = NewStrBufDup(filename);
1544 StrBufShrinkToFit(NewTemplate->FileName, 1);
1545 NewTemplate->nTokensUsed = 0;
1546 NewTemplate->TokenSpace = 0;
1547 NewTemplate->Tokens = NULL;
1548 NewTemplate->MimeType = NewStrBufPlain(GuessMimeByFilename (SKEY(NewTemplate->FileName)), -1);
1549 if (strstr(ChrPtr(NewTemplate->MimeType), "text") != NULL) {
1550 StrBufAppendBufPlain(NewTemplate->MimeType, HKEY("; charset=utf-8"), 0);
1553 if (strstr(ChrPtr(NewTemplate->MimeType), "text") != NULL) {
1554 StrBufAppendBufPlain(NewTemplate->MimeType, HKEY("; charset=utf-8"), 0);
1557 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1562 * \brief Display a variable-substituted template
1563 * \param templatename template file to load
1565 void *duplicate_template(WCTemplate *OldTemplate)
1567 WCTemplate *NewTemplate;
1569 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1570 memset(NewTemplate, 0, sizeof(WCTemplate));
1571 NewTemplate->Data = NULL;
1572 NewTemplate->FileName = NewStrBufDup(OldTemplate->FileName);
1573 StrBufShrinkToFit(NewTemplate->FileName, 1);
1574 NewTemplate->nTokensUsed = 0;
1575 NewTemplate->TokenSpace = 0;
1576 NewTemplate->Tokens = NULL;
1577 NewTemplate->MimeType = NewStrBufDup(OldTemplate->MimeType);
1582 * \brief Display a variable-substituted template
1583 * \param templatename template file to load
1585 void *load_template(WCTemplate *NewTemplate)
1588 struct stat statbuf;
1589 const char *pS, *pE, *pch, *Err;
1593 fd = open(ChrPtr(NewTemplate->FileName), O_RDONLY);
1595 lprintf(1, "ERROR: could not open template '%s' - %s\n",
1596 ChrPtr(NewTemplate->FileName), strerror(errno));
1600 if (fstat(fd, &statbuf) == -1) {
1601 lprintf(1, "ERROR: could not stat template '%s' - %s\n",
1602 ChrPtr(NewTemplate->FileName), strerror(errno));
1606 NewTemplate->Data = NewStrBufPlain(NULL, statbuf.st_size + 1);
1607 if (StrBufReadBLOB(NewTemplate->Data, &fd, 1, statbuf.st_size, &Err) < 0) {
1609 lprintf(1, "ERROR: reading template '%s' - %s<br />\n",
1610 ChrPtr(NewTemplate->FileName), strerror(errno));
1611 //FreeWCTemplate(NewTemplate);/////tODO
1617 StrBufShrinkToFit(NewTemplate->Data, 1);
1618 StrBufShrinkToFit(NewTemplate->MimeType, 1);
1619 pS = pch = ChrPtr(NewTemplate->Data);
1620 pE = pS + StrLength(NewTemplate->Data);
1622 const char *pts, *pte;
1624 int InDoubleQuotes = 0;
1626 /** Find one <? > */
1628 for (; pch < pE; pch ++) {
1629 if ((*pch=='<')&&(*(pch + 1)=='?') &&
1630 !((pch == pS) && /* we must ommit a <?xml */
1631 (*(pch + 2) == 'x') &&
1632 (*(pch + 3) == 'm') &&
1633 (*(pch + 4) == 'l')))
1635 if (*pch=='\n') Line ++;
1641 /** Found one? parse it. */
1642 for (; pch <= pE - 1; pch ++) {
1644 InDoubleQuotes = ! InDoubleQuotes;
1645 else if (*pch == '\'')
1646 InQuotes = ! InQuotes;
1647 else if ((!InQuotes && !InDoubleQuotes) &&
1648 ((*pch!='\\')&&(*(pch + 1)=='>'))) {
1656 PutNewToken(NewTemplate,
1657 NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte, Line, NewTemplate));
1664 const char* PrintTemplate(void *vSubst)
1666 WCTemplate *Tmpl = vSubst;
1668 return ChrPtr(Tmpl->FileName);
1672 int LoadTemplateDir(const StrBuf *DirName, HashList *wireless, HashList *big, const StrBuf *BaseKey)
1678 StrBuf *SubDirectory;
1679 DIR *filedir = NULL;
1680 struct dirent *filedir_entry;
1687 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
1692 filedir = opendir (ChrPtr(DirName));
1693 if (filedir == NULL) {
1698 Toplevel = StrLength(BaseKey) == 0;
1699 SubDirectory = NewStrBuf();
1700 SubKey = NewStrBuf();
1701 FileName = NewStrBufPlain(NULL, PATH_MAX);
1703 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
1704 (filedir_entry != NULL))
1708 #ifdef _DIRENT_HAVE_D_NAMELEN
1709 d_namelen = filedir_entry->d_namelen;
1710 d_type = filedir_entry->d_type;
1714 #define DT_UNKNOWN 0
1719 #define IFTODT(mode) (((mode) & 0170000) >> 12)
1720 #define DTTOIF(dirtype) ((dirtype) << 12)
1722 d_namelen = strlen(filedir_entry->d_name);
1723 d_type = DT_UNKNOWN;
1725 d_without_ext = d_namelen;
1727 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
1728 continue; /* Ignore backup files... */
1730 if ((d_namelen == 1) &&
1731 (filedir_entry->d_name[0] == '.'))
1734 if ((d_namelen == 2) &&
1735 (filedir_entry->d_name[0] == '.') &&
1736 (filedir_entry->d_name[1] == '.'))
1739 if (d_type == DT_UNKNOWN) {
1741 char path[PATH_MAX];
1742 snprintf(path, PATH_MAX, "%s/%s",
1743 ChrPtr(DirName), filedir_entry->d_name);
1744 if (stat(path, &s) == 0) {
1745 d_type = IFTODT(s.st_mode);
1751 /* Skip directories we are not interested in... */
1752 if (strcmp(filedir_entry->d_name, ".svn") == 0)
1755 FlushStrBuf(SubKey);
1757 /* If we're not toplevel, the upper dirs count as foo_bar_<local name>*/
1758 StrBufAppendBuf(SubKey, BaseKey, 0);
1759 StrBufAppendBufPlain(SubKey, HKEY("_"), 0);
1761 StrBufAppendBufPlain(SubKey, filedir_entry->d_name, d_namelen, 0);
1763 FlushStrBuf(SubDirectory);
1764 StrBufAppendBuf(SubDirectory, DirName, 0);
1765 if (ChrPtr(SubDirectory)[StrLength(SubDirectory) - 1] != '/')
1766 StrBufAppendBufPlain(SubDirectory, HKEY("/"), 0);
1767 StrBufAppendBufPlain(SubDirectory, filedir_entry->d_name, d_namelen, 0);
1769 LoadTemplateDir(SubDirectory, wireless, big, SubKey);
1772 case DT_LNK: /* TODO: check whether its a file or a directory */
1776 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1778 if ((d_without_ext == 0) || (d_namelen < 3))
1780 if (((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~') ||
1781 (strcmp(&filedir_entry->d_name[d_without_ext], ".orig") == 0) ||
1782 (strcmp(&filedir_entry->d_name[d_without_ext], ".swp") == 0))
1783 continue; /* Ignore backup files... */
1784 /* .m.xxx is for mobile useragents! */
1786 if (d_without_ext > 2)
1787 IsMobile = (filedir_entry->d_name[d_without_ext - 1] == 'm') &&
1788 (filedir_entry->d_name[d_without_ext - 2] == '.');
1789 PStart = filedir_entry->d_name;
1790 StrBufPrintf(FileName, "%s/%s", ChrPtr(DirName), filedir_entry->d_name);
1791 MinorPtr = strchr(filedir_entry->d_name, '.');
1792 if (MinorPtr != NULL)
1796 /* If we're not toplevel, the upper dirs count as foo_bar_<local name>*/
1797 StrBufAppendBuf(Key, BaseKey, 0);
1798 StrBufAppendBufPlain(Key, HKEY("_"), 0);
1800 StrBufAppendBufPlain(Key, filedir_entry->d_name, MinorPtr - filedir_entry->d_name, 0);
1802 if (LoadTemplates >= 1)
1803 lprintf(1, "%s %d %s\n", ChrPtr(FileName), IsMobile, ChrPtr(Key));
1804 prepare_template(FileName, Key, (IsMobile)?wireless:big);
1811 FreeStrBuf(&FileName);
1813 FreeStrBuf(&SubDirectory);
1814 FreeStrBuf(&SubKey);
1818 void InitTemplateCache(void)
1823 HashList *Templates[4];
1828 /* Primary Template set... */
1829 StrBufPrintf(Dir, "%s/t", static_dirs[0]);
1830 LoadTemplateDir(Dir,
1831 WirelessTemplateCache,
1835 /* User local Template set */
1836 StrBufPrintf(Dir, "%s/t", static_dirs[1]);
1837 LoadTemplateDir(Dir,
1838 WirelessLocalTemplateCache,
1842 /* Debug Templates, just to be loaded while debugging. */
1844 StrBufPrintf(Dir, "%s/dbg", static_dirs[0]);
1845 LoadTemplateDir(Dir,
1846 WirelessTemplateCache,
1849 Templates[0] = WirelessTemplateCache;
1850 Templates[1] = TemplateCache;
1851 Templates[2] = WirelessLocalTemplateCache;
1852 Templates[3] = LocalTemplateCache;
1855 if (LoadTemplates == 0)
1856 for (i=0; i < 4; i++) {
1862 At = GetNewHashPos(Templates[i], 0);
1863 while (GetNextHashPos(Templates[i],
1868 (vTemplate != NULL))
1870 load_template((WCTemplate *)vTemplate);
1882 /*-----------------------------------------------------------------------------
1883 * Filling & processing Templates
1886 * \brief executes one token
1887 * \param Target buffer to append to
1888 * \param Token da to process.
1889 * \param Template we're iterating
1890 * \param Context Contextpoointer to pass in
1891 * \param state are we in conditional state?
1892 * \param ContextType what type of information does context giv us?
1894 int EvaluateToken(StrBuf *Target, int state, WCTemplputParams *TP)
1896 const char *AppendMe;
1898 HashHandler *Handler;
1901 /* much output, since pName is not terminated...
1902 lprintf(1,"Doing token: %s\n",Token->pName);
1905 switch (TP->Tokens->Flags) {
1907 TmplGettext(Target, TP);
1909 case SV_CONDITIONAL: /** Forward conditional evaluation */
1910 return EvaluateConditional(Target, 1, state, TP);
1912 case SV_NEG_CONDITIONAL: /** Reverse conditional evaluation */
1913 return EvaluateConditional(Target, 0, state, TP);
1915 case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
1916 if (TP->Tokens->nParameters >= 6) {
1917 if (EvaluateConditional(Target, 0, state, TP)) {
1918 GetTemplateTokenString(Target, TP, 5, &AppendMe, &AppendMeLen);
1919 StrBufAppendBufPlain(Target,
1925 GetTemplateTokenString(Target, TP, 4, &AppendMe, &AppendMeLen);
1926 StrBufAppendBufPlain(Target,
1934 Target, "Conditional", ERR_NAME, TP,
1935 "needs at least 6 Params!");
1939 if (TP->Tokens->nParameters == 1)
1940 DoTemplate(TKEY(0), Target, TP);
1942 case SV_PREEVALUATED:
1943 Handler = (HashHandler*) TP->Tokens->PreEval;
1944 if (!CheckContext(Target, &Handler->Filter, TP, "Token")) {
1947 Handler->HandlerFunc(Target, TP);
1950 if (GetHash(GlobalNS, TP->Tokens->pName, TP->Tokens->NameEnd, &vVar)) {
1951 Handler = (HashHandler*) vVar;
1952 if (!CheckContext(Target, &Handler->Filter, TP, "Token")) {
1956 Handler->HandlerFunc(Target, TP);
1960 print_value_of(Target, TP);
1968 const StrBuf *ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, WCTemplputParams *CallingTP)
1970 WCTemplate *pTmpl = Tmpl;
1973 const char *pData, *pS;
1975 WCTemplputParams TP;
1977 memcpy(&TP.Filter, &CallingTP->Filter, sizeof(ContextFilter));
1979 TP.Context = CallingTP->Context;
1980 TP.ControlContext = CallingTP->ControlContext;
1982 if (LoadTemplates != 0) {
1983 if (LoadTemplates > 1)
1984 lprintf(1, "DBG: ----- loading: [%s] ------ \n",
1985 ChrPtr(Tmpl->FileName));
1986 pTmpl = duplicate_template(Tmpl);
1987 if(load_template(pTmpl) == NULL) {
1990 "<pre>\nError loading Template [%s]\n See Logfile for details\n</pre>\n",
1991 ChrPtr(Tmpl->FileName));
1992 FreeWCTemplate(pTmpl);
1998 pS = pData = ChrPtr(pTmpl->Data);
1999 len = StrLength(pTmpl->Data);
2003 if (i >= pTmpl->nTokensUsed) {
2004 StrBufAppendBufPlain(Target,
2006 len - (pData - pS), 0);
2010 StrBufAppendBufPlain(
2012 pTmpl->Tokens[i]->pTokenStart - pData, 0);
2013 TP.Tokens = pTmpl->Tokens[i];
2014 TP.nArgs = pTmpl->Tokens[i]->nParameters;
2015 state = EvaluateToken(Target, state, &TP);
2017 while ((state != 0) && (i+1 < pTmpl->nTokensUsed)) {
2018 /* condition told us to skip till its end condition */
2020 TP.Tokens = pTmpl->Tokens[i];
2021 TP.nArgs = pTmpl->Tokens[i]->nParameters;
2022 if ((pTmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
2023 (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
2024 if (state == EvaluateConditional(
2026 pTmpl->Tokens[i]->Flags,
2032 pData = pTmpl->Tokens[i++]->pTokenEnd + 1;
2033 if (i > pTmpl->nTokensUsed)
2037 if (LoadTemplates != 0) {
2038 FreeWCTemplate(pTmpl);
2040 return Tmpl->MimeType;
2045 * \brief Display a variable-substituted template
2046 * \param templatename template file to load
2047 * \returns the mimetype of the template its doing
2049 const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP)
2051 WCTemplputParams LocalTP;
2053 HashList *StaticLocal;
2059 memset(&LocalTP, 0, sizeof(WCTemplputParams));
2063 if (WC->is_mobile > 0) {
2064 Static = WirelessTemplateCache;
2065 StaticLocal = WirelessLocalTemplateCache;
2068 Static = TemplateCache;
2069 StaticLocal = LocalTemplateCache;
2074 lprintf (1, "Can't to load a template with empty name!\n");
2075 StrBufAppendPrintf(Target, "<pre>\nCan't to load a template with empty name!\n</pre>");
2079 if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
2080 !GetHash(Static, templatename, len, &vTmpl)) {
2081 lprintf (1, "didn't find Template [%s] %ld %ld\n", templatename, len , (long)strlen(templatename));
2082 StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>",
2084 (long)strlen(templatename));
2086 dbg_PrintHash(Static, PrintTemplate, NULL);
2087 PrintHash(Static, VarPrintTransition, PrintTemplate);
2093 return ProcessTemplate(vTmpl, Target, TP);
2097 /*-----------------------------------------------------------------------------
2100 typedef struct _HashIterator {
2101 HashList *StaticList;
2102 int AdditionalParams;
2104 int XPectContextType;
2106 RetrieveHashlistFunc GetHash;
2107 HashDestructorFunc Destructor;
2108 SubTemplFunc DoSubTemplate;
2111 void RegisterITERATOR(const char *Name, long len,
2112 int AdditionalParams,
2113 HashList *StaticList,
2114 RetrieveHashlistFunc GetHash,
2115 SubTemplFunc DoSubTempl,
2116 HashDestructorFunc Destructor,
2118 int XPectContextType,
2123 It = (HashIterator*)malloc(sizeof(HashIterator));
2124 memset(It, 0, sizeof(HashIterator));
2125 It->StaticList = StaticList;
2126 It->AdditionalParams = AdditionalParams;
2127 It->GetHash = GetHash;
2128 It->DoSubTemplate = DoSubTempl;
2129 It->Destructor = Destructor;
2130 It->ContextType = ContextType;
2131 It->XPectContextType = XPectContextType;
2133 Put(Iterators, Name, len, It, NULL);
2136 typedef struct _iteratestruct {
2145 int preeval_iterate(WCTemplateToken *Token)
2147 WCTemplputParams TPP;
2148 WCTemplputParams *TP;
2152 memset(&TPP, 0, sizeof(WCTemplputParams));
2155 if (!GetHash(Iterators, TKEY(0), &vIt)) {
2157 NULL, "Iterator", ERR_PARM1, TP,
2161 if (TP->Tokens->Params[1]->Type != TYPE_SUBTEMPLATE) {
2162 LogTemplateError(NULL, "Iterator", ERR_PARM1, TP,
2163 "Need token with type Subtemplate as param 1, have %s",
2164 TP->Tokens->Params[1]->Start);
2167 /* well, we don't check the mobile stuff here... */
2168 if (!GetHash(LocalTemplateCache, TKEY(1), &vTmpl) &&
2169 !GetHash(TemplateCache, TKEY(1), &vTmpl)) {
2170 LogTemplateError(NULL, "SubTemplate", ERR_PARM1, TP,
2171 "referenced here doesn't exist");
2173 Token->Preeval2 = vIt;
2177 void tmpl_iterate_subtmpl(StrBuf *Target, WCTemplputParams *TP)
2182 SortStruct *SortBy = NULL;
2184 int DetectGroupChange = 0;
2187 void *vLastContext = NULL;
2189 WCTemplputParams SubTP;
2190 IterateStruct Status;
2196 memset(&Status, 0, sizeof(IterateStruct));
2197 memcpy (&SubTP, &TP, sizeof(WCTemplputParams));
2199 It = (HashIterator*) TP->Tokens->Preeval2;
2202 Target, "Iterator", ERR_PARM1, TP, "Unknown!");
2206 if (TP->Tokens->nParameters < It->AdditionalParams + 2) {
2208 Target, "Iterator", ERR_PARM1, TP,
2209 "doesn't work with %d params",
2210 TP->Tokens->nParameters);
2214 if ((It->XPectContextType != CTX_NONE) &&
2215 (It->XPectContextType != TP->Filter.ContextType)) {
2217 Target, "Iterator", ERR_PARM1, TP,
2218 "requires context of type %d, have %d",
2219 It->XPectContextType,
2220 TP->Filter.ContextType);
2225 if (It->StaticList == NULL)
2226 List = It->GetHash(Target, TP);
2228 List = It->StaticList;
2230 DetectGroupChange = (It->Flags & IT_FLAG_DETECT_GROUPCHANGE) != 0;
2231 if (DetectGroupChange) {
2232 const StrBuf *BSort;
2233 DetectGroupChange = 0;
2234 if (havebstr("SortBy")) {
2235 BSort = sbstr("SortBy");
2236 if (GetHash(SortHash, SKEY(BSort), &vSortBy) &&
2237 (vSortBy != NULL)) {
2238 SortBy = (SortStruct*)vSortBy;
2239 /** Ok, its us, lets see in which direction we should sort... */
2240 if (havebstr("SortOrder")) {
2242 SortOrder = LBSTR("SortOrder");
2244 DetectGroupChange = 1;
2249 nMembersUsed = GetCount(List);
2250 SubBuf = NewStrBuf();
2251 SubTP.Filter.ContextType = It->ContextType;
2252 SubTP.Filter.ControlContextType = CTX_ITERATE;
2253 SubTP.ControlContext = &Status;
2255 if (HAVE_PARAM(2)) {
2256 StartAt = GetTemplateTokenNumber(Target, TP, 2, 0);
2258 if (HAVE_PARAM(3)) {
2259 StepWidth = GetTemplateTokenNumber(Target, TP, 3, 0);
2261 if (HAVE_PARAM(4)) {
2262 StopAt = GetTemplateTokenNumber(Target, TP, 4, -1);
2264 it = GetNewHashPos(List, StepWidth);
2266 StopAt = GetCount(List);
2268 while (GetNextHashPos(List, it, &Status.KeyLen, &Status.Key, &vContext)) {
2269 if ((Status.n >= StartAt) && (Status.n <= StopAt)) {
2270 if (DetectGroupChange && Status.n > 0) {
2271 Status.GroupChange = SortBy->GroupChange(vContext, vLastContext);
2273 Status.LastN = (Status.n + 1) == nMembersUsed;
2274 SubTP.Context = vContext;
2275 if (It->DoSubTemplate != NULL)
2276 It->DoSubTemplate(SubBuf, &SubTP);
2277 DoTemplate(TKEY(1), SubBuf, &SubTP);
2279 StrBufAppendBuf(Target, SubBuf, 0);
2280 FlushStrBuf(SubBuf);
2281 Status.oddeven = ! Status.oddeven;
2282 vLastContext = vContext;
2286 FreeStrBuf(&SubBuf);
2288 if (It->Destructor != NULL)
2289 It->Destructor(&List);
2293 int conditional_ITERATE_ISGROUPCHANGE(StrBuf *Target, WCTemplputParams *TP)
2295 IterateStruct *Ctx = CCTX;
2296 if (TP->Tokens->nParameters < 3)
2297 return Ctx->GroupChange;
2299 return TP->Tokens->Params[2]->lvalue == Ctx->GroupChange;
2302 void tmplput_ITERATE_ODDEVEN(StrBuf *Target, WCTemplputParams *TP)
2304 IterateStruct *Ctx = CCTX;
2306 StrBufAppendBufPlain(Target, HKEY("odd"), 0);
2308 StrBufAppendBufPlain(Target, HKEY("even"), 0);
2312 void tmplput_ITERATE_KEY(StrBuf *Target, WCTemplputParams *TP)
2314 IterateStruct *Ctx = CCTX;
2316 StrBufAppendBufPlain(Target, Ctx->Key, Ctx->KeyLen, 0);
2320 void tmplput_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
2322 IterateStruct *Ctx = CCTX;
2323 StrBufAppendPrintf(Target, "%d", Ctx->n);
2326 int conditional_ITERATE_FIRSTN(StrBuf *Target, WCTemplputParams *TP)
2328 IterateStruct *Ctx = CCTX;
2332 int conditional_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
2334 IterateStruct *Ctx = CCTX;
2340 /*-----------------------------------------------------------------------------
2343 int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP)
2345 ConditionalStruct *Cond;
2347 if ((TP->Tokens->Params[0]->len == 1) &&
2348 (TP->Tokens->Params[0]->Start[0] == 'X'))
2349 return (state != 0)?TP->Tokens->Params[1]->lvalue:0;
2351 Cond = (ConditionalStruct *) TP->Tokens->PreEval;
2354 Target, "Conditional", ERR_PARM1, TP,
2359 if (!CheckContext(Target, &Cond->Filter, TP, "Conditional")) {
2363 if (Cond->CondF(Target, TP) == Neg)
2364 return TP->Tokens->Params[1]->lvalue;
2368 int ConditionalVar(StrBuf *Target, WCTemplputParams *TP)
2373 if (!GetHash(WC->vars, TKEY(2), &vsubst))
2375 subst = (wcsubst*) vsubst;
2377 switch(subst->wcs_type) {
2379 return (subst->wcs_function!=NULL);
2381 lprintf(1, " -> Server [%s]\n", subst->wcs_value);/* TODO */
2385 case WCS_STRBUF_REF:
2386 if (TP->Tokens->nParameters < 4)
2388 return (strcmp(TP->Tokens->Params[3]->Start, ChrPtr(subst->wcs_value)) == 0);
2390 if (TP->Tokens->nParameters < 4)
2391 return (subst->lvalue != 0);
2392 return (subst->lvalue == TP->Tokens->Params[3]->lvalue);
2394 lprintf(1," WARNING: invalid type: [%ld]!\n", subst->wcs_type);
2400 void RegisterConditional(const char *Name, long len,
2402 WCConditionalFunc CondF,
2403 int ContextRequired)
2405 ConditionalStruct *Cond;
2407 Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
2408 memset(Cond, 0, sizeof(ConditionalStruct));
2409 Cond->PlainName = Name;
2410 Cond->Filter.nMaxArgs = nParams;
2411 Cond->Filter.nMinArgs = nParams;
2412 Cond->CondF = CondF;
2413 Cond->Filter.ContextType = ContextRequired;
2414 Cond->Filter.ControlContextType = CTX_NONE;
2415 Put(Conditionals, Name, len, Cond, NULL);
2418 void RegisterControlConditional(const char *Name, long len,
2420 WCConditionalFunc CondF,
2421 int ControlContextRequired)
2423 ConditionalStruct *Cond;
2425 Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
2426 memset(Cond, 0, sizeof(ConditionalStruct));
2427 Cond->PlainName = Name;
2428 Cond->Filter.nMaxArgs = nParams;
2429 Cond->Filter.nMinArgs = nParams;
2430 Cond->CondF = CondF;
2431 Cond->Filter.ContextType = CTX_NONE;
2432 Cond->Filter.ControlContextType = ControlContextRequired;
2433 Put(Conditionals, Name, len, Cond, NULL);
2436 void RegisterTokenParamDefine(const char *Name, long len,
2441 PVal = (long*)malloc(sizeof(long));
2443 Put(Defines, Name, len, PVal, NULL);
2446 long GetTokenDefine(const char *Name, long len,
2451 if (GetHash(Defines, Name, len, &vPVal) &&
2454 return *(long*) vPVal;
2462 void tmplput_DefStr(StrBuf *Target, WCTemplputParams *TP)
2466 GetTemplateTokenString(Target, TP, 2, &Str, &len);
2468 StrBufAppendBufPlain(Target, Str, len, 0);
2471 void tmplput_DefVal(StrBuf *Target, WCTemplputParams *TP)
2475 val = GetTemplateTokenNumber(Target, TP, 0, 0);
2476 StrBufAppendPrintf(Target, "%d", val);
2481 /*-----------------------------------------------------------------------------
2484 void tmplput_ContextString(StrBuf *Target, WCTemplputParams *TP)
2486 StrBufAppendTemplate(Target, TP, (StrBuf*)CTX, 0);
2488 int ConditionalContextStr(StrBuf *Target, WCTemplputParams *TP)
2490 StrBuf *TokenText = (StrBuf*) CTX;
2491 const char *CompareToken;
2494 GetTemplateTokenString(Target, TP, 2, &CompareToken, &len);
2495 return strcmp(ChrPtr(TokenText), CompareToken) == 0;
2498 void tmplput_ContextStringArray(StrBuf *Target, WCTemplputParams *TP)
2500 HashList *Arr = (HashList*) CTX;
2504 val = GetTemplateTokenNumber(Target, TP, 0, 0);
2505 if (GetHash(Arr, IKEY(val), &pV) &&
2507 StrBufAppendTemplate(Target, TP, (StrBuf*)pV, 1);
2510 int ConditionalContextStrinArray(StrBuf *Target, WCTemplputParams *TP)
2512 HashList *Arr = (HashList*) CTX;
2515 const char *CompareToken;
2518 GetTemplateTokenString(Target, TP, 2, &CompareToken, &len);
2519 val = GetTemplateTokenNumber(Target, TP, 0, 0);
2520 if (GetHash(Arr, IKEY(val), &pV) &&
2522 return strcmp(ChrPtr((StrBuf*)pV), CompareToken) == 0;
2528 /*-----------------------------------------------------------------------------
2532 void tmpl_do_boxed(StrBuf *Target, WCTemplputParams *TP)
2534 WCTemplputParams SubTP;
2536 StrBuf *Headline = NULL;
2537 if (TP->Tokens->nParameters == 2) {
2538 if (TP->Tokens->Params[1]->Type == TYPE_STR) {
2539 Headline = NewStrBuf();
2540 DoTemplate(TKEY(1), Headline, TP);
2545 GetTemplateTokenString(Target,
2550 Headline = NewStrBufPlain(Ch, len);
2553 /* else TODO error? logging? */
2554 memcpy (&SubTP, TP, sizeof(WCTemplputParams));
2555 SubTP.Context = Headline;
2556 SubTP.Filter.ContextType = CTX_STRBUF;
2557 DoTemplate(HKEY("beginbox"), Target, &SubTP);
2558 DoTemplate(TKEY(0), Target, TP);
2559 DoTemplate(HKEY("endbox"), Target, TP);
2560 FreeStrBuf(&Headline);
2563 /*-----------------------------------------------------------------------------
2566 int preeval_do_tabbed(WCTemplateToken *Token)
2568 WCTemplputParams TPP;
2569 WCTemplputParams *TP;
2575 memset(&TPP, 0, sizeof(WCTemplputParams));
2578 nTabs = TP->Tokens->nParameters / 2 - 1;
2579 if (TP->Tokens->nParameters % 2 != 0)
2581 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2582 "need even number of arguments");
2586 else for (i = 0; i < nTabs; i++) {
2587 if (!HaveTemplateTokenString(NULL,
2592 (TP->Tokens->Params[i * 2]->len == 0))
2594 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2595 "Tab-Subject %d needs to be able to produce a string, have %s",
2596 i, TP->Tokens->Params[i * 2]->Start);
2599 if (!HaveTemplateTokenString(NULL,
2604 (TP->Tokens->Params[i * 2 + 1]->len == 0))
2606 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2607 "Tab-Content %d needs to be able to produce a string, have %s",
2608 i, TP->Tokens->Params[i * 2 + 1]->Start);
2613 if (!HaveTemplateTokenString(NULL,
2618 (TP->Tokens->Params[i * 2 + 1]->len == 0))
2620 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2621 "Tab-Content %d needs to be able to produce a string, have %s",
2622 i, TP->Tokens->Params[i * 2 + 1]->Start);
2629 void tmpl_do_tabbed(StrBuf *Target, WCTemplputParams *TP)
2632 int i, ntabs, nTabs;
2634 nTabs = ntabs = TP->Tokens->nParameters / 2;
2635 TabNames = (StrBuf **) malloc(ntabs * sizeof(StrBuf*));
2636 memset(TabNames, 0, ntabs * sizeof(StrBuf*));
2638 for (i = 0; i < ntabs; i++) {
2639 if ((TP->Tokens->Params[i * 2]->Type == TYPE_STR) &&
2640 (TP->Tokens->Params[i * 2]->len > 0)) {
2641 TabNames[i] = NewStrBuf();
2642 DoTemplate(TKEY(i * 2), TabNames[i], TP);
2644 else if (TP->Tokens->Params[i * 2]->Type == TYPE_GETTEXT) {
2647 GetTemplateTokenString(Target,
2652 TabNames[i] = NewStrBufPlain(Ch, -1);
2655 /** A Tab without subject? we can't count that, add it as silent */
2660 StrTabbedDialog(Target, nTabs, TabNames);
2661 for (i = 0; i < ntabs; i++) {
2662 StrBeginTab(Target, i, nTabs);
2663 DoTemplate(TKEY(i * 2 + 1), Target, TP);
2664 StrEndTab(Target, i, nTabs);
2666 for (i = 0; i < ntabs; i++)
2667 FreeStrBuf(&TabNames[i]);
2671 /*-----------------------------------------------------------------------------
2676 void RegisterSortFunc(const char *name, long len,
2677 const char *prepend, long preplen,
2678 CompareFunc Forward,
2679 CompareFunc Reverse,
2680 CompareFunc GroupChange,
2683 SortStruct *NewSort;
2685 NewSort = (SortStruct*) malloc(sizeof(SortStruct));
2686 memset(NewSort, 0, sizeof(SortStruct));
2687 NewSort->Name = NewStrBufPlain(name, len);
2688 if (prepend != NULL)
2689 NewSort->PrefPrepend = NewStrBufPlain(prepend, preplen);
2691 NewSort->PrefPrepend = NULL;
2692 NewSort->Forward = Forward;
2693 NewSort->Reverse = Reverse;
2694 NewSort->GroupChange = GroupChange;
2695 NewSort->ContextType = ContextType;
2696 if (ContextType == CTX_NONE) {
2697 lprintf(1, "sorting requires a context. CTX_NONE won't make it.\n");
2701 Put(SortHash, name, len, NewSort, DestroySortStruct);
2704 CompareFunc RetrieveSort(WCTemplputParams *TP,
2705 const char *OtherPrefix, long OtherPrefixLen,
2706 const char *Default, long ldefault, long DefaultDirection)
2709 const StrBuf *BSort = NULL;
2712 long SortOrder = -1;
2714 if (havebstr("SortBy")) {
2715 BSort = sbstr("SortBy");
2716 if (OtherPrefix == NULL) {
2717 set_room_pref("sort", NewStrBufDup(BSort), 0);
2720 set_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen, NewStrBufDup(BSort), 0);
2723 else { /** Try to fallback to our remembered values... */
2724 if (OtherPrefix == NULL) {
2725 BSort = get_room_pref("sort");
2728 BSort = get_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen);
2731 putbstr("SortBy", NewStrBufDup(BSort));
2735 BSort = Buf = NewStrBufPlain(Default, ldefault);
2736 putbstr("SortBy", Buf);
2740 if (!GetHash(SortHash, SKEY(BSort), &vSortBy) ||
2741 (vSortBy == NULL)) {
2743 if (!GetHash(SortHash, Default, ldefault, &vSortBy) ||
2744 (vSortBy == NULL)) {
2746 NULL, "Sorting", ERR_PARM1, TP,
2747 "Illegal default sort: [%s]", Default);
2751 SortBy = (SortStruct*)vSortBy;
2753 if (SortBy->ContextType != TP->Filter.ContextType)
2756 /** Ok, its us, lets see in which direction we should sort... */
2757 if (havebstr("SortOrder")) {
2758 SortOrder = LBSTR("SortOrder");
2760 else { /** Try to fallback to our remembered values... */
2762 if (SortBy->PrefPrepend == NULL) {
2763 Buf = get_room_pref("SortOrder");
2764 SortOrder = StrTol(Buf);
2767 BSort = get_X_PREFS(HKEY("SortOrder"), OtherPrefix, OtherPrefixLen);
2771 SortOrder = DefaultDirection;
2773 Buf = NewStrBufPlain(NULL, 64);
2774 StrBufPrintf(Buf, "%ld", SortOrder);
2775 putbstr("SortOrder", Buf);
2777 switch (SortOrder) {
2782 return SortBy->Forward;
2784 return SortBy->Reverse;
2796 ConstStr SortIcons[] = {
2797 {HKEY("static/sort_none.gif")},
2798 {HKEY("static/up_pointer.gif")},
2799 {HKEY("static/down_pointer.gif")},
2802 ConstStr SortNextOrder[] = {
2809 int GetSortMetric(WCTemplputParams *TP, SortStruct **Next, SortStruct **Param, long *SortOrder, int N)
2811 int bSortError = eNOT_SPECIFIED;
2812 const StrBuf *BSort;
2817 if (!GetHash(SortHash, TKEY(0), &vSort) ||
2819 return eNO_SUCH_SORT;
2820 *Param = (SortStruct*) vSort;
2823 if (havebstr("SortBy")) {
2824 BSort = sbstr("SortBy");
2825 bSortError = eINVALID_PARAM;
2826 if ((*Param)->PrefPrepend == NULL) {
2827 set_room_pref("sort", NewStrBufDup(BSort), 0);
2830 set_X_PREFS(HKEY("sort"), TKEY(N), NewStrBufDup(BSort), 0);
2833 else { /** Try to fallback to our remembered values... */
2834 if ((*Param)->PrefPrepend == NULL) {
2835 BSort = get_room_pref("sort");
2838 BSort = get_X_PREFS(HKEY("sort"), TKEY(N));
2842 if (!GetHash(SortHash, SKEY(BSort), &vSort) ||
2846 *Next = (SortStruct*) vSort;
2848 /** Ok, its us, lets see in which direction we should sort... */
2849 if (havebstr("SortOrder")) {
2850 *SortOrder = LBSTR("SortOrder");
2852 else { /** Try to fallback to our remembered values... */
2853 if ((*Param)->PrefPrepend == NULL) {
2854 *SortOrder = StrTol(get_room_pref("SortOrder"));
2857 *SortOrder = StrTol(get_X_PREFS(HKEY("SortOrder"), TKEY(N)));
2867 void tmplput_SORT_ICON(StrBuf *Target, WCTemplputParams *TP)
2872 const ConstStr *SortIcon;
2874 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2877 Target, "Sorter", ERR_PARM1, TP,
2878 " Sorter [%s] unknown!",
2879 TP->Tokens->Params[0]->Start);
2881 case eINVALID_PARAM:
2882 LogTemplateError(NULL, "Sorter", ERR_PARM1, TP,
2883 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2885 case eNOT_SPECIFIED:
2887 if (Next == Param) {
2888 SortIcon = &SortIcons[SortOrder];
2890 else { /** Not Us... */
2891 SortIcon = &SortIcons[0];
2893 StrBufAppendBufPlain(Target, SortIcon->Key, SortIcon->len, 0);
2897 void tmplput_SORT_NEXT(StrBuf *Target, WCTemplputParams *TP)
2903 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2906 Target, "Sorter", ERR_PARM1, TP,
2907 " Sorter [%s] unknown!",
2908 TP->Tokens->Params[0]->Start);
2910 case eINVALID_PARAM:
2912 NULL, "Sorter", ERR_PARM1, TP,
2913 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2915 case eNOT_SPECIFIED:
2917 StrBufAppendBuf(Target, Param->Name, 0);
2922 void tmplput_SORT_ORDER(StrBuf *Target, WCTemplputParams *TP)
2925 const ConstStr *SortOrderStr;
2929 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2932 Target, "Sorter", ERR_PARM1, TP,
2933 " Sorter [%s] unknown!",
2934 TP->Tokens->Params[0]->Start);
2936 case eINVALID_PARAM:
2938 NULL, "Sorter", ERR_PARM1, TP,
2939 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2941 case eNOT_SPECIFIED:
2943 if (Next == Param) {
2944 SortOrderStr = &SortNextOrder[SortOrder];
2946 else { /** Not Us... */
2947 SortOrderStr = &SortNextOrder[0];
2949 StrBufAppendBufPlain(Target, SortOrderStr->Key, SortOrderStr->len, 0);
2954 void tmplput_long_vector(StrBuf *Target, WCTemplputParams *TP)
2956 long *LongVector = (long*) CTX;
2958 if ((TP->Tokens->Params[0]->Type == TYPE_LONG) &&
2959 (TP->Tokens->Params[0]->lvalue <= LongVector[0]))
2961 StrBufAppendPrintf(Target, "%ld", LongVector[TP->Tokens->Params[0]->lvalue]);
2965 if (TP->Tokens->Params[0]->Type != TYPE_LONG) {
2967 Target, "Longvector", ERR_NAME, TP,
2968 "needs a numerical Parameter!");
2972 Target, "LongVector", ERR_PARM1, TP,
2973 "doesn't have %ld Parameters, its just the size of %ld!",
2974 TP->Tokens->Params[0]->lvalue,
2980 void dbg_print_longvector(long *LongVector)
2982 StrBuf *Buf = NewStrBufPlain(HKEY("Longvector: ["));
2983 int nItems = LongVector[0];
2986 for (i = 0; i < nItems; i++) {
2988 StrBufAppendPrintf(Buf, "%d: %ld | ", i, LongVector[i]);
2990 StrBufAppendPrintf(Buf, "%d: %ld]\n", i, LongVector[i]);
2993 lprintf(1, ChrPtr(Buf));
2997 int ConditionalLongVector(StrBuf *Target, WCTemplputParams *TP)
2999 long *LongVector = (long*) CTX;
3001 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) &&
3002 (TP->Tokens->Params[2]->lvalue <= LongVector[0])&&
3003 (TP->Tokens->Params[3]->Type == TYPE_LONG) &&
3004 (TP->Tokens->Params[3]->lvalue <= LongVector[0]))
3006 return LongVector[TP->Tokens->Params[2]->lvalue] ==
3007 LongVector[TP->Tokens->Params[3]->lvalue];
3011 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) ||
3012 (TP->Tokens->Params[2]->Type == TYPE_LONG)) {
3014 Target, "ConditionalLongvector", ERR_PARM1, TP,
3015 "needs two long Parameter!");
3019 Target, "Longvector", ERR_PARM1, TP,
3020 "doesn't have %ld / %ld Parameters, its just the size of %ld!",
3021 TP->Tokens->Params[2]->lvalue,
3022 TP->Tokens->Params[3]->lvalue,
3030 void tmplput_CURRENT_FILE(StrBuf *Target, WCTemplputParams *TP)
3032 StrBufAppendTemplate(Target, TP, TP->Tokens->FileName, 0);
3039 memset(&NoCtx, 0, sizeof(WCTemplputParams));
3040 RegisterNamespace("SORT:ICON", 1, 2, tmplput_SORT_ICON, NULL, CTX_NONE);
3041 RegisterNamespace("SORT:ORDER", 1, 2, tmplput_SORT_ORDER, NULL, CTX_NONE);
3042 RegisterNamespace("SORT:NEXT", 1, 2, tmplput_SORT_NEXT, NULL, CTX_NONE);
3043 RegisterNamespace("CONTEXTSTR", 0, 1, tmplput_ContextString, NULL, CTX_STRBUF);
3044 RegisterNamespace("CONTEXTSTRARR", 1, 2, tmplput_ContextStringArray, NULL, CTX_STRBUF);
3045 RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl, preeval_iterate, CTX_NONE);
3046 RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed, NULL, CTX_NONE);
3047 RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed, preeval_do_tabbed, CTX_NONE);
3048 RegisterNamespace("LONGVECTOR", 1, 1, tmplput_long_vector, NULL, CTX_LONGVECTOR);
3051 RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar, CTX_NONE);
3052 RegisterConditional(HKEY("COND:CONTEXTSTR"), 3, ConditionalContextStr, CTX_STRBUF);
3053 RegisterConditional(HKEY("COND:CONTEXTSTRARR"), 4, ConditionalContextStrinArray, CTX_STRBUFARR);
3054 RegisterConditional(HKEY("COND:LONGVECTOR"), 4, ConditionalLongVector, CTX_LONGVECTOR);
3056 RegisterControlConditional(HKEY("COND:ITERATE:ISGROUPCHANGE"), 2,
3057 conditional_ITERATE_ISGROUPCHANGE,
3059 RegisterControlConditional(HKEY("COND:ITERATE:LASTN"), 2,
3060 conditional_ITERATE_LASTN,
3062 RegisterControlConditional(HKEY("COND:ITERATE:FIRSTN"), 2,
3063 conditional_ITERATE_FIRSTN,
3066 RegisterControlNS(HKEY("ITERATE:ODDEVEN"), 0, 0, tmplput_ITERATE_ODDEVEN, CTX_ITERATE);
3067 RegisterControlNS(HKEY("ITERATE:KEY"), 0, 0, tmplput_ITERATE_KEY, CTX_ITERATE);
3068 RegisterControlNS(HKEY("ITERATE:N"), 0, 0, tmplput_ITERATE_LASTN, CTX_ITERATE);
3069 RegisterNamespace("CURRENTFILE", 0, 1, tmplput_CURRENT_FILE, NULL, CTX_NONE);
3070 RegisterNamespace("DEF:STR", 0, 0, tmplput_DefStr, NULL, CTX_NONE);
3071 RegisterNamespace("DEF:VAL", 0, 0, tmplput_DefVal, NULL, CTX_NONE);
3079 ServerStartModule_SUBST
3082 WirelessTemplateCache = NewHash(1, NULL);
3083 WirelessLocalTemplateCache = NewHash(1, NULL);
3084 LocalTemplateCache = NewHash(1, NULL);
3085 TemplateCache = NewHash(1, NULL);
3087 GlobalNS = NewHash(1, NULL);
3088 Iterators = NewHash(1, NULL);
3089 Conditionals = NewHash(1, NULL);
3090 SortHash = NewHash(1, NULL);
3091 Defines = NewHash(1, NULL);
3095 FinalizeModule_SUBST
3102 ServerShutdownModule_SUBST
3105 DeleteHash(&WirelessTemplateCache);
3106 DeleteHash(&WirelessLocalTemplateCache);
3107 DeleteHash(&TemplateCache);
3108 DeleteHash(&LocalTemplateCache);
3110 DeleteHash(&GlobalNS);
3111 DeleteHash(&Iterators);
3112 DeleteHash(&Conditionals);
3113 DeleteHash(&SortHash);
3114 DeleteHash(&Defines);
3119 SessionNewModule_SUBST
3126 SessionAttachModule_SUBST
3129 sess->vars = NewHash(1,NULL);
3133 SessionDetachModule_SUBST
3136 DeleteHash(&sess->vars);
3140 SessionDestroyModule_SUBST