5 * \defgroup Subst Variable substitution type stuff
6 * \ingroup CitadelConfig
12 #include <sys/types.h>
18 #define SHOW_ME_VAPPEND_PRINTF
21 #include "webserver.h"
23 extern char *static_dirs[PATH_MAX]; /**< Disk representation */
25 HashList *WirelessTemplateCache;
26 HashList *WirelessLocalTemplateCache;
27 HashList *TemplateCache;
28 HashList *LocalTemplateCache;
32 HashList *Contitionals;
34 int LoadTemplates = 0;
37 #define SV_CONDITIONAL 2
38 #define SV_NEG_CONDITIONAL 3
39 #define SV_CUST_STR_CONDITIONAL 4
41 #define SV_PREEVALUATED 6
43 typedef struct _WCTemplate {
48 WCTemplateToken **Tokens;
51 typedef struct _HashHandler {
55 WCHandlerFunc HandlerFunc;
58 void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere);
60 void RegisterNS(const char *NSName,
64 WCHandlerFunc HandlerFunc,
67 HashHandler *NewHandler;
69 NewHandler = (HashHandler*) malloc(sizeof(HashHandler));
70 NewHandler->nMinArgs = nMinArgs;
71 NewHandler->nMaxArgs = nMaxArgs;
72 NewHandler->HandlerFunc = HandlerFunc;
73 NewHandler->ContextRequired = ContextRequired;
74 Put(GlobalNS, NSName, len, NewHandler, NULL);
77 void FreeToken(WCTemplateToken **Token)
80 FreeStrBuf(&(*Token)->FlatToken);
81 if ((*Token)->HaveParameters)
82 for (i = 0; i < (*Token)->nParameters; i++)
83 free((*Token)->Params[i]);
90 void FreeWCTemplate(void *vFreeMe)
93 WCTemplate *FreeMe = (WCTemplate*)vFreeMe;
95 if (FreeMe->TokenSpace > 0) {
96 for (i = 0; i < FreeMe->nTokensUsed; i ++) {
97 FreeToken(&FreeMe->Tokens[i]);
101 FreeStrBuf(&FreeMe->FileName);
102 FreeStrBuf(&FreeMe->Data);
108 * \brief debugging function to print array to log
110 void VarPrintTransition(void *vVar1, void *vVar2, int odd){}
112 * \brief debugging function to print array to log
114 void VarPrintEntry(const char *Key, void *vSubst, int odd)
117 lprintf(1,"Subst[%s] : ", Key);
118 ptr = (wcsubst*) vSubst;
120 switch(ptr->wcs_type) {
122 lprintf(1, " -> %s\n", ChrPtr(ptr->wcs_value));
125 lprintf(1, " -> Server [%s]\n", ChrPtr(ptr->wcs_value));
128 lprintf(1, " -> function at [%0xd]\n", ptr->wcs_function);
131 lprintf(1," WARNING: invalid type: [%ld]!\n", ptr->wcs_type);
138 * \brief Clear out the list of substitution variables local to this session
140 void clear_substs(struct wcsession *wc) {
142 if (wc->vars != NULL) {
143 DeleteHash(&wc->vars);
148 * \brief Clear out the list of substitution variables local to this session
150 void clear_local_substs(void) {
169 void FlushPayload(wcsubst *ptr, int reusestrbuf, int type)
171 int NeedNew = NeedNewBuf(type);
172 switch(ptr->wcs_type) {
176 if (reusestrbuf && NeedNew) {
177 FlushStrBuf(ptr->wcs_value);
181 FreeStrBuf(&ptr->wcs_value);
182 ptr->wcs_value = NULL;
186 ptr->wcs_function = NULL;
187 if (reusestrbuf && NeedNew)
188 ptr->wcs_value = NewStrBuf();
191 ptr->wcs_value = NULL;
192 if (reusestrbuf && NeedNew)
193 ptr->wcs_value = NewStrBuf();
197 if (reusestrbuf && NeedNew)
198 ptr->wcs_value = NewStrBuf();
201 if (reusestrbuf && NeedNew)
202 ptr->wcs_value = NewStrBuf();
209 * \brief destructor; kill one entry.
211 void deletevar(void *data)
213 wcsubst *ptr = (wcsubst*)data;
214 FlushPayload(ptr, 0, ptr->wcs_type);
219 wcsubst *NewSubstVar(const char *keyname, int keylen, int type)
222 struct wcsession *WCC = WC;
224 ptr = (wcsubst *) malloc(sizeof(wcsubst));
225 memset(ptr, 0, sizeof(wcsubst));
227 ptr->wcs_type = type;
228 safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
229 Put(WCC->vars, keyname, keylen, ptr, deletevar);
231 switch(ptr->wcs_type) {
234 ptr->wcs_value = NewStrBuf();
248 * \brief Add a substitution variable (local to this session) (strlen version...)
249 * \param keyname the replacementstring to substitute
250 * \param keytype the kind of the key
251 * \param format the format string ala printf
252 * \param ... the arguments to substitute in the formatstring
254 void SVPRINTF(char *keyname, int keytype, const char *format,...)
260 struct wcsession *WCC = WC;
262 keylen = strlen(keyname);
264 * First look if we're doing a replacement of
267 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
268 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
269 ptr = (wcsubst*)vPtr;
270 FlushPayload(ptr, keytype, keytype);
271 ptr->wcs_type = keytype;
273 else /** Otherwise allocate a new one */
275 ptr = NewSubstVar(keyname, keylen, keytype);
278 /** Format the string */
279 va_start(arg_ptr, format);
280 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
285 * \brief Add a substitution variable (local to this session)
286 * \param keyname the replacementstring to substitute
287 * \param keytype the kind of the key
288 * \param format the format string ala printf
289 * \param ... the arguments to substitute in the formatstring
291 void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...)
296 struct wcsession *WCC = WC;
299 * First look if we're doing a replacement of
302 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
303 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
304 ptr = (wcsubst*)vPtr;
305 FlushPayload(ptr, 1, keytype);
306 ptr->wcs_type = keytype;
308 else /** Otherwise allocate a new one */
310 ptr = NewSubstVar(keyname, keylen, keytype);
313 /** Format the string and save it */
314 va_start(arg_ptr, format);
315 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
320 * \brief Add a substitution variable (local to this session)
321 * \param keyname the replacementstring to substitute
322 * \param keytype the kind of the key
323 * \param format the format string ala printf
324 * \param ... the arguments to substitute in the formatstring
326 void SVPut(char *keyname, size_t keylen, int keytype, char *Data)
330 struct wcsession *WCC = WC;
334 * First look if we're doing a replacement of
337 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
338 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
339 ptr = (wcsubst*)vPtr;
340 FlushPayload(ptr, 1, keytype);
341 ptr->wcs_type = keytype;
343 else /** Otherwise allocate a new one */
345 ptr = NewSubstVar(keyname, keylen, keytype);
347 StrBufAppendBufPlain(ptr->wcs_value, Data, -1, 0);
351 * \brief Add a substitution variable (local to this session)
352 * \param keyname the replacementstring to substitute
353 * \param keytype the kind of the key
354 * \param format the format string ala printf
355 * \param ... the arguments to substitute in the formatstring
357 void SVPutLong(char *keyname, size_t keylen, long Data)
361 struct wcsession *WCC = WC;
365 * First look if we're doing a replacement of
368 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
369 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
370 ptr = (wcsubst*)vPtr;
371 FlushPayload(ptr, 1, WCS_LONG);
372 ptr->wcs_type = WCS_LONG;
374 else /** Otherwise allocate a new one */
376 ptr = NewSubstVar(keyname, keylen, WCS_LONG);
382 * \brief Add a substitution variable (local to this session) that does a callback
383 * \param keyname the keystring to substitute
384 * \param fcn_ptr the function callback to give the substitution string
386 void SVCallback(char *keyname, size_t keylen, WCHandlerFunc fcn_ptr)
390 struct wcsession *WCC = WC;
393 * First look if we're doing a replacement of
396 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
397 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
398 ptr = (wcsubst*)vPtr;
399 FlushPayload(ptr, 1, WCS_FUNCTION);
400 ptr->wcs_type = WCS_FUNCTION;
402 else /** Otherwise allocate a new one */
404 ptr = NewSubstVar(keyname, keylen, WCS_FUNCTION);
407 ptr->wcs_function = fcn_ptr;
409 inline void SVCALLBACK(char *keyname, WCHandlerFunc fcn_ptr)
411 SVCallback(keyname, strlen(keyname), fcn_ptr);
416 void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref)
420 struct wcsession *WCC = WC;
423 * First look if we're doing a replacement of
426 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
427 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
428 ptr = (wcsubst*)vPtr;
429 FlushPayload(ptr, 0, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
430 ptr->wcs_type = (ref)?WCS_STRBUF_REF:WCS_STRBUF;
432 else /** Otherwise allocate a new one */
434 ptr = NewSubstVar(keyname, keylen, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
436 ptr->wcs_value = (StrBuf*)Buf;
440 * \brief back end for print_value_of() ... does a server command
441 * \param servcmd server command to execute on the citadel server
443 void pvo_do_cmd(StrBuf *Target, StrBuf *servcmd) {
447 serv_puts(ChrPtr(servcmd));
448 len = serv_getln(buf, sizeof buf);
454 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
457 _fmout(Target, "CENTER");
460 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
467 * \brief Print the value of a variable
468 * \param keyname get a key to print
470 void print_value_of(StrBuf *Target, WCTemplateToken *Token, void *Context, int ContextType) {
471 struct wcsession *WCC = WC;
475 /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
476 /// TODO: debricated!
477 if (Token->pName[0] == '=') {
478 DoTemplate(Token->pName+1, Token->NameEnd - 1, NULL, NULL, 0);
481 //////TODO: if param[1] == "U" -> urlescape
483 /** Page-local variables */
484 if ((WCC->vars!= NULL) && GetHash(WCC->vars, Token->pName, Token->NameEnd, &vVar)) {
485 ptr = (wcsubst*) vVar;
486 switch(ptr->wcs_type) {
488 StrBufAppendBuf(Target, ptr->wcs_value, 0);
491 pvo_do_cmd(Target, ptr->wcs_value);
494 (*ptr->wcs_function) (Target, Token->nParameters, Token, Context, ContextType);
498 StrBufAppendBuf(Target, ptr->wcs_value, 0);
501 StrBufAppendPrintf(Target, "%ld", ptr->lvalue);
504 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n", Token->pName);
505 StrBufAppendPrintf(Target, "<pre>WARNING: \ninvalid value in SV-Hash at %s!\n</pre>", Token->pName);
510 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup)
512 struct wcsession *WCC = WC;
516 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
517 ParamToLookup->len, &vVar)) {
518 ptr = (wcsubst*) vVar;
519 switch(ptr->wcs_type) {
523 if (ParamToCompare->Type == TYPE_STR)
524 return ((ParamToCompare->len == StrLength(ptr->wcs_value)) &&
525 (strcmp(ParamToCompare->Start, ChrPtr(ptr->wcs_value)) == 0));
527 return ParamToCompare->lvalue == StrTol(ptr->wcs_value);
535 if (ParamToCompare->Type == TYPE_STR)
538 return ParamToCompare->lvalue == ptr->lvalue;
541 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
542 ParamToLookup->Start);
548 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup)
550 struct wcsession *WCC = WC;
554 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
555 ParamToLookup->len, &vVar)) {
556 ptr = (wcsubst*) vVar;
557 switch(ptr->wcs_type) {
561 return ((StrLength(Compare) == StrLength(ptr->wcs_value)) &&
562 (strcmp(ChrPtr(Compare), ChrPtr(ptr->wcs_value)) == 0));
569 return StrTol(Compare) == ptr->lvalue;
571 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
572 ParamToLookup->Start);
579 void PutNewToken(WCTemplate *Template, WCTemplateToken *NewToken)
581 if (Template->nTokensUsed + 1 >= Template->TokenSpace) {
582 if (Template->TokenSpace <= 0) {
583 Template->Tokens = (WCTemplateToken**)malloc(
584 sizeof(WCTemplateToken*) * 10);
585 Template->TokenSpace = 10;
588 WCTemplateToken **NewTokens;
589 NewTokens= (WCTemplateToken**)malloc(
590 sizeof(WCTemplateToken*) *
591 Template->TokenSpace * 2);
592 memcpy(NewTokens, Template->Tokens,
593 sizeof(WCTemplateToken*) * Template->nTokensUsed);
594 free(Template->Tokens);
595 Template->TokenSpace *= 2;
596 Template->Tokens = NewTokens;
599 Template->Tokens[(Template->nTokensUsed)++] = NewToken;
602 TemplateParam *GetNextParameter(StrBuf *Buf, const char **pCh, const char *pe, WCTemplateToken *Token, WCTemplate *pTmpl)
604 const char *pch = *pCh;
605 const char *pchs, *pche;
606 TemplateParam *Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
609 /* Skip leading whitespaces */
610 while ((*pch == ' ' )||
613 (*pch == '\n')) pch ++;
616 else if (*pch == '\'')
621 Parm->Type = TYPE_STR;
624 ( (pch > pchs) && (*(pch - 1) == '\\'))
630 lprintf(1, "Error (in '%s' line %ld); "
631 "evaluating template param [%s] in Token [%s]\n",
632 ChrPtr(pTmpl->FileName),
634 ChrPtr(Token->FlatToken),
641 StrBufPeek(Buf, pch, -1, '\0');
642 if (LoadTemplates > 1) {
643 lprintf(1, "DBG: got param [%s] %ld %ld\n",
644 pchs, pche - pchs, strlen(pchs));
647 Parm->len = pche - pchs;
648 pch ++; /* move after trailing quote */
652 Parm->Type = TYPE_LONG;
654 while ((pch <= pe) &&
661 StrBufPeek(Buf, pch, -1, '\0');
662 Parm->lvalue = atol(pchs);
668 lprintf(1, "Error (in '%s' line %ld); "
669 "evaluating long template param [%s] in Token [%s]\n",
670 ChrPtr(pTmpl->FileName),
672 ChrPtr(Token->FlatToken),
678 while ((*pch == ' ' )||
682 (*pch == '\n')) pch ++;
688 WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
690 const char *pTmplStart,
691 const char *pTmplEnd,
696 TemplateParam *Param;
697 WCTemplateToken *NewToken = (WCTemplateToken*)malloc(sizeof(WCTemplateToken));
700 NewToken->Line = Line + 1;
701 NewToken->pTokenStart = pTmplStart;
702 NewToken->TokenStart = pTmplStart - pStart;
703 NewToken->TokenEnd = (pTmplEnd - pStart) - NewToken->TokenStart;
704 NewToken->pTokenEnd = pTmplEnd;
705 NewToken->NameEnd = NewToken->TokenEnd - 2;
706 NewToken->PreEval = NULL;
707 NewToken->FlatToken = NewStrBufPlain(pTmplStart + 2, pTmplEnd - pTmplStart - 2);
709 StrBufPeek(Buf, pTmplStart, + 1, '\0');
710 StrBufPeek(Buf, pTmplEnd, -1, '\0');
711 pch = NewToken->pName = pTmplStart + 2;
713 NewToken->HaveParameters = 0;;
714 NewToken->nParameters = 0;
716 while (pch < pTmplEnd - 1) {
718 StrBufPeek(Buf, pch, -1, '\0');
719 NewToken->NameEnd = pch - NewToken->pName;
721 while (pch < pTmplEnd - 1) {
722 Param = GetNextParameter(Buf, &pch, pTmplEnd - 1, NewToken, pTmpl);
724 NewToken->HaveParameters = 1;
725 if (NewToken->nParameters > MAXPARAM) {
726 lprintf(1, "Error (in '%s' line %ld); "
727 "only [%ld] Params allowed in Tokens [%s]\n",
728 ChrPtr(pTmpl->FileName),
731 ChrPtr(NewToken->FlatToken));
733 FreeToken(&NewToken);
736 NewToken->Params[NewToken->nParameters++] = Param;
740 if((NewToken->NameEnd == 1) &&
741 (NewToken->HaveParameters == 1))
744 if ((NewToken->nParameters == 1) &&
745 (*(NewToken->pName) == '_'))
746 NewToken->Flags = SV_GETTEXT;
747 else if ((NewToken->nParameters == 1) &&
748 (*(NewToken->pName) == '='))
749 NewToken->Flags = SV_SUBTEMPL;
750 else if ((NewToken->nParameters >= 2) &&
751 (*(NewToken->pName) == '%'))
752 NewToken->Flags = SV_CUST_STR_CONDITIONAL;
753 else if ((NewToken->nParameters >= 2) &&
754 (*(NewToken->pName) == '?'))
755 NewToken->Flags = SV_CONDITIONAL;
756 else if ((NewToken->nParameters >=2) &&
757 (*(NewToken->pName) == '!'))
758 NewToken->Flags = SV_NEG_CONDITIONAL;
764 if (NewToken->Flags == 0) {
765 /* If we're able to find out more about the token, do it now while its fresh. */
767 if (GetHash(GlobalNS, NewToken->pName, NewToken->NameEnd, &vVar)) {
768 HashHandler *Handler;
769 Handler = (HashHandler*) vVar;
770 if ((NewToken->nParameters < Handler->nMinArgs) ||
771 (NewToken->nParameters > Handler->nMaxArgs)) {
772 lprintf(1, "Handler [%s] (in '%s' line %ld); "
773 "doesn't work with %ld params [%s]\n",
775 ChrPtr(pTmpl->FileName),
777 NewToken->nParameters,
778 ChrPtr(NewToken->FlatToken));
781 NewToken->PreEval = Handler;
782 NewToken->Flags = SV_PREEVALUATED;
792 int EvaluateConditional(StrBuf *Target, WCTemplateToken *Token, WCTemplate *pTmpl, void *Context, int Neg, int state, int ContextType)
794 void *vConditional = NULL;
795 ConditionalStruct *Cond;
797 if ((Token->Params[0]->len == 1) &&
798 (Token->Params[0]->Start[0] == 'X'))
799 return (state != 0)?Token->Params[1]->lvalue:0;
801 if (!GetHash(Contitionals,
802 Token->Params[0]->Start,
803 Token->Params[0]->len,
805 (vConditional == NULL)) {
806 lprintf(1, "Conditional [%s] (in '%s' line %ld); Not found![%s]\n",
807 Token->Params[0]->Start,
808 ChrPtr(pTmpl->FileName),
810 ChrPtr(Token->FlatToken));
813 "<pre>\nConditional [%s] (in '%s' line %ld); Not found!\n[%s]\n</pre>\n",
814 Token->Params[0]->Start,
815 ChrPtr(pTmpl->FileName),
817 ChrPtr(Token->FlatToken));
821 Cond = (ConditionalStruct *) vConditional;
823 if (Token->nParameters < Cond->nParams) {
824 lprintf(1, "Conditional [%s] (in '%s' line %ld); needs %ld Params![%s]\n",
825 Token->Params[0]->Start,
826 ChrPtr(pTmpl->FileName),
829 ChrPtr(Token->FlatToken));
832 "<pre>\nConditional [%s] (in '%s' line %ld); needs %ld Params!\n[%s]\n</pre>\n",
833 Token->Params[0]->Start,
834 ChrPtr(pTmpl->FileName),
837 ChrPtr(Token->FlatToken));
840 if (Cond->CondF(Token, Context, ContextType) == Neg)
841 return Token->Params[1]->lvalue;
845 int EvaluateToken(StrBuf *Target, WCTemplateToken *Token, WCTemplate *pTmpl, void *Context, int state, int ContextType)
847 HashHandler *Handler;
849 // much output, since pName is not terminated...
850 // lprintf(1,"Doing token: %s\n",Token->pName);
852 switch (Token->Flags) {
854 TmplGettext(Target, Token->nParameters, Token);
856 case SV_CONDITIONAL: /** Forward conditional evaluation */
857 return EvaluateConditional(Target, Token, pTmpl, Context, 1, state, ContextType);
859 case SV_NEG_CONDITIONAL: /** Reverse conditional evaluation */
860 return EvaluateConditional(Target, Token, pTmpl, Context, 0, state, ContextType);
862 case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
863 if (Token->nParameters >= 6) {
864 if (EvaluateConditional(Target, Token, pTmpl, Context, 0, state, ContextType))
865 StrBufAppendBufPlain(Target,
866 Token->Params[5]->Start,
867 Token->Params[5]->len,
870 StrBufAppendBufPlain(Target,
871 Token->Params[4]->Start,
872 Token->Params[4]->len,
877 if (Token->nParameters == 1)
878 DoTemplate(Token->Params[0]->Start, Token->Params[0]->len, NULL, NULL, ContextType);
880 case SV_PREEVALUATED:
881 Handler = (HashHandler*) Token->PreEval;
882 if ((Handler->ContextRequired != CTX_NONE) &&
883 (Handler->ContextRequired != ContextType)) {
884 lprintf(1, "Handler [%s] (in '%s' line %ld); "
885 "requires context of type %ld, have %ld [%s]\n",
887 ChrPtr(pTmpl->FileName),
889 Handler->ContextRequired,
891 ChrPtr(Token->FlatToken));
894 "<pre>\nHandler [%s] (in '%s' line %ld);"
895 " requires context of type %ld, have %ld!\n[%s]\n</pre>\n",
897 ChrPtr(pTmpl->FileName),
899 Handler->ContextRequired,
901 ChrPtr(Token->FlatToken));
905 Handler->HandlerFunc(Target,
912 if (GetHash(GlobalNS, Token->pName, Token->NameEnd, &vVar)) {
913 Handler = (HashHandler*) vVar;
914 if ((Handler->ContextRequired != CTX_NONE) &&
915 (Handler->ContextRequired != ContextType)) {
916 lprintf(1, "Handler [%s] (in '%s' line %ld); "
917 "requires context of type %ld, have %ld [%s]\n",
919 ChrPtr(pTmpl->FileName),
921 Handler->ContextRequired,
923 ChrPtr(Token->FlatToken));
926 "<pre>\nHandler [%s] (in '%s' line %ld);"
927 " requires context of type %ld, have %ld!\n[%s]\n</pre>\n",
929 ChrPtr(pTmpl->FileName),
931 Handler->ContextRequired,
933 ChrPtr(Token->FlatToken));
936 else if ((Token->nParameters < Handler->nMinArgs) ||
937 (Token->nParameters > Handler->nMaxArgs)) {
938 lprintf(1, "Handler [%s] (in '%s' line %ld); "
939 "doesn't work with %ld params [%s]\n",
941 ChrPtr(pTmpl->FileName),
944 ChrPtr(Token->FlatToken));
947 "<pre>\nHandler [%s] (in '%s' line %ld);"
948 " doesn't work with %ld params!\n[%s]\n</pre>\n",
950 ChrPtr(pTmpl->FileName),
953 ChrPtr(Token->FlatToken));
956 Handler->HandlerFunc(Target,
960 ContextType); /*TODO: subset of that */
965 print_value_of(Target, Token, Context, ContextType);
971 void ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, void *Context, int ContextType)
973 WCTemplate *pTmpl = Tmpl;
976 const char *pData, *pS;
979 if (LoadTemplates != 0) {
980 if (LoadTemplates > 1)
981 lprintf(1, "DBG: ----- loading: [%s] ------ \n",
982 ChrPtr(Tmpl->FileName));
983 pTmpl = load_template(Tmpl->FileName, NULL, NULL);
987 "<pre>\nError loading Template [%s]\n See Logfile for details\n</pre>\n",
988 ChrPtr(Tmpl->FileName));
994 pS = pData = ChrPtr(pTmpl->Data);
995 len = StrLength(pTmpl->Data);
999 if (i >= pTmpl->nTokensUsed) {
1000 StrBufAppendBufPlain(Target,
1002 len - (pData - pS), 0);
1006 StrBufAppendBufPlain(
1008 pTmpl->Tokens[i]->pTokenStart - pData, 0);
1009 state = EvaluateToken(Target, pTmpl->Tokens[i], pTmpl, Context, state, ContextType);
1010 while ((state != 0) && (i+1 < pTmpl->nTokensUsed)) {
1011 /* condition told us to skip till its end condition */
1013 if ((pTmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
1014 (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
1015 if (state == EvaluateConditional(
1020 pTmpl->Tokens[i]->Flags,
1026 pData = pTmpl->Tokens[i++]->pTokenEnd + 1;
1027 if (i > pTmpl->nTokensUsed)
1031 if (LoadTemplates != 0) {
1032 FreeWCTemplate(pTmpl);
1038 * \brief Display a variable-substituted template
1039 * \param templatename template file to load
1041 void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1043 WCTemplate *NewTemplate;
1044 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1045 NewTemplate->Data = NULL;
1046 NewTemplate->FileName = NewStrBufDup(filename);
1047 NewTemplate->nTokensUsed = 0;
1048 NewTemplate->TokenSpace = 0;
1049 NewTemplate->Tokens = NULL;
1051 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1056 * \brief Display a variable-substituted template
1057 * \param templatename template file to load
1059 void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1062 struct stat statbuf;
1063 const char *pS, *pE, *pch, *Err;
1066 WCTemplate *NewTemplate;
1068 fd = open(ChrPtr(filename), O_RDONLY);
1070 lprintf(1, "ERROR: could not open template '%s' - %s\n",
1071 ChrPtr(filename), strerror(errno));
1075 if (fstat(fd, &statbuf) == -1) {
1076 lprintf(1, "ERROR: could not stat template '%s' - %s\n",
1077 ChrPtr(filename), strerror(errno));
1081 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1082 NewTemplate->Data = NewStrBufPlain(NULL, statbuf.st_size);
1083 NewTemplate->FileName = NewStrBufDup(filename);
1084 NewTemplate->nTokensUsed = 0;
1085 NewTemplate->TokenSpace = 0;
1086 NewTemplate->Tokens = NULL;
1087 if (StrBufReadBLOB(NewTemplate->Data, &fd, 1, statbuf.st_size, &Err) < 0) {
1089 FreeWCTemplate(NewTemplate);
1090 lprintf(1, "ERROR: reading template '%s' - %s<br />\n",
1091 ChrPtr(filename), strerror(errno));
1097 pS = pch = ChrPtr(NewTemplate->Data);
1098 pE = pS + StrLength(NewTemplate->Data);
1100 const char *pts, *pte;
1102 int InDoubleQuotes = 0;
1104 /** Find one <? > */
1106 for (; pch < pE; pch ++) {
1107 if ((*pch=='<')&&(*(pch + 1)=='?'))
1109 if (*pch=='\n') Line ++;
1115 /** Found one? parse it. */
1116 for (; pch < pE - 1; pch ++) {
1118 InDoubleQuotes = ! InDoubleQuotes;
1119 else if (*pch == '\'')
1120 InQuotes = ! InQuotes;
1121 else if ((!InQuotes && !InDoubleQuotes) &&
1122 ((*pch!='\\')&&(*(pch + 1)=='>'))) {
1130 PutNewToken(NewTemplate,
1131 NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte, Line, NewTemplate));
1134 if (LoadTemplates == 0)
1135 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1140 ///void PrintTemplate(const char *Key, void *vSubst, int odd)
1141 const char* PrintTemplate(void *vSubst)
1143 WCTemplate *Tmpl = vSubst;
1145 return ChrPtr(Tmpl->FileName);
1151 * \brief Display a variable-substituted template
1152 * \param templatename template file to load
1154 void DoTemplate(const char *templatename, long len, StrBuf *Target, void *Context, int ContextType)
1157 HashList *StaticLocal;
1162 if (WC->is_mobile) {
1163 Static = WirelessTemplateCache;
1164 StaticLocal = WirelessLocalTemplateCache;
1167 Static = TemplateCache;
1168 StaticLocal = LocalTemplateCache;
1173 lprintf (1, "Can't to load a template with empty name!\n");
1174 StrBufAppendPrintf(Target, "<pre>\nCan't to load a template with empty name!\n</pre>");
1178 if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
1179 !GetHash(Static, templatename, len, &vTmpl)) {
1180 lprintf (1, "didn't find Template [%s] %ld %ld\n", templatename, len , (long)strlen(templatename));
1181 StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>",
1183 (long)strlen(templatename));
1184 /// dbg_PrintHash(Static, PrintTemplate, NULL);
1185 // PrintHash(Static, VarPrintTransition, PrintTemplate);
1190 ProcessTemplate(vTmpl, Target, Context, ContextType);
1193 int LoadTemplateDir(const char *DirName, HashList *wireless, HashList *big)
1198 DIR *filedir = NULL;
1199 struct dirent *filedir_entry;
1205 StrBufPrintf(Dir, "%s/t", DirName);
1206 filedir = opendir (ChrPtr(Dir));
1207 if (filedir == NULL) {
1212 FileName = NewStrBuf();
1214 while ((filedir_entry = readdir(filedir)))
1218 #ifdef _DIRENT_HAVE_D_NAMELEN
1219 d_namelen = filedir_entry->d_namelen;
1221 d_namelen = strlen(filedir_entry->d_name);
1223 d_without_ext = d_namelen;
1224 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1226 if ((d_without_ext == 0) || (d_namelen < 3))
1228 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
1229 continue; /* Ignore backup files... */
1231 IsMobile = (strstr(filedir_entry->d_name, ".m.html")!= NULL);
1232 PStart = filedir_entry->d_name;
1233 StrBufPrintf(FileName, "%s/%s", ChrPtr(Dir), filedir_entry->d_name);
1234 MinorPtr = strchr(filedir_entry->d_name, '.');
1235 if (MinorPtr != NULL)
1237 StrBufPlain(Tag, filedir_entry->d_name, MinorPtr - filedir_entry->d_name);
1240 lprintf(1, "%s %d %s\n",ChrPtr(FileName), IsMobile, ChrPtr(Tag));
1241 if (LoadTemplates == 0)
1242 load_template(FileName, Tag, (IsMobile)?wireless:big);
1244 prepare_template(FileName, Tag, (IsMobile)?wireless:big);
1247 FreeStrBuf(&FileName);
1253 void InitTemplateCache(void)
1255 LoadTemplateDir(static_dirs[0],
1256 WirelessTemplateCache,
1258 LoadTemplateDir(static_dirs[1],
1259 WirelessLocalTemplateCache,
1260 LocalTemplateCache);
1263 void tmplput_serv_ip(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1265 StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
1268 void tmplput_serv_nodename(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1270 StrEscAppend(Target, NULL, serv_info.serv_nodename, 0, 0);
1273 void tmplput_serv_humannode(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1275 StrEscAppend(Target, NULL, serv_info.serv_humannode, 0, 0);
1278 void tmplput_serv_fqdn(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1280 StrEscAppend(Target, NULL, serv_info.serv_fqdn, 0, 0);
1283 void tmmplput_serv_software(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1285 StrEscAppend(Target, NULL, serv_info.serv_software, 0, 0);
1288 void tmplput_serv_rev_level(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1290 StrBufAppendPrintf(Target, "%d.%02d",
1291 serv_info.serv_rev_level / 100,
1292 serv_info.serv_rev_level % 100);
1295 void tmmplput_serv_bbs_city(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1297 StrEscAppend(Target, NULL, serv_info.serv_bbs_city, 0, 0);
1300 void tmplput_current_user(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1302 StrEscAppend(Target, NULL, WC->wc_fullname, 0, 0);
1305 void tmplput_current_room(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1307 StrEscAppend(Target, NULL, WC->wc_roomname, 0, 0);
1311 typedef struct _HashIterator {
1312 HashList *StaticList;
1313 int AdditionalParams;
1315 RetrieveHashlistFunc GetHash;
1316 HashDestructorFunc Destructor;
1317 SubTemplFunc DoSubTemplate;
1320 void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1332 if (!GetHash(Iterators,
1333 Tokens->Params[0]->Start,
1334 Tokens->Params[0]->len,
1336 lprintf(1, "unknown Iterator [%s] (in line %ld); "
1338 Tokens->Params[0]->Start,
1340 ChrPtr(Tokens->FlatToken));
1343 "<pre>\nunknown Iterator [%s] (in line %ld); \n"
1345 Tokens->Params[0]->Start,
1347 ChrPtr(Tokens->FlatToken));
1351 It = (HashIterator*) vIt;
1353 if (Tokens->nParameters < It->AdditionalParams + 2) {
1354 lprintf(1, "Iterator [%s] (in line %ld); "
1355 "doesn't work with %ld params [%s]\n",
1356 Tokens->Params[0]->Start,
1358 Tokens->nParameters,
1359 ChrPtr(Tokens->FlatToken));
1362 "<pre>Iterator [%s] \n(in line %ld);\n"
1363 "doesn't work with %ld params \n[%s]\n</pre>",
1364 Tokens->Params[0]->Start,
1366 Tokens->nParameters,
1367 ChrPtr(Tokens->FlatToken));
1371 if (It->StaticList == NULL)
1372 List = It->GetHash(Target, nArgs, Tokens, Context, ContextType);
1374 List = It->StaticList;
1376 SubBuf = NewStrBuf();
1377 it = GetNewHashPos();
1378 while (GetNextHashPos(List, it, &len, &Key, &vContext)) {
1379 svprintf(HKEY("ITERATE:ODDEVEN"), WCS_STRING, "%s",
1380 (oddeven) ? "odd" : "even");
1381 svprintf(HKEY("ITERATE:KEY"), WCS_STRING, "%s", Key);
1383 if (It->DoSubTemplate != NULL)
1384 It->DoSubTemplate(SubBuf, vContext, Tokens);
1385 DoTemplate(Tokens->Params[1]->Start,
1386 Tokens->Params[1]->len,
1390 StrBufAppendBuf(Target, SubBuf, 0);
1391 FlushStrBuf(SubBuf);
1392 oddeven = ~ oddeven;
1394 FreeStrBuf(&SubBuf);
1396 if (It->Destructor != NULL)
1397 It->Destructor(&List);
1400 int ConditionalVar(WCTemplateToken *Tokens, void *Context, int ContextType)
1405 if (!GetHash(WC->vars,
1406 Tokens->Params[2]->Start,
1407 Tokens->Params[2]->len,
1410 subst = (wcsubst*) vsubst;
1411 if ((subst->ContextRequired != CTX_NONE) &&
1412 (subst->ContextRequired != ContextType)) {
1413 lprintf(1," WARNING: Conditional requires Context: [%ld]!\n", Tokens->Params[2]->Start);
1417 switch(subst->wcs_type) {
1419 return (subst->wcs_function!=NULL);
1421 lprintf(1, " -> Server [%s]\n", subst->wcs_value);////todo
1425 case WCS_STRBUF_REF:
1426 if (Tokens->nParameters < 4)
1428 return (strcmp(Tokens->Params[3]->Start, ChrPtr(subst->wcs_value)) == 0);
1430 if (Tokens->nParameters < 4)
1431 return (subst->lvalue != 0);
1432 return (subst->lvalue == Tokens->Params[3]->lvalue);
1434 lprintf(1," WARNING: invalid type: [%ld]!\n", subst->wcs_type);
1440 void RegisterITERATOR(const char *Name, long len,
1441 int AdditionalParams,
1442 HashList *StaticList,
1443 RetrieveHashlistFunc GetHash,
1444 SubTemplFunc DoSubTempl,
1445 HashDestructorFunc Destructor,
1448 HashIterator *It = (HashIterator*)malloc(sizeof(HashIterator));
1449 It->StaticList = StaticList;
1450 It->AdditionalParams = AdditionalParams;
1451 It->GetHash = GetHash;
1452 It->DoSubTemplate = DoSubTempl;
1453 It->Destructor = Destructor;
1454 It->ContextType = ContextType;
1455 Put(Iterators, Name, len, It, NULL);
1458 void RegisterConditional(const char *Name, long len,
1460 WCConditionalFunc CondF,
1461 int ContextRequired)
1463 ConditionalStruct *Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
1464 Cond->nParams = nParams;
1465 Cond->CondF = CondF;
1466 Cond->ContextRequired = ContextRequired;
1467 Put(Contitionals, Name, len, Cond, NULL);
1470 void tmpl_do_boxed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1473 StrBuf *Headline = NewStrBuf();
1474 DoTemplate(Tokens->Params[1]->Start,
1475 Tokens->Params[1]->len,
1479 SVPutBuf("BOXTITLE", Headline, 0);
1482 DoTemplate(HKEY("beginbox"), Target, Context, ContextType);
1483 DoTemplate(Tokens->Params[0]->Start,
1484 Tokens->Params[0]->len,
1488 DoTemplate(HKEY("endbox"), Target, Context, ContextType);
1491 void tmpl_do_tabbed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1494 int i, ntabs, nTabs;
1496 nTabs = ntabs = Tokens->nParameters / 2;
1497 TabNames = (StrBuf **) malloc(ntabs * sizeof(StrBuf*));
1499 for (i = 0; i < ntabs; i++) {
1500 TabNames[i] = NewStrBuf();
1501 if (Tokens->Params[i * 2]->len > 0) {
1502 DoTemplate(Tokens->Params[i * 2]->Start,
1503 Tokens->Params[i * 2]->len,
1509 /** A Tab without subject? we can't count that, add it as silent */
1514 StrTabbedDialog(Target, nTabs, TabNames);
1515 for (i = 0; i < ntabs; i++) {
1516 StrBeginTab(Target, i, nTabs);
1518 DoTemplate(Tokens->Params[i * 2 + 1]->Start,
1519 Tokens->Params[i * 2 + 1]->len,
1523 StrEndTab(Target, i, nTabs);
1531 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, CTX_NONE);
1532 RegisterNamespace("SERV:NODENAME", 0, 0, tmplput_serv_nodename, CTX_NONE);
1533 RegisterNamespace("SERV:HUMANNODE", 0, 0, tmplput_serv_humannode, CTX_NONE);
1534 RegisterNamespace("SERV:FQDN", 0, 0, tmplput_serv_fqdn, CTX_NONE);
1535 RegisterNamespace("SERV:SOFTWARE", 0, 0, tmmplput_serv_software, CTX_NONE);
1536 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, CTX_NONE);
1537 RegisterNamespace("SERV:BBS_CITY", 0, 0, tmmplput_serv_bbs_city, CTX_NONE);
1538 /// RegisterNamespace("SERV:LDAP_SUPP", 0, 0, tmmplput_serv_ldap_enabled, 0);
1539 RegisterNamespace("CURRENT_USER", 0, 0, tmplput_current_user, CTX_NONE);
1540 RegisterNamespace("CURRENT_ROOM", 0, 0, tmplput_current_room, CTX_NONE);
1541 RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl, CTX_NONE);
1542 RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed, CTX_NONE);
1543 RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed, CTX_NONE);
1544 RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar, CTX_NONE);