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_backtrace_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",
121 void DestroySortStruct(void *vSort)
123 SortStruct *Sort = (SortStruct*) vSort;
124 FreeStrBuf(&Sort->Name);
125 FreeStrBuf(&Sort->PrefPrepend);
129 const char *ContextName(int ContextType)
131 if (ContextType < CTX_UNKNOWN)
132 return CtxNames[ContextType];
134 return CtxNames[CTX_UNKNOWN];
137 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);
153 case ERR_NAME: /* the main token name... */
154 Err = (TP->Tokens!= NULL)? TP->Tokens->pName:"";
157 Err = ((TP->Tokens!= NULL) &&
158 (TP->Tokens->nParameters > ErrorPos - 1))?
159 TP->Tokens->Params[ErrorPos - 1]->Start : "";
162 if (TP->Tokens != NULL)
164 lprintf(1, "%s [%s] (in '%s' line %ld); %s; [%s]\n",
167 ChrPtr(TP->Tokens->FileName),
170 ChrPtr(TP->Tokens->FlatToken));
174 lprintf(1, "%s: %s;\n",
189 if (WCC->WFBuf == NULL) WCC->WFBuf = NewStrBuf();
190 if (TP->Tokens != NULL)
195 "<pre>\n%s [%s] (in '%s' line %ld); %s\n[%s]\n</pre>\n",
198 ChrPtr(TP->Tokens->FileName),
201 ChrPtr(TP->Tokens->FlatToken));
203 StrBufPrintf(Info, "%s [%s] %s; [%s]",
207 ChrPtr(TP->Tokens->FlatToken));
210 SerializeJson(WCC->WFBuf, WildFireException(SKEY(TP->Tokens->FileName),
215 SerializeJson(Header, WildFireMessage(SKEY(TP->Tokens->FileName),
227 "<pre>\n%s: %s\n</pre>\n",
231 StrBufPrintf(Info, "%s [%s] %s; [%s]",
235 ChrPtr(TP->Tokens->FlatToken));
236 SerializeJson(WCC->WFBuf, WildFireException(HKEY(__FILE__), __LINE__, Info, 1), 1);
241 if (dbg_backtrace_template_errors)
249 void LogError (StrBuf *Target, const char *Type, const char *Format, ...)
259 va_start(arg_ptr, Format);
260 StrBufVAppendPrintf(Error, Format, arg_ptr);
263 lprintf(1, ChrPtr(Error));
266 if (WCC->WFBuf == NULL) WCC->WFBuf = NewStrBuf();
268 SerializeJson(WCC->WFBuf, WildFireException(Type, strlen(Type),
276 if (dbg_backtrace_template_errors)
282 void RegisterNS(const char *NSName,
286 WCHandlerFunc HandlerFunc,
287 WCPreevalFunc PreevalFunc,
290 HashHandler *NewHandler;
292 NewHandler = (HashHandler*) malloc(sizeof(HashHandler));
293 memset(NewHandler, 0, sizeof(HashHandler));
294 NewHandler->Filter.nMinArgs = nMinArgs;
295 NewHandler->Filter.nMaxArgs = nMaxArgs;
296 NewHandler->Filter.ContextType = ContextRequired;
297 NewHandler->Filter.ControlContextType = CTX_NONE;
299 NewHandler->PreEvalFunc = PreevalFunc;
300 NewHandler->HandlerFunc = HandlerFunc;
301 Put(GlobalNS, NSName, len, NewHandler, NULL);
304 void RegisterControlNS(const char *NSName,
308 WCHandlerFunc HandlerFunc,
309 int ControlContextRequired)
311 HashHandler *NewHandler;
313 NewHandler = (HashHandler*) malloc(sizeof(HashHandler));
314 memset(NewHandler, 0, sizeof(HashHandler));
315 NewHandler->Filter.nMinArgs = nMinArgs;
316 NewHandler->Filter.nMaxArgs = nMaxArgs;
317 NewHandler->Filter.ContextType = CTX_NONE;
318 NewHandler->Filter.ControlContextType = ControlContextRequired;
319 NewHandler->HandlerFunc = HandlerFunc;
320 Put(GlobalNS, NSName, len, NewHandler, NULL);
325 int CheckContext(StrBuf *Target, ContextFilter *Need, WCTemplputParams *TP, const char *ErrType)
327 if ((Need->ContextType != CTX_NONE) &&
328 (Need->ContextType != TP->Filter.ContextType)) {
330 Target, ErrType, ERR_PARM1, TP,
331 " WARNING: requires Context: [%s], have [%s]!",
332 ContextName(Need->ContextType),
333 ContextName(TP->Filter.ContextType));
337 if ((Need->ControlContextType != CTX_NONE) &&
338 (Need->ControlContextType != TP->Filter.ControlContextType)) {
340 Target, ErrType, ERR_PARM1, TP,
341 " WARNING: requires Control Context: [%s], have [%s]!",
342 ContextName(Need->ControlContextType),
343 ContextName(TP->Filter.ControlContextType));
347 if (TP->Tokens->nParameters < Need->nMinArgs) {
348 LogTemplateError(Target, ErrType, ERR_NAME, TP,
349 "needs at least %ld params, have %ld",
351 TP->Tokens->nParameters);
355 else if (TP->Tokens->nParameters > Need->nMaxArgs) {
356 LogTemplateError(Target, ErrType, ERR_NAME, TP,
357 "just needs %ld params, you gave %ld",
359 TP->Tokens->nParameters);
367 void FreeToken(WCTemplateToken **Token)
370 FreeStrBuf(&(*Token)->FlatToken);
371 if ((*Token)->HaveParameters)
372 for (i = 0; i < (*Token)->nParameters; i++)
373 free((*Token)->Params[i]);
380 void FreeWCTemplate(void *vFreeMe)
383 WCTemplate *FreeMe = (WCTemplate*)vFreeMe;
385 if (FreeMe->TokenSpace > 0) {
386 for (i = 0; i < FreeMe->nTokensUsed; i ++) {
387 FreeToken(&FreeMe->Tokens[i]);
389 free(FreeMe->Tokens);
391 FreeStrBuf(&FreeMe->FileName);
392 FreeStrBuf(&FreeMe->Data);
393 FreeStrBuf(&FreeMe->MimeType);
399 * debugging function to print array to log
401 void VarPrintTransition(void *vVar1, void *vVar2, int odd){}
404 * debugging function to print array to log
406 void VarPrintEntry(const char *Key, void *vSubst, int odd)
409 lprintf(1,"Subst[%s] : ", Key);
410 ptr = (wcsubst*) vSubst;
412 switch(ptr->wcs_type) {
414 lprintf(1, " -> %s\n", ChrPtr(ptr->wcs_value));
417 lprintf(1, " -> Server [%s]\n", ChrPtr(ptr->wcs_value));
420 lprintf(1, " -> function at [%0xd]\n", ptr->wcs_function);
423 lprintf(1," WARNING: invalid type: [%ld]!\n", ptr->wcs_type);
430 * Clear out the list of substitution variables local to this session
432 void clear_substs(wcsession *wc) {
434 if (wc->vars != NULL) {
435 DeleteHash(&wc->vars);
440 * Clear out the list of substitution variables local to this session
442 void clear_local_substs(void) {
446 int NeedNewBuf(int type)
461 void FlushPayload(wcsubst *ptr, int reusestrbuf, int type)
463 int NeedNew = NeedNewBuf(type);
464 switch(ptr->wcs_type) {
468 if (reusestrbuf && NeedNew) {
469 FlushStrBuf(ptr->wcs_value);
473 FreeStrBuf(&ptr->wcs_value);
474 ptr->wcs_value = NULL;
478 ptr->wcs_function = NULL;
479 if (reusestrbuf && NeedNew)
480 ptr->wcs_value = NewStrBuf();
483 ptr->wcs_value = NULL;
484 if (reusestrbuf && NeedNew)
485 ptr->wcs_value = NewStrBuf();
489 if (reusestrbuf && NeedNew)
490 ptr->wcs_value = NewStrBuf();
493 if (reusestrbuf && NeedNew)
494 ptr->wcs_value = NewStrBuf();
501 * destructor; kill one entry.
503 void deletevar(void *data)
505 wcsubst *ptr = (wcsubst*)data;
506 FlushPayload(ptr, 0, ptr->wcs_type);
511 wcsubst *NewSubstVar(const char *keyname, int keylen, int type)
516 ptr = (wcsubst *) malloc(sizeof(wcsubst));
517 memset(ptr, 0, sizeof(wcsubst));
519 ptr->wcs_type = type;
520 safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
521 Put(WCC->vars, keyname, keylen, ptr, deletevar);
523 switch(ptr->wcs_type) {
526 ptr->wcs_value = NewStrBuf();
540 * Add a substitution variable (local to this session) (strlen version...)
541 * keyname the replacementstring to substitute
542 * keytype the kind of the key
543 * format the format string ala printf
544 * ... the arguments to substitute in the formatstring
546 void SVPRINTF(char *keyname, int keytype, const char *format,...)
554 keylen = strlen(keyname);
556 * First look if we're doing a replacement of
559 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
560 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
561 ptr = (wcsubst*)vPtr;
562 FlushPayload(ptr, keytype, keytype);
563 ptr->wcs_type = keytype;
565 else /** Otherwise allocate a new one */
567 ptr = NewSubstVar(keyname, keylen, keytype);
570 /* Format the string */
571 va_start(arg_ptr, format);
572 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
577 * Add a substitution variable (local to this session)
578 * keyname the replacementstring to substitute
579 * keytype the kind of the key
580 * format the format string ala printf
581 * ... the arguments to substitute in the formatstring
583 void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...)
591 * First look if we're doing a replacement of
594 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
595 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
596 ptr = (wcsubst*)vPtr;
597 FlushPayload(ptr, 1, keytype);
598 ptr->wcs_type = keytype;
600 else /** Otherwise allocate a new one */
602 ptr = NewSubstVar(keyname, keylen, keytype);
605 /** Format the string and save it */
606 va_start(arg_ptr, format);
607 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
612 * Add a substitution variable (local to this session)
613 * keyname the replacementstring to substitute
614 * keytype the kind of the key
615 * format the format string ala printf
616 * ... the arguments to substitute in the formatstring
618 void SVPut(char *keyname, size_t keylen, int keytype, char *Data)
626 * First look if we're doing a replacement of
629 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
630 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
631 ptr = (wcsubst*)vPtr;
632 FlushPayload(ptr, 1, keytype);
633 ptr->wcs_type = keytype;
635 else /** Otherwise allocate a new one */
637 ptr = NewSubstVar(keyname, keylen, keytype);
639 StrBufAppendBufPlain(ptr->wcs_value, Data, -1, 0);
643 * \brief Add a substitution variable (local to this session)
644 * \param keyname the replacementstring to substitute
645 * \param keytype the kind of the key
646 * \param format the format string ala printf
647 * \param ... the arguments to substitute in the formatstring
649 void SVPutLong(char *keyname, size_t keylen, long Data)
657 * First look if we're doing a replacement of
660 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
661 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
662 ptr = (wcsubst*)vPtr;
663 FlushPayload(ptr, 1, WCS_LONG);
664 ptr->wcs_type = WCS_LONG;
666 else /** Otherwise allocate a new one */
668 ptr = NewSubstVar(keyname, keylen, WCS_LONG);
674 * \brief Add a substitution variable (local to this session) that does a callback
675 * \param keyname the keystring to substitute
676 * \param fcn_ptr the function callback to give the substitution string
678 void SVCallback(char *keyname, size_t keylen, WCHandlerFunc fcn_ptr)
685 * First look if we're doing a replacement of
688 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
689 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
690 ptr = (wcsubst*)vPtr;
691 FlushPayload(ptr, 1, WCS_FUNCTION);
692 ptr->wcs_type = WCS_FUNCTION;
694 else /** Otherwise allocate a new one */
696 ptr = NewSubstVar(keyname, keylen, WCS_FUNCTION);
699 ptr->wcs_function = fcn_ptr;
701 inline void SVCALLBACK(char *keyname, WCHandlerFunc fcn_ptr)
703 SVCallback(keyname, strlen(keyname), fcn_ptr);
708 void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref)
715 * First look if we're doing a replacement of
718 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
719 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
720 ptr = (wcsubst*)vPtr;
721 FlushPayload(ptr, 0, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
722 ptr->wcs_type = (ref)?WCS_STRBUF_REF:WCS_STRBUF;
724 else /** Otherwise allocate a new one */
726 ptr = NewSubstVar(keyname, keylen, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
728 ptr->wcs_value = (StrBuf*)Buf;
732 * \brief back end for print_value_of() ... does a server command
733 * \param servcmd server command to execute on the citadel server
735 void pvo_do_cmd(StrBuf *Target, StrBuf *servcmd) {
739 serv_puts(ChrPtr(servcmd));
740 len = serv_getln(buf, sizeof buf);
746 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
749 _fmout(Target, "CENTER");
752 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
758 int HaveTemplateTokenString(StrBuf *Target,
759 WCTemplputParams *TP,
764 if (N >= TP->Tokens->nParameters) {
768 switch (TP->Tokens->Params[N]->Type) {
774 case TYPE_SUBTEMPLATE:
783 void GetTemplateTokenString(StrBuf *Target,
784 WCTemplputParams *TP,
790 WCTemplputParams SubTP;
792 if (N >= TP->Tokens->nParameters) {
793 LogTemplateError(Target,
794 "TokenParameter", N, TP,
795 "invalid token %d. this shouldn't have come till here.\n", N);
801 switch (TP->Tokens->Params[N]->Type) {
805 *Value = TP->Tokens->Params[N]->Start;
806 *len = TP->Tokens->Params[N]->len;
809 if (TP->Tokens->Params[N]->len == 0) {
810 LogTemplateError(Target,
811 "TokenParameter", N, TP,
812 "Requesting parameter %d; of type BSTR, empty lookup string not admitted.", N);
817 Buf = (StrBuf*) SBstr(TKEY(N));
818 *Value = ChrPtr(Buf);
819 *len = StrLength(Buf);
822 if (TP->Tokens->Params[N]->len == 0) {
823 LogTemplateError(Target,
824 "TokenParameter", N, TP,
825 "Requesting parameter %d; of type PREFSTR, empty lookup string not admitted.", N);
830 get_PREFERENCE(TKEY(N), &Buf);
831 *Value = ChrPtr(Buf);
832 *len = StrLength(Buf);
835 LogTemplateError(Target,
836 "TokenParameter", N, TP,
837 "Requesting parameter %d; of type LONG, want string.", N);
840 LogTemplateError(Target,
841 "TokenParameter", N, TP,
842 "Requesting parameter %d; of type PREFINT, want string.", N);
845 *Value = _(TP->Tokens->Params[N]->Start);
846 *len = strlen(*Value);
848 case TYPE_SUBTEMPLATE:
849 if (TP->Tokens->Params[N]->len == 0) {
850 LogTemplateError(Target,
851 "TokenParameter", N, TP,
852 "Requesting parameter %d; of type SUBTEMPLATE, empty lookup string not admitted.", N);
858 memset(&SubTP, 0, sizeof(WCTemplputParams *));
859 SubTP.Context = TP->Context;
860 SubTP.Filter.ContextType = TP->Filter.ContextType;
862 DoTemplate(TKEY(N), Buf, &SubTP);
863 *Value = ChrPtr(Buf);
864 *len = StrLength(Buf);
865 /* we can't free it here, so we put it into the subst so its discarded later on. */
866 SVPUTBuf(TKEY(N), Buf, 0);
870 LogTemplateError(Target,
871 "TokenParameter", N, TP,
872 "unknown param type %d; [%d]", N, TP->Tokens->Params[N]->Type);
877 long GetTemplateTokenNumber(StrBuf *Target, WCTemplputParams *TP, int N, long dflt)
880 if (N >= TP->Tokens->nParameters) {
881 LogTemplateError(Target,
882 "TokenParameter", N, TP,
883 "invalid token %d. this shouldn't have come till here.\n", N);
888 switch (TP->Tokens->Params[N]->Type) {
891 return atol(TP->Tokens->Params[N]->Start);
894 if (TP->Tokens->Params[N]->len == 0) {
895 LogTemplateError(Target,
896 "TokenParameter", N, TP,
897 "Requesting parameter %d; of type BSTR, empty lookup string not admitted.", N);
900 return LBstr(TKEY(N));
903 LogTemplateError(Target,
904 "TokenParameter", N, TP,
905 "requesting a prefstring in param %d want a number", N);
906 if (TP->Tokens->Params[N]->len == 0) {
907 LogTemplateError(Target,
908 "TokenParameter", N, TP,
909 "Requesting parameter %d; of type PREFSTR, empty lookup string not admitted.", N);
912 if (get_PREF_LONG(TKEY(N), &Ret, dflt))
917 return TP->Tokens->Params[N]->lvalue;
919 if (TP->Tokens->Params[N]->len == 0) {
920 LogTemplateError(Target,
921 "TokenParameter", N, TP,
922 "Requesting parameter %d; of type PREFINT, empty lookup string not admitted.", N);
925 if (get_PREF_LONG(TKEY(N), &Ret, dflt))
929 LogTemplateError(Target,
930 "TokenParameter", N, TP,
931 "requesting a I18N string in param %d; want a number", N);
933 case TYPE_SUBTEMPLATE:
934 LogTemplateError(Target,
935 "TokenParameter", N, TP,
936 "requesting a subtemplate in param %d; not supported for numbers", N);
939 LogTemplateError(Target,
940 "TokenParameter", N, TP,
941 "unknown param type %d; [%d]", N, TP->Tokens->Params[N]->Type);
949 * \brief Print the value of a variable
950 * \param keyname get a key to print
952 void print_value_of(StrBuf *Target, WCTemplputParams *TP)
959 /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
960 /* TODO: depricated! */
961 if (TP->Tokens->pName[0] == '=') {
962 DoTemplate(TP->Tokens->pName+1, TP->Tokens->NameEnd - 1, NULL, &NoCtx);
964 /*/////TODO: if param[1] == "U" -> urlescape
966 /** Page-local variables */
967 if ((WCC->vars!= NULL) && GetHash(WCC->vars, TP->Tokens->pName, TP->Tokens->NameEnd, &vVar)) {
968 ptr = (wcsubst*) vVar;
969 switch(ptr->wcs_type) {
971 StrBufAppendBuf(Target, ptr->wcs_value, 0);
974 pvo_do_cmd(Target, ptr->wcs_value);
977 (*ptr->wcs_function) (Target, TP);
981 StrBufAppendBuf(Target, ptr->wcs_value, 0);
984 StrBufAppendPrintf(Target, "%ld", ptr->lvalue);
988 Target, "Subst", ERR_NAME, TP,
989 "WARNING: invalid value in SV-Hash at %s!", TP->Tokens->pName);
994 Target, "Token", ERR_NAME, TP,
995 "didn't find Handler \"%s\"", TP->Tokens->pName);
1000 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup)
1002 wcsession *WCC = WC;
1006 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
1007 ParamToLookup->len, &vVar)) {
1008 ptr = (wcsubst*) vVar;
1009 switch(ptr->wcs_type) {
1012 case WCS_STRBUF_REF:
1013 if (ParamToCompare->Type == TYPE_STR)
1014 return ((ParamToCompare->len == StrLength(ptr->wcs_value)) &&
1015 (strcmp(ParamToCompare->Start, ChrPtr(ptr->wcs_value)) == 0));
1017 return ParamToCompare->lvalue == StrTol(ptr->wcs_value);
1025 if (ParamToCompare->Type == TYPE_STR)
1028 return ParamToCompare->lvalue == ptr->lvalue;
1031 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
1032 ParamToLookup->Start);
1038 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup)
1040 wcsession *WCC = WC;
1044 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
1045 ParamToLookup->len, &vVar)) {
1046 ptr = (wcsubst*) vVar;
1047 switch(ptr->wcs_type) {
1050 case WCS_STRBUF_REF:
1051 return ((StrLength(Compare) == StrLength(ptr->wcs_value)) &&
1052 (strcmp(ChrPtr(Compare), ChrPtr(ptr->wcs_value)) == 0));
1059 return StrTol(Compare) == ptr->lvalue;
1061 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
1062 ParamToLookup->Start);
1071 * \brief puts string into the template and computes which escape methon we should use
1072 * \param Source the string we should put into the template
1073 * \param FormatTypeIndex where should we look for escape types if?
1075 void StrBufAppendTemplate(StrBuf *Target,
1076 WCTemplputParams *TP,
1077 const StrBuf *Source, int FormatTypeIndex)
1080 char EscapeAs = ' ';
1082 if ((FormatTypeIndex < TP->Tokens->nParameters) &&
1083 (TP->Tokens->Params[FormatTypeIndex]->Type == TYPE_STR) &&
1084 (TP->Tokens->Params[FormatTypeIndex]->len == 1)) {
1085 EscapeAs = *TP->Tokens->Params[FormatTypeIndex]->Start;
1092 StrEscAppend(Target, Source, NULL, 0, 2);
1095 StrEscAppend(Target, Source, NULL, 0, 0);
1098 StrECMAEscAppend(Target, Source, NULL);
1101 StrHtmlEcmaEscAppend(Target, Source, NULL, 0, 0);
1104 StrBufUrlescAppend(Target, Source, NULL);
1107 StrBufAppendBuf(Target, Source, 0);
1112 void PutNewToken(WCTemplate *Template, WCTemplateToken *NewToken)
1114 if (Template->nTokensUsed + 1 >= Template->TokenSpace) {
1115 if (Template->TokenSpace <= 0) {
1116 Template->Tokens = (WCTemplateToken**)malloc(
1117 sizeof(WCTemplateToken*) * 10);
1118 memset(Template->Tokens, 0, sizeof(WCTemplateToken*) * 10);
1119 Template->TokenSpace = 10;
1122 WCTemplateToken **NewTokens;
1124 NewTokens= (WCTemplateToken**) malloc(
1125 sizeof(WCTemplateToken*) * Template->TokenSpace * 2);
1128 0, sizeof(WCTemplateToken*) * Template->TokenSpace * 2);
1132 sizeof(WCTemplateToken*) * Template->nTokensUsed);
1134 free(Template->Tokens);
1135 Template->TokenSpace *= 2;
1136 Template->Tokens = NewTokens;
1139 Template->Tokens[(Template->nTokensUsed)++] = NewToken;
1142 int GetNextParameter(StrBuf *Buf,
1145 WCTemplateToken *Tokens,
1147 WCTemplputParams *TP,
1148 TemplateParam **pParm)
1150 const char *pch = *pCh;
1151 const char *pchs, *pche;
1152 TemplateParam *Parm;
1156 *pParm = Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
1157 memset(Parm, 0, sizeof(TemplateParam));
1158 Parm->Type = TYPE_STR;
1160 /* Skip leading whitespaces */
1161 while ((*pch == ' ' )||
1164 (*pch == '\n')) pch ++;
1167 Parm->Type = TYPE_PREFSTR;
1174 else if (*pch == ';') {
1175 Parm->Type = TYPE_PREFINT;
1182 else if (*pch == '#') {
1183 Parm->Type = TYPE_INTDEFINE;
1186 else if (*pch == '_') {
1187 Parm->Type = TYPE_GETTEXT;
1194 else if (*pch == 'B') {
1195 Parm->Type = TYPE_BSTR;
1202 else if (*pch == '=') {
1203 Parm->Type = TYPE_SUBTEMPLATE;
1214 else if (*pch == '\'')
1216 if (quote != '\0') {
1221 ( (pch > pchs) && (*(pch - 1) == '\\'))
1226 if (*pch != quote) {
1227 lprintf(1, "Error (in '%s' line %ld); "
1228 "evaluating template param [%s] in Token [%s]\n",
1229 ChrPtr(pTmpl->FileName),
1231 ChrPtr(Tokens->FlatToken),
1239 StrBufPeek(Buf, pch, -1, '\0');
1240 if (LoadTemplates > 1) {
1241 lprintf(1, "DBG: got param [%s] %ld %ld\n",
1242 pchs, pche - pchs, strlen(pchs));
1245 Parm->len = pche - pchs;
1246 pch ++; /* move after trailing quote */
1247 if (ParamBrace && (*pch == ')')) {
1254 Parm->Type = TYPE_LONG;
1256 while ((pch <= pe) &&
1262 if (pch - pchs > 1){
1263 StrBufPeek(Buf, pch, -1, '\0');
1264 Parm->lvalue = atol(pchs);
1271 lprintf(1, "Error (in '%s' line %ld); "
1272 "evaluating long template param [%s] in Token [%s]\n",
1273 ChrPtr(pTmpl->FileName),
1275 ChrPtr(Tokens->FlatToken),
1283 while ((*pch == ' ' )||
1287 (*pch == '\n')) pch ++;
1292 if (DumpTemplateI18NStrings) {
1293 StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", Parm->Start);
1296 case TYPE_INTDEFINE: {
1299 if (GetHash(Defines, Parm->Start, Parm->len, &vPVal) &&
1303 PVal = (long*) vPVal;
1305 Parm->lvalue = *PVal;
1307 else if (strchr(Parm->Start, '|') != NULL)
1314 pToken = NewStrBufPlain (Parm->Start, Parm->len);
1315 Match = NewStrBufPlain (NULL, Parm->len);
1316 Pos = ChrPtr(pToken);
1318 while ((Pos != NULL) && (Pos != StrBufNOTNULL))
1320 StrBufExtract_NextToken(Match, pToken, &Pos, '|');
1322 if (StrLength (Match) > 0)
1324 if (GetHash(Defines, SKEY(Match), &vPVal) &&
1328 PVal = (long*) vPVal;
1330 Parm->lvalue |= *PVal;
1333 LogTemplateError(NULL, "Define",
1334 Tokens->nParameters,
1343 else if (strchr(Parm->Start, '&') != NULL)
1349 Parm->MaskBy = eAND;
1350 pToken = NewStrBufPlain (Parm->Start, Parm->len);
1351 Match = NewStrBufPlain (NULL, Parm->len);
1352 Pos = ChrPtr(pToken);
1354 while ((Pos != NULL) && (Pos != StrBufNOTNULL))
1356 StrBufExtract_NextToken(Match, pToken, &Pos, '&');
1358 if (StrLength (Match) > 0)
1360 if (GetHash(Defines, SKEY(Match), &vPVal) &&
1364 PVal = (long*) vPVal;
1366 Parm->lvalue |= *PVal;
1369 LogTemplateError(NULL, "Define",
1370 Tokens->nParameters,
1382 LogTemplateError(NULL, "Define",
1383 Tokens->nParameters,
1389 case TYPE_SUBTEMPLATE:{
1391 /* well, we don't check the mobile stuff here... */
1392 if (!GetHash(LocalTemplateCache, Parm->Start, Parm->len, &vTmpl) &&
1393 !GetHash(TemplateCache, Parm->Start, Parm->len, &vTmpl)) {
1394 LogTemplateError(NULL,
1396 Tokens->nParameters,
1398 "referenced here doesn't exist");
1406 WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
1408 const char *pTokenStart,
1409 const char *pTokenEnd,
1415 WCTemplateToken *NewToken;
1416 WCTemplputParams TP;
1418 NewToken = (WCTemplateToken*)malloc(sizeof(WCTemplateToken));
1419 memset(NewToken, 0, sizeof(WCTemplateToken));
1420 TP.Tokens = NewToken;
1421 NewToken->FileName = pTmpl->FileName; /* to print meaningfull log messages... */
1422 NewToken->Flags = 0;
1423 NewToken->Line = Line + 1;
1424 NewToken->pTokenStart = pTokenStart;
1425 NewToken->TokenStart = pTokenStart - pStart;
1426 NewToken->TokenEnd = (pTokenEnd - pStart) - NewToken->TokenStart;
1427 NewToken->pTokenEnd = pTokenEnd;
1428 NewToken->NameEnd = NewToken->TokenEnd - 2;
1429 NewToken->PreEval = NULL;
1430 NewToken->FlatToken = NewStrBufPlain(pTokenStart + 2, pTokenEnd - pTokenStart - 2);
1431 StrBufShrinkToFit(NewToken->FlatToken, 1);
1433 StrBufPeek(Buf, pTokenStart, + 1, '\0');
1434 StrBufPeek(Buf, pTokenEnd, -1, '\0');
1435 pch = NewToken->pName = pTokenStart + 2;
1437 NewToken->HaveParameters = 0;;
1438 NewToken->nParameters = 0;
1440 while (pch < pTokenEnd - 1) {
1442 StrBufPeek(Buf, pch, -1, '\0');
1443 NewToken->NameEnd = pch - NewToken->pName;
1445 if (*(pTokenEnd - 1) != ')') {
1447 NULL, "Parseerror", ERR_NAME, &TP,
1448 "Warning, Non welformed Token; missing right parenthesis");
1450 while (pch < pTokenEnd - 1) {
1451 NewToken->nParameters++;
1452 if (GetNextParameter(Buf,
1458 &NewToken->Params[NewToken->nParameters - 1]))
1460 NewToken->HaveParameters = 1;
1461 if (NewToken->nParameters > MAXPARAM) {
1463 NULL, "Parseerror", ERR_NAME, &TP,
1464 "only [%d] Params allowed in Tokens",
1467 FreeToken(&NewToken);
1473 if((NewToken->NameEnd == 1) &&
1474 (NewToken->HaveParameters == 1))
1477 if (*(NewToken->pName) == '_')
1478 NewToken->Flags = SV_GETTEXT;
1479 else if (*(NewToken->pName) == '=')
1480 NewToken->Flags = SV_SUBTEMPL;
1481 else if (*(NewToken->pName) == '%')
1482 NewToken->Flags = SV_CUST_STR_CONDITIONAL;
1483 else if (*(NewToken->pName) == '?')
1484 NewToken->Flags = SV_CONDITIONAL;
1485 else if (*(NewToken->pName) == '!')
1486 NewToken->Flags = SV_NEG_CONDITIONAL;
1492 switch (NewToken->Flags) {
1494 /* If we're able to find out more about the token, do it now while its fresh. */
1495 if (GetHash(GlobalNS, NewToken->pName, NewToken->NameEnd, &vVar)) {
1496 HashHandler *Handler;
1497 Handler = (HashHandler*) vVar;
1498 if ((NewToken->nParameters < Handler->Filter.nMinArgs) ||
1499 (NewToken->nParameters > Handler->Filter.nMaxArgs)) {
1501 NULL, "Token", ERR_NAME, &TP,
1502 "doesn't work with %d params",
1503 NewToken->nParameters);
1507 NewToken->PreEval = Handler;
1508 NewToken->Flags = SV_PREEVALUATED;
1509 if (Handler->PreEvalFunc != NULL)
1510 Handler->PreEvalFunc(NewToken);
1515 if (NewToken->nParameters !=1) {
1517 NULL, "Gettext", ERR_NAME, &TP,
1518 "requires exactly 1 parameter, you gave %d params",
1519 NewToken->nParameters);
1520 NewToken->Flags = 0;
1523 if (DumpTemplateI18NStrings) {
1524 StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", NewToken->Params[0]->Start);
1528 if (NewToken->nParameters != 1) {
1530 NULL, "Subtemplates", ERR_NAME, &TP,
1531 "require exactly 1 parameter, you gave %d params",
1532 NewToken->nParameters);
1537 /* well, we don't check the mobile stuff here... */
1538 if (!GetHash(LocalTemplateCache,
1539 NewToken->Params[0]->Start,
1540 NewToken->Params[0]->len,
1542 !GetHash(TemplateCache,
1543 NewToken->Params[0]->Start,
1544 NewToken->Params[0]->len,
1547 NULL, "SubTemplate", ERR_PARM1, &TP,
1552 case SV_CUST_STR_CONDITIONAL:
1553 case SV_CONDITIONAL:
1554 case SV_NEG_CONDITIONAL:
1555 if (NewToken->nParameters <2) {
1557 NULL, "Conditional", ERR_PARM1, &TP,
1558 "require at least 2 parameters, you gave %d params",
1559 NewToken->nParameters);
1560 NewToken->Flags = 0;
1563 if (NewToken->Params[1]->lvalue == 0) {
1565 NULL, "Conditional", ERR_PARM1, &TP,
1566 "Conditional ID (Parameter 1) mustn't be 0!");
1567 NewToken->Flags = 0;
1570 if (!GetHash(Conditionals,
1571 NewToken->Params[0]->Start,
1572 NewToken->Params[0]->len,
1575 if ((NewToken->Params[0]->len == 1) &&
1576 (NewToken->Params[0]->Start[0] == 'X'))
1579 NULL, "Conditional", ERR_PARM1, &TP,
1582 NewToken->Error = NewStrBuf();
1585 "<pre>\nConditional [%s] (in '%s' line %ld); Not found!\n[%s]\n</pre>\n",
1586 NewToken->Params[0]->Start,
1587 ChrPtr(pTmpl->FileName),
1589 ChrPtr(NewToken->FlatToken));
1593 NewToken->PreEval = vVar;
1605 * \brief Display a variable-substituted template
1606 * \param templatename template file to load
1608 void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1610 WCTemplate *NewTemplate;
1612 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1613 memset(NewTemplate, 0, sizeof(WCTemplate));
1614 NewTemplate->Data = NULL;
1615 NewTemplate->FileName = NewStrBufDup(filename);
1616 StrBufShrinkToFit(NewTemplate->FileName, 1);
1617 NewTemplate->nTokensUsed = 0;
1618 NewTemplate->TokenSpace = 0;
1619 NewTemplate->Tokens = NULL;
1620 NewTemplate->MimeType = NewStrBufPlain(GuessMimeByFilename (SKEY(NewTemplate->FileName)), -1);
1621 if (strstr(ChrPtr(NewTemplate->MimeType), "text") != NULL) {
1622 StrBufAppendBufPlain(NewTemplate->MimeType, HKEY("; charset=utf-8"), 0);
1625 if (strstr(ChrPtr(NewTemplate->MimeType), "text") != NULL) {
1626 StrBufAppendBufPlain(NewTemplate->MimeType, HKEY("; charset=utf-8"), 0);
1629 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1634 * \brief Display a variable-substituted template
1635 * \param templatename template file to load
1637 void *duplicate_template(WCTemplate *OldTemplate)
1639 WCTemplate *NewTemplate;
1641 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1642 memset(NewTemplate, 0, sizeof(WCTemplate));
1643 NewTemplate->Data = NULL;
1644 NewTemplate->FileName = NewStrBufDup(OldTemplate->FileName);
1645 StrBufShrinkToFit(NewTemplate->FileName, 1);
1646 NewTemplate->nTokensUsed = 0;
1647 NewTemplate->TokenSpace = 0;
1648 NewTemplate->Tokens = NULL;
1649 NewTemplate->MimeType = NewStrBufDup(OldTemplate->MimeType);
1654 * \brief Display a variable-substituted template
1655 * \param templatename template file to load
1657 void *load_template(WCTemplate *NewTemplate)
1660 struct stat statbuf;
1661 const char *pS, *pE, *pch, *Err;
1665 fd = open(ChrPtr(NewTemplate->FileName), O_RDONLY);
1667 lprintf(1, "ERROR: could not open template '%s' - %s\n",
1668 ChrPtr(NewTemplate->FileName), strerror(errno));
1672 if (fstat(fd, &statbuf) == -1) {
1673 lprintf(1, "ERROR: could not stat template '%s' - %s\n",
1674 ChrPtr(NewTemplate->FileName), strerror(errno));
1678 NewTemplate->Data = NewStrBufPlain(NULL, statbuf.st_size + 1);
1679 if (StrBufReadBLOB(NewTemplate->Data, &fd, 1, statbuf.st_size, &Err) < 0) {
1681 lprintf(1, "ERROR: reading template '%s' - %s<br />\n",
1682 ChrPtr(NewTemplate->FileName), strerror(errno));
1683 //FreeWCTemplate(NewTemplate);/////tODO
1689 StrBufShrinkToFit(NewTemplate->Data, 1);
1690 StrBufShrinkToFit(NewTemplate->MimeType, 1);
1691 pS = pch = ChrPtr(NewTemplate->Data);
1692 pE = pS + StrLength(NewTemplate->Data);
1694 const char *pts, *pte;
1696 int InDoubleQuotes = 0;
1698 /** Find one <? > */
1700 for (; pch < pE; pch ++) {
1701 if ((*pch=='<')&&(*(pch + 1)=='?') &&
1702 !((pch == pS) && /* we must ommit a <?xml */
1703 (*(pch + 2) == 'x') &&
1704 (*(pch + 3) == 'm') &&
1705 (*(pch + 4) == 'l')))
1707 if (*pch=='\n') Line ++;
1713 /** Found one? parse it. */
1714 for (; pch <= pE - 1; pch ++) {
1716 InDoubleQuotes = ! InDoubleQuotes;
1717 else if (*pch == '\'')
1718 InQuotes = ! InQuotes;
1719 else if ((!InQuotes && !InDoubleQuotes) &&
1720 ((*pch!='\\')&&(*(pch + 1)=='>'))) {
1728 PutNewToken(NewTemplate,
1729 NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte, Line, NewTemplate));
1736 const char* PrintTemplate(void *vSubst)
1738 WCTemplate *Tmpl = vSubst;
1740 return ChrPtr(Tmpl->FileName);
1744 int LoadTemplateDir(const StrBuf *DirName, HashList *wireless, HashList *big, const StrBuf *BaseKey)
1750 StrBuf *SubDirectory;
1751 DIR *filedir = NULL;
1752 struct dirent *filedir_entry;
1759 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
1764 filedir = opendir (ChrPtr(DirName));
1765 if (filedir == NULL) {
1770 Toplevel = StrLength(BaseKey) == 0;
1771 SubDirectory = NewStrBuf();
1772 SubKey = NewStrBuf();
1773 FileName = NewStrBufPlain(NULL, PATH_MAX);
1775 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
1776 (filedir_entry != NULL))
1780 #ifdef _DIRENT_HAVE_D_NAMELEN
1781 d_namelen = filedir_entry->d_namelen;
1782 d_type = filedir_entry->d_type;
1786 #define DT_UNKNOWN 0
1791 #define IFTODT(mode) (((mode) & 0170000) >> 12)
1792 #define DTTOIF(dirtype) ((dirtype) << 12)
1794 d_namelen = strlen(filedir_entry->d_name);
1795 d_type = DT_UNKNOWN;
1797 d_without_ext = d_namelen;
1799 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
1800 continue; /* Ignore backup files... */
1802 if ((d_namelen == 1) &&
1803 (filedir_entry->d_name[0] == '.'))
1806 if ((d_namelen == 2) &&
1807 (filedir_entry->d_name[0] == '.') &&
1808 (filedir_entry->d_name[1] == '.'))
1811 if (d_type == DT_UNKNOWN) {
1813 char path[PATH_MAX];
1814 snprintf(path, PATH_MAX, "%s/%s",
1815 ChrPtr(DirName), filedir_entry->d_name);
1816 if (stat(path, &s) == 0) {
1817 d_type = IFTODT(s.st_mode);
1823 /* Skip directories we are not interested in... */
1824 if (strcmp(filedir_entry->d_name, ".svn") == 0)
1827 FlushStrBuf(SubKey);
1829 /* If we're not toplevel, the upper dirs count as foo_bar_<local name>*/
1830 StrBufAppendBuf(SubKey, BaseKey, 0);
1831 StrBufAppendBufPlain(SubKey, HKEY("_"), 0);
1833 StrBufAppendBufPlain(SubKey, filedir_entry->d_name, d_namelen, 0);
1835 FlushStrBuf(SubDirectory);
1836 StrBufAppendBuf(SubDirectory, DirName, 0);
1837 if (ChrPtr(SubDirectory)[StrLength(SubDirectory) - 1] != '/')
1838 StrBufAppendBufPlain(SubDirectory, HKEY("/"), 0);
1839 StrBufAppendBufPlain(SubDirectory, filedir_entry->d_name, d_namelen, 0);
1841 LoadTemplateDir(SubDirectory, wireless, big, SubKey);
1844 case DT_LNK: /* TODO: check whether its a file or a directory */
1848 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1850 if ((d_without_ext == 0) || (d_namelen < 3))
1852 if (((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~') ||
1853 (strcmp(&filedir_entry->d_name[d_without_ext], ".orig") == 0) ||
1854 (strcmp(&filedir_entry->d_name[d_without_ext], ".swp") == 0))
1855 continue; /* Ignore backup files... */
1856 /* .m.xxx is for mobile useragents! */
1858 if (d_without_ext > 2)
1859 IsMobile = (filedir_entry->d_name[d_without_ext - 1] == 'm') &&
1860 (filedir_entry->d_name[d_without_ext - 2] == '.');
1861 PStart = filedir_entry->d_name;
1862 StrBufPrintf(FileName, "%s/%s", ChrPtr(DirName), filedir_entry->d_name);
1863 MinorPtr = strchr(filedir_entry->d_name, '.');
1864 if (MinorPtr != NULL)
1868 /* If we're not toplevel, the upper dirs count as foo_bar_<local name>*/
1869 StrBufAppendBuf(Key, BaseKey, 0);
1870 StrBufAppendBufPlain(Key, HKEY("_"), 0);
1872 StrBufAppendBufPlain(Key, filedir_entry->d_name, MinorPtr - filedir_entry->d_name, 0);
1874 if (LoadTemplates >= 1)
1875 lprintf(1, "%s %d %s\n", ChrPtr(FileName), IsMobile, ChrPtr(Key));
1876 prepare_template(FileName, Key, (IsMobile)?wireless:big);
1883 FreeStrBuf(&FileName);
1885 FreeStrBuf(&SubDirectory);
1886 FreeStrBuf(&SubKey);
1890 void InitTemplateCache(void)
1895 HashList *Templates[4];
1900 /* Primary Template set... */
1901 StrBufPrintf(Dir, "%s/t", static_dirs[0]);
1902 LoadTemplateDir(Dir,
1903 WirelessTemplateCache,
1907 /* User local Template set */
1908 StrBufPrintf(Dir, "%s/t", static_dirs[1]);
1909 LoadTemplateDir(Dir,
1910 WirelessLocalTemplateCache,
1914 /* Debug Templates, just to be loaded while debugging. */
1916 StrBufPrintf(Dir, "%s/dbg", static_dirs[0]);
1917 LoadTemplateDir(Dir,
1918 WirelessTemplateCache,
1921 Templates[0] = WirelessTemplateCache;
1922 Templates[1] = TemplateCache;
1923 Templates[2] = WirelessLocalTemplateCache;
1924 Templates[3] = LocalTemplateCache;
1927 if (LoadTemplates == 0)
1928 for (i=0; i < 4; i++) {
1934 At = GetNewHashPos(Templates[i], 0);
1935 while (GetNextHashPos(Templates[i],
1940 (vTemplate != NULL))
1942 load_template((WCTemplate *)vTemplate);
1954 /*-----------------------------------------------------------------------------
1955 * Filling & processing Templates
1958 * \brief executes one token
1959 * \param Target buffer to append to
1960 * \param Token da to process.
1961 * \param Template we're iterating
1962 * \param Context Contextpoointer to pass in
1963 * \param state are we in conditional state?
1964 * \param ContextType what type of information does context giv us?
1966 int EvaluateToken(StrBuf *Target, int state, WCTemplputParams *TP)
1968 const char *AppendMe;
1970 HashHandler *Handler;
1973 /* much output, since pName is not terminated...
1974 lprintf(1,"Doing token: %s\n",Token->pName);
1977 switch (TP->Tokens->Flags) {
1979 TmplGettext(Target, TP);
1981 case SV_CONDITIONAL: /** Forward conditional evaluation */
1982 return EvaluateConditional(Target, 1, state, TP);
1984 case SV_NEG_CONDITIONAL: /** Reverse conditional evaluation */
1985 return EvaluateConditional(Target, 0, state, TP);
1987 case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
1988 if (TP->Tokens->nParameters >= 6) {
1989 if (EvaluateConditional(Target, 0, state, TP)) {
1990 GetTemplateTokenString(Target, TP, 5, &AppendMe, &AppendMeLen);
1991 StrBufAppendBufPlain(Target,
1997 GetTemplateTokenString(Target, TP, 4, &AppendMe, &AppendMeLen);
1998 StrBufAppendBufPlain(Target,
2006 Target, "Conditional", ERR_NAME, TP,
2007 "needs at least 6 Params!");
2011 if (TP->Tokens->nParameters == 1)
2012 DoTemplate(TKEY(0), Target, TP);
2014 case SV_PREEVALUATED:
2015 Handler = (HashHandler*) TP->Tokens->PreEval;
2016 if (!CheckContext(Target, &Handler->Filter, TP, "Token")) {
2019 Handler->HandlerFunc(Target, TP);
2022 if (GetHash(GlobalNS, TP->Tokens->pName, TP->Tokens->NameEnd, &vVar)) {
2023 Handler = (HashHandler*) vVar;
2024 if (!CheckContext(Target, &Handler->Filter, TP, "Token")) {
2028 Handler->HandlerFunc(Target, TP);
2032 print_value_of(Target, TP);
2040 const StrBuf *ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, WCTemplputParams *CallingTP)
2042 WCTemplate *pTmpl = Tmpl;
2045 const char *pData, *pS;
2047 WCTemplputParams TP;
2049 memcpy(&TP.Filter, &CallingTP->Filter, sizeof(ContextFilter));
2051 TP.Context = CallingTP->Context;
2052 TP.ControlContext = CallingTP->ControlContext;
2054 if (LoadTemplates != 0) {
2055 if (LoadTemplates > 1)
2056 lprintf(1, "DBG: ----- loading: [%s] ------ \n",
2057 ChrPtr(Tmpl->FileName));
2058 pTmpl = duplicate_template(Tmpl);
2059 if(load_template(pTmpl) == NULL) {
2062 "<pre>\nError loading Template [%s]\n See Logfile for details\n</pre>\n",
2063 ChrPtr(Tmpl->FileName));
2064 FreeWCTemplate(pTmpl);
2070 pS = pData = ChrPtr(pTmpl->Data);
2071 len = StrLength(pTmpl->Data);
2075 if (i >= pTmpl->nTokensUsed) {
2076 StrBufAppendBufPlain(Target,
2078 len - (pData - pS), 0);
2082 StrBufAppendBufPlain(
2084 pTmpl->Tokens[i]->pTokenStart - pData, 0);
2085 TP.Tokens = pTmpl->Tokens[i];
2086 TP.nArgs = pTmpl->Tokens[i]->nParameters;
2087 state = EvaluateToken(Target, state, &TP);
2089 while ((state != 0) && (i+1 < pTmpl->nTokensUsed)) {
2090 /* condition told us to skip till its end condition */
2092 TP.Tokens = pTmpl->Tokens[i];
2093 TP.nArgs = pTmpl->Tokens[i]->nParameters;
2094 if ((pTmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
2095 (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
2096 if (state == EvaluateConditional(
2098 pTmpl->Tokens[i]->Flags,
2104 pData = pTmpl->Tokens[i++]->pTokenEnd + 1;
2105 if (i > pTmpl->nTokensUsed)
2109 if (LoadTemplates != 0) {
2110 FreeWCTemplate(pTmpl);
2112 return Tmpl->MimeType;
2117 * \brief Display a variable-substituted template
2118 * \param templatename template file to load
2119 * \returns the mimetype of the template its doing
2121 const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP)
2123 WCTemplputParams LocalTP;
2125 HashList *StaticLocal;
2131 memset(&LocalTP, 0, sizeof(WCTemplputParams));
2135 if (WC->is_mobile > 0) {
2136 Static = WirelessTemplateCache;
2137 StaticLocal = WirelessLocalTemplateCache;
2140 Static = TemplateCache;
2141 StaticLocal = LocalTemplateCache;
2146 lprintf (1, "Can't to load a template with empty name!\n");
2147 StrBufAppendPrintf(Target, "<pre>\nCan't to load a template with empty name!\n</pre>");
2151 if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
2152 !GetHash(Static, templatename, len, &vTmpl)) {
2153 lprintf (1, "didn't find Template [%s] %ld %ld\n", templatename, len , (long)strlen(templatename));
2154 StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>",
2156 (long)strlen(templatename));
2158 dbg_PrintHash(Static, PrintTemplate, NULL);
2159 PrintHash(Static, VarPrintTransition, PrintTemplate);
2165 return ProcessTemplate(vTmpl, Target, TP);
2170 void tmplput_Comment(StrBuf *Target, WCTemplputParams *TP)
2172 if (LoadTemplates != 0)
2178 GetTemplateTokenString(Target, TP, 0, &pch, &len);
2179 Comment = NewStrBufPlain(pch, len);
2180 StrBufAppendBufPlain(Target, HKEY("<!--"), 0);
2181 StrBufAppendTemplate(Target, TP, Comment, 1);
2182 StrBufAppendBufPlain(Target, HKEY("-->"), 0);
2183 FreeStrBuf(&Comment);
2187 /*-----------------------------------------------------------------------------
2190 typedef struct _HashIterator {
2191 HashList *StaticList;
2192 int AdditionalParams;
2194 int XPectContextType;
2196 RetrieveHashlistFunc GetHash;
2197 HashDestructorFunc Destructor;
2198 SubTemplFunc DoSubTemplate;
2201 void RegisterITERATOR(const char *Name, long len,
2202 int AdditionalParams,
2203 HashList *StaticList,
2204 RetrieveHashlistFunc GetHash,
2205 SubTemplFunc DoSubTempl,
2206 HashDestructorFunc Destructor,
2208 int XPectContextType,
2213 It = (HashIterator*)malloc(sizeof(HashIterator));
2214 memset(It, 0, sizeof(HashIterator));
2215 It->StaticList = StaticList;
2216 It->AdditionalParams = AdditionalParams;
2217 It->GetHash = GetHash;
2218 It->DoSubTemplate = DoSubTempl;
2219 It->Destructor = Destructor;
2220 It->ContextType = ContextType;
2221 It->XPectContextType = XPectContextType;
2223 Put(Iterators, Name, len, It, NULL);
2226 typedef struct _iteratestruct {
2235 int preeval_iterate(WCTemplateToken *Token)
2237 WCTemplputParams TPP;
2238 WCTemplputParams *TP;
2243 memset(&TPP, 0, sizeof(WCTemplputParams));
2246 if (!GetHash(Iterators, TKEY(0), &vIt)) {
2248 NULL, "Iterator", ERR_PARM1, TP,
2252 if (TP->Tokens->Params[1]->Type != TYPE_SUBTEMPLATE) {
2253 LogTemplateError(NULL, "Iterator", ERR_PARM1, TP,
2254 "Need token with type Subtemplate as param 1, have %s",
2255 TP->Tokens->Params[1]->Start);
2258 /* well, we don't check the mobile stuff here... */
2259 if (!GetHash(LocalTemplateCache, TKEY(1), &vTmpl) &&
2260 !GetHash(TemplateCache, TKEY(1), &vTmpl)) {
2261 LogTemplateError(NULL, "SubTemplate", ERR_PARM1, TP,
2262 "referenced here doesn't exist");
2264 Token->Preeval2 = vIt;
2265 It = (HashIterator *) vIt;
2267 if (TP->Tokens->nParameters < It->AdditionalParams + 2) {
2269 NULL, "Iterator", ERR_PARM1, TP,
2270 "doesn't work with %d params",
2271 TP->Tokens->nParameters);
2278 void tmpl_iterate_subtmpl(StrBuf *Target, WCTemplputParams *TP)
2283 SortStruct *SortBy = NULL;
2285 int DetectGroupChange = 0;
2288 void *vLastContext = NULL;
2290 WCTemplputParams SubTP;
2291 IterateStruct Status;
2297 memset(&Status, 0, sizeof(IterateStruct));
2298 memcpy (&SubTP, &TP, sizeof(WCTemplputParams));
2300 It = (HashIterator*) TP->Tokens->Preeval2;
2303 Target, "Iterator", ERR_PARM1, TP, "Unknown!");
2307 if (TP->Tokens->nParameters < It->AdditionalParams + 2) {
2309 Target, "Iterator", ERR_PARM1, TP,
2310 "doesn't work with %d params",
2311 TP->Tokens->nParameters - 1);
2315 if ((It->XPectContextType != CTX_NONE) &&
2316 (It->XPectContextType != TP->Filter.ContextType)) {
2318 Target, "Iterator", ERR_PARM1, TP,
2319 "requires context of type %s, have %s",
2320 ContextName(It->XPectContextType),
2321 ContextName(TP->Filter.ContextType));
2326 if (It->StaticList == NULL)
2327 List = It->GetHash(Target, TP);
2329 List = It->StaticList;
2331 DetectGroupChange = (It->Flags & IT_FLAG_DETECT_GROUPCHANGE) != 0;
2332 if (DetectGroupChange) {
2333 const StrBuf *BSort;
2334 DetectGroupChange = 0;
2335 if (havebstr("SortBy")) {
2336 BSort = sbstr("SortBy");
2337 if (GetHash(SortHash, SKEY(BSort), &vSortBy) &&
2338 (vSortBy != NULL)) {
2339 SortBy = (SortStruct*)vSortBy;
2340 /** Ok, its us, lets see in which direction we should sort... */
2341 if (havebstr("SortOrder")) {
2343 SortOrder = LBSTR("SortOrder");
2345 DetectGroupChange = 1;
2350 nMembersUsed = GetCount(List);
2351 SubBuf = NewStrBuf();
2352 SubTP.Filter.ContextType = It->ContextType;
2353 SubTP.Filter.ControlContextType = CTX_ITERATE;
2354 SubTP.ControlContext = &Status;
2356 if (HAVE_PARAM(2)) {
2357 StartAt = GetTemplateTokenNumber(Target, TP, 2, 0);
2359 if (HAVE_PARAM(3)) {
2360 StepWidth = GetTemplateTokenNumber(Target, TP, 3, 0);
2362 if (HAVE_PARAM(4)) {
2363 StopAt = GetTemplateTokenNumber(Target, TP, 4, -1);
2365 it = GetNewHashPos(List, StepWidth);
2367 StopAt = GetCount(List);
2369 while (GetNextHashPos(List, it, &Status.KeyLen, &Status.Key, &vContext)) {
2370 if ((Status.n >= StartAt) && (Status.n <= StopAt)) {
2371 if (DetectGroupChange && Status.n > 0) {
2372 Status.GroupChange = SortBy->GroupChange(vContext, vLastContext);
2374 Status.LastN = (Status.n + 1) == nMembersUsed;
2375 SubTP.Context = vContext;
2376 if (It->DoSubTemplate != NULL)
2377 It->DoSubTemplate(SubBuf, &SubTP);
2378 DoTemplate(TKEY(1), SubBuf, &SubTP);
2380 StrBufAppendBuf(Target, SubBuf, 0);
2381 FlushStrBuf(SubBuf);
2382 Status.oddeven = ! Status.oddeven;
2383 vLastContext = vContext;
2387 FreeStrBuf(&SubBuf);
2389 if (It->Destructor != NULL)
2390 It->Destructor(&List);
2394 int conditional_ITERATE_ISGROUPCHANGE(StrBuf *Target, WCTemplputParams *TP)
2396 IterateStruct *Ctx = CCTX;
2397 if (TP->Tokens->nParameters < 3)
2398 return Ctx->GroupChange;
2400 return TP->Tokens->Params[2]->lvalue == Ctx->GroupChange;
2403 void tmplput_ITERATE_ODDEVEN(StrBuf *Target, WCTemplputParams *TP)
2405 IterateStruct *Ctx = CCTX;
2407 StrBufAppendBufPlain(Target, HKEY("odd"), 0);
2409 StrBufAppendBufPlain(Target, HKEY("even"), 0);
2413 void tmplput_ITERATE_KEY(StrBuf *Target, WCTemplputParams *TP)
2415 IterateStruct *Ctx = CCTX;
2417 StrBufAppendBufPlain(Target, Ctx->Key, Ctx->KeyLen, 0);
2421 void tmplput_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
2423 IterateStruct *Ctx = CCTX;
2424 StrBufAppendPrintf(Target, "%d", Ctx->n);
2427 int conditional_ITERATE_FIRSTN(StrBuf *Target, WCTemplputParams *TP)
2429 IterateStruct *Ctx = CCTX;
2433 int conditional_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
2435 IterateStruct *Ctx = CCTX;
2441 /*-----------------------------------------------------------------------------
2444 int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP)
2446 ConditionalStruct *Cond;
2448 if ((TP->Tokens->Params[0]->len == 1) &&
2449 (TP->Tokens->Params[0]->Start[0] == 'X'))
2450 return (state != 0)?TP->Tokens->Params[1]->lvalue:0;
2452 Cond = (ConditionalStruct *) TP->Tokens->PreEval;
2455 Target, "Conditional", ERR_PARM1, TP,
2460 if (!CheckContext(Target, &Cond->Filter, TP, "Conditional")) {
2464 if (Cond->CondF(Target, TP) == Neg)
2465 return TP->Tokens->Params[1]->lvalue;
2469 int ConditionalVar(StrBuf *Target, WCTemplputParams *TP)
2474 if (!GetHash(WC->vars, TKEY(2), &vsubst))
2476 subst = (wcsubst*) vsubst;
2478 switch(subst->wcs_type) {
2480 return (subst->wcs_function!=NULL);
2482 lprintf(1, " -> Server [%s]\n", subst->wcs_value);/* TODO */
2486 case WCS_STRBUF_REF:
2487 if (TP->Tokens->nParameters < 4)
2489 return (strcmp(TP->Tokens->Params[3]->Start, ChrPtr(subst->wcs_value)) == 0);
2491 if (TP->Tokens->nParameters < 4)
2492 return (subst->lvalue != 0);
2493 return (subst->lvalue == TP->Tokens->Params[3]->lvalue);
2495 lprintf(1," WARNING: invalid type: [%ld]!\n", subst->wcs_type);
2501 void RegisterConditional(const char *Name, long len,
2503 WCConditionalFunc CondF,
2504 int ContextRequired)
2506 ConditionalStruct *Cond;
2508 Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
2509 memset(Cond, 0, sizeof(ConditionalStruct));
2510 Cond->PlainName = Name;
2511 Cond->Filter.nMaxArgs = nParams;
2512 Cond->Filter.nMinArgs = nParams;
2513 Cond->CondF = CondF;
2514 Cond->Filter.ContextType = ContextRequired;
2515 Cond->Filter.ControlContextType = CTX_NONE;
2516 Put(Conditionals, Name, len, Cond, NULL);
2519 void RegisterControlConditional(const char *Name, long len,
2521 WCConditionalFunc CondF,
2522 int ControlContextRequired)
2524 ConditionalStruct *Cond;
2526 Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
2527 memset(Cond, 0, sizeof(ConditionalStruct));
2528 Cond->PlainName = Name;
2529 Cond->Filter.nMaxArgs = nParams;
2530 Cond->Filter.nMinArgs = nParams;
2531 Cond->CondF = CondF;
2532 Cond->Filter.ContextType = CTX_NONE;
2533 Cond->Filter.ControlContextType = ControlContextRequired;
2534 Put(Conditionals, Name, len, Cond, NULL);
2537 void RegisterTokenParamDefine(const char *Name, long len,
2542 PVal = (long*)malloc(sizeof(long));
2544 Put(Defines, Name, len, PVal, NULL);
2547 long GetTokenDefine(const char *Name, long len,
2552 if (GetHash(Defines, Name, len, &vPVal) &&
2555 return *(long*) vPVal;
2563 void tmplput_DefStr(StrBuf *Target, WCTemplputParams *TP)
2567 GetTemplateTokenString(Target, TP, 2, &Str, &len);
2569 StrBufAppendBufPlain(Target, Str, len, 0);
2572 void tmplput_DefVal(StrBuf *Target, WCTemplputParams *TP)
2576 val = GetTemplateTokenNumber(Target, TP, 0, 0);
2577 StrBufAppendPrintf(Target, "%d", val);
2582 /*-----------------------------------------------------------------------------
2585 void tmplput_ContextString(StrBuf *Target, WCTemplputParams *TP)
2587 StrBufAppendTemplate(Target, TP, (StrBuf*)CTX, 0);
2589 int ConditionalContextStr(StrBuf *Target, WCTemplputParams *TP)
2591 StrBuf *TokenText = (StrBuf*) CTX;
2592 const char *CompareToken;
2595 GetTemplateTokenString(Target, TP, 2, &CompareToken, &len);
2596 return strcmp(ChrPtr(TokenText), CompareToken) == 0;
2599 void tmplput_ContextStringArray(StrBuf *Target, WCTemplputParams *TP)
2601 HashList *Arr = (HashList*) CTX;
2605 val = GetTemplateTokenNumber(Target, TP, 0, 0);
2606 if (GetHash(Arr, IKEY(val), &pV) &&
2608 StrBufAppendTemplate(Target, TP, (StrBuf*)pV, 1);
2611 int ConditionalContextStrinArray(StrBuf *Target, WCTemplputParams *TP)
2613 HashList *Arr = (HashList*) CTX;
2616 const char *CompareToken;
2619 GetTemplateTokenString(Target, TP, 2, &CompareToken, &len);
2620 val = GetTemplateTokenNumber(Target, TP, 0, 0);
2621 if (GetHash(Arr, IKEY(val), &pV) &&
2623 return strcmp(ChrPtr((StrBuf*)pV), CompareToken) == 0;
2629 /*-----------------------------------------------------------------------------
2633 void tmpl_do_boxed(StrBuf *Target, WCTemplputParams *TP)
2635 WCTemplputParams SubTP;
2637 StrBuf *Headline = NULL;
2638 if (TP->Tokens->nParameters == 2) {
2639 if (TP->Tokens->Params[1]->Type == TYPE_STR) {
2640 Headline = NewStrBuf();
2641 DoTemplate(TKEY(1), Headline, TP);
2646 GetTemplateTokenString(Target,
2651 Headline = NewStrBufPlain(Ch, len);
2654 /* else TODO error? logging? */
2655 memcpy (&SubTP, TP, sizeof(WCTemplputParams));
2656 SubTP.Context = Headline;
2657 SubTP.Filter.ContextType = CTX_STRBUF;
2658 DoTemplate(HKEY("beginbox"), Target, &SubTP);
2659 DoTemplate(TKEY(0), Target, TP);
2660 DoTemplate(HKEY("endbox"), Target, TP);
2661 FreeStrBuf(&Headline);
2664 /*-----------------------------------------------------------------------------
2668 typedef struct _tab_struct {
2673 int preeval_do_tabbed(WCTemplateToken *Token)
2675 WCTemplputParams TPP;
2676 WCTemplputParams *TP;
2682 memset(&TPP, 0, sizeof(WCTemplputParams));
2685 nTabs = TP->Tokens->nParameters / 2 - 1;
2686 if (TP->Tokens->nParameters % 2 != 0)
2688 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2689 "need even number of arguments");
2693 else for (i = 0; i < nTabs; i++) {
2694 if (!HaveTemplateTokenString(NULL,
2699 (TP->Tokens->Params[i * 2]->len == 0))
2701 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2702 "Tab-Subject %d needs to be able to produce a string, have %s",
2703 i, TP->Tokens->Params[i * 2]->Start);
2706 if (!HaveTemplateTokenString(NULL,
2711 (TP->Tokens->Params[i * 2 + 1]->len == 0))
2713 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2714 "Tab-Content %d needs to be able to produce a string, have %s",
2715 i, TP->Tokens->Params[i * 2 + 1]->Start);
2720 if (!HaveTemplateTokenString(NULL,
2725 (TP->Tokens->Params[i * 2 + 1]->len == 0))
2727 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2728 "Tab-Content %d needs to be able to produce a string, have %s",
2729 i, TP->Tokens->Params[i * 2 + 1]->Start);
2736 void tmpl_do_tabbed(StrBuf *Target, WCTemplputParams *TP)
2739 int i, ntabs, nTabs;
2741 WCTemplputParams SubTP;
2743 memset(&TS, 0, sizeof(tab_struct));
2744 memcpy (&SubTP, &TP, sizeof(WCTemplputParams));
2746 nTabs = ntabs = TP->Tokens->nParameters / 2;
2747 TabNames = (StrBuf **) malloc(ntabs * sizeof(StrBuf*));
2748 memset(TabNames, 0, ntabs * sizeof(StrBuf*));
2750 for (i = 0; i < ntabs; i++) {
2751 if ((TP->Tokens->Params[i * 2]->Type == TYPE_STR) &&
2752 (TP->Tokens->Params[i * 2]->len > 0)) {
2753 TabNames[i] = NewStrBuf();
2754 DoTemplate(TKEY(i * 2), TabNames[i], TP);
2756 else if (TP->Tokens->Params[i * 2]->Type == TYPE_GETTEXT) {
2759 GetTemplateTokenString(Target,
2764 TabNames[i] = NewStrBufPlain(Ch, -1);
2767 /** A Tab without subject? we can't count that, add it as silent */
2771 memcpy (&SubTP, TP, sizeof(WCTemplputParams));
2772 SubTP.Filter.ControlContextType = CTX_TAB;
2773 SubTP.ControlContext = &TS;
2775 StrTabbedDialog(Target, nTabs, TabNames);
2776 for (i = 0; i < ntabs; i++) {
2777 memset(&TS, 0, sizeof(tab_struct));
2779 TS.TabTitle = TabNames[i];
2780 StrBeginTab(Target, i, nTabs, TabNames);
2781 DoTemplate(TKEY(i * 2 + 1), Target, &SubTP);
2782 StrEndTab(Target, i, nTabs);
2784 for (i = 0; i < ntabs; i++)
2785 FreeStrBuf(&TabNames[i]);
2788 void tmplput_TAB_N(StrBuf *Target, WCTemplputParams *TP)
2790 tab_struct *Ctx = CCTX;
2792 StrBufAppendPrintf(Target, "%d", Ctx->CurrentTab);
2795 void tmplput_TAB_TITLE(StrBuf *Target, WCTemplputParams *TP)
2797 tab_struct *Ctx = CCTX;
2798 StrBufAppendTemplate(Target, TP, Ctx->TabTitle, 0);
2801 /*-----------------------------------------------------------------------------
2806 void RegisterSortFunc(const char *name, long len,
2807 const char *prepend, long preplen,
2808 CompareFunc Forward,
2809 CompareFunc Reverse,
2810 CompareFunc GroupChange,
2813 SortStruct *NewSort;
2815 NewSort = (SortStruct*) malloc(sizeof(SortStruct));
2816 memset(NewSort, 0, sizeof(SortStruct));
2817 NewSort->Name = NewStrBufPlain(name, len);
2818 if (prepend != NULL)
2819 NewSort->PrefPrepend = NewStrBufPlain(prepend, preplen);
2821 NewSort->PrefPrepend = NULL;
2822 NewSort->Forward = Forward;
2823 NewSort->Reverse = Reverse;
2824 NewSort->GroupChange = GroupChange;
2825 NewSort->ContextType = ContextType;
2826 if (ContextType == CTX_NONE) {
2827 lprintf(1, "sorting requires a context. CTX_NONE won't make it.\n");
2831 Put(SortHash, name, len, NewSort, DestroySortStruct);
2834 CompareFunc RetrieveSort(WCTemplputParams *TP,
2835 const char *OtherPrefix, long OtherPrefixLen,
2836 const char *Default, long ldefault, long DefaultDirection)
2839 const StrBuf *BSort = NULL;
2842 long SortOrder = -1;
2844 if (havebstr("SortBy")) {
2845 BSort = sbstr("SortBy");
2846 if (OtherPrefix == NULL) {
2847 set_room_pref("sort", NewStrBufDup(BSort), 0);
2850 set_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen, NewStrBufDup(BSort), 0);
2853 else { /** Try to fallback to our remembered values... */
2854 if (OtherPrefix == NULL) {
2855 BSort = get_room_pref("sort");
2858 BSort = get_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen);
2861 putbstr("SortBy", NewStrBufDup(BSort));
2865 BSort = Buf = NewStrBufPlain(Default, ldefault);
2866 putbstr("SortBy", Buf);
2870 if (!GetHash(SortHash, SKEY(BSort), &vSortBy) ||
2871 (vSortBy == NULL)) {
2873 if (!GetHash(SortHash, Default, ldefault, &vSortBy) ||
2874 (vSortBy == NULL)) {
2876 NULL, "Sorting", ERR_PARM1, TP,
2877 "Illegal default sort: [%s]", Default);
2881 SortBy = (SortStruct*)vSortBy;
2883 if (SortBy->ContextType != TP->Filter.ContextType)
2886 /** Ok, its us, lets see in which direction we should sort... */
2887 if (havebstr("SortOrder")) {
2888 SortOrder = LBSTR("SortOrder");
2890 else { /** Try to fallback to our remembered values... */
2892 if (SortBy->PrefPrepend == NULL) {
2893 Buf = get_room_pref("SortOrder");
2894 SortOrder = StrTol(Buf);
2897 BSort = get_X_PREFS(HKEY("SortOrder"), OtherPrefix, OtherPrefixLen);
2901 SortOrder = DefaultDirection;
2903 Buf = NewStrBufPlain(NULL, 64);
2904 StrBufPrintf(Buf, "%ld", SortOrder);
2905 putbstr("SortOrder", Buf);
2907 switch (SortOrder) {
2912 return SortBy->Forward;
2914 return SortBy->Reverse;
2926 ConstStr SortIcons[] = {
2927 {HKEY("static/sort_none.gif")},
2928 {HKEY("static/up_pointer.gif")},
2929 {HKEY("static/down_pointer.gif")},
2932 ConstStr SortNextOrder[] = {
2939 int GetSortMetric(WCTemplputParams *TP, SortStruct **Next, SortStruct **Param, long *SortOrder, int N)
2941 int bSortError = eNOT_SPECIFIED;
2942 const StrBuf *BSort;
2947 if (!GetHash(SortHash, TKEY(0), &vSort) ||
2949 return eNO_SUCH_SORT;
2950 *Param = (SortStruct*) vSort;
2953 if (havebstr("SortBy")) {
2954 BSort = sbstr("SortBy");
2955 bSortError = eINVALID_PARAM;
2956 if ((*Param)->PrefPrepend == NULL) {
2957 set_room_pref("sort", NewStrBufDup(BSort), 0);
2960 set_X_PREFS(HKEY("sort"), TKEY(N), NewStrBufDup(BSort), 0);
2963 else { /** Try to fallback to our remembered values... */
2964 if ((*Param)->PrefPrepend == NULL) {
2965 BSort = get_room_pref("sort");
2968 BSort = get_X_PREFS(HKEY("sort"), TKEY(N));
2972 if (!GetHash(SortHash, SKEY(BSort), &vSort) ||
2976 *Next = (SortStruct*) vSort;
2978 /** Ok, its us, lets see in which direction we should sort... */
2979 if (havebstr("SortOrder")) {
2980 *SortOrder = LBSTR("SortOrder");
2982 else { /** Try to fallback to our remembered values... */
2983 if ((*Param)->PrefPrepend == NULL) {
2984 *SortOrder = StrTol(get_room_pref("SortOrder"));
2987 *SortOrder = StrTol(get_X_PREFS(HKEY("SortOrder"), TKEY(N)));
2997 void tmplput_SORT_ICON(StrBuf *Target, WCTemplputParams *TP)
3002 const ConstStr *SortIcon;
3004 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
3007 Target, "Sorter", ERR_PARM1, TP,
3008 " Sorter [%s] unknown!",
3009 TP->Tokens->Params[0]->Start);
3011 case eINVALID_PARAM:
3012 LogTemplateError(NULL, "Sorter", ERR_PARM1, TP,
3013 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
3015 case eNOT_SPECIFIED:
3017 if (Next == Param) {
3018 SortIcon = &SortIcons[SortOrder];
3020 else { /** Not Us... */
3021 SortIcon = &SortIcons[0];
3023 StrBufAppendBufPlain(Target, SortIcon->Key, SortIcon->len, 0);
3027 void tmplput_SORT_NEXT(StrBuf *Target, WCTemplputParams *TP)
3033 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
3036 Target, "Sorter", ERR_PARM1, TP,
3037 " Sorter [%s] unknown!",
3038 TP->Tokens->Params[0]->Start);
3040 case eINVALID_PARAM:
3042 NULL, "Sorter", ERR_PARM1, TP,
3043 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
3045 case eNOT_SPECIFIED:
3047 StrBufAppendBuf(Target, Param->Name, 0);
3052 void tmplput_SORT_ORDER(StrBuf *Target, WCTemplputParams *TP)
3055 const ConstStr *SortOrderStr;
3059 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
3062 Target, "Sorter", ERR_PARM1, TP,
3063 " Sorter [%s] unknown!",
3064 TP->Tokens->Params[0]->Start);
3066 case eINVALID_PARAM:
3068 NULL, "Sorter", ERR_PARM1, TP,
3069 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
3071 case eNOT_SPECIFIED:
3073 if (Next == Param) {
3074 SortOrderStr = &SortNextOrder[SortOrder];
3076 else { /** Not Us... */
3077 SortOrderStr = &SortNextOrder[0];
3079 StrBufAppendBufPlain(Target, SortOrderStr->Key, SortOrderStr->len, 0);
3084 void tmplput_long_vector(StrBuf *Target, WCTemplputParams *TP)
3086 long *LongVector = (long*) CTX;
3088 if ((TP->Tokens->Params[0]->Type == TYPE_LONG) &&
3089 (TP->Tokens->Params[0]->lvalue <= LongVector[0]))
3091 StrBufAppendPrintf(Target, "%ld", LongVector[TP->Tokens->Params[0]->lvalue]);
3095 if (TP->Tokens->Params[0]->Type != TYPE_LONG) {
3097 Target, "Longvector", ERR_NAME, TP,
3098 "needs a numerical Parameter!");
3102 Target, "LongVector", ERR_PARM1, TP,
3103 "doesn't have %ld Parameters, its just the size of %ld!",
3104 TP->Tokens->Params[0]->lvalue,
3110 void dbg_print_longvector(long *LongVector)
3112 StrBuf *Buf = NewStrBufPlain(HKEY("Longvector: ["));
3113 int nItems = LongVector[0];
3116 for (i = 0; i < nItems; i++) {
3118 StrBufAppendPrintf(Buf, "%d: %ld | ", i, LongVector[i]);
3120 StrBufAppendPrintf(Buf, "%d: %ld]\n", i, LongVector[i]);
3123 lprintf(1, ChrPtr(Buf));
3127 int ConditionalLongVector(StrBuf *Target, WCTemplputParams *TP)
3129 long *LongVector = (long*) CTX;
3131 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) &&
3132 (TP->Tokens->Params[2]->lvalue <= LongVector[0])&&
3133 (TP->Tokens->Params[3]->Type == TYPE_LONG) &&
3134 (TP->Tokens->Params[3]->lvalue <= LongVector[0]))
3136 return LongVector[TP->Tokens->Params[2]->lvalue] ==
3137 LongVector[TP->Tokens->Params[3]->lvalue];
3141 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) ||
3142 (TP->Tokens->Params[2]->Type == TYPE_LONG)) {
3144 Target, "ConditionalLongvector", ERR_PARM1, TP,
3145 "needs two long Parameter!");
3149 Target, "Longvector", ERR_PARM1, TP,
3150 "doesn't have %ld / %ld Parameters, its just the size of %ld!",
3151 TP->Tokens->Params[2]->lvalue,
3152 TP->Tokens->Params[3]->lvalue,
3160 void tmplput_CURRENT_FILE(StrBuf *Target, WCTemplputParams *TP)
3162 StrBufAppendTemplate(Target, TP, TP->Tokens->FileName, 0);
3169 memset(&NoCtx, 0, sizeof(WCTemplputParams));
3170 RegisterNamespace("--", 0, 2, tmplput_Comment, NULL, CTX_NONE);
3171 RegisterNamespace("SORT:ICON", 1, 2, tmplput_SORT_ICON, NULL, CTX_NONE);
3172 RegisterNamespace("SORT:ORDER", 1, 2, tmplput_SORT_ORDER, NULL, CTX_NONE);
3173 RegisterNamespace("SORT:NEXT", 1, 2, tmplput_SORT_NEXT, NULL, CTX_NONE);
3174 RegisterNamespace("CONTEXTSTR", 0, 1, tmplput_ContextString, NULL, CTX_STRBUF);
3175 RegisterNamespace("CONTEXTSTRARR", 1, 2, tmplput_ContextStringArray, NULL, CTX_STRBUFARR);
3176 RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl, preeval_iterate, CTX_NONE);
3177 RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed, NULL, CTX_NONE);
3178 RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed, preeval_do_tabbed, CTX_NONE);
3179 RegisterControlNS(HKEY("TAB:N"), 0, 0, tmplput_TAB_N, CTX_TAB);
3180 RegisterControlNS(HKEY("TAB:SUBJECT"), 0, 1, tmplput_TAB_TITLE, CTX_TAB);
3183 RegisterNamespace("LONGVECTOR", 1, 1, tmplput_long_vector, NULL, CTX_LONGVECTOR);
3186 RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar, CTX_NONE);
3187 RegisterConditional(HKEY("COND:CONTEXTSTR"), 3, ConditionalContextStr, CTX_STRBUF);
3188 RegisterConditional(HKEY("COND:CONTEXTSTRARR"), 4, ConditionalContextStrinArray, CTX_STRBUFARR);
3189 RegisterConditional(HKEY("COND:LONGVECTOR"), 4, ConditionalLongVector, CTX_LONGVECTOR);
3192 RegisterControlConditional(HKEY("COND:ITERATE:ISGROUPCHANGE"), 2,
3193 conditional_ITERATE_ISGROUPCHANGE,
3195 RegisterControlConditional(HKEY("COND:ITERATE:LASTN"), 2,
3196 conditional_ITERATE_LASTN,
3198 RegisterControlConditional(HKEY("COND:ITERATE:FIRSTN"), 2,
3199 conditional_ITERATE_FIRSTN,
3202 RegisterControlNS(HKEY("ITERATE:ODDEVEN"), 0, 0, tmplput_ITERATE_ODDEVEN, CTX_ITERATE);
3203 RegisterControlNS(HKEY("ITERATE:KEY"), 0, 0, tmplput_ITERATE_KEY, CTX_ITERATE);
3204 RegisterControlNS(HKEY("ITERATE:N"), 0, 0, tmplput_ITERATE_LASTN, CTX_ITERATE);
3205 RegisterNamespace("CURRENTFILE", 0, 1, tmplput_CURRENT_FILE, NULL, CTX_NONE);
3206 RegisterNamespace("DEF:STR", 1, 1, tmplput_DefStr, NULL, CTX_NONE);
3207 RegisterNamespace("DEF:VAL", 1, 1, tmplput_DefVal, NULL, CTX_NONE);
3215 ServerStartModule_SUBST
3218 WirelessTemplateCache = NewHash(1, NULL);
3219 WirelessLocalTemplateCache = NewHash(1, NULL);
3220 LocalTemplateCache = NewHash(1, NULL);
3221 TemplateCache = NewHash(1, NULL);
3223 GlobalNS = NewHash(1, NULL);
3224 Iterators = NewHash(1, NULL);
3225 Conditionals = NewHash(1, NULL);
3226 SortHash = NewHash(1, NULL);
3227 Defines = NewHash(1, NULL);
3231 FinalizeModule_SUBST
3238 ServerShutdownModule_SUBST
3241 DeleteHash(&WirelessTemplateCache);
3242 DeleteHash(&WirelessLocalTemplateCache);
3243 DeleteHash(&TemplateCache);
3244 DeleteHash(&LocalTemplateCache);
3246 DeleteHash(&GlobalNS);
3247 DeleteHash(&Iterators);
3248 DeleteHash(&Conditionals);
3249 DeleteHash(&SortHash);
3250 DeleteHash(&Defines);
3255 SessionNewModule_SUBST
3262 SessionAttachModule_SUBST
3265 sess->vars = NewHash(1,NULL);
3269 SessionDetachModule_SUBST
3272 DeleteHash(&sess->vars);
3273 FreeStrBuf(&sess->WFBuf);
3277 SessionDestroyModule_SUBST