12 #define SHOW_ME_VAPPEND_PRINTF
15 #include "webserver.h"
17 extern char *static_dirs[PATH_MAX]; /* Disk representation */
19 HashList *WirelessTemplateCache;
20 HashList *WirelessLocalTemplateCache;
21 HashList *TemplateCache;
22 HashList *LocalTemplateCache;
26 HashList *Conditionals;
30 int DumpTemplateI18NStrings = 0;
31 int LoadTemplates = 0;
32 int dbg_bactrace_template_errors = 0;
33 WCTemplputParams NoCtx;
34 StrBuf *I18nDump = NULL;
36 const char EmptyStr[]="";
39 #define SV_CONDITIONAL 2
40 #define SV_NEG_CONDITIONAL 3
41 #define SV_CUST_STR_CONDITIONAL 4
43 #define SV_PREEVALUATED 6
47 * Dynamic content for variable substitution in templates
49 typedef struct _wcsubst {
51 int wcs_type; /* which type of Substitution are we */
52 char wcs_key[32]; /* copy of our hashkey for debugging */
53 StrBuf *wcs_value; /* if we're a string, keep it here */
54 long lvalue; /* type long? keep data here */
55 WCHandlerFunc wcs_function; /* funcion hook ???*/
59 typedef struct _WCTemplate {
65 WCTemplateToken **Tokens;
68 typedef struct _HashHandler {
70 WCPreevalFunc PreEvalFunc;
71 WCHandlerFunc HandlerFunc;
74 void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere);
75 int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP);
79 typedef struct _SortStruct {
84 CompareFunc GroupChange;
89 const char *CtxNames[] = {
100 "Context MIME_ATACH",
103 "Context LONGVECTOR",
108 "Context DavNamespace",
114 void DestroySortStruct(void *vSort)
116 SortStruct *Sort = (SortStruct*) vSort;
117 FreeStrBuf(&Sort->Name);
118 FreeStrBuf(&Sort->PrefPrepend);
122 const char *ContextName(int ContextType)
124 if (ContextType < CTX_UNKNOWN)
125 return CtxNames[ContextType];
127 return CtxNames[CTX_UNKNOWN];
130 void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplputParams *TP, const char *Format, ...)
137 const char *Err = NULL;
142 va_start(arg_ptr, Format);
143 StrBufVAppendPrintf(Error, Format, arg_ptr);
148 case ERR_NAME: /* the main token name... */
149 Err = (TP->Tokens!= NULL)? TP->Tokens->pName:"";
152 Err = ((TP->Tokens!= NULL) &&
153 (TP->Tokens->nParameters > 0))?
154 TP->Tokens->Params[0]->Start : "";
157 Err = ((TP->Tokens!= NULL) &&
158 (TP->Tokens->nParameters > 1))?
159 TP->Tokens->Params[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 Header = 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(Header, WildFireException(SKEY(TP->Tokens->FileName),
215 SerializeJson(Header, WildFireMessage(SKEY(TP->Tokens->FileName),
220 WildFireSerializePayload(Header, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
227 "<pre>\n%s: %s\n</pre>\n",
231 StrBufPrintf(Info, "%s [%s] %s; [%s]",
235 ChrPtr(TP->Tokens->FlatToken));
236 SerializeJson(Header, WildFireException(HKEY(__FILE__), __LINE__, Info, 1), 1);
237 WildFireSerializePayload(Header, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
243 if (dbg_bactrace_template_errors)
251 void LogError (StrBuf *Target, const char *Type, const char *Format, ...)
262 va_start(arg_ptr, Format);
263 StrBufVAppendPrintf(Error, Format, arg_ptr);
266 lprintf(1, ChrPtr(Error));
269 Header = NewStrBuf();
272 SerializeJson(Header, WildFireException(Type, strlen(Type),
276 WildFireSerializePayload(Header, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
282 if (dbg_bactrace_template_errors)
288 void RegisterNS(const char *NSName,
292 WCHandlerFunc HandlerFunc,
293 WCPreevalFunc PreevalFunc,
296 HashHandler *NewHandler;
298 NewHandler = (HashHandler*) malloc(sizeof(HashHandler));
299 memset(NewHandler, 0, sizeof(HashHandler));
300 NewHandler->Filter.nMinArgs = nMinArgs;
301 NewHandler->Filter.nMaxArgs = nMaxArgs;
302 NewHandler->Filter.ContextType = ContextRequired;
303 NewHandler->Filter.ControlContextType = CTX_NONE;
305 NewHandler->PreEvalFunc = PreevalFunc;
306 NewHandler->HandlerFunc = HandlerFunc;
307 Put(GlobalNS, NSName, len, NewHandler, NULL);
310 void RegisterControlNS(const char *NSName,
314 WCHandlerFunc HandlerFunc,
315 int ControlContextRequired)
317 HashHandler *NewHandler;
319 NewHandler = (HashHandler*) malloc(sizeof(HashHandler));
320 memset(NewHandler, 0, sizeof(HashHandler));
321 NewHandler->Filter.nMinArgs = nMinArgs;
322 NewHandler->Filter.nMaxArgs = nMaxArgs;
323 NewHandler->Filter.ContextType = CTX_NONE;
324 NewHandler->Filter.ControlContextType = ControlContextRequired;
325 NewHandler->HandlerFunc = HandlerFunc;
326 Put(GlobalNS, NSName, len, NewHandler, NULL);
331 int CheckContext(StrBuf *Target, ContextFilter *Need, WCTemplputParams *TP, const char *ErrType)
333 if ((Need->ContextType != CTX_NONE) &&
334 (Need->ContextType != TP->Filter.ContextType)) {
336 Target, ErrType, ERR_PARM1, TP,
337 " WARNING: requires Context: [%s], have [%s]!",
338 ContextName(Need->ContextType),
339 ContextName(TP->Filter.ContextType));
343 if ((Need->ControlContextType != CTX_NONE) &&
344 (Need->ControlContextType != TP->Filter.ControlContextType)) {
346 Target, ErrType, ERR_PARM1, TP,
347 " WARNING: requires Control Context: [%s], have [%s]!",
348 ContextName(Need->ControlContextType),
349 ContextName(TP->Filter.ControlContextType));
353 if (TP->Tokens->nParameters < Need->nMinArgs) {
354 LogTemplateError(Target, ErrType, ERR_NAME, TP,
355 "needs at least %ld params, have %ld",
357 TP->Tokens->nParameters);
361 else if (TP->Tokens->nParameters > Need->nMaxArgs) {
362 LogTemplateError(Target, ErrType, ERR_NAME, TP,
363 "just needs %ld params, you gave %ld",
365 TP->Tokens->nParameters);
373 void FreeToken(WCTemplateToken **Token)
376 FreeStrBuf(&(*Token)->FlatToken);
377 if ((*Token)->HaveParameters)
378 for (i = 0; i < (*Token)->nParameters; i++)
379 free((*Token)->Params[i]);
386 void FreeWCTemplate(void *vFreeMe)
389 WCTemplate *FreeMe = (WCTemplate*)vFreeMe;
391 if (FreeMe->TokenSpace > 0) {
392 for (i = 0; i < FreeMe->nTokensUsed; i ++) {
393 FreeToken(&FreeMe->Tokens[i]);
395 free(FreeMe->Tokens);
397 FreeStrBuf(&FreeMe->FileName);
398 FreeStrBuf(&FreeMe->Data);
399 FreeStrBuf(&FreeMe->MimeType);
405 * debugging function to print array to log
407 void VarPrintTransition(void *vVar1, void *vVar2, int odd){}
410 * debugging function to print array to log
412 void VarPrintEntry(const char *Key, void *vSubst, int odd)
415 lprintf(1,"Subst[%s] : ", Key);
416 ptr = (wcsubst*) vSubst;
418 switch(ptr->wcs_type) {
420 lprintf(1, " -> %s\n", ChrPtr(ptr->wcs_value));
423 lprintf(1, " -> Server [%s]\n", ChrPtr(ptr->wcs_value));
426 lprintf(1, " -> function at [%0xd]\n", ptr->wcs_function);
429 lprintf(1," WARNING: invalid type: [%ld]!\n", ptr->wcs_type);
436 * Clear out the list of substitution variables local to this session
438 void clear_substs(wcsession *wc) {
440 if (wc->vars != NULL) {
441 DeleteHash(&wc->vars);
446 * Clear out the list of substitution variables local to this session
448 void clear_local_substs(void) {
467 void FlushPayload(wcsubst *ptr, int reusestrbuf, int type)
469 int NeedNew = NeedNewBuf(type);
470 switch(ptr->wcs_type) {
474 if (reusestrbuf && NeedNew) {
475 FlushStrBuf(ptr->wcs_value);
479 FreeStrBuf(&ptr->wcs_value);
480 ptr->wcs_value = NULL;
484 ptr->wcs_function = NULL;
485 if (reusestrbuf && NeedNew)
486 ptr->wcs_value = NewStrBuf();
489 ptr->wcs_value = NULL;
490 if (reusestrbuf && NeedNew)
491 ptr->wcs_value = NewStrBuf();
495 if (reusestrbuf && NeedNew)
496 ptr->wcs_value = NewStrBuf();
499 if (reusestrbuf && NeedNew)
500 ptr->wcs_value = NewStrBuf();
507 * destructor; kill one entry.
509 void deletevar(void *data)
511 wcsubst *ptr = (wcsubst*)data;
512 FlushPayload(ptr, 0, ptr->wcs_type);
517 wcsubst *NewSubstVar(const char *keyname, int keylen, int type)
522 ptr = (wcsubst *) malloc(sizeof(wcsubst));
523 memset(ptr, 0, sizeof(wcsubst));
525 ptr->wcs_type = type;
526 safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
527 Put(WCC->vars, keyname, keylen, ptr, deletevar);
529 switch(ptr->wcs_type) {
532 ptr->wcs_value = NewStrBuf();
546 * Add a substitution variable (local to this session) (strlen version...)
547 * keyname the replacementstring to substitute
548 * keytype the kind of the key
549 * format the format string ala printf
550 * ... the arguments to substitute in the formatstring
552 void SVPRINTF(char *keyname, int keytype, const char *format,...)
560 keylen = strlen(keyname);
562 * First look if we're doing a replacement of
565 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
566 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
567 ptr = (wcsubst*)vPtr;
568 FlushPayload(ptr, keytype, keytype);
569 ptr->wcs_type = keytype;
571 else /** Otherwise allocate a new one */
573 ptr = NewSubstVar(keyname, keylen, keytype);
576 /* Format the string */
577 va_start(arg_ptr, format);
578 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
583 * Add a substitution variable (local to this session)
584 * keyname the replacementstring to substitute
585 * keytype the kind of the key
586 * format the format string ala printf
587 * ... the arguments to substitute in the formatstring
589 void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...)
597 * First look if we're doing a replacement of
600 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
601 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
602 ptr = (wcsubst*)vPtr;
603 FlushPayload(ptr, 1, keytype);
604 ptr->wcs_type = keytype;
606 else /** Otherwise allocate a new one */
608 ptr = NewSubstVar(keyname, keylen, keytype);
611 /** Format the string and save it */
612 va_start(arg_ptr, format);
613 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
618 * Add a substitution variable (local to this session)
619 * keyname the replacementstring to substitute
620 * keytype the kind of the key
621 * format the format string ala printf
622 * ... the arguments to substitute in the formatstring
624 void SVPut(char *keyname, size_t keylen, int keytype, char *Data)
632 * First look if we're doing a replacement of
635 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
636 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
637 ptr = (wcsubst*)vPtr;
638 FlushPayload(ptr, 1, keytype);
639 ptr->wcs_type = keytype;
641 else /** Otherwise allocate a new one */
643 ptr = NewSubstVar(keyname, keylen, keytype);
645 StrBufAppendBufPlain(ptr->wcs_value, Data, -1, 0);
649 * \brief Add a substitution variable (local to this session)
650 * \param keyname the replacementstring to substitute
651 * \param keytype the kind of the key
652 * \param format the format string ala printf
653 * \param ... the arguments to substitute in the formatstring
655 void SVPutLong(char *keyname, size_t keylen, long Data)
663 * First look if we're doing a replacement of
666 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
667 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
668 ptr = (wcsubst*)vPtr;
669 FlushPayload(ptr, 1, WCS_LONG);
670 ptr->wcs_type = WCS_LONG;
672 else /** Otherwise allocate a new one */
674 ptr = NewSubstVar(keyname, keylen, WCS_LONG);
680 * \brief Add a substitution variable (local to this session) that does a callback
681 * \param keyname the keystring to substitute
682 * \param fcn_ptr the function callback to give the substitution string
684 void SVCallback(char *keyname, size_t keylen, WCHandlerFunc fcn_ptr)
691 * First look if we're doing a replacement of
694 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
695 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
696 ptr = (wcsubst*)vPtr;
697 FlushPayload(ptr, 1, WCS_FUNCTION);
698 ptr->wcs_type = WCS_FUNCTION;
700 else /** Otherwise allocate a new one */
702 ptr = NewSubstVar(keyname, keylen, WCS_FUNCTION);
705 ptr->wcs_function = fcn_ptr;
707 inline void SVCALLBACK(char *keyname, WCHandlerFunc fcn_ptr)
709 SVCallback(keyname, strlen(keyname), fcn_ptr);
714 void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref)
721 * First look if we're doing a replacement of
724 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
725 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
726 ptr = (wcsubst*)vPtr;
727 FlushPayload(ptr, 0, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
728 ptr->wcs_type = (ref)?WCS_STRBUF_REF:WCS_STRBUF;
730 else /** Otherwise allocate a new one */
732 ptr = NewSubstVar(keyname, keylen, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
734 ptr->wcs_value = (StrBuf*)Buf;
738 * \brief back end for print_value_of() ... does a server command
739 * \param servcmd server command to execute on the citadel server
741 void pvo_do_cmd(StrBuf *Target, StrBuf *servcmd) {
745 serv_puts(ChrPtr(servcmd));
746 len = serv_getln(buf, sizeof buf);
752 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
755 _fmout(Target, "CENTER");
758 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
764 void GetTemplateTokenString(StrBuf *Target,
765 WCTemplputParams *TP,
771 WCTemplputParams SubTP;
773 if (N >= TP->Tokens->nParameters) {
774 LogTemplateError(Target,
775 "TokenParameter", N, TP,
776 "invalid token %d. this shouldn't have come till here.\n", N);
782 switch (TP->Tokens->Params[N]->Type) {
786 *Value = TP->Tokens->Params[N]->Start;
787 *len = TP->Tokens->Params[N]->len;
790 if (TP->Tokens->Params[N]->len == 0) {
791 LogTemplateError(Target,
792 "TokenParameter", N, TP,
793 "Requesting parameter %d; of type BSTR, empty lookup string not admitted.", N);
798 Buf = (StrBuf*) SBstr(TKEY(N));
799 *Value = ChrPtr(Buf);
800 *len = StrLength(Buf);
803 if (TP->Tokens->Params[N]->len == 0) {
804 LogTemplateError(Target,
805 "TokenParameter", N, TP,
806 "Requesting parameter %d; of type PREFSTR, empty lookup string not admitted.", N);
811 get_PREFERENCE(TKEY(N), &Buf);
812 *Value = ChrPtr(Buf);
813 *len = StrLength(Buf);
816 LogTemplateError(Target,
817 "TokenParameter", N, TP,
818 "Requesting parameter %d; of type LONG, want string.", N);
821 LogTemplateError(Target,
822 "TokenParameter", N, TP,
823 "Requesting parameter %d; of type PREFINT, want string.", N);
826 *Value = _(TP->Tokens->Params[N]->Start);
827 *len = strlen(*Value);
829 case TYPE_SUBTEMPLATE:
830 if (TP->Tokens->Params[N]->len == 0) {
831 LogTemplateError(Target,
832 "TokenParameter", N, TP,
833 "Requesting parameter %d; of type SUBTEMPLATE, empty lookup string not admitted.", N);
839 memset(&SubTP, 0, sizeof(WCTemplputParams *));
840 SubTP.Context = TP->Context;
841 SubTP.Filter.ContextType = TP->Filter.ContextType;
843 DoTemplate(TKEY(N), Buf, &SubTP);
844 *Value = ChrPtr(Buf);
845 *len = StrLength(Buf);
846 /* we can't free it here, so we put it into the subst so its discarded later on. */
847 SVPUTBuf(TKEY(N), Buf, 0);
851 LogTemplateError(Target,
852 "TokenParameter", N, TP,
853 "unknown param type %d; [%d]", N, TP->Tokens->Params[N]->Type);
858 long GetTemplateTokenNumber(StrBuf *Target, WCTemplputParams *TP, int N, long dflt)
861 if (N >= TP->Tokens->nParameters) {
862 LogTemplateError(Target,
863 "TokenParameter", N, TP,
864 "invalid token %d. this shouldn't have come till here.\n", N);
869 switch (TP->Tokens->Params[N]->Type) {
872 return atol(TP->Tokens->Params[N]->Start);
875 if (TP->Tokens->Params[N]->len == 0) {
876 LogTemplateError(Target,
877 "TokenParameter", N, TP,
878 "Requesting parameter %d; of type BSTR, empty lookup string not admitted.", N);
881 return LBstr(TKEY(N));
884 LogTemplateError(Target,
885 "TokenParameter", N, TP,
886 "requesting a prefstring in param %d want a number", N);
887 if (TP->Tokens->Params[N]->len == 0) {
888 LogTemplateError(Target,
889 "TokenParameter", N, TP,
890 "Requesting parameter %d; of type PREFSTR, empty lookup string not admitted.", N);
893 if (get_PREF_LONG(TKEY(N), &Ret, dflt))
898 return TP->Tokens->Params[N]->lvalue;
900 if (TP->Tokens->Params[N]->len == 0) {
901 LogTemplateError(Target,
902 "TokenParameter", N, TP,
903 "Requesting parameter %d; of type PREFINT, empty lookup string not admitted.", N);
906 if (get_PREF_LONG(TKEY(N), &Ret, dflt))
910 LogTemplateError(Target,
911 "TokenParameter", N, TP,
912 "requesting a I18N string in param %d; want a number", N);
914 case TYPE_SUBTEMPLATE:
915 LogTemplateError(Target,
916 "TokenParameter", N, TP,
917 "requesting a subtemplate in param %d; not supported for numbers", N);
920 LogTemplateError(Target,
921 "TokenParameter", N, TP,
922 "unknown param type %d; [%d]", N, TP->Tokens->Params[N]->Type);
930 * \brief Print the value of a variable
931 * \param keyname get a key to print
933 void print_value_of(StrBuf *Target, WCTemplputParams *TP)
940 /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
941 /* TODO: depricated! */
942 if (TP->Tokens->pName[0] == '=') {
943 DoTemplate(TP->Tokens->pName+1, TP->Tokens->NameEnd - 1, NULL, &NoCtx);
945 /*/////TODO: if param[1] == "U" -> urlescape
947 /** Page-local variables */
948 if ((WCC->vars!= NULL) && GetHash(WCC->vars, TP->Tokens->pName, TP->Tokens->NameEnd, &vVar)) {
949 ptr = (wcsubst*) vVar;
950 switch(ptr->wcs_type) {
952 StrBufAppendBuf(Target, ptr->wcs_value, 0);
955 pvo_do_cmd(Target, ptr->wcs_value);
958 (*ptr->wcs_function) (Target, TP);
962 StrBufAppendBuf(Target, ptr->wcs_value, 0);
965 StrBufAppendPrintf(Target, "%ld", ptr->lvalue);
969 Target, "Subst", ERR_NAME, TP,
970 "WARNING: invalid value in SV-Hash at %s!", TP->Tokens->pName);
975 Target, "Token", ERR_NAME, TP,
976 "didn't find Handler \"%s\"", TP->Tokens->pName);
981 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup)
987 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
988 ParamToLookup->len, &vVar)) {
989 ptr = (wcsubst*) vVar;
990 switch(ptr->wcs_type) {
994 if (ParamToCompare->Type == TYPE_STR)
995 return ((ParamToCompare->len == StrLength(ptr->wcs_value)) &&
996 (strcmp(ParamToCompare->Start, ChrPtr(ptr->wcs_value)) == 0));
998 return ParamToCompare->lvalue == StrTol(ptr->wcs_value);
1006 if (ParamToCompare->Type == TYPE_STR)
1009 return ParamToCompare->lvalue == ptr->lvalue;
1012 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
1013 ParamToLookup->Start);
1019 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup)
1021 wcsession *WCC = WC;
1025 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
1026 ParamToLookup->len, &vVar)) {
1027 ptr = (wcsubst*) vVar;
1028 switch(ptr->wcs_type) {
1031 case WCS_STRBUF_REF:
1032 return ((StrLength(Compare) == StrLength(ptr->wcs_value)) &&
1033 (strcmp(ChrPtr(Compare), ChrPtr(ptr->wcs_value)) == 0));
1040 return StrTol(Compare) == ptr->lvalue;
1042 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
1043 ParamToLookup->Start);
1052 * \brief puts string into the template and computes which escape methon we should use
1053 * \param Source the string we should put into the template
1054 * \param FormatTypeIndex where should we look for escape types if?
1056 void StrBufAppendTemplate(StrBuf *Target,
1057 WCTemplputParams *TP,
1058 const StrBuf *Source, int FormatTypeIndex)
1061 char EscapeAs = ' ';
1063 if ((FormatTypeIndex < TP->Tokens->nParameters) &&
1064 (TP->Tokens->Params[FormatTypeIndex]->Type == TYPE_STR) &&
1065 (TP->Tokens->Params[FormatTypeIndex]->len == 1)) {
1066 EscapeAs = *TP->Tokens->Params[FormatTypeIndex]->Start;
1073 StrEscAppend(Target, Source, NULL, 0, 2);
1076 StrEscAppend(Target, Source, NULL, 0, 0);
1079 StrECMAEscAppend(Target, Source, NULL);
1082 StrHtmlEcmaEscAppend(Target, Source, NULL, 0, 0);
1085 StrBufUrlescAppend(Target, Source, NULL);
1088 StrBufAppendBuf(Target, Source, 0);
1093 void PutNewToken(WCTemplate *Template, WCTemplateToken *NewToken)
1095 if (Template->nTokensUsed + 1 >= Template->TokenSpace) {
1096 if (Template->TokenSpace <= 0) {
1097 Template->Tokens = (WCTemplateToken**)malloc(
1098 sizeof(WCTemplateToken*) * 10);
1099 memset(Template->Tokens, 0, sizeof(WCTemplateToken*));
1100 Template->TokenSpace = 10;
1103 WCTemplateToken **NewTokens;
1104 NewTokens= (WCTemplateToken**)malloc(
1105 sizeof(WCTemplateToken*) *
1106 Template->TokenSpace * 2);
1107 memcpy(NewTokens, Template->Tokens,
1108 sizeof(WCTemplateToken*) * Template->nTokensUsed);
1109 free(Template->Tokens);
1110 Template->TokenSpace *= 2;
1111 Template->Tokens = NewTokens;
1114 Template->Tokens[(Template->nTokensUsed)++] = NewToken;
1117 TemplateParam *GetNextParameter(StrBuf *Buf,
1120 WCTemplateToken *Tokens,
1122 WCTemplputParams *TP)
1124 const char *pch = *pCh;
1125 const char *pchs, *pche;
1126 TemplateParam *Parm;
1130 Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
1131 memset(Parm, 0, sizeof(TemplateParam));
1132 Parm->Type = TYPE_STR;
1134 /* Skip leading whitespaces */
1135 while ((*pch == ' ' )||
1138 (*pch == '\n')) pch ++;
1141 Parm->Type = TYPE_PREFSTR;
1148 else if (*pch == ';') {
1149 Parm->Type = TYPE_PREFINT;
1156 else if (*pch == '#') {
1157 Parm->Type = TYPE_INTDEFINE;
1160 else if (*pch == '_') {
1161 Parm->Type = TYPE_GETTEXT;
1168 else if (*pch == 'B') {
1169 Parm->Type = TYPE_BSTR;
1176 else if (*pch == '=') {
1177 Parm->Type = TYPE_SUBTEMPLATE;
1188 else if (*pch == '\'')
1190 if (quote != '\0') {
1195 ( (pch > pchs) && (*(pch - 1) == '\\'))
1200 if (*pch != quote) {
1201 lprintf(1, "Error (in '%s' line %ld); "
1202 "evaluating template param [%s] in Token [%s]\n",
1203 ChrPtr(pTmpl->FileName),
1205 ChrPtr(Tokens->FlatToken),
1212 StrBufPeek(Buf, pch, -1, '\0');
1213 if (LoadTemplates > 1) {
1214 lprintf(1, "DBG: got param [%s] %ld %ld\n",
1215 pchs, pche - pchs, strlen(pchs));
1218 Parm->len = pche - pchs;
1219 pch ++; /* move after trailing quote */
1220 if (ParamBrace && (*pch == ')')) {
1227 Parm->Type = TYPE_LONG;
1229 while ((pch <= pe) &&
1235 if (pch - pchs > 1){
1236 StrBufPeek(Buf, pch, -1, '\0');
1237 Parm->lvalue = atol(pchs);
1244 lprintf(1, "Error (in '%s' line %ld); "
1245 "evaluating long template param [%s] in Token [%s]\n",
1246 ChrPtr(pTmpl->FileName),
1248 ChrPtr(Tokens->FlatToken),
1255 while ((*pch == ' ' )||
1259 (*pch == '\n')) pch ++;
1261 if (DumpTemplateI18NStrings && (Parm->Type == TYPE_GETTEXT)) {
1262 StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", Parm->Start);
1264 if (Parm->Type == TYPE_INTDEFINE)
1268 if (GetHash(Defines, Parm->Start, Parm->len, &vPVal) &&
1272 PVal = (long*) vPVal;
1274 Parm->lvalue = *PVal;
1278 LogTemplateError(NULL, "Define", ERR_PARM1, TP,
1287 WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
1289 const char *pTmplStart,
1290 const char *pTmplEnd,
1296 TemplateParam *Param;
1297 WCTemplateToken *NewToken;
1298 WCTemplputParams TP;
1300 NewToken = (WCTemplateToken*)malloc(sizeof(WCTemplateToken));
1301 memset(NewToken, 0, sizeof(WCTemplateToken));
1302 TP.Tokens = NewToken;
1303 NewToken->FileName = pTmpl->FileName; /* to print meaningfull log messages... */
1304 NewToken->Flags = 0;
1305 NewToken->Line = Line + 1;
1306 NewToken->pTokenStart = pTmplStart;
1307 NewToken->TokenStart = pTmplStart - pStart;
1308 NewToken->TokenEnd = (pTmplEnd - pStart) - NewToken->TokenStart;
1309 NewToken->pTokenEnd = pTmplEnd;
1310 NewToken->NameEnd = NewToken->TokenEnd - 2;
1311 NewToken->PreEval = NULL;
1312 NewToken->FlatToken = NewStrBufPlain(pTmplStart + 2, pTmplEnd - pTmplStart - 2);
1313 StrBufShrinkToFit(NewToken->FlatToken, 1);
1315 StrBufPeek(Buf, pTmplStart, + 1, '\0');
1316 StrBufPeek(Buf, pTmplEnd, -1, '\0');
1317 pch = NewToken->pName = pTmplStart + 2;
1319 NewToken->HaveParameters = 0;;
1320 NewToken->nParameters = 0;
1322 while (pch < pTmplEnd - 1) {
1324 StrBufPeek(Buf, pch, -1, '\0');
1325 NewToken->NameEnd = pch - NewToken->pName;
1327 if (*(pTmplEnd - 1) != ')') {
1329 NULL, "Parseerror", ERR_NAME, &TP,
1330 "Warning, Non welformed Token; missing right parenthesis");
1332 while (pch < pTmplEnd - 1) {
1333 Param = GetNextParameter(Buf, &pch, pTmplEnd - 1, NewToken, pTmpl, &TP);
1334 if (Param != NULL) {
1335 NewToken->HaveParameters = 1;
1336 if (NewToken->nParameters > MAXPARAM) {
1338 NULL, "Parseerror", ERR_NAME, &TP,
1339 "only [%d] Params allowed in Tokens",
1343 FreeToken(&NewToken);
1346 NewToken->Params[NewToken->nParameters++] = Param;
1350 if((NewToken->NameEnd == 1) &&
1351 (NewToken->HaveParameters == 1))
1354 if (*(NewToken->pName) == '_')
1355 NewToken->Flags = SV_GETTEXT;
1356 else if (*(NewToken->pName) == '=')
1357 NewToken->Flags = SV_SUBTEMPL;
1358 else if (*(NewToken->pName) == '%')
1359 NewToken->Flags = SV_CUST_STR_CONDITIONAL;
1360 else if (*(NewToken->pName) == '?')
1361 NewToken->Flags = SV_CONDITIONAL;
1362 else if (*(NewToken->pName) == '!')
1363 NewToken->Flags = SV_NEG_CONDITIONAL;
1369 switch (NewToken->Flags) {
1371 /* If we're able to find out more about the token, do it now while its fresh. */
1372 if (GetHash(GlobalNS, NewToken->pName, NewToken->NameEnd, &vVar)) {
1373 HashHandler *Handler;
1374 Handler = (HashHandler*) vVar;
1375 if ((NewToken->nParameters < Handler->Filter.nMinArgs) ||
1376 (NewToken->nParameters > Handler->Filter.nMaxArgs)) {
1378 NULL, "Token", ERR_NAME, &TP,
1379 "doesn't work with %d params",
1380 NewToken->nParameters);
1384 NewToken->PreEval = Handler;
1385 NewToken->Flags = SV_PREEVALUATED;
1386 if (Handler->PreEvalFunc != NULL)
1387 Handler->PreEvalFunc(NewToken);
1392 if (NewToken->nParameters !=1) {
1394 NULL, "Gettext", ERR_NAME, &TP,
1395 "requires exactly 1 parameter, you gave %d params",
1396 NewToken->nParameters);
1397 NewToken->Flags = 0;
1400 if (DumpTemplateI18NStrings) {
1401 StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", NewToken->Params[0]->Start);
1405 if (NewToken->nParameters != 1) {
1407 NULL, "Subtemplates", ERR_NAME, &TP,
1408 "require exactly 1 parameter, you gave %d params",
1409 NewToken->nParameters);
1413 case SV_CUST_STR_CONDITIONAL:
1414 case SV_CONDITIONAL:
1415 case SV_NEG_CONDITIONAL:
1416 if (NewToken->nParameters <2) {
1418 NULL, "Conditional", ERR_NAME, &TP,
1419 "require at least 2 parameters, you gave %d params",
1420 NewToken->nParameters);
1421 NewToken->Flags = 0;
1424 if (NewToken->Params[1]->lvalue == 0) {
1426 NULL, "Conditional", ERR_NAME, &TP,
1427 "Conditional ID (Parameter 1) mustn't be 0!");
1428 NewToken->Flags = 0;
1431 if (!GetHash(Conditionals,
1432 NewToken->Params[0]->Start,
1433 NewToken->Params[0]->len,
1436 if ((NewToken->Params[0]->len == 1) &&
1437 (NewToken->Params[0]->Start[0] == 'X'))
1440 NULL, "Conditional", ERR_NAME, &TP,
1443 NewToken->Error = NewStrBuf();
1446 "<pre>\nConditional [%s] (in '%s' line %ld); Not found!\n[%s]\n</pre>\n",
1447 NewToken->Params[0]->Start,
1448 ChrPtr(pTmpl->FileName),
1450 ChrPtr(NewToken->FlatToken));
1454 NewToken->PreEval = vVar;
1466 * \brief Display a variable-substituted template
1467 * \param templatename template file to load
1469 void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1471 WCTemplate *NewTemplate;
1473 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1474 memset(NewTemplate, 0, sizeof(WCTemplate));
1475 NewTemplate->Data = NULL;
1476 NewTemplate->FileName = NewStrBufDup(filename);
1477 StrBufShrinkToFit(NewTemplate->FileName, 1);
1478 NewTemplate->nTokensUsed = 0;
1479 NewTemplate->TokenSpace = 0;
1480 NewTemplate->Tokens = NULL;
1481 NewTemplate->MimeType = NewStrBufPlain(GuessMimeByFilename (SKEY(NewTemplate->FileName)), -1);
1482 if (strstr(ChrPtr(NewTemplate->MimeType), "text") != NULL) {
1483 StrBufAppendBufPlain(NewTemplate->MimeType, HKEY("; charset=utf-8"), 0);
1486 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1491 * \brief Display a variable-substituted template
1492 * \param templatename template file to load
1494 void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1497 struct stat statbuf;
1498 const char *pS, *pE, *pch, *Err;
1501 WCTemplate *NewTemplate;
1503 fd = open(ChrPtr(filename), O_RDONLY);
1505 lprintf(1, "ERROR: could not open template '%s' - %s\n",
1506 ChrPtr(filename), strerror(errno));
1510 if (fstat(fd, &statbuf) == -1) {
1511 lprintf(1, "ERROR: could not stat template '%s' - %s\n",
1512 ChrPtr(filename), strerror(errno));
1516 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1517 memset(NewTemplate, 0, sizeof(WCTemplate));
1518 NewTemplate->Data = NewStrBufPlain(NULL, statbuf.st_size);
1519 NewTemplate->FileName = NewStrBufDup(filename);
1520 NewTemplate->nTokensUsed = 0;
1521 NewTemplate->TokenSpace = 0;
1522 NewTemplate->Tokens = NULL;
1523 NewTemplate->MimeType = NewStrBufPlain(GuessMimeByFilename (SKEY(NewTemplate->FileName)), -1);
1524 if (strstr(ChrPtr(NewTemplate->MimeType), "text") != NULL) {
1525 StrBufAppendBufPlain(NewTemplate->MimeType, HKEY("; charset=utf-8"), 0);
1528 if (StrBufReadBLOB(NewTemplate->Data, &fd, 1, statbuf.st_size, &Err) < 0) {
1530 FreeWCTemplate(NewTemplate);
1531 lprintf(1, "ERROR: reading template '%s' - %s<br />\n",
1532 ChrPtr(filename), strerror(errno));
1538 StrBufShrinkToFit(NewTemplate->Data, 1);
1539 StrBufShrinkToFit(NewTemplate->MimeType, 1);
1540 pS = pch = ChrPtr(NewTemplate->Data);
1541 pE = pS + StrLength(NewTemplate->Data);
1543 const char *pts, *pte;
1545 int InDoubleQuotes = 0;
1547 /** Find one <? > */
1549 for (; pch < pE; pch ++) {
1550 if ((*pch=='<')&&(*(pch + 1)=='?') &&
1551 !((pch == pS) && /* we must ommit a <?xml */
1552 (*(pch + 2) == 'x') &&
1553 (*(pch + 3) == 'm') &&
1554 (*(pch + 4) == 'l')))
1556 if (*pch=='\n') Line ++;
1562 /** Found one? parse it. */
1563 for (; pch <= pE - 1; pch ++) {
1565 InDoubleQuotes = ! InDoubleQuotes;
1566 else if (*pch == '\'')
1567 InQuotes = ! InQuotes;
1568 else if ((!InQuotes && !InDoubleQuotes) &&
1569 ((*pch!='\\')&&(*(pch + 1)=='>'))) {
1577 PutNewToken(NewTemplate,
1578 NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte, Line, NewTemplate));
1581 if (LoadTemplates == 0)
1582 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1587 const char* PrintTemplate(void *vSubst)
1589 WCTemplate *Tmpl = vSubst;
1591 return ChrPtr(Tmpl->FileName);
1595 int LoadTemplateDir(const char *DirName, HashList *wireless, HashList *big)
1600 DIR *filedir = NULL;
1601 struct dirent *filedir_entry;
1607 StrBufPrintf(Dir, "%s/t", DirName);
1608 filedir = opendir (ChrPtr(Dir));
1609 if (filedir == NULL) {
1614 FileName = NewStrBuf();
1616 while ((filedir_entry = readdir(filedir)))
1620 #ifdef _DIRENT_HAVE_D_NAMELEN
1621 d_namelen = filedir_entry->d_namelen;
1623 d_namelen = strlen(filedir_entry->d_name);
1625 d_without_ext = d_namelen;
1626 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1628 if ((d_without_ext == 0) || (d_namelen < 3))
1630 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
1631 continue; /* Ignore backup files... */
1633 IsMobile = (strstr(filedir_entry->d_name, ".m.html")!= NULL);
1634 PStart = filedir_entry->d_name;
1635 StrBufPrintf(FileName, "%s/%s", ChrPtr(Dir), filedir_entry->d_name);
1636 MinorPtr = strchr(filedir_entry->d_name, '.');
1637 if (MinorPtr != NULL)
1639 StrBufPlain(Tag, filedir_entry->d_name, MinorPtr - filedir_entry->d_name);
1641 if (LoadTemplates > 1)
1642 lprintf(1, "%s %d %s\n",ChrPtr(FileName), IsMobile, ChrPtr(Tag));
1643 if (LoadTemplates == 0)
1644 load_template(FileName, Tag, (IsMobile)?wireless:big);
1646 prepare_template(FileName, Tag, (IsMobile)?wireless:big);
1649 FreeStrBuf(&FileName);
1655 void InitTemplateCache(void)
1657 LoadTemplateDir(static_dirs[0],
1658 WirelessTemplateCache,
1660 LoadTemplateDir(static_dirs[1],
1661 WirelessLocalTemplateCache,
1662 LocalTemplateCache);
1667 /*-----------------------------------------------------------------------------
1668 * Filling & processing Templates
1671 * \brief executes one token
1672 * \param Target buffer to append to
1673 * \param Token da to process.
1674 * \param Template we're iterating
1675 * \param Context Contextpoointer to pass in
1676 * \param state are we in conditional state?
1677 * \param ContextType what type of information does context giv us?
1679 int EvaluateToken(StrBuf *Target, int state, WCTemplputParams *TP)
1681 const char *AppendMe;
1683 HashHandler *Handler;
1686 /* much output, since pName is not terminated...
1687 lprintf(1,"Doing token: %s\n",Token->pName);
1690 switch (TP->Tokens->Flags) {
1692 TmplGettext(Target, TP);
1694 case SV_CONDITIONAL: /** Forward conditional evaluation */
1695 return EvaluateConditional(Target, 1, state, TP);
1697 case SV_NEG_CONDITIONAL: /** Reverse conditional evaluation */
1698 return EvaluateConditional(Target, 0, state, TP);
1700 case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
1701 if (TP->Tokens->nParameters >= 6) {
1702 if (EvaluateConditional(Target, 0, state, TP)) {
1703 GetTemplateTokenString(Target, TP, 5, &AppendMe, &AppendMeLen);
1704 StrBufAppendBufPlain(Target,
1710 GetTemplateTokenString(Target, TP, 4, &AppendMe, &AppendMeLen);
1711 StrBufAppendBufPlain(Target,
1719 Target, "Conditional", ERR_NAME, TP,
1720 "needs at least 6 Params!");
1724 if (TP->Tokens->nParameters == 1)
1725 DoTemplate(TKEY(0), Target, TP);
1727 case SV_PREEVALUATED:
1728 Handler = (HashHandler*) TP->Tokens->PreEval;
1729 if (!CheckContext(Target, &Handler->Filter, TP, "Token")) {
1732 Handler->HandlerFunc(Target, TP);
1735 if (GetHash(GlobalNS, TP->Tokens->pName, TP->Tokens->NameEnd, &vVar)) {
1736 Handler = (HashHandler*) vVar;
1737 if (!CheckContext(Target, &Handler->Filter, TP, "Token")) {
1741 Handler->HandlerFunc(Target, TP);
1745 print_value_of(Target, TP);
1753 const StrBuf *ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, WCTemplputParams *CallingTP)
1755 WCTemplate *pTmpl = Tmpl;
1758 const char *pData, *pS;
1760 WCTemplputParams TP;
1762 memcpy(&TP.Filter, &CallingTP->Filter, sizeof(ContextFilter));
1764 TP.Context = CallingTP->Context;
1765 TP.ControlContext = CallingTP->ControlContext;
1767 if (LoadTemplates != 0) {
1768 if (LoadTemplates > 1)
1769 lprintf(1, "DBG: ----- loading: [%s] ------ \n",
1770 ChrPtr(Tmpl->FileName));
1772 pTmpl = load_template(Tmpl->FileName, NULL, NULL);
1776 "<pre>\nError loading Template [%s]\n See Logfile for details\n</pre>\n",
1777 ChrPtr(Tmpl->FileName));
1783 pS = pData = ChrPtr(pTmpl->Data);
1784 len = StrLength(pTmpl->Data);
1788 if (i >= pTmpl->nTokensUsed) {
1789 StrBufAppendBufPlain(Target,
1791 len - (pData - pS), 0);
1795 StrBufAppendBufPlain(
1797 pTmpl->Tokens[i]->pTokenStart - pData, 0);
1798 TP.Tokens = pTmpl->Tokens[i];
1799 TP.nArgs = pTmpl->Tokens[i]->nParameters;
1800 state = EvaluateToken(Target, state, &TP);
1802 while ((state != 0) && (i+1 < pTmpl->nTokensUsed)) {
1803 /* condition told us to skip till its end condition */
1805 TP.Tokens = pTmpl->Tokens[i];
1806 TP.nArgs = pTmpl->Tokens[i]->nParameters;
1807 if ((pTmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
1808 (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
1809 if (state == EvaluateConditional(
1811 pTmpl->Tokens[i]->Flags,
1817 pData = pTmpl->Tokens[i++]->pTokenEnd + 1;
1818 if (i > pTmpl->nTokensUsed)
1822 if (LoadTemplates != 0) {
1823 FreeWCTemplate(pTmpl);
1825 return Tmpl->MimeType;
1830 * \brief Display a variable-substituted template
1831 * \param templatename template file to load
1832 * \returns the mimetype of the template its doing
1834 const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP)
1836 WCTemplputParams LocalTP;
1838 HashList *StaticLocal;
1844 memset(&LocalTP, 0, sizeof(WCTemplputParams));
1848 if (WC->is_mobile) {
1849 Static = WirelessTemplateCache;
1850 StaticLocal = WirelessLocalTemplateCache;
1853 Static = TemplateCache;
1854 StaticLocal = LocalTemplateCache;
1859 lprintf (1, "Can't to load a template with empty name!\n");
1860 StrBufAppendPrintf(Target, "<pre>\nCan't to load a template with empty name!\n</pre>");
1864 if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
1865 !GetHash(Static, templatename, len, &vTmpl)) {
1866 lprintf (1, "didn't find Template [%s] %ld %ld\n", templatename, len , (long)strlen(templatename));
1867 StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>",
1869 (long)strlen(templatename));
1871 dbg_PrintHash(Static, PrintTemplate, NULL);
1872 PrintHash(Static, VarPrintTransition, PrintTemplate);
1878 return ProcessTemplate(vTmpl, Target, TP);
1882 /*-----------------------------------------------------------------------------
1885 typedef struct _HashIterator {
1886 HashList *StaticList;
1887 int AdditionalParams;
1889 int XPectContextType;
1891 RetrieveHashlistFunc GetHash;
1892 HashDestructorFunc Destructor;
1893 SubTemplFunc DoSubTemplate;
1896 void RegisterITERATOR(const char *Name, long len,
1897 int AdditionalParams,
1898 HashList *StaticList,
1899 RetrieveHashlistFunc GetHash,
1900 SubTemplFunc DoSubTempl,
1901 HashDestructorFunc Destructor,
1903 int XPectContextType,
1908 It = (HashIterator*)malloc(sizeof(HashIterator));
1909 memset(It, 0, sizeof(HashIterator));
1910 It->StaticList = StaticList;
1911 It->AdditionalParams = AdditionalParams;
1912 It->GetHash = GetHash;
1913 It->DoSubTemplate = DoSubTempl;
1914 It->Destructor = Destructor;
1915 It->ContextType = ContextType;
1916 It->XPectContextType = XPectContextType;
1918 Put(Iterators, Name, len, It, NULL);
1921 typedef struct _iteratestruct {
1930 int preeval_iterate(WCTemplateToken *Token)
1932 WCTemplputParams TPP;
1933 WCTemplputParams *TP;
1936 memset(&TPP, 0, sizeof(WCTemplputParams));
1939 if (!GetHash(Iterators, TKEY(0), &vIt)) {
1941 NULL, "Iterator", ERR_NAME, TP,
1945 Token->Preeval2 = vIt;
1949 void tmpl_iterate_subtmpl(StrBuf *Target, WCTemplputParams *TP)
1954 SortStruct *SortBy = NULL;
1956 int DetectGroupChange = 0;
1959 void *vLastContext = NULL;
1961 WCTemplputParams SubTP;
1962 IterateStruct Status;
1968 memset(&Status, 0, sizeof(IterateStruct));
1969 memcpy (&SubTP, &TP, sizeof(WCTemplputParams));
1971 It = (HashIterator*) TP->Tokens->Preeval2;
1974 Target, "Iterator", ERR_PARM1, TP, "Unknown!");
1978 if (TP->Tokens->nParameters < It->AdditionalParams + 2) {
1980 Target, "Iterator", ERR_PARM1, TP,
1981 "doesn't work with %d params",
1982 TP->Tokens->nParameters);
1986 if ((It->XPectContextType != CTX_NONE) &&
1987 (It->XPectContextType != TP->Filter.ContextType)) {
1989 Target, "Iterator", ERR_PARM1, TP,
1990 "requires context of type %d, have %d",
1991 It->XPectContextType,
1992 TP->Filter.ContextType);
1997 if (It->StaticList == NULL)
1998 List = It->GetHash(Target, TP);
2000 List = It->StaticList;
2002 DetectGroupChange = (It->Flags & IT_FLAG_DETECT_GROUPCHANGE) != 0;
2003 if (DetectGroupChange) {
2004 const StrBuf *BSort;
2005 DetectGroupChange = 0;
2006 if (havebstr("SortBy")) {
2007 BSort = sbstr("SortBy");
2008 if (GetHash(SortHash, SKEY(BSort), &vSortBy) &&
2009 (vSortBy != NULL)) {
2010 SortBy = (SortStruct*)vSortBy;
2011 /** Ok, its us, lets see in which direction we should sort... */
2012 if (havebstr("SortOrder")) {
2014 SortOrder = LBSTR("SortOrder");
2016 DetectGroupChange = 1;
2021 nMembersUsed = GetCount(List);
2022 SubBuf = NewStrBuf();
2023 SubTP.Filter.ContextType = It->ContextType;
2024 SubTP.Filter.ControlContextType = CTX_ITERATE;
2025 SubTP.ControlContext = &Status;
2027 if (HAVE_PARAM(2)) {
2028 StartAt = GetTemplateTokenNumber(Target, TP, 2, 0);
2030 if (HAVE_PARAM(3)) {
2031 StepWidth = GetTemplateTokenNumber(Target, TP, 3, 0);
2033 if (HAVE_PARAM(4)) {
2034 StopAt = GetTemplateTokenNumber(Target, TP, 4, -1);
2036 it = GetNewHashPos(List, StepWidth);
2038 StopAt = GetCount(List);
2040 while (GetNextHashPos(List, it, &Status.KeyLen, &Status.Key, &vContext)) {
2041 if ((Status.n >= StartAt) && (Status.n <= StopAt)) {
2042 if (DetectGroupChange && Status.n > 0) {
2043 Status.GroupChange = SortBy->GroupChange(vContext, vLastContext);
2045 Status.LastN = (Status.n + 1) == nMembersUsed;
2046 SubTP.Context = vContext;
2047 if (It->DoSubTemplate != NULL)
2048 It->DoSubTemplate(SubBuf, &SubTP);
2049 DoTemplate(TKEY(1), SubBuf, &SubTP);
2051 StrBufAppendBuf(Target, SubBuf, 0);
2052 FlushStrBuf(SubBuf);
2053 Status.oddeven = ! Status.oddeven;
2054 vLastContext = vContext;
2058 FreeStrBuf(&SubBuf);
2060 if (It->Destructor != NULL)
2061 It->Destructor(&List);
2065 int conditional_ITERATE_ISGROUPCHANGE(StrBuf *Target, WCTemplputParams *TP)
2067 IterateStruct *Ctx = CCTX;
2068 if (TP->Tokens->nParameters < 3)
2069 return Ctx->GroupChange;
2071 return TP->Tokens->Params[2]->lvalue == Ctx->GroupChange;
2074 void tmplput_ITERATE_ODDEVEN(StrBuf *Target, WCTemplputParams *TP)
2076 IterateStruct *Ctx = CCTX;
2078 StrBufAppendBufPlain(Target, HKEY("odd"), 0);
2080 StrBufAppendBufPlain(Target, HKEY("even"), 0);
2084 void tmplput_ITERATE_KEY(StrBuf *Target, WCTemplputParams *TP)
2086 IterateStruct *Ctx = CCTX;
2088 StrBufAppendBufPlain(Target, Ctx->Key, Ctx->KeyLen, 0);
2092 void tmplput_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
2094 IterateStruct *Ctx = CCTX;
2095 StrBufAppendPrintf(Target, "%d", Ctx->n);
2098 int conditional_ITERATE_FIRSTN(StrBuf *Target, WCTemplputParams *TP)
2100 IterateStruct *Ctx = CCTX;
2104 int conditional_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
2106 IterateStruct *Ctx = CCTX;
2112 /*-----------------------------------------------------------------------------
2115 int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP)
2117 ConditionalStruct *Cond;
2119 if ((TP->Tokens->Params[0]->len == 1) &&
2120 (TP->Tokens->Params[0]->Start[0] == 'X'))
2121 return (state != 0)?TP->Tokens->Params[1]->lvalue:0;
2123 Cond = (ConditionalStruct *) TP->Tokens->PreEval;
2126 Target, "Conditional", ERR_PARM1, TP,
2131 if (!CheckContext(Target, &Cond->Filter, TP, "Conditional")) {
2135 if (Cond->CondF(Target, TP) == Neg)
2136 return TP->Tokens->Params[1]->lvalue;
2140 int ConditionalVar(StrBuf *Target, WCTemplputParams *TP)
2145 if (!GetHash(WC->vars, TKEY(2), &vsubst))
2147 subst = (wcsubst*) vsubst;
2149 switch(subst->wcs_type) {
2151 return (subst->wcs_function!=NULL);
2153 lprintf(1, " -> Server [%s]\n", subst->wcs_value);/* TODO */
2157 case WCS_STRBUF_REF:
2158 if (TP->Tokens->nParameters < 4)
2160 return (strcmp(TP->Tokens->Params[3]->Start, ChrPtr(subst->wcs_value)) == 0);
2162 if (TP->Tokens->nParameters < 4)
2163 return (subst->lvalue != 0);
2164 return (subst->lvalue == TP->Tokens->Params[3]->lvalue);
2166 lprintf(1," WARNING: invalid type: [%ld]!\n", subst->wcs_type);
2172 void RegisterConditional(const char *Name, long len,
2174 WCConditionalFunc CondF,
2175 int ContextRequired)
2177 ConditionalStruct *Cond;
2179 Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
2180 memset(Cond, 0, sizeof(ConditionalStruct));
2181 Cond->PlainName = Name;
2182 Cond->Filter.nMaxArgs = nParams;
2183 Cond->Filter.nMinArgs = nParams;
2184 Cond->CondF = CondF;
2185 Cond->Filter.ContextType = ContextRequired;
2186 Cond->Filter.ControlContextType = CTX_NONE;
2187 Put(Conditionals, Name, len, Cond, NULL);
2190 void RegisterControlConditional(const char *Name, long len,
2192 WCConditionalFunc CondF,
2193 int ControlContextRequired)
2195 ConditionalStruct *Cond;
2197 Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
2198 memset(Cond, 0, sizeof(ConditionalStruct));
2199 Cond->PlainName = Name;
2200 Cond->Filter.nMaxArgs = nParams;
2201 Cond->Filter.nMinArgs = nParams;
2202 Cond->CondF = CondF;
2203 Cond->Filter.ContextType = CTX_NONE;
2204 Cond->Filter.ControlContextType = ControlContextRequired;
2205 Put(Conditionals, Name, len, Cond, NULL);
2208 void RegisterTokenParamDefine(const char *Name, long len,
2213 PVal = (long*)malloc(sizeof(long));
2215 Put(Defines, Name, len, PVal, NULL);
2220 /*-----------------------------------------------------------------------------
2223 void tmplput_ContextString(StrBuf *Target, WCTemplputParams *TP)
2225 StrBufAppendTemplate(Target, TP, (StrBuf*)CTX, 0);
2227 int ConditionalContextStr(StrBuf *Target, WCTemplputParams *TP)
2229 StrBuf *TokenText = (StrBuf*) CTX;
2230 const char *CompareToken;
2233 GetTemplateTokenString(Target, TP, 2, &CompareToken, &len);
2234 return strcmp(ChrPtr(TokenText), CompareToken) == 0;
2237 /*-----------------------------------------------------------------------------
2241 void tmpl_do_boxed(StrBuf *Target, WCTemplputParams *TP)
2243 WCTemplputParams SubTP;
2246 if (TP->Tokens->nParameters == 2) {
2247 if (TP->Tokens->Params[1]->Type == TYPE_STR) {
2248 Headline = NewStrBuf();
2249 DoTemplate(TKEY(1), Headline, TP);
2254 GetTemplateTokenString(Target,
2259 Headline = NewStrBufPlain(Ch, len);
2262 memcpy (&SubTP, TP, sizeof(WCTemplputParams));
2263 SubTP.Context = Headline;
2264 SubTP.Filter.ContextType = CTX_STRBUF;
2265 DoTemplate(HKEY("beginbox"), Target, &SubTP);
2266 DoTemplate(TKEY(0), Target, TP);
2267 DoTemplate(HKEY("endbox"), Target, TP);
2268 FreeStrBuf(&Headline);
2271 /*-----------------------------------------------------------------------------
2275 void tmpl_do_tabbed(StrBuf *Target, WCTemplputParams *TP)
2278 int i, ntabs, nTabs;
2280 nTabs = ntabs = TP->Tokens->nParameters / 2;
2281 TabNames = (StrBuf **) malloc(ntabs * sizeof(StrBuf*));
2282 memset(TabNames, 0, ntabs * sizeof(StrBuf*));
2284 for (i = 0; i < ntabs; i++) {
2285 if ((TP->Tokens->Params[i * 2]->Type == TYPE_STR) &&
2286 (TP->Tokens->Params[i * 2]->len > 0)) {
2287 TabNames[i] = NewStrBuf();
2288 DoTemplate(TKEY(i * 2), TabNames[i], TP);
2290 else if (TP->Tokens->Params[i * 2]->Type == TYPE_GETTEXT) {
2293 GetTemplateTokenString(Target,
2298 TabNames[i] = NewStrBufPlain(Ch, -1);
2301 /** A Tab without subject? we can't count that, add it as silent */
2306 StrTabbedDialog(Target, nTabs, TabNames);
2307 for (i = 0; i < ntabs; i++) {
2308 StrBeginTab(Target, i, nTabs);
2309 DoTemplate(TKEY(i * 2 + 1), Target, TP);
2310 StrEndTab(Target, i, nTabs);
2315 /*-----------------------------------------------------------------------------
2320 void RegisterSortFunc(const char *name, long len,
2321 const char *prepend, long preplen,
2322 CompareFunc Forward,
2323 CompareFunc Reverse,
2324 CompareFunc GroupChange,
2327 SortStruct *NewSort;
2329 NewSort = (SortStruct*) malloc(sizeof(SortStruct));
2330 memset(NewSort, 0, sizeof(SortStruct));
2331 NewSort->Name = NewStrBufPlain(name, len);
2332 if (prepend != NULL)
2333 NewSort->PrefPrepend = NewStrBufPlain(prepend, preplen);
2335 NewSort->PrefPrepend = NULL;
2336 NewSort->Forward = Forward;
2337 NewSort->Reverse = Reverse;
2338 NewSort->GroupChange = GroupChange;
2339 NewSort->ContextType = ContextType;
2340 if (ContextType == CTX_NONE) {
2341 lprintf(1, "sorting requires a context. CTX_NONE won't make it.\n");
2345 Put(SortHash, name, len, NewSort, DestroySortStruct);
2348 CompareFunc RetrieveSort(WCTemplputParams *TP,
2349 const char *OtherPrefix, long OtherPrefixLen,
2350 const char *Default, long ldefault, long DefaultDirection)
2353 const StrBuf *BSort = NULL;
2356 long SortOrder = -1;
2358 if (havebstr("SortBy")) {
2359 BSort = sbstr("SortBy");
2360 if (OtherPrefix == NULL) {
2361 set_room_pref("sort", NewStrBufDup(BSort), 0);
2364 set_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen, NewStrBufDup(BSort), 0);
2367 else { /** Try to fallback to our remembered values... */
2368 if (OtherPrefix == NULL) {
2369 BSort = get_room_pref("sort");
2372 BSort = get_X_PREFS(HKEY("sort"), OtherPrefix, OtherPrefixLen);
2375 putbstr("SortBy", NewStrBufDup(BSort));
2379 BSort = Buf = NewStrBufPlain(Default, ldefault);
2380 putbstr("SortBy", Buf);
2384 if (!GetHash(SortHash, SKEY(BSort), &vSortBy) ||
2385 (vSortBy == NULL)) {
2387 if (!GetHash(SortHash, Default, ldefault, &vSortBy) ||
2388 (vSortBy == NULL)) {
2390 NULL, "Sorting", ERR_PARM1, TP,
2391 "Illegal default sort: [%s]", Default);
2395 SortBy = (SortStruct*)vSortBy;
2397 if (SortBy->ContextType != TP->Filter.ContextType)
2400 /** Ok, its us, lets see in which direction we should sort... */
2401 if (havebstr("SortOrder")) {
2402 SortOrder = LBSTR("SortOrder");
2404 else { /** Try to fallback to our remembered values... */
2406 if (SortBy->PrefPrepend == NULL) {
2407 Buf = get_room_pref("SortOrder");
2408 SortOrder = StrTol(Buf);
2411 BSort = get_X_PREFS(HKEY("SortOrder"), OtherPrefix, OtherPrefixLen);
2415 SortOrder = DefaultDirection;
2417 Buf = NewStrBufPlain(NULL, 64);
2418 StrBufPrintf(Buf, "%ld", SortOrder);
2419 putbstr("SortOrder", Buf);
2421 switch (SortOrder) {
2426 return SortBy->Forward;
2428 return SortBy->Reverse;
2440 ConstStr SortIcons[] = {
2441 {HKEY("static/sort_none.gif")},
2442 {HKEY("static/up_pointer.gif")},
2443 {HKEY("static/down_pointer.gif")},
2446 ConstStr SortNextOrder[] = {
2453 int GetSortMetric(WCTemplputParams *TP, SortStruct **Next, SortStruct **Param, long *SortOrder, int N)
2455 int bSortError = eNOT_SPECIFIED;
2456 const StrBuf *BSort;
2461 if (!GetHash(SortHash, TKEY(0), &vSort) ||
2463 return eNO_SUCH_SORT;
2464 *Param = (SortStruct*) vSort;
2467 if (havebstr("SortBy")) {
2468 BSort = sbstr("SortBy");
2469 bSortError = eINVALID_PARAM;
2470 if ((*Param)->PrefPrepend == NULL) {
2471 set_room_pref("sort", NewStrBufDup(BSort), 0);
2474 set_X_PREFS(HKEY("sort"), TKEY(N), NewStrBufDup(BSort), 0);
2477 else { /** Try to fallback to our remembered values... */
2478 if ((*Param)->PrefPrepend == NULL) {
2479 BSort = get_room_pref("sort");
2482 BSort = get_X_PREFS(HKEY("sort"), TKEY(N));
2486 if (!GetHash(SortHash, SKEY(BSort), &vSort) ||
2490 *Next = (SortStruct*) vSort;
2492 /** Ok, its us, lets see in which direction we should sort... */
2493 if (havebstr("SortOrder")) {
2494 *SortOrder = LBSTR("SortOrder");
2496 else { /** Try to fallback to our remembered values... */
2497 if ((*Param)->PrefPrepend == NULL) {
2498 *SortOrder = StrTol(get_room_pref("SortOrder"));
2501 *SortOrder = StrTol(get_X_PREFS(HKEY("SortOrder"), TKEY(N)));
2511 void tmplput_SORT_ICON(StrBuf *Target, WCTemplputParams *TP)
2516 const ConstStr *SortIcon;
2518 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2521 Target, "Sorter", ERR_PARM1, TP,
2522 " Sorter [%s] unknown!",
2523 TP->Tokens->Params[0]->Start);
2525 case eINVALID_PARAM:
2526 LogTemplateError(NULL, "Sorter", ERR_PARM1, TP,
2527 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2529 case eNOT_SPECIFIED:
2531 if (Next == Param) {
2532 SortIcon = &SortIcons[SortOrder];
2534 else { /** Not Us... */
2535 SortIcon = &SortIcons[0];
2537 StrBufAppendBufPlain(Target, SortIcon->Key, SortIcon->len, 0);
2541 void tmplput_SORT_NEXT(StrBuf *Target, WCTemplputParams *TP)
2547 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2550 Target, "Sorter", ERR_PARM1, TP,
2551 " Sorter [%s] unknown!",
2552 TP->Tokens->Params[0]->Start);
2554 case eINVALID_PARAM:
2556 NULL, "Sorter", ERR_PARM1, TP,
2557 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2559 case eNOT_SPECIFIED:
2561 StrBufAppendBuf(Target, Param->Name, 0);
2566 void tmplput_SORT_ORDER(StrBuf *Target, WCTemplputParams *TP)
2569 const ConstStr *SortOrderStr;
2573 switch (GetSortMetric(TP, &Next, &Param, &SortOrder, 2)){
2576 Target, "Sorter", ERR_PARM1, TP,
2577 " Sorter [%s] unknown!",
2578 TP->Tokens->Params[0]->Start);
2580 case eINVALID_PARAM:
2582 NULL, "Sorter", ERR_PARM1, TP,
2583 " Sorter specified by BSTR 'SortBy' [%s] unknown!",
2585 case eNOT_SPECIFIED:
2587 if (Next == Param) {
2588 SortOrderStr = &SortNextOrder[SortOrder];
2590 else { /** Not Us... */
2591 SortOrderStr = &SortNextOrder[0];
2593 StrBufAppendBufPlain(Target, SortOrderStr->Key, SortOrderStr->len, 0);
2598 void tmplput_long_vector(StrBuf *Target, WCTemplputParams *TP)
2600 long *LongVector = (long*) CTX;
2602 if ((TP->Tokens->Params[0]->Type == TYPE_LONG) &&
2603 (TP->Tokens->Params[0]->lvalue <= LongVector[0]))
2605 StrBufAppendPrintf(Target, "%ld", LongVector[TP->Tokens->Params[0]->lvalue]);
2609 if (TP->Tokens->Params[0]->Type != TYPE_LONG) {
2611 Target, "Longvector", ERR_NAME, TP,
2612 "needs a numerical Parameter!");
2616 Target, "LongVector", ERR_PARM1, TP,
2617 "doesn't have %ld Parameters, its just the size of %ld!",
2618 TP->Tokens->Params[0]->lvalue,
2624 void dbg_print_longvector(long *LongVector)
2626 StrBuf *Buf = NewStrBufPlain(HKEY("Longvector: ["));
2627 int nItems = LongVector[0];
2630 for (i = 0; i < nItems; i++) {
2632 StrBufAppendPrintf(Buf, "%d: %ld | ", i, LongVector[i]);
2634 StrBufAppendPrintf(Buf, "%d: %ld]\n", i, LongVector[i]);
2637 lprintf(1, ChrPtr(Buf));
2641 int ConditionalLongVector(StrBuf *Target, WCTemplputParams *TP)
2643 long *LongVector = (long*) CTX;
2645 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) &&
2646 (TP->Tokens->Params[2]->lvalue <= LongVector[0])&&
2647 (TP->Tokens->Params[3]->Type == TYPE_LONG) &&
2648 (TP->Tokens->Params[3]->lvalue <= LongVector[0]))
2650 return LongVector[TP->Tokens->Params[2]->lvalue] ==
2651 LongVector[TP->Tokens->Params[3]->lvalue];
2655 if ((TP->Tokens->Params[2]->Type == TYPE_LONG) ||
2656 (TP->Tokens->Params[2]->Type == TYPE_LONG)) {
2658 Target, "ConditionalLongvector", ERR_PARM1, TP,
2659 "needs two long Parameter!");
2663 Target, "Longvector", ERR_PARM1, TP,
2664 "doesn't have %ld / %ld Parameters, its just the size of %ld!",
2665 TP->Tokens->Params[2]->lvalue,
2666 TP->Tokens->Params[3]->lvalue,
2674 void tmplput_CURRENT_FILE(StrBuf *Target, WCTemplputParams *TP)
2676 StrBufAppendTemplate(Target, TP, TP->Tokens->FileName, 0);
2683 memset(&NoCtx, 0, sizeof(WCTemplputParams));
2684 RegisterNamespace("SORT:ICON", 1, 2, tmplput_SORT_ICON, NULL, CTX_NONE);
2685 RegisterNamespace("SORT:ORDER", 1, 2, tmplput_SORT_ORDER, NULL, CTX_NONE);
2686 RegisterNamespace("SORT:NEXT", 1, 2, tmplput_SORT_NEXT, NULL, CTX_NONE);
2687 RegisterNamespace("CONTEXTSTR", 0, 1, tmplput_ContextString, NULL, CTX_STRBUF);
2688 RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl, preeval_iterate, CTX_NONE);
2689 RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed, NULL, CTX_NONE);
2690 RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed, NULL, CTX_NONE);
2691 RegisterNamespace("LONGVECTOR", 1, 1, tmplput_long_vector, NULL, CTX_LONGVECTOR);
2692 RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar, CTX_NONE);
2693 RegisterConditional(HKEY("COND:CONTEXTSTR"), 3, ConditionalContextStr, CTX_STRBUF);
2694 RegisterConditional(HKEY("COND:LONGVECTOR"), 4, ConditionalLongVector, CTX_LONGVECTOR);
2696 RegisterControlConditional(HKEY("COND:ITERATE:ISGROUPCHANGE"), 2,
2697 conditional_ITERATE_ISGROUPCHANGE,
2699 RegisterControlConditional(HKEY("COND:ITERATE:LASTN"), 2,
2700 conditional_ITERATE_LASTN,
2702 RegisterControlConditional(HKEY("COND:ITERATE:FIRSTN"), 2,
2703 conditional_ITERATE_FIRSTN,
2706 RegisterControlNS(HKEY("ITERATE:ODDEVEN"), 0, 0, tmplput_ITERATE_ODDEVEN, CTX_ITERATE);
2707 RegisterControlNS(HKEY("ITERATE:KEY"), 0, 0, tmplput_ITERATE_KEY, CTX_ITERATE);
2708 RegisterControlNS(HKEY("ITERATE:N"), 0, 0, tmplput_ITERATE_LASTN, CTX_ITERATE);
2709 RegisterNamespace("CURRENTFILE", 0, 1, tmplput_CURRENT_FILE, NULL, CTX_NONE);
2715 ServerStartModule_SUBST
2718 WirelessTemplateCache = NewHash(1, NULL);
2719 WirelessLocalTemplateCache = NewHash(1, NULL);
2720 LocalTemplateCache = NewHash(1, NULL);
2721 TemplateCache = NewHash(1, NULL);
2723 GlobalNS = NewHash(1, NULL);
2724 Iterators = NewHash(1, NULL);
2725 Conditionals = NewHash(1, NULL);
2726 SortHash = NewHash(1, NULL);
2727 Defines = NewHash(1, NULL);
2731 FinalizeModule_SUBST
2738 ServerShutdownModule_SUBST
2741 DeleteHash(&WirelessTemplateCache);
2742 DeleteHash(&WirelessLocalTemplateCache);
2743 DeleteHash(&TemplateCache);
2744 DeleteHash(&LocalTemplateCache);
2746 DeleteHash(&GlobalNS);
2747 DeleteHash(&Iterators);
2748 DeleteHash(&Conditionals);
2749 DeleteHash(&SortHash);
2750 DeleteHash(&Defines);
2755 SessionNewModule_SUBST
2762 SessionAttachModule_SUBST
2765 sess->vars = NewHash(1,NULL);
2769 SessionDetachModule_SUBST
2772 DeleteHash(&sess->vars);
2776 SessionDestroyModule_SUBST