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);
509 lprintf(1, "didn't find Handler [%s] (in '%s' line %ld); "
512 ChrPtr(Token->FileName),
514 ChrPtr(Token->FlatToken));
518 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup)
520 struct wcsession *WCC = WC;
524 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
525 ParamToLookup->len, &vVar)) {
526 ptr = (wcsubst*) vVar;
527 switch(ptr->wcs_type) {
531 if (ParamToCompare->Type == TYPE_STR)
532 return ((ParamToCompare->len == StrLength(ptr->wcs_value)) &&
533 (strcmp(ParamToCompare->Start, ChrPtr(ptr->wcs_value)) == 0));
535 return ParamToCompare->lvalue == StrTol(ptr->wcs_value);
543 if (ParamToCompare->Type == TYPE_STR)
546 return ParamToCompare->lvalue == ptr->lvalue;
549 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
550 ParamToLookup->Start);
556 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup)
558 struct wcsession *WCC = WC;
562 if ((WCC->vars!= NULL) && GetHash(WCC->vars, ParamToLookup->Start,
563 ParamToLookup->len, &vVar)) {
564 ptr = (wcsubst*) vVar;
565 switch(ptr->wcs_type) {
569 return ((StrLength(Compare) == StrLength(ptr->wcs_value)) &&
570 (strcmp(ChrPtr(Compare), ChrPtr(ptr->wcs_value)) == 0));
577 return StrTol(Compare) == ptr->lvalue;
579 lprintf(1,"WARNING: invalid value in SV-Hash at %s!\n",
580 ParamToLookup->Start);
587 void PutNewToken(WCTemplate *Template, WCTemplateToken *NewToken)
589 if (Template->nTokensUsed + 1 >= Template->TokenSpace) {
590 if (Template->TokenSpace <= 0) {
591 Template->Tokens = (WCTemplateToken**)malloc(
592 sizeof(WCTemplateToken*) * 10);
593 Template->TokenSpace = 10;
596 WCTemplateToken **NewTokens;
597 NewTokens= (WCTemplateToken**)malloc(
598 sizeof(WCTemplateToken*) *
599 Template->TokenSpace * 2);
600 memcpy(NewTokens, Template->Tokens,
601 sizeof(WCTemplateToken*) * Template->nTokensUsed);
602 free(Template->Tokens);
603 Template->TokenSpace *= 2;
604 Template->Tokens = NewTokens;
607 Template->Tokens[(Template->nTokensUsed)++] = NewToken;
610 TemplateParam *GetNextParameter(StrBuf *Buf, const char **pCh, const char *pe, WCTemplateToken *Token, WCTemplate *pTmpl)
612 const char *pch = *pCh;
613 const char *pchs, *pche;
614 TemplateParam *Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
617 /* Skip leading whitespaces */
618 while ((*pch == ' ' )||
621 (*pch == '\n')) pch ++;
624 else if (*pch == '\'')
629 Parm->Type = TYPE_STR;
632 ( (pch > pchs) && (*(pch - 1) == '\\'))
638 lprintf(1, "Error (in '%s' line %ld); "
639 "evaluating template param [%s] in Token [%s]\n",
640 ChrPtr(pTmpl->FileName),
642 ChrPtr(Token->FlatToken),
649 StrBufPeek(Buf, pch, -1, '\0');
650 if (LoadTemplates > 1) {
651 lprintf(1, "DBG: got param [%s] %ld %ld\n",
652 pchs, pche - pchs, strlen(pchs));
655 Parm->len = pche - pchs;
656 pch ++; /* move after trailing quote */
660 Parm->Type = TYPE_LONG;
662 while ((pch <= pe) &&
669 StrBufPeek(Buf, pch, -1, '\0');
670 Parm->lvalue = atol(pchs);
676 lprintf(1, "Error (in '%s' line %ld); "
677 "evaluating long template param [%s] in Token [%s]\n",
678 ChrPtr(pTmpl->FileName),
680 ChrPtr(Token->FlatToken),
686 while ((*pch == ' ' )||
690 (*pch == '\n')) pch ++;
696 WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
698 const char *pTmplStart,
699 const char *pTmplEnd,
704 TemplateParam *Param;
705 WCTemplateToken *NewToken = (WCTemplateToken*)malloc(sizeof(WCTemplateToken));
707 NewToken->FileName = pTmpl->FileName; /* to print meaningfull log messages... */
709 NewToken->Line = Line + 1;
710 NewToken->pTokenStart = pTmplStart;
711 NewToken->TokenStart = pTmplStart - pStart;
712 NewToken->TokenEnd = (pTmplEnd - pStart) - NewToken->TokenStart;
713 NewToken->pTokenEnd = pTmplEnd;
714 NewToken->NameEnd = NewToken->TokenEnd - 2;
715 NewToken->PreEval = NULL;
716 NewToken->FlatToken = NewStrBufPlain(pTmplStart + 2, pTmplEnd - pTmplStart - 2);
718 StrBufPeek(Buf, pTmplStart, + 1, '\0');
719 StrBufPeek(Buf, pTmplEnd, -1, '\0');
720 pch = NewToken->pName = pTmplStart + 2;
722 NewToken->HaveParameters = 0;;
723 NewToken->nParameters = 0;
725 while (pch < pTmplEnd - 1) {
727 StrBufPeek(Buf, pch, -1, '\0');
728 NewToken->NameEnd = pch - NewToken->pName;
730 while (pch < pTmplEnd - 1) {
731 Param = GetNextParameter(Buf, &pch, pTmplEnd - 1, NewToken, pTmpl);
733 NewToken->HaveParameters = 1;
734 if (NewToken->nParameters > MAXPARAM) {
735 lprintf(1, "Error (in '%s' line %ld); "
736 "only [%ld] Params allowed in Tokens [%s]\n",
737 ChrPtr(pTmpl->FileName),
740 ChrPtr(NewToken->FlatToken));
742 FreeToken(&NewToken);
745 NewToken->Params[NewToken->nParameters++] = Param;
749 if((NewToken->NameEnd == 1) &&
750 (NewToken->HaveParameters == 1))
753 if ((NewToken->nParameters == 1) &&
754 (*(NewToken->pName) == '_'))
755 NewToken->Flags = SV_GETTEXT;
756 else if ((NewToken->nParameters == 1) &&
757 (*(NewToken->pName) == '='))
758 NewToken->Flags = SV_SUBTEMPL;
759 else if ((NewToken->nParameters >= 2) &&
760 (*(NewToken->pName) == '%'))
761 NewToken->Flags = SV_CUST_STR_CONDITIONAL;
762 else if ((NewToken->nParameters >= 2) &&
763 (*(NewToken->pName) == '?'))
764 NewToken->Flags = SV_CONDITIONAL;
765 else if ((NewToken->nParameters >=2) &&
766 (*(NewToken->pName) == '!'))
767 NewToken->Flags = SV_NEG_CONDITIONAL;
773 if (NewToken->Flags == 0) {
774 /* If we're able to find out more about the token, do it now while its fresh. */
776 if (GetHash(GlobalNS, NewToken->pName, NewToken->NameEnd, &vVar)) {
777 HashHandler *Handler;
778 Handler = (HashHandler*) vVar;
779 if ((NewToken->nParameters < Handler->nMinArgs) ||
780 (NewToken->nParameters > Handler->nMaxArgs)) {
781 lprintf(1, "Handler [%s] (in '%s' line %ld); "
782 "doesn't work with %ld params [%s]\n",
784 ChrPtr(pTmpl->FileName),
786 NewToken->nParameters,
787 ChrPtr(NewToken->FlatToken));
790 NewToken->PreEval = Handler;
791 NewToken->Flags = SV_PREEVALUATED;
801 int EvaluateConditional(StrBuf *Target, WCTemplateToken *Token, WCTemplate *pTmpl, void *Context, int Neg, int state, int ContextType)
803 void *vConditional = NULL;
804 ConditionalStruct *Cond;
806 if ((Token->Params[0]->len == 1) &&
807 (Token->Params[0]->Start[0] == 'X'))
808 return (state != 0)?Token->Params[1]->lvalue:0;
810 if (!GetHash(Contitionals,
811 Token->Params[0]->Start,
812 Token->Params[0]->len,
814 (vConditional == NULL)) {
815 lprintf(1, "Conditional [%s] (in '%s' line %ld); Not found![%s]\n",
816 Token->Params[0]->Start,
817 ChrPtr(pTmpl->FileName),
819 ChrPtr(Token->FlatToken));
822 "<pre>\nConditional [%s] (in '%s' line %ld); Not found!\n[%s]\n</pre>\n",
823 Token->Params[0]->Start,
824 ChrPtr(pTmpl->FileName),
826 ChrPtr(Token->FlatToken));
830 Cond = (ConditionalStruct *) vConditional;
832 if (Token->nParameters < Cond->nParams) {
833 lprintf(1, "Conditional [%s] (in '%s' line %ld); needs %ld Params![%s]\n",
834 Token->Params[0]->Start,
835 ChrPtr(pTmpl->FileName),
838 ChrPtr(Token->FlatToken));
841 "<pre>\nConditional [%s] (in '%s' line %ld); needs %ld Params!\n[%s]\n</pre>\n",
842 Token->Params[0]->Start,
843 ChrPtr(pTmpl->FileName),
846 ChrPtr(Token->FlatToken));
849 if (Cond->CondF(Token, Context, ContextType) == Neg)
850 return Token->Params[1]->lvalue;
854 int EvaluateToken(StrBuf *Target, WCTemplateToken *Token, WCTemplate *pTmpl, void *Context, int state, int ContextType)
856 HashHandler *Handler;
858 // much output, since pName is not terminated...
859 // lprintf(1,"Doing token: %s\n",Token->pName);
861 switch (Token->Flags) {
863 TmplGettext(Target, Token->nParameters, Token);
865 case SV_CONDITIONAL: /** Forward conditional evaluation */
866 return EvaluateConditional(Target, Token, pTmpl, Context, 1, state, ContextType);
868 case SV_NEG_CONDITIONAL: /** Reverse conditional evaluation */
869 return EvaluateConditional(Target, Token, pTmpl, Context, 0, state, ContextType);
871 case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
872 if (Token->nParameters >= 6) {
873 if (EvaluateConditional(Target, Token, pTmpl, Context, 0, state, ContextType))
874 StrBufAppendBufPlain(Target,
875 Token->Params[5]->Start,
876 Token->Params[5]->len,
879 StrBufAppendBufPlain(Target,
880 Token->Params[4]->Start,
881 Token->Params[4]->len,
886 if (Token->nParameters == 1)
887 DoTemplate(Token->Params[0]->Start, Token->Params[0]->len, NULL, NULL, ContextType);
889 case SV_PREEVALUATED:
890 Handler = (HashHandler*) Token->PreEval;
891 if ((Handler->ContextRequired != CTX_NONE) &&
892 (Handler->ContextRequired != ContextType)) {
893 lprintf(1, "Handler [%s] (in '%s' line %ld); "
894 "requires context of type %ld, have %ld [%s]\n",
896 ChrPtr(pTmpl->FileName),
898 Handler->ContextRequired,
900 ChrPtr(Token->FlatToken));
903 "<pre>\nHandler [%s] (in '%s' line %ld);"
904 " requires context of type %ld, have %ld!\n[%s]\n</pre>\n",
906 ChrPtr(pTmpl->FileName),
908 Handler->ContextRequired,
910 ChrPtr(Token->FlatToken));
914 Handler->HandlerFunc(Target,
921 if (GetHash(GlobalNS, Token->pName, Token->NameEnd, &vVar)) {
922 Handler = (HashHandler*) vVar;
923 if ((Handler->ContextRequired != CTX_NONE) &&
924 (Handler->ContextRequired != ContextType)) {
925 lprintf(1, "Handler [%s] (in '%s' line %ld); "
926 "requires context of type %ld, have %ld [%s]\n",
928 ChrPtr(pTmpl->FileName),
930 Handler->ContextRequired,
932 ChrPtr(Token->FlatToken));
935 "<pre>\nHandler [%s] (in '%s' line %ld);"
936 " requires context of type %ld, have %ld!\n[%s]\n</pre>\n",
938 ChrPtr(pTmpl->FileName),
940 Handler->ContextRequired,
942 ChrPtr(Token->FlatToken));
945 else if ((Token->nParameters < Handler->nMinArgs) ||
946 (Token->nParameters > Handler->nMaxArgs)) {
947 lprintf(1, "Handler [%s] (in '%s' line %ld); "
948 "doesn't work with %ld params [%s]\n",
950 ChrPtr(pTmpl->FileName),
953 ChrPtr(Token->FlatToken));
956 "<pre>\nHandler [%s] (in '%s' line %ld);"
957 " doesn't work with %ld params!\n[%s]\n</pre>\n",
959 ChrPtr(pTmpl->FileName),
962 ChrPtr(Token->FlatToken));
965 Handler->HandlerFunc(Target,
969 ContextType); /*TODO: subset of that */
974 print_value_of(Target, Token, Context, ContextType);
980 void ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, void *Context, int ContextType)
982 WCTemplate *pTmpl = Tmpl;
985 const char *pData, *pS;
988 if (LoadTemplates != 0) {
989 if (LoadTemplates > 1)
990 lprintf(1, "DBG: ----- loading: [%s] ------ \n",
991 ChrPtr(Tmpl->FileName));
992 pTmpl = load_template(Tmpl->FileName, NULL, NULL);
996 "<pre>\nError loading Template [%s]\n See Logfile for details\n</pre>\n",
997 ChrPtr(Tmpl->FileName));
1003 pS = pData = ChrPtr(pTmpl->Data);
1004 len = StrLength(pTmpl->Data);
1008 if (i >= pTmpl->nTokensUsed) {
1009 StrBufAppendBufPlain(Target,
1011 len - (pData - pS), 0);
1015 StrBufAppendBufPlain(
1017 pTmpl->Tokens[i]->pTokenStart - pData, 0);
1018 state = EvaluateToken(Target, pTmpl->Tokens[i], pTmpl, Context, state, ContextType);
1019 while ((state != 0) && (i+1 < pTmpl->nTokensUsed)) {
1020 /* condition told us to skip till its end condition */
1022 if ((pTmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
1023 (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
1024 if (state == EvaluateConditional(
1029 pTmpl->Tokens[i]->Flags,
1035 pData = pTmpl->Tokens[i++]->pTokenEnd + 1;
1036 if (i > pTmpl->nTokensUsed)
1040 if (LoadTemplates != 0) {
1041 FreeWCTemplate(pTmpl);
1047 * \brief Display a variable-substituted template
1048 * \param templatename template file to load
1050 void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1052 WCTemplate *NewTemplate;
1053 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1054 NewTemplate->Data = NULL;
1055 NewTemplate->FileName = NewStrBufDup(filename);
1056 NewTemplate->nTokensUsed = 0;
1057 NewTemplate->TokenSpace = 0;
1058 NewTemplate->Tokens = NULL;
1060 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1065 * \brief Display a variable-substituted template
1066 * \param templatename template file to load
1068 void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
1071 struct stat statbuf;
1072 const char *pS, *pE, *pch, *Err;
1075 WCTemplate *NewTemplate;
1077 fd = open(ChrPtr(filename), O_RDONLY);
1079 lprintf(1, "ERROR: could not open template '%s' - %s\n",
1080 ChrPtr(filename), strerror(errno));
1084 if (fstat(fd, &statbuf) == -1) {
1085 lprintf(1, "ERROR: could not stat template '%s' - %s\n",
1086 ChrPtr(filename), strerror(errno));
1090 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
1091 NewTemplate->Data = NewStrBufPlain(NULL, statbuf.st_size);
1092 NewTemplate->FileName = NewStrBufDup(filename);
1093 NewTemplate->nTokensUsed = 0;
1094 NewTemplate->TokenSpace = 0;
1095 NewTemplate->Tokens = NULL;
1096 if (StrBufReadBLOB(NewTemplate->Data, &fd, 1, statbuf.st_size, &Err) < 0) {
1098 FreeWCTemplate(NewTemplate);
1099 lprintf(1, "ERROR: reading template '%s' - %s<br />\n",
1100 ChrPtr(filename), strerror(errno));
1106 pS = pch = ChrPtr(NewTemplate->Data);
1107 pE = pS + StrLength(NewTemplate->Data);
1109 const char *pts, *pte;
1111 int InDoubleQuotes = 0;
1113 /** Find one <? > */
1115 for (; pch < pE; pch ++) {
1116 if ((*pch=='<')&&(*(pch + 1)=='?'))
1118 if (*pch=='\n') Line ++;
1124 /** Found one? parse it. */
1125 for (; pch < pE - 1; pch ++) {
1127 InDoubleQuotes = ! InDoubleQuotes;
1128 else if (*pch == '\'')
1129 InQuotes = ! InQuotes;
1130 else if ((!InQuotes && !InDoubleQuotes) &&
1131 ((*pch!='\\')&&(*(pch + 1)=='>'))) {
1139 PutNewToken(NewTemplate,
1140 NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte, Line, NewTemplate));
1143 if (LoadTemplates == 0)
1144 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
1149 ///void PrintTemplate(const char *Key, void *vSubst, int odd)
1150 const char* PrintTemplate(void *vSubst)
1152 WCTemplate *Tmpl = vSubst;
1154 return ChrPtr(Tmpl->FileName);
1160 * \brief Display a variable-substituted template
1161 * \param templatename template file to load
1163 void DoTemplate(const char *templatename, long len, StrBuf *Target, void *Context, int ContextType)
1166 HashList *StaticLocal;
1171 if (WC->is_mobile) {
1172 Static = WirelessTemplateCache;
1173 StaticLocal = WirelessLocalTemplateCache;
1176 Static = TemplateCache;
1177 StaticLocal = LocalTemplateCache;
1182 lprintf (1, "Can't to load a template with empty name!\n");
1183 StrBufAppendPrintf(Target, "<pre>\nCan't to load a template with empty name!\n</pre>");
1187 if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
1188 !GetHash(Static, templatename, len, &vTmpl)) {
1189 lprintf (1, "didn't find Template [%s] %ld %ld\n", templatename, len , (long)strlen(templatename));
1190 StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>",
1192 (long)strlen(templatename));
1193 /// dbg_PrintHash(Static, PrintTemplate, NULL);
1194 // PrintHash(Static, VarPrintTransition, PrintTemplate);
1199 ProcessTemplate(vTmpl, Target, Context, ContextType);
1202 int LoadTemplateDir(const char *DirName, HashList *wireless, HashList *big)
1207 DIR *filedir = NULL;
1208 struct dirent *filedir_entry;
1214 StrBufPrintf(Dir, "%s/t", DirName);
1215 filedir = opendir (ChrPtr(Dir));
1216 if (filedir == NULL) {
1221 FileName = NewStrBuf();
1223 while ((filedir_entry = readdir(filedir)))
1227 #ifdef _DIRENT_HAVE_D_NAMELEN
1228 d_namelen = filedir_entry->d_namelen;
1230 d_namelen = strlen(filedir_entry->d_name);
1232 d_without_ext = d_namelen;
1233 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1235 if ((d_without_ext == 0) || (d_namelen < 3))
1237 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
1238 continue; /* Ignore backup files... */
1240 IsMobile = (strstr(filedir_entry->d_name, ".m.html")!= NULL);
1241 PStart = filedir_entry->d_name;
1242 StrBufPrintf(FileName, "%s/%s", ChrPtr(Dir), filedir_entry->d_name);
1243 MinorPtr = strchr(filedir_entry->d_name, '.');
1244 if (MinorPtr != NULL)
1246 StrBufPlain(Tag, filedir_entry->d_name, MinorPtr - filedir_entry->d_name);
1249 lprintf(1, "%s %d %s\n",ChrPtr(FileName), IsMobile, ChrPtr(Tag));
1250 if (LoadTemplates == 0)
1251 load_template(FileName, Tag, (IsMobile)?wireless:big);
1253 prepare_template(FileName, Tag, (IsMobile)?wireless:big);
1256 FreeStrBuf(&FileName);
1262 void InitTemplateCache(void)
1264 LoadTemplateDir(static_dirs[0],
1265 WirelessTemplateCache,
1267 LoadTemplateDir(static_dirs[1],
1268 WirelessLocalTemplateCache,
1269 LocalTemplateCache);
1272 void tmplput_serv_ip(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1274 StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
1277 void tmplput_serv_nodename(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1279 StrEscAppend(Target, NULL, serv_info.serv_nodename, 0, 0);
1282 void tmplput_serv_humannode(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1284 StrEscAppend(Target, NULL, serv_info.serv_humannode, 0, 0);
1287 void tmplput_serv_fqdn(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1289 StrEscAppend(Target, NULL, serv_info.serv_fqdn, 0, 0);
1292 void tmmplput_serv_software(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1294 StrEscAppend(Target, NULL, serv_info.serv_software, 0, 0);
1297 void tmplput_serv_rev_level(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1299 StrBufAppendPrintf(Target, "%d.%02d",
1300 serv_info.serv_rev_level / 100,
1301 serv_info.serv_rev_level % 100);
1304 void tmmplput_serv_bbs_city(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1306 StrEscAppend(Target, NULL, serv_info.serv_bbs_city, 0, 0);
1309 void tmplput_current_user(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1311 StrEscAppend(Target, NULL, WC->wc_fullname, 0, 0);
1314 void tmplput_current_room(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1316 StrEscAppend(Target, NULL, WC->wc_roomname, 0, 0);
1320 typedef struct _HashIterator {
1321 HashList *StaticList;
1322 int AdditionalParams;
1324 int XPectContextType;
1325 RetrieveHashlistFunc GetHash;
1326 HashDestructorFunc Destructor;
1327 SubTemplFunc DoSubTemplate;
1330 void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1342 if (!GetHash(Iterators,
1343 Tokens->Params[0]->Start,
1344 Tokens->Params[0]->len,
1346 lprintf(1, "unknown Iterator [%s] (in '%s' line %ld); "
1348 Tokens->Params[0]->Start,
1349 ChrPtr(Tokens->FileName),
1351 ChrPtr(Tokens->FlatToken));
1354 "<pre>\nunknown Iterator [%s] (in '%s' line %ld); \n"
1356 Tokens->Params[0]->Start,
1357 ChrPtr(Tokens->FileName),
1359 ChrPtr(Tokens->FlatToken));
1363 It = (HashIterator*) vIt;
1365 if (Tokens->nParameters < It->AdditionalParams + 2) {
1366 lprintf(1, "Iterator [%s] (in '%s' line %ld); "
1367 "doesn't work with %ld params [%s]\n",
1368 Tokens->Params[0]->Start,
1369 ChrPtr(Tokens->FileName),
1371 Tokens->nParameters,
1372 ChrPtr(Tokens->FlatToken));
1375 "<pre>Iterator [%s] \n(in '%s' line %ld);\n"
1376 "doesn't work with %ld params \n[%s]\n</pre>",
1377 Tokens->Params[0]->Start,
1378 ChrPtr(Tokens->FileName),
1380 Tokens->nParameters,
1381 ChrPtr(Tokens->FlatToken));
1385 if ((It->XPectContextType != CTX_NONE) &&
1386 (It->XPectContextType != ContextType)) {
1387 lprintf(1, "Iterator [%s] (in '%s' line %ld); "
1388 "requires context of type %ld, have %ld [%s]\n",
1390 ChrPtr(Tokens->FileName),
1392 It->XPectContextType,
1394 ChrPtr(Tokens->FlatToken));
1397 "<pre>\nIterator [%s] (in '%s' line %ld);"
1398 " requires context of type %ld, have %ld!\n[%s]\n</pre>\n",
1400 ChrPtr(Tokens->FileName),
1402 It->XPectContextType,
1404 ChrPtr(Tokens->FlatToken));
1412 if (It->StaticList == NULL)
1413 List = It->GetHash(Target, nArgs, Tokens, Context, ContextType);
1415 List = It->StaticList;
1417 SubBuf = NewStrBuf();
1418 it = GetNewHashPos();
1419 while (GetNextHashPos(List, it, &len, &Key, &vContext)) {
1420 svprintf(HKEY("ITERATE:ODDEVEN"), WCS_STRING, "%s",
1421 (oddeven) ? "odd" : "even");
1422 svprintf(HKEY("ITERATE:KEY"), WCS_STRING, "%s", Key);
1424 if (It->DoSubTemplate != NULL)
1425 It->DoSubTemplate(SubBuf, vContext, Tokens);
1426 DoTemplate(Tokens->Params[1]->Start,
1427 Tokens->Params[1]->len,
1431 StrBufAppendBuf(Target, SubBuf, 0);
1432 FlushStrBuf(SubBuf);
1433 oddeven = ~ oddeven;
1435 FreeStrBuf(&SubBuf);
1437 if (It->Destructor != NULL)
1438 It->Destructor(&List);
1441 int ConditionalVar(WCTemplateToken *Tokens, void *Context, int ContextType)
1446 if (!GetHash(WC->vars,
1447 Tokens->Params[2]->Start,
1448 Tokens->Params[2]->len,
1451 subst = (wcsubst*) vsubst;
1452 if ((subst->ContextRequired != CTX_NONE) &&
1453 (subst->ContextRequired != ContextType)) {
1454 lprintf(1," WARNING: Conditional requires Context: [%ld]!\n", Tokens->Params[2]->Start);
1458 switch(subst->wcs_type) {
1460 return (subst->wcs_function!=NULL);
1462 lprintf(1, " -> Server [%s]\n", subst->wcs_value);////todo
1466 case WCS_STRBUF_REF:
1467 if (Tokens->nParameters < 4)
1469 return (strcmp(Tokens->Params[3]->Start, ChrPtr(subst->wcs_value)) == 0);
1471 if (Tokens->nParameters < 4)
1472 return (subst->lvalue != 0);
1473 return (subst->lvalue == Tokens->Params[3]->lvalue);
1475 lprintf(1," WARNING: invalid type: [%ld]!\n", subst->wcs_type);
1481 void RegisterITERATOR(const char *Name, long len,
1482 int AdditionalParams,
1483 HashList *StaticList,
1484 RetrieveHashlistFunc GetHash,
1485 SubTemplFunc DoSubTempl,
1486 HashDestructorFunc Destructor,
1488 int XPectContextType)
1490 HashIterator *It = (HashIterator*)malloc(sizeof(HashIterator));
1491 It->StaticList = StaticList;
1492 It->AdditionalParams = AdditionalParams;
1493 It->GetHash = GetHash;
1494 It->DoSubTemplate = DoSubTempl;
1495 It->Destructor = Destructor;
1496 It->ContextType = ContextType;
1497 It->XPectContextType = XPectContextType;
1498 Put(Iterators, Name, len, It, NULL);
1501 void RegisterConditional(const char *Name, long len,
1503 WCConditionalFunc CondF,
1504 int ContextRequired)
1506 ConditionalStruct *Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
1507 Cond->nParams = nParams;
1508 Cond->CondF = CondF;
1509 Cond->ContextRequired = ContextRequired;
1510 Put(Contitionals, Name, len, Cond, NULL);
1513 void tmpl_do_boxed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1516 StrBuf *Headline = NewStrBuf();
1517 DoTemplate(Tokens->Params[1]->Start,
1518 Tokens->Params[1]->len,
1522 SVPutBuf("BOXTITLE", Headline, 0);
1525 DoTemplate(HKEY("beginbox"), Target, Context, ContextType);
1526 DoTemplate(Tokens->Params[0]->Start,
1527 Tokens->Params[0]->len,
1531 DoTemplate(HKEY("endbox"), Target, Context, ContextType);
1534 void tmpl_do_tabbed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
1537 int i, ntabs, nTabs;
1539 nTabs = ntabs = Tokens->nParameters / 2;
1540 TabNames = (StrBuf **) malloc(ntabs * sizeof(StrBuf*));
1542 for (i = 0; i < ntabs; i++) {
1543 TabNames[i] = NewStrBuf();
1544 if (Tokens->Params[i * 2]->len > 0) {
1545 DoTemplate(Tokens->Params[i * 2]->Start,
1546 Tokens->Params[i * 2]->len,
1552 /** A Tab without subject? we can't count that, add it as silent */
1557 StrTabbedDialog(Target, nTabs, TabNames);
1558 for (i = 0; i < ntabs; i++) {
1559 StrBeginTab(Target, i, nTabs);
1561 DoTemplate(Tokens->Params[i * 2 + 1]->Start,
1562 Tokens->Params[i * 2 + 1]->len,
1566 StrEndTab(Target, i, nTabs);
1574 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip, CTX_NONE);
1575 RegisterNamespace("SERV:NODENAME", 0, 0, tmplput_serv_nodename, CTX_NONE);
1576 RegisterNamespace("SERV:HUMANNODE", 0, 0, tmplput_serv_humannode, CTX_NONE);
1577 RegisterNamespace("SERV:FQDN", 0, 0, tmplput_serv_fqdn, CTX_NONE);
1578 RegisterNamespace("SERV:SOFTWARE", 0, 0, tmmplput_serv_software, CTX_NONE);
1579 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level, CTX_NONE);
1580 RegisterNamespace("SERV:BBS_CITY", 0, 0, tmmplput_serv_bbs_city, CTX_NONE);
1581 /// RegisterNamespace("SERV:LDAP_SUPP", 0, 0, tmmplput_serv_ldap_enabled, 0);
1582 RegisterNamespace("CURRENT_USER", 0, 0, tmplput_current_user, CTX_NONE);
1583 RegisterNamespace("CURRENT_ROOM", 0, 0, tmplput_current_room, CTX_NONE);
1584 RegisterNamespace("ITERATE", 2, 100, tmpl_iterate_subtmpl, CTX_NONE);
1585 RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed, CTX_NONE);
1586 RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed, CTX_NONE);
1587 RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar, CTX_NONE);