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",
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_bactrace_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_bactrace_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);
2169 /*-----------------------------------------------------------------------------
2172 typedef struct _HashIterator {
2173 HashList *StaticList;
2174 int AdditionalParams;
2176 int XPectContextType;
2178 RetrieveHashlistFunc GetHash;
2179 HashDestructorFunc Destructor;
2180 SubTemplFunc DoSubTemplate;
2183 void RegisterITERATOR(const char *Name, long len,
2184 int AdditionalParams,
2185 HashList *StaticList,
2186 RetrieveHashlistFunc GetHash,
2187 SubTemplFunc DoSubTempl,
2188 HashDestructorFunc Destructor,
2190 int XPectContextType,
2195 It = (HashIterator*)malloc(sizeof(HashIterator));
2196 memset(It, 0, sizeof(HashIterator));
2197 It->StaticList = StaticList;
2198 It->AdditionalParams = AdditionalParams;
2199 It->GetHash = GetHash;
2200 It->DoSubTemplate = DoSubTempl;
2201 It->Destructor = Destructor;
2202 It->ContextType = ContextType;
2203 It->XPectContextType = XPectContextType;
2205 Put(Iterators, Name, len, It, NULL);
2208 typedef struct _iteratestruct {
2217 int preeval_iterate(WCTemplateToken *Token)
2219 WCTemplputParams TPP;
2220 WCTemplputParams *TP;
2225 memset(&TPP, 0, sizeof(WCTemplputParams));
2228 if (!GetHash(Iterators, TKEY(0), &vIt)) {
2230 NULL, "Iterator", ERR_PARM1, TP,
2234 if (TP->Tokens->Params[1]->Type != TYPE_SUBTEMPLATE) {
2235 LogTemplateError(NULL, "Iterator", ERR_PARM1, TP,
2236 "Need token with type Subtemplate as param 1, have %s",
2237 TP->Tokens->Params[1]->Start);
2240 /* well, we don't check the mobile stuff here... */
2241 if (!GetHash(LocalTemplateCache, TKEY(1), &vTmpl) &&
2242 !GetHash(TemplateCache, TKEY(1), &vTmpl)) {
2243 LogTemplateError(NULL, "SubTemplate", ERR_PARM1, TP,
2244 "referenced here doesn't exist");
2246 Token->Preeval2 = vIt;
2247 It = (HashIterator *) vIt;
2249 if (TP->Tokens->nParameters < It->AdditionalParams + 2) {
2251 NULL, "Iterator", ERR_PARM1, TP,
2252 "doesn't work with %d params",
2253 TP->Tokens->nParameters);
2260 void tmpl_iterate_subtmpl(StrBuf *Target, WCTemplputParams *TP)
2265 SortStruct *SortBy = NULL;
2267 int DetectGroupChange = 0;
2270 void *vLastContext = NULL;
2272 WCTemplputParams SubTP;
2273 IterateStruct Status;
2279 memset(&Status, 0, sizeof(IterateStruct));
2280 memcpy (&SubTP, &TP, sizeof(WCTemplputParams));
2282 It = (HashIterator*) TP->Tokens->Preeval2;
2285 Target, "Iterator", ERR_PARM1, TP, "Unknown!");
2289 if (TP->Tokens->nParameters < It->AdditionalParams + 2) {
2291 Target, "Iterator", ERR_PARM1, TP,
2292 "doesn't work with %d params",
2293 TP->Tokens->nParameters - 1);
2297 if ((It->XPectContextType != CTX_NONE) &&
2298 (It->XPectContextType != TP->Filter.ContextType)) {
2300 Target, "Iterator", ERR_PARM1, TP,
2301 "requires context of type %d, have %d",
2302 It->XPectContextType,
2303 TP->Filter.ContextType);
2308 if (It->StaticList == NULL)
2309 List = It->GetHash(Target, TP);
2311 List = It->StaticList;
2313 DetectGroupChange = (It->Flags & IT_FLAG_DETECT_GROUPCHANGE) != 0;
2314 if (DetectGroupChange) {
2315 const StrBuf *BSort;
2316 DetectGroupChange = 0;
2317 if (havebstr("SortBy")) {
2318 BSort = sbstr("SortBy");
2319 if (GetHash(SortHash, SKEY(BSort), &vSortBy) &&
2320 (vSortBy != NULL)) {
2321 SortBy = (SortStruct*)vSortBy;
2322 /** Ok, its us, lets see in which direction we should sort... */
2323 if (havebstr("SortOrder")) {
2325 SortOrder = LBSTR("SortOrder");
2327 DetectGroupChange = 1;
2332 nMembersUsed = GetCount(List);
2333 SubBuf = NewStrBuf();
2334 SubTP.Filter.ContextType = It->ContextType;
2335 SubTP.Filter.ControlContextType = CTX_ITERATE;
2336 SubTP.ControlContext = &Status;
2338 if (HAVE_PARAM(2)) {
2339 StartAt = GetTemplateTokenNumber(Target, TP, 2, 0);
2341 if (HAVE_PARAM(3)) {
2342 StepWidth = GetTemplateTokenNumber(Target, TP, 3, 0);
2344 if (HAVE_PARAM(4)) {
2345 StopAt = GetTemplateTokenNumber(Target, TP, 4, -1);
2347 it = GetNewHashPos(List, StepWidth);
2349 StopAt = GetCount(List);
2351 while (GetNextHashPos(List, it, &Status.KeyLen, &Status.Key, &vContext)) {
2352 if ((Status.n >= StartAt) && (Status.n <= StopAt)) {
2353 if (DetectGroupChange && Status.n > 0) {
2354 Status.GroupChange = SortBy->GroupChange(vContext, vLastContext);
2356 Status.LastN = (Status.n + 1) == nMembersUsed;
2357 SubTP.Context = vContext;
2358 if (It->DoSubTemplate != NULL)
2359 It->DoSubTemplate(SubBuf, &SubTP);
2360 DoTemplate(TKEY(1), SubBuf, &SubTP);
2362 StrBufAppendBuf(Target, SubBuf, 0);
2363 FlushStrBuf(SubBuf);
2364 Status.oddeven = ! Status.oddeven;
2365 vLastContext = vContext;
2369 FreeStrBuf(&SubBuf);
2371 if (It->Destructor != NULL)
2372 It->Destructor(&List);
2376 int conditional_ITERATE_ISGROUPCHANGE(StrBuf *Target, WCTemplputParams *TP)
2378 IterateStruct *Ctx = CCTX;
2379 if (TP->Tokens->nParameters < 3)
2380 return Ctx->GroupChange;
2382 return TP->Tokens->Params[2]->lvalue == Ctx->GroupChange;
2385 void tmplput_ITERATE_ODDEVEN(StrBuf *Target, WCTemplputParams *TP)
2387 IterateStruct *Ctx = CCTX;
2389 StrBufAppendBufPlain(Target, HKEY("odd"), 0);
2391 StrBufAppendBufPlain(Target, HKEY("even"), 0);
2395 void tmplput_ITERATE_KEY(StrBuf *Target, WCTemplputParams *TP)
2397 IterateStruct *Ctx = CCTX;
2399 StrBufAppendBufPlain(Target, Ctx->Key, Ctx->KeyLen, 0);
2403 void tmplput_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
2405 IterateStruct *Ctx = CCTX;
2406 StrBufAppendPrintf(Target, "%d", Ctx->n);
2409 int conditional_ITERATE_FIRSTN(StrBuf *Target, WCTemplputParams *TP)
2411 IterateStruct *Ctx = CCTX;
2415 int conditional_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
2417 IterateStruct *Ctx = CCTX;
2423 /*-----------------------------------------------------------------------------
2426 int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP)
2428 ConditionalStruct *Cond;
2430 if ((TP->Tokens->Params[0]->len == 1) &&
2431 (TP->Tokens->Params[0]->Start[0] == 'X'))
2432 return (state != 0)?TP->Tokens->Params[1]->lvalue:0;
2434 Cond = (ConditionalStruct *) TP->Tokens->PreEval;
2437 Target, "Conditional", ERR_PARM1, TP,
2442 if (!CheckContext(Target, &Cond->Filter, TP, "Conditional")) {
2446 if (Cond->CondF(Target, TP) == Neg)
2447 return TP->Tokens->Params[1]->lvalue;
2451 int ConditionalVar(StrBuf *Target, WCTemplputParams *TP)
2456 if (!GetHash(WC->vars, TKEY(2), &vsubst))
2458 subst = (wcsubst*) vsubst;
2460 switch(subst->wcs_type) {
2462 return (subst->wcs_function!=NULL);
2464 lprintf(1, " -> Server [%s]\n", subst->wcs_value);/* TODO */
2468 case WCS_STRBUF_REF:
2469 if (TP->Tokens->nParameters < 4)
2471 return (strcmp(TP->Tokens->Params[3]->Start, ChrPtr(subst->wcs_value)) == 0);
2473 if (TP->Tokens->nParameters < 4)
2474 return (subst->lvalue != 0);
2475 return (subst->lvalue == TP->Tokens->Params[3]->lvalue);
2477 lprintf(1," WARNING: invalid type: [%ld]!\n", subst->wcs_type);
2483 void RegisterConditional(const char *Name, long len,
2485 WCConditionalFunc CondF,
2486 int ContextRequired)
2488 ConditionalStruct *Cond;
2490 Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
2491 memset(Cond, 0, sizeof(ConditionalStruct));
2492 Cond->PlainName = Name;
2493 Cond->Filter.nMaxArgs = nParams;
2494 Cond->Filter.nMinArgs = nParams;
2495 Cond->CondF = CondF;
2496 Cond->Filter.ContextType = ContextRequired;
2497 Cond->Filter.ControlContextType = CTX_NONE;
2498 Put(Conditionals, Name, len, Cond, NULL);
2501 void RegisterControlConditional(const char *Name, long len,
2503 WCConditionalFunc CondF,
2504 int ControlContextRequired)
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 = CTX_NONE;
2515 Cond->Filter.ControlContextType = ControlContextRequired;
2516 Put(Conditionals, Name, len, Cond, NULL);
2519 void RegisterTokenParamDefine(const char *Name, long len,
2524 PVal = (long*)malloc(sizeof(long));
2526 Put(Defines, Name, len, PVal, NULL);
2529 long GetTokenDefine(const char *Name, long len,
2534 if (GetHash(Defines, Name, len, &vPVal) &&
2537 return *(long*) vPVal;
2545 void tmplput_DefStr(StrBuf *Target, WCTemplputParams *TP)
2549 GetTemplateTokenString(Target, TP, 2, &Str, &len);
2551 StrBufAppendBufPlain(Target, Str, len, 0);
2554 void tmplput_DefVal(StrBuf *Target, WCTemplputParams *TP)
2558 val = GetTemplateTokenNumber(Target, TP, 0, 0);
2559 StrBufAppendPrintf(Target, "%d", val);
2564 /*-----------------------------------------------------------------------------
2567 void tmplput_ContextString(StrBuf *Target, WCTemplputParams *TP)
2569 StrBufAppendTemplate(Target, TP, (StrBuf*)CTX, 0);
2571 int ConditionalContextStr(StrBuf *Target, WCTemplputParams *TP)
2573 StrBuf *TokenText = (StrBuf*) CTX;
2574 const char *CompareToken;
2577 GetTemplateTokenString(Target, TP, 2, &CompareToken, &len);
2578 return strcmp(ChrPtr(TokenText), CompareToken) == 0;
2581 void tmplput_ContextStringArray(StrBuf *Target, WCTemplputParams *TP)
2583 HashList *Arr = (HashList*) CTX;
2587 val = GetTemplateTokenNumber(Target, TP, 0, 0);
2588 if (GetHash(Arr, IKEY(val), &pV) &&
2590 StrBufAppendTemplate(Target, TP, (StrBuf*)pV, 1);
2593 int ConditionalContextStrinArray(StrBuf *Target, WCTemplputParams *TP)
2595 HashList *Arr = (HashList*) CTX;
2598 const char *CompareToken;
2601 GetTemplateTokenString(Target, TP, 2, &CompareToken, &len);
2602 val = GetTemplateTokenNumber(Target, TP, 0, 0);
2603 if (GetHash(Arr, IKEY(val), &pV) &&
2605 return strcmp(ChrPtr((StrBuf*)pV), CompareToken) == 0;
2611 /*-----------------------------------------------------------------------------
2615 void tmpl_do_boxed(StrBuf *Target, WCTemplputParams *TP)
2617 WCTemplputParams SubTP;
2619 StrBuf *Headline = NULL;
2620 if (TP->Tokens->nParameters == 2) {
2621 if (TP->Tokens->Params[1]->Type == TYPE_STR) {
2622 Headline = NewStrBuf();
2623 DoTemplate(TKEY(1), Headline, TP);
2628 GetTemplateTokenString(Target,
2633 Headline = NewStrBufPlain(Ch, len);
2636 /* else TODO error? logging? */
2637 memcpy (&SubTP, TP, sizeof(WCTemplputParams));
2638 SubTP.Context = Headline;
2639 SubTP.Filter.ContextType = CTX_STRBUF;
2640 DoTemplate(HKEY("beginbox"), Target, &SubTP);
2641 DoTemplate(TKEY(0), Target, TP);
2642 DoTemplate(HKEY("endbox"), Target, TP);
2643 FreeStrBuf(&Headline);
2646 /*-----------------------------------------------------------------------------
2650 typedef struct _tab_struct {
2655 int preeval_do_tabbed(WCTemplateToken *Token)
2657 WCTemplputParams TPP;
2658 WCTemplputParams *TP;
2664 memset(&TPP, 0, sizeof(WCTemplputParams));
2667 nTabs = TP->Tokens->nParameters / 2 - 1;
2668 if (TP->Tokens->nParameters % 2 != 0)
2670 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2671 "need even number of arguments");
2675 else for (i = 0; i < nTabs; i++) {
2676 if (!HaveTemplateTokenString(NULL,
2681 (TP->Tokens->Params[i * 2]->len == 0))
2683 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2684 "Tab-Subject %d needs to be able to produce a string, have %s",
2685 i, TP->Tokens->Params[i * 2]->Start);
2688 if (!HaveTemplateTokenString(NULL,
2693 (TP->Tokens->Params[i * 2 + 1]->len == 0))
2695 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2696 "Tab-Content %d needs to be able to produce a string, have %s",
2697 i, TP->Tokens->Params[i * 2 + 1]->Start);
2702 if (!HaveTemplateTokenString(NULL,
2707 (TP->Tokens->Params[i * 2 + 1]->len == 0))
2709 LogTemplateError(NULL, "TabbedApi", ERR_PARM1, TP,
2710 "Tab-Content %d needs to be able to produce a string, have %s",
2711 i, TP->Tokens->Params[i * 2 + 1]->Start);
2718 void tmpl_do_tabbed(StrBuf *Target, WCTemplputParams *TP)
2721 int i, ntabs, nTabs;
2723 WCTemplputParams SubTP;
2725 memset(&TS, 0, sizeof(tab_struct));
2726 memcpy (&SubTP, &TP, sizeof(WCTemplputParams));
2728 nTabs = ntabs = TP->Tokens->nParameters / 2;
2729 TabNames = (StrBuf **) malloc(ntabs * sizeof(StrBuf*));
2730 memset(TabNames, 0, ntabs * sizeof(StrBuf*));
2732 for (i = 0; i < ntabs; i++) {
2733 if ((TP->Tokens->Params[i * 2]->Type == TYPE_STR) &&
2734 (TP->Tokens->Params[i * 2]->len > 0)) {
2735 TabNames[i] = NewStrBuf();
2736 DoTemplate(TKEY(i * 2), TabNames[i], TP);
2738 else if (TP->Tokens->Params[i * 2]->Type == TYPE_GETTEXT) {
2741 GetTemplateTokenString(Target,
2746 TabNames[i] = NewStrBufPlain(Ch, -1);
2749 /** A Tab without subject? we can't count that, add it as silent */
2753 memcpy (&SubTP, TP, sizeof(WCTemplputParams));
2754 SubTP.Filter.ControlContextType = CTX_TAB;
2755 SubTP.ControlContext = &TS;
2757 StrTabbedDialog(Target, nTabs, TabNames);
2758 for (i = 0; i < ntabs; i++) {
2759 memset(&TS, 0, sizeof(tab_struct));
2761 TS.TabTitle = TabNames[i];
2762 StrBeginTab(Target, i, nTabs, TabNames);
2763 DoTemplate(TKEY(i * 2 + 1), Target, &SubTP);
2764 StrEndTab(Target, i, nTabs);
2766 for (i = 0; i < ntabs; i++)
2767 FreeStrBuf(&TabNames[i]);
2770 void tmplput_TAB_N(StrBuf *Target, WCTemplputParams *TP)
2772 tab_struct *Ctx = CCTX;
2774 StrBufAppendPrintf(Target, "%d", Ctx->CurrentTab);
2777 void tmplput_TAB_TITLE(StrBuf *Target, WCTemplputParams *TP)
2779 tab_struct *Ctx = CCTX;
2780 StrBufAppendTemplate(Target, TP, Ctx->TabTitle, 0);
2783 /*-----------------------------------------------------------------------------
2788 void RegisterSortFunc(const char *name, long len,
2789 const char *prepend, long preplen,
2790 CompareFunc Forward,
2791 CompareFunc Reverse,
2792 CompareFunc GroupChange,
2795 SortStruct *NewSort;
2797 NewSort = (SortStruct*) malloc(sizeof(SortStruct));
2798 memset(NewSort, 0, sizeof(SortStruct));
2799 NewSort->Name = NewStrBufPlain(name, len);
2800 if (prepend != NULL)
2801 NewSort->PrefPrepend = NewStrBufPlain(prepend, preplen);
2803 NewSort->PrefPrepend = NULL;
2804 NewSort->Forward = Forward;
2805 NewSort->Reverse = Reverse;
2806 NewSort->GroupChange = GroupChange;
2807 NewSort->ContextType = ContextType;
2808 if (ContextType == CTX_NONE) {
2809 lprintf(1, "sorting requires a context. CTX_NONE won't make it.\n");
2813 Put(SortHash, name, len, NewSort, DestroySortStruct);
2816 CompareFunc RetrieveSort(WCTemplputParams *TP,
2817 const char *OtherPrefix, long OtherPrefixLen,
2818 const char *Default, long ldefault, long DefaultDirection)
2821 const StrBuf *BSort = NULL;
2824 long SortOrder = -1;
2826 if (havebstr("SortBy")) {
2827 BSort = sbstr("SortBy");
2828 if (OtherPrefix == NULL) {
2829 set_room_pref("sort", NewStrBufDup(BSort), 0);
2832 set_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen, NewStrBufDup(BSort), 0);
2835 else { /** Try to fallback to our remembered values... */
2836 if (OtherPrefix == NULL) {
2837 BSort = get_room_pref("sort");
2840 BSort = get_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen);
2843 putbstr("SortBy", NewStrBufDup(BSort));
2847 BSort = Buf = NewStrBufPlain(Default, ldefault);
2848 putbstr("SortBy", Buf);
2852 if (!GetHash(SortHash, SKEY(BSort), &vSortBy) ||
2853 (vSortBy == NULL)) {
2855 if (!GetHash(SortHash, Default, ldefault, &vSortBy) ||
2856 (vSortBy == NULL)) {
2858 NULL, "Sorting", ERR_PARM1, TP,
2859 "Illegal default sort: [%s]", Default);
2863 SortBy = (SortStruct*)vSortBy;
2865 if (SortBy->ContextType != TP->Filter.ContextType)
2868 /** Ok, its us, lets see in which direction we should sort... */
2869 if (havebstr("SortOrder")) {
2870 SortOrder = LBSTR("SortOrder");
2872 else { /** Try to fallback to our remembered values... */
2874 if (SortBy->PrefPrepend == NULL) {
2875 Buf = get_room_pref("SortOrder");
2876 SortOrder = StrTol(Buf);
2879 BSort = get_X_PREFS(HKEY("SortOrder"), OtherPrefix, OtherPrefixLen);
2883 SortOrder = DefaultDirection;
2885 Buf = NewStrBufPlain(NULL, 64);
2886 StrBufPrintf(Buf, "%ld", SortOrder);
2887 putbstr("SortOrder", Buf);
2889 switch (SortOrder) {
2894 return SortBy->Forward;
2896 return SortBy->Reverse;
2908 ConstStr SortIcons[] = {
2909 {HKEY("static/sort_none.gif")},
2910 {HKEY("static/up_pointer.gif")},
2911 {HKEY("static/down_pointer.gif")},
2914 ConstStr SortNextOrder[] = {
2921 int GetSortMetric(WCTemplputParams *TP, SortStruct **Next, SortStruct **Param, long *SortOrder, int N)
2923 int bSortError = eNOT_SPECIFIED;
2924 const StrBuf *BSort;
2929 if (!GetHash(SortHash, TKEY(0), &vSort) ||
2931 return eNO_SUCH_SORT;
2932 *Param = (SortStruct*) vSort;
2935 if (havebstr("SortBy")) {
2936 BSort = sbstr("SortBy");
2937 bSortError = eINVALID_PARAM;
2938 if ((*Param)->PrefPrepend == NULL) {
2939 set_room_pref("sort", NewStrBufDup(BSort), 0);
2942 set_X_PREFS(HKEY("sort"), TKEY(N), NewStrBufDup(BSort), 0);
2945 else { /** Try to fallback to our remembered values... */
2946 if ((*Param)->PrefPrepend == NULL) {
2947 BSort = get_room_pref("sort");
2950 BSort = get_X_PREFS(HKEY("sort"), TKEY(N));
2954 if (!GetHash(SortHash, SKEY(BSort), &vSort) ||
2958 *Next = (SortStruct*) vSort;
2960 /** Ok, its us, lets see in which direction we should sort... */
2961 if (havebstr("SortOrder")) {
2962 *SortOrder = LBSTR("SortOrder");
2964 else { /** Try to fallback to our remembered values... */
2965 if ((*Param)->PrefPrepend == NULL) {
2966 *SortOrder = StrTol(get_room_pref("SortOrder"));
2969 *SortOrder = StrTol(get_X_PREFS(HKEY("SortOrder"), TKEY(N)));
2979 void tmplput_SORT_ICON(StrBuf *Target, WCTemplputParams *TP)
2984 const ConstStr *SortIcon;
2986 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2989 Target, "Sorter", ERR_PARM1, TP,
2990 " Sorter [%s] unknown!",
2991 TP->Tokens->Params[0]->Start);
2993 case eINVALID_PARAM:
2994 LogTemplateError(NULL, "Sorter", ERR_PARM1, TP,
2995 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2997 case eNOT_SPECIFIED:
2999 if (Next == Param) {
3000 SortIcon = &SortIcons[SortOrder];
3002 else { /** Not Us... */
3003 SortIcon = &SortIcons[0];
3005 StrBufAppendBufPlain(Target, SortIcon->Key, SortIcon->len, 0);
3009 void tmplput_SORT_NEXT(StrBuf *Target, WCTemplputParams *TP)
3015 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
3018 Target, "Sorter", ERR_PARM1, TP,
3019 " Sorter [%s] unknown!",
3020 TP->Tokens->Params[0]->Start);
3022 case eINVALID_PARAM:
3024 NULL, "Sorter", ERR_PARM1, TP,
3025 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
3027 case eNOT_SPECIFIED:
3029 StrBufAppendBuf(Target, Param->Name, 0);
3034 void tmplput_SORT_ORDER(StrBuf *Target, WCTemplputParams *TP)
3037 const ConstStr *SortOrderStr;
3041 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
3044 Target, "Sorter", ERR_PARM1, TP,
3045 " Sorter [%s] unknown!",
3046 TP->Tokens->Params[0]->Start);
3048 case eINVALID_PARAM:
3050 NULL, "Sorter", ERR_PARM1, TP,
3051 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
3053 case eNOT_SPECIFIED:
3055 if (Next == Param) {
3056 SortOrderStr = &SortNextOrder[SortOrder];
3058 else { /** Not Us... */
3059 SortOrderStr = &SortNextOrder[0];
3061 StrBufAppendBufPlain(Target, SortOrderStr->Key, SortOrderStr->len, 0);
3066 void tmplput_long_vector(StrBuf *Target, WCTemplputParams *TP)
3068 long *LongVector = (long*) CTX;
3070 if ((TP->Tokens->Params[0]->Type == TYPE_LONG) &&
3071 (TP->Tokens->Params[0]->lvalue <= LongVector[0]))
3073 StrBufAppendPrintf(Target, "%ld", LongVector[TP->Tokens->Params[0]->lvalue]);
3077 if (TP->Tokens->Params[0]->Type != TYPE_LONG) {
3079 Target, "Longvector", ERR_NAME, TP,
3080 "needs a numerical Parameter!");
3084 Target, "LongVector", ERR_PARM1, TP,
3085 "doesn't have %ld Parameters, its just the size of %ld!",
3086 TP->Tokens->Params[0]->lvalue,
3092 void dbg_print_longvector(long *LongVector)
3094 StrBuf *Buf = NewStrBufPlain(HKEY("Longvector: ["));
3095 int nItems = LongVector[0];
3098 for (i = 0; i < nItems; i++) {
3100 StrBufAppendPrintf(Buf, "%d: %ld | ", i, LongVector[i]);
3102 StrBufAppendPrintf(Buf, "%d: %ld]\n", i, LongVector[i]);
3105 lprintf(1, ChrPtr(Buf));
3109 int ConditionalLongVector(StrBuf *Target, WCTemplputParams *TP)
3111 long *LongVector = (long*) CTX;
3113 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) &&
3114 (TP->Tokens->Params[2]->lvalue <= LongVector[0])&&
3115 (TP->Tokens->Params[3]->Type == TYPE_LONG) &&
3116 (TP->Tokens->Params[3]->lvalue <= LongVector[0]))
3118 return LongVector[TP->Tokens->Params[2]->lvalue] ==
3119 LongVector[TP->Tokens->Params[3]->lvalue];
3123 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) ||
3124 (TP->Tokens->Params[2]->Type == TYPE_LONG)) {
3126 Target, "ConditionalLongvector", ERR_PARM1, TP,
3127 "needs two long Parameter!");
3131 Target, "Longvector", ERR_PARM1, TP,
3132 "doesn't have %ld / %ld Parameters, its just the size of %ld!",
3133 TP->Tokens->Params[2]->lvalue,
3134 TP->Tokens->Params[3]->lvalue,
3142 void tmplput_CURRENT_FILE(StrBuf *Target, WCTemplputParams *TP)
3144 StrBufAppendTemplate(Target, TP, TP->Tokens->FileName, 0);
3151 memset(&NoCtx, 0, sizeof(WCTemplputParams));
3152 RegisterNamespace("SORT:ICON", 1, 2, tmplput_SORT_ICON, NULL, CTX_NONE);
3153 RegisterNamespace("SORT:ORDER", 1, 2, tmplput_SORT_ORDER, NULL, CTX_NONE);
3154 RegisterNamespace("SORT:NEXT", 1, 2, tmplput_SORT_NEXT, NULL, CTX_NONE);
3155 RegisterNamespace("CONTEXTSTR", 0, 1, tmplput_ContextString, NULL, CTX_STRBUF);
3156 RegisterNamespace("CONTEXTSTRARR", 1, 2, tmplput_ContextStringArray, NULL, CTX_STRBUFARR);
3157 RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl, preeval_iterate, CTX_NONE);
3158 RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed, NULL, CTX_NONE);
3159 RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed, preeval_do_tabbed, CTX_NONE);
3160 RegisterControlNS(HKEY("TAB:N"), 0, 0, tmplput_TAB_N, CTX_TAB);
3161 RegisterControlNS(HKEY("TAB:SUBJECT"), 0, 1, tmplput_TAB_TITLE, CTX_TAB);
3164 RegisterNamespace("LONGVECTOR", 1, 1, tmplput_long_vector, NULL, CTX_LONGVECTOR);
3167 RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar, CTX_NONE);
3168 RegisterConditional(HKEY("COND:CONTEXTSTR"), 3, ConditionalContextStr, CTX_STRBUF);
3169 RegisterConditional(HKEY("COND:CONTEXTSTRARR"), 4, ConditionalContextStrinArray, CTX_STRBUFARR);
3170 RegisterConditional(HKEY("COND:LONGVECTOR"), 4, ConditionalLongVector, CTX_LONGVECTOR);
3173 RegisterControlConditional(HKEY("COND:ITERATE:ISGROUPCHANGE"), 2,
3174 conditional_ITERATE_ISGROUPCHANGE,
3176 RegisterControlConditional(HKEY("COND:ITERATE:LASTN"), 2,
3177 conditional_ITERATE_LASTN,
3179 RegisterControlConditional(HKEY("COND:ITERATE:FIRSTN"), 2,
3180 conditional_ITERATE_FIRSTN,
3183 RegisterControlNS(HKEY("ITERATE:ODDEVEN"), 0, 0, tmplput_ITERATE_ODDEVEN, CTX_ITERATE);
3184 RegisterControlNS(HKEY("ITERATE:KEY"), 0, 0, tmplput_ITERATE_KEY, CTX_ITERATE);
3185 RegisterControlNS(HKEY("ITERATE:N"), 0, 0, tmplput_ITERATE_LASTN, CTX_ITERATE);
3186 RegisterNamespace("CURRENTFILE", 0, 1, tmplput_CURRENT_FILE, NULL, CTX_NONE);
3187 RegisterNamespace("DEF:STR", 1, 1, tmplput_DefStr, NULL, CTX_NONE);
3188 RegisterNamespace("DEF:VAL", 1, 1, tmplput_DefVal, NULL, CTX_NONE);
3196 ServerStartModule_SUBST
3199 WirelessTemplateCache = NewHash(1, NULL);
3200 WirelessLocalTemplateCache = NewHash(1, NULL);
3201 LocalTemplateCache = NewHash(1, NULL);
3202 TemplateCache = NewHash(1, NULL);
3204 GlobalNS = NewHash(1, NULL);
3205 Iterators = NewHash(1, NULL);
3206 Conditionals = NewHash(1, NULL);
3207 SortHash = NewHash(1, NULL);
3208 Defines = NewHash(1, NULL);
3212 FinalizeModule_SUBST
3219 ServerShutdownModule_SUBST
3222 DeleteHash(&WirelessTemplateCache);
3223 DeleteHash(&WirelessLocalTemplateCache);
3224 DeleteHash(&TemplateCache);
3225 DeleteHash(&LocalTemplateCache);
3227 DeleteHash(&GlobalNS);
3228 DeleteHash(&Iterators);
3229 DeleteHash(&Conditionals);
3230 DeleteHash(&SortHash);
3231 DeleteHash(&Defines);
3236 SessionNewModule_SUBST
3243 SessionAttachModule_SUBST
3246 sess->vars = NewHash(1,NULL);
3250 SessionDetachModule_SUBST
3253 DeleteHash(&sess->vars);
3254 FreeStrBuf(&sess->WFBuf);
3258 SessionDestroyModule_SUBST