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
42 typedef struct _WCTemplate {
47 WCTemplateToken **Tokens;
50 typedef struct _HashHandler {
53 WCHandlerFunc HandlerFunc;
56 void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere);
58 void RegisterNS(const char *NSName, long len, int nMinArgs, int nMaxArgs, WCHandlerFunc HandlerFunc)
60 HashHandler *NewHandler;
62 NewHandler = (HashHandler*) malloc(sizeof(HashHandler));
63 NewHandler->nMinArgs = nMinArgs;
64 NewHandler->nMaxArgs = nMaxArgs;
65 NewHandler->HandlerFunc = HandlerFunc;
66 Put(GlobalNS, NSName, len, NewHandler, NULL);
71 * \brief debugging function to print array to log
73 void VarPrintTransition(void *vVar1, void *vVar2, int odd){}
75 * \brief debugging function to print array to log
77 void VarPrintEntry(const char *Key, void *vSubst, int odd)
80 lprintf(1,"Subst[%s] : ", Key);
81 ptr = (wcsubst*) vSubst;
83 switch(ptr->wcs_type) {
85 lprintf(1, " -> %s\n", ChrPtr(ptr->wcs_value));
88 lprintf(1, " -> Server [%s]\n", ChrPtr(ptr->wcs_value));
91 lprintf(1, " -> function at [%0xd]\n", ptr->wcs_function);
94 lprintf(1," WARNING: invalid type: [%ld]!\n", ptr->wcs_type);
101 * \brief Clear out the list of substitution variables local to this session
103 void clear_substs(struct wcsession *wc) {
105 if (wc->vars != NULL) {
106 DeleteHash(&wc->vars);
111 * \brief Clear out the list of substitution variables local to this session
113 void clear_local_substs(void) {
132 void FlushPayload(wcsubst *ptr, int reusestrbuf, int type)
134 int NeedNew = NeedNewBuf(type);
135 switch(ptr->wcs_type) {
139 if (reusestrbuf && NeedNew) {
140 FlushStrBuf(ptr->wcs_value);
144 FreeStrBuf(&ptr->wcs_value);
145 ptr->wcs_value = NULL;
149 ptr->wcs_function = NULL;
150 if (reusestrbuf && NeedNew)
151 ptr->wcs_value = NewStrBuf();
154 ptr->wcs_value = NULL;
155 if (reusestrbuf && NeedNew)
156 ptr->wcs_value = NewStrBuf();
160 if (reusestrbuf && NeedNew)
161 ptr->wcs_value = NewStrBuf();
164 if (reusestrbuf && NeedNew)
165 ptr->wcs_value = NewStrBuf();
171 * \brief destructor; kill one entry.
173 void deletevar(void *data)
175 wcsubst *ptr = (wcsubst*)data;
176 FlushPayload(ptr, 0, ptr->wcs_type);
181 wcsubst *NewSubstVar(const char *keyname, int keylen, int type)
184 struct wcsession *WCC = WC;
186 ptr = (wcsubst *) malloc(sizeof(wcsubst));
187 memset(ptr, 0, sizeof(wcsubst));
189 ptr->wcs_type = type;
190 safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
191 Put(WCC->vars, keyname, keylen, ptr, deletevar);
193 switch(ptr->wcs_type) {
196 ptr->wcs_value = NewStrBuf();
210 * \brief Add a substitution variable (local to this session) (strlen version...)
211 * \param keyname the replacementstring to substitute
212 * \param keytype the kind of the key
213 * \param format the format string ala printf
214 * \param ... the arguments to substitute in the formatstring
216 void SVPRINTF(char *keyname, int keytype, const char *format,...)
222 struct wcsession *WCC = WC;
224 keylen = strlen(keyname);
226 * First look if we're doing a replacement of
229 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
230 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
231 ptr = (wcsubst*)vPtr;
232 FlushPayload(ptr, keytype, keytype);
233 ptr->wcs_type = keytype;
235 else /** Otherwise allocate a new one */
237 ptr = NewSubstVar(keyname, keylen, keytype);
240 /** Format the string */
241 va_start(arg_ptr, format);
242 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
247 * \brief Add a substitution variable (local to this session)
248 * \param keyname the replacementstring to substitute
249 * \param keytype the kind of the key
250 * \param format the format string ala printf
251 * \param ... the arguments to substitute in the formatstring
253 void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...)
258 struct wcsession *WCC = WC;
261 * First look if we're doing a replacement of
264 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
265 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
266 ptr = (wcsubst*)vPtr;
267 FlushPayload(ptr, 1, keytype);
268 ptr->wcs_type = keytype;
270 else /** Otherwise allocate a new one */
272 ptr = NewSubstVar(keyname, keylen, keytype);
275 /** Format the string and save it */
276 va_start(arg_ptr, format);
277 StrBufVAppendPrintf(ptr->wcs_value, format, arg_ptr);
282 * \brief Add a substitution variable (local to this session)
283 * \param keyname the replacementstring to substitute
284 * \param keytype the kind of the key
285 * \param format the format string ala printf
286 * \param ... the arguments to substitute in the formatstring
288 void SVPut(char *keyname, size_t keylen, int keytype, char *Data)
292 struct wcsession *WCC = WC;
296 * First look if we're doing a replacement of
299 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
300 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
301 ptr = (wcsubst*)vPtr;
302 FlushPayload(ptr, 1, keytype);
303 ptr->wcs_type = keytype;
305 else /** Otherwise allocate a new one */
307 ptr = NewSubstVar(keyname, keylen, keytype);
309 StrBufAppendBufPlain(ptr->wcs_value, Data, -1, 0);
313 * \brief Add a substitution variable (local to this session)
314 * \param keyname the replacementstring to substitute
315 * \param keytype the kind of the key
316 * \param format the format string ala printf
317 * \param ... the arguments to substitute in the formatstring
319 void SVPutLong(char *keyname, size_t keylen, long Data)
323 struct wcsession *WCC = WC;
327 * First look if we're doing a replacement of
330 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
331 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
332 ptr = (wcsubst*)vPtr;
333 FlushPayload(ptr, 1, WCS_LONG);
334 ptr->wcs_type = WCS_LONG;
336 else /** Otherwise allocate a new one */
338 ptr = NewSubstVar(keyname, keylen, WCS_LONG);
344 * \brief Add a substitution variable (local to this session) that does a callback
345 * \param keyname the keystring to substitute
346 * \param fcn_ptr the function callback to give the substitution string
348 void SVCallback(char *keyname, size_t keylen, var_callback_fptr fcn_ptr)
352 struct wcsession *WCC = WC;
355 * First look if we're doing a replacement of
358 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
359 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
360 ptr = (wcsubst*)vPtr;
361 FlushPayload(ptr, 1, WCS_FUNCTION);
362 ptr->wcs_type = WCS_FUNCTION;
364 else /** Otherwise allocate a new one */
366 ptr = NewSubstVar(keyname, keylen, WCS_FUNCTION);
369 ptr->wcs_function = fcn_ptr;
371 inline void SVCALLBACK(char *keyname, var_callback_fptr fcn_ptr)
373 SVCallback(keyname, strlen(keyname), fcn_ptr);
378 void SVPUTBuf(const char *keyname, int keylen, StrBuf *Buf, int ref)
382 struct wcsession *WCC = WC;
385 * First look if we're doing a replacement of
388 /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
389 if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
390 ptr = (wcsubst*)vPtr;
391 FlushPayload(ptr, 0, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
392 ptr->wcs_type = (ref)?WCS_STRBUF_REF:WCS_STRBUF;
394 else /** Otherwise allocate a new one */
396 ptr = NewSubstVar(keyname, keylen, (ref)?WCS_STRBUF_REF:WCS_STRBUF);
398 ptr->wcs_value = Buf;
402 * \brief back end for print_value_of() ... does a server command
403 * \param servcmd server command to execute on the citadel server
405 void pvo_do_cmd(StrBuf *Target, StrBuf *servcmd) {
409 serv_puts(ChrPtr(servcmd));
410 len = serv_getln(buf, sizeof buf);
416 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
419 _fmout(Target, "CENTER");
422 StrBufAppendPrintf(Target, "%s\n", &buf[4]);
429 * \brief Print the value of a variable
430 * \param keyname get a key to print
432 void print_value_of(StrBuf *Target, const char *keyname, size_t keylen) {
433 struct wcsession *WCC = WC;
438 /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
439 /// TODO: debricated!
440 if (keyname[0] == '=') {
441 DoTemplate(keyname+1, keylen - 1, NULL, NULL);
444 //////TODO: if param[1] == "U" -> urlescape
446 /** Page-local variables */
447 if ((WCC->vars!= NULL) && GetHash(WCC->vars, keyname, keylen, &vVar)) {
448 ptr = (wcsubst*) vVar;
449 switch(ptr->wcs_type) {
451 StrBufAppendBuf(Target, ptr->wcs_value, 0);
454 pvo_do_cmd(Target, ptr->wcs_value);
457 (*ptr->wcs_function) ();
461 StrBufAppendBuf(Target, ptr->wcs_value, 0);
464 StrBufAppendPrintf(Target, "%ld", ptr->lvalue);
467 lprintf(1,"WARNING: invalid value in SV-Hash at %s!", keyname);
473 void PutNewToken(WCTemplate *Template, WCTemplateToken *NewToken)
475 if (Template->nTokensUsed + 1 >= Template->TokenSpace) {
476 if (Template->TokenSpace <= 0) {
477 Template->Tokens = (WCTemplateToken**)malloc(
478 sizeof(WCTemplateToken*) * 10);
479 Template->TokenSpace = 10;
482 WCTemplateToken **NewTokens;
483 NewTokens= (WCTemplateToken**)malloc(
484 sizeof(WCTemplateToken*) *
485 Template->TokenSpace * 2);
486 memcpy(NewTokens, Template->Tokens,
487 sizeof(WCTemplateToken*) * Template->nTokensUsed);
488 free(Template->Tokens);
489 Template->TokenSpace *= 2;
490 Template->Tokens = NewTokens;
493 Template->Tokens[(Template->nTokensUsed)++] = NewToken;
496 TemplateParam *GetNextParameter(StrBuf *Buf, const char **pCh, const char *pe, WCTemplateToken *Token, WCTemplate *pTmpl)
498 const char *pch = *pCh;
499 const char *pchs, *pche;
500 TemplateParam *Parm = (TemplateParam *) malloc(sizeof(TemplateParam));
503 /* Skip leading whitespaces */
504 while ((*pch == ' ' )||
507 (*pch == '\n')) pch ++;
510 else if (*pch == '\'')
515 Parm->Type = TYPE_STR;
518 ( (pch > pchs) && (*(pch - 1) == '\\'))
524 lprintf(1, "Error (in '%s' line %ld); "
525 "evaluating template param [%s] in Token [%s]\n",
526 ChrPtr(pTmpl->FileName),
528 ChrPtr(Token->FlatToken),
535 StrBufPeek(Buf, pch, -1, '\0');
536 if (LoadTemplates > 1) {
537 lprintf(1, "DBG: got param [%s] %ld %ld\n",
538 pchs, pche - pchs, strlen(pchs));
541 Parm->len = pche - pchs;
542 pch ++; /* move after trailing quote */
546 Parm->Type = TYPE_LONG;
548 while ((pch <= pe) &&
555 StrBufPeek(Buf, pch, -1, '\0');
556 Parm->lvalue = atol(pchs);
562 lprintf(1, "Error (in '%s' line %ld); "
563 "evaluating long template param [%s] in Token [%s]\n",
564 ChrPtr(pTmpl->FileName),
566 ChrPtr(Token->FlatToken),
572 while ((*pch == ' ' )||
576 (*pch == '\n')) pch ++;
582 WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
584 const char *pTmplStart,
585 const char *pTmplEnd,
590 TemplateParam *Param;
591 WCTemplateToken *NewToken = (WCTemplateToken*)malloc(sizeof(WCTemplateToken));
594 NewToken->Line = Line + 1;
595 NewToken->pTokenStart = pTmplStart;
596 NewToken->TokenStart = pTmplStart - pStart;
597 NewToken->TokenEnd = (pTmplEnd - pStart) - NewToken->TokenStart;
598 NewToken->pTokenEnd = pTmplEnd;
599 NewToken->NameEnd = NewToken->TokenEnd - 2;
600 NewToken->FlatToken = NewStrBufPlain(pTmplStart, pTmplEnd - pTmplStart);
602 StrBufPeek(Buf, pTmplStart, + 1, '\0');
603 StrBufPeek(Buf, pTmplEnd, -1, '\0');
604 pch = NewToken->pName = pTmplStart + 2;
606 NewToken->HaveParameters = 0;;
607 NewToken->nParameters = 0;
609 while (pch < pTmplEnd - 1) {
611 StrBufPeek(Buf, pch, -1, '\0');
612 NewToken->NameEnd = pch - NewToken->pName;
614 while (pch < pTmplEnd - 1) {
615 Param = GetNextParameter(Buf, &pch, pTmplEnd - 1, NewToken, pTmpl);
617 NewToken->HaveParameters = 1;
618 if (NewToken->nParameters > MAXPARAM) {
619 lprintf(1, "Only %ld Tokens supported!\n", MAXPARAM);
623 NewToken->Params[NewToken->nParameters++] = Param;
627 if((NewToken->NameEnd == 1) &&
628 (NewToken->HaveParameters == 1))
631 if ((NewToken->nParameters == 1) &&
632 (*(NewToken->pName) == '_'))
633 NewToken->Flags = SV_GETTEXT;
634 else if ((NewToken->nParameters == 1) &&
635 (*(NewToken->pName) == '='))
636 NewToken->Flags = SV_SUBTEMPL;
637 else if ((NewToken->nParameters >= 2) &&
638 (*(NewToken->pName) == '%'))
639 NewToken->Flags = SV_CUST_STR_CONDITIONAL;
640 else if ((NewToken->nParameters >= 2) &&
641 (*(NewToken->pName) == '?'))
642 NewToken->Flags = SV_CONDITIONAL;
643 else if ((NewToken->nParameters >=2) &&
644 (*(NewToken->pName) == '!'))
645 NewToken->Flags = SV_NEG_CONDITIONAL;
653 void FreeToken(WCTemplateToken **Token)
656 FreeStrBuf(&(*Token)->FlatToken);
657 if ((*Token)->HaveParameters)
658 for (i = 0; i < (*Token)->nParameters; i++)
659 free((*Token)->Params[i]);
666 void FreeWCTemplate(void *vFreeMe)
669 WCTemplate *FreeMe = (WCTemplate*)vFreeMe;
671 if (FreeMe->TokenSpace > 0) {
672 for (i = 0; i < FreeMe->nTokensUsed; i ++) {
673 FreeToken(&FreeMe->Tokens[i]);
675 free(FreeMe->Tokens);
677 FreeStrBuf(&FreeMe->FileName);
678 FreeStrBuf(&FreeMe->Data);
683 int EvaluateConditional(WCTemplateToken *Token, WCTemplate *pTmpl, void *Context, int Neg, int state)
686 ConditionalStruct *Cond;
688 if ((Token->Params[0]->len == 1) &&
689 (Token->Params[0]->Start[0] == 'X'))
690 return (state != 0)?Token->Params[1]->lvalue:0;
692 if (!GetHash(Contitionals,
693 Token->Params[0]->Start,
694 Token->Params[0]->len,
696 lprintf(1, "Conditional [%s] (in '%s' line %ld); Not found![%s]\n",
697 Token->Params[0]->Start,
698 ChrPtr(pTmpl->FileName),
700 ChrPtr(Token->FlatToken));
703 Cond = (ConditionalStruct *) vConditional;
706 lprintf(1, "Conditional [%s] (in '%s' line %ld); Not found![%s]\n",
707 Token->Params[0]->Start,
708 ChrPtr(pTmpl->FileName),
710 ChrPtr(Token->FlatToken));
713 if (Token->nParameters < Cond->nParams) {
714 lprintf(1, "Conditional [%s] (in '%s' line %ld); needs %ld Params![%s]\n",
715 Token->Params[0]->Start,
716 ChrPtr(pTmpl->FileName),
719 ChrPtr(Token->FlatToken));
722 if (Cond->CondF(Token, Context) == Neg)
723 return Token->Params[1]->lvalue;
727 int EvaluateToken(StrBuf *Target, WCTemplateToken *Token, WCTemplate *pTmpl, void *Context, int state)
730 // much output, since pName is not terminated...
731 // lprintf(1,"Doing token: %s\n",Token->pName);
732 switch (Token->Flags) {
734 TmplGettext(Target, Token->nParameters, Token);
736 case SV_CONDITIONAL: /** Forward conditional evaluation */
737 return EvaluateConditional(Token, pTmpl, Context, 1, state);
739 case SV_NEG_CONDITIONAL: /** Reverse conditional evaluation */
740 return EvaluateConditional(Token, pTmpl, Context, 0, state);
742 case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
743 if (Token->nParameters >= 6) {
744 if (EvaluateConditional(Token, pTmpl, Context, 0, state))
745 StrBufAppendBufPlain(Target,
746 Token->Params[5]->Start,
747 Token->Params[5]->len,
750 StrBufAppendBufPlain(Target,
751 Token->Params[4]->Start,
752 Token->Params[4]->len,
757 if (Token->nParameters == 1)
758 DoTemplate(Token->Params[0]->Start, Token->Params[0]->len, NULL, NULL);
761 if (GetHash(GlobalNS, Token->pName, Token->NameEnd, &vVar)) {
762 HashHandler *Handler;
763 Handler = (HashHandler*) vVar;
764 if ((Token->nParameters < Handler->nMinArgs) ||
765 (Token->nParameters > Handler->nMaxArgs)) {
766 lprintf(1, "Handler [%s] doesn't work with %ld params",
771 Handler->HandlerFunc(Target,
774 Context); /*TODO: subset of that */
780 print_value_of(Target, Token->pName, Token->NameEnd);
786 void ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, void *Context)
788 WCTemplate *pTmpl = Tmpl;
791 const char *pData, *pS;
794 if (LoadTemplates != 0) {
795 if (LoadTemplates > 1)
796 lprintf(1, "DBG: ----- loading: [%s] ------ \n",
797 ChrPtr(Tmpl->FileName));
798 pTmpl = load_template(Tmpl->FileName, NULL, NULL);
801 pS = pData = ChrPtr(pTmpl->Data);
802 len = StrLength(pTmpl->Data);
806 if (i >= pTmpl->nTokensUsed) {
807 StrBufAppendBufPlain(Target,
809 len - (pData - pS), 0);
813 StrBufAppendBufPlain(
815 pTmpl->Tokens[i]->pTokenStart - pData, 0);
816 state = EvaluateToken(Target, pTmpl->Tokens[i], pTmpl, Context, state);
817 while ((state != 0) && (i+1 < pTmpl->nTokensUsed)) {
818 /* condition told us to skip till its end condition */
820 if ((pTmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
821 (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
822 if (state == EvaluateConditional(pTmpl->Tokens[i],
825 pTmpl->Tokens[i]->Flags,
830 pData = pTmpl->Tokens[i++]->pTokenEnd + 1;
831 if (i > pTmpl->nTokensUsed)
835 if (LoadTemplates != 0) {
836 FreeWCTemplate(pTmpl);
842 * \brief Display a variable-substituted template
843 * \param templatename template file to load
845 void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
847 WCTemplate *NewTemplate;
848 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
849 NewTemplate->Data = NULL;
850 NewTemplate->FileName = NewStrBufDup(filename);
851 NewTemplate->nTokensUsed = 0;
852 NewTemplate->TokenSpace = 0;
853 NewTemplate->Tokens = NULL;
855 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
860 * \brief Display a variable-substituted template
861 * \param templatename template file to load
863 void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
867 const char *pS, *pE, *pch, *Err;
870 WCTemplate *NewTemplate;
872 fd = open(ChrPtr(filename), O_RDONLY);
874 lprintf(1, "ERROR: could not open template '%s' - %s\n",
875 ChrPtr(filename), strerror(errno));
879 if (fstat(fd, &statbuf) == -1) {
880 lprintf(1, "ERROR: could not stat template '%s' - %s\n",
881 ChrPtr(filename), strerror(errno));
885 NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate));
886 NewTemplate->Data = NewStrBufPlain(NULL, statbuf.st_size);
887 NewTemplate->FileName = NewStrBufDup(filename);
888 NewTemplate->nTokensUsed = 0;
889 NewTemplate->TokenSpace = 0;
890 NewTemplate->Tokens = NULL;
891 if (StrBufReadBLOB(NewTemplate->Data, &fd, 1, statbuf.st_size, &Err) < 0) {
893 FreeWCTemplate(NewTemplate);
894 lprintf(1, "ERROR: reading template '%s' - %s<br />\n",
895 ChrPtr(filename), strerror(errno));
901 pS = pch = ChrPtr(NewTemplate->Data);
902 pE = pS + StrLength(NewTemplate->Data);
904 const char *pts, *pte;
906 int InDoubleQuotes = 0;
908 for (; pch < pE; pch ++) {
909 if ((*pch=='<')&&(*(pch + 1)=='?'))
911 if (*pch=='\n') Line ++;
916 for (; pch < pE - 1; pch ++) {
918 InDoubleQuotes = ! InDoubleQuotes;
919 else if (*pch == '\'')
920 InQuotes = ! InQuotes;
921 else if ((!InQuotes && !InDoubleQuotes) &&
922 ((*pch!='\\')&&(*(pch + 1)=='>'))) {
930 PutNewToken(NewTemplate,
931 NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte, Line, NewTemplate));
934 if (LoadTemplates == 0)
935 Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
940 ///void PrintTemplate(const char *Key, void *vSubst, int odd)
941 const char* PrintTemplate(void *vSubst)
943 WCTemplate *Tmpl = vSubst;
945 return ChrPtr(Tmpl->FileName);
951 * \brief Display a variable-substituted template
952 * \param templatename template file to load
954 void DoTemplate(const char *templatename, long len, void *Context, StrBuf *Target)
957 HashList *StaticLocal;
963 Static = WirelessTemplateCache;
964 StaticLocal = WirelessLocalTemplateCache;
967 Static = TemplateCache;
968 StaticLocal = LocalTemplateCache;
971 if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
972 !GetHash(Static, templatename, len, &vTmpl)) {
973 printf ("didn't find %s %ld %ld\n", templatename, len , (long)strlen(templatename));
974 /// dbg_PrintHash(Static, PrintTemplate, NULL);
975 // PrintHash(Static, VarPrintTransition, PrintTemplate);
980 ProcessTemplate(vTmpl, Target, Context);
983 int LoadTemplateDir(const char *DirName, HashList *wireless, HashList *big)
989 struct dirent *filedir_entry;
995 StrBufPrintf(Dir, "%s/t", DirName);
996 filedir = opendir (ChrPtr(Dir));
997 if (filedir == NULL) {
1001 FileName = NewStrBuf();
1003 while ((filedir_entry = readdir(filedir)))
1007 #ifdef _DIRENT_HAVE_D_NAMELEN
1008 d_namelen = filedir_entry->d_namelen;
1010 d_namelen = strlen(filedir_entry->d_name);
1012 d_without_ext = d_namelen;
1013 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1015 if ((d_without_ext == 0) || (d_namelen < 3))
1018 IsMobile = (strstr(filedir_entry->d_name, ".m.html")!= NULL);
1019 PStart = filedir_entry->d_name;
1020 StrBufPrintf(FileName, "%s/%s", ChrPtr(Dir), filedir_entry->d_name);
1021 MinorPtr = strchr(filedir_entry->d_name, '.');
1022 if (MinorPtr != NULL)
1024 StrBufPlain(Tag, filedir_entry->d_name, MinorPtr - filedir_entry->d_name);
1027 printf("%s %d %s\n",ChrPtr(FileName), IsMobile, ChrPtr(Tag));
1028 if (LoadTemplates == 0)
1029 load_template(FileName, Tag, (IsMobile)?wireless:big);
1031 prepare_template(FileName, Tag, (IsMobile)?wireless:big);
1034 FreeStrBuf(&FileName);
1040 void InitTemplateCache(void)
1042 LoadTemplateDir(static_dirs[0],
1043 WirelessTemplateCache,
1045 LoadTemplateDir(static_dirs[1],
1046 WirelessLocalTemplateCache,
1047 LocalTemplateCache);
1050 void tmplput_serv_ip(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1052 StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
1055 void tmplput_serv_nodename(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1057 StrEscAppend(Target, NULL, serv_info.serv_nodename, 0, 0);
1060 void tmplput_serv_humannode(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1062 StrEscAppend(Target, NULL, serv_info.serv_humannode, 0, 0);
1065 void tmplput_serv_fqdn(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1067 StrEscAppend(Target, NULL, serv_info.serv_fqdn, 0, 0);
1070 void tmmplput_serv_software(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1072 StrEscAppend(Target, NULL, serv_info.serv_software, 0, 0);
1075 void tmplput_serv_rev_level(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1077 StrBufAppendPrintf(Target, "%d.%02d",
1078 serv_info.serv_rev_level / 100,
1079 serv_info.serv_rev_level % 100);
1082 void tmmplput_serv_bbs_city(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1084 StrEscAppend(Target, NULL, serv_info.serv_bbs_city, 0, 0);
1087 void tmplput_current_user(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1089 StrEscAppend(Target, NULL, WC->wc_fullname, 0, 0);
1092 void tmplput_current_room(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1094 StrEscAppend(Target, NULL, WC->wc_roomname, 0, 0);
1098 typedef struct _HashIterator {
1099 HashList *StaticList;
1100 RetrieveHashlistFunc GetHash;
1101 HashDestructorFunc Destructor;
1102 SubTemplFunc DoSubTemplate;
1105 void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1117 if (!GetHash(Iterators,
1118 Tokens->Params[0]->Start,
1119 Tokens->Params[0]->len,
1122 It = (HashIterator*) vIt;
1123 if (It->StaticList == NULL)
1124 List = It->GetHash();
1126 List = It->StaticList;
1128 SubBuf = NewStrBuf();
1129 it = GetNewHashPos();
1130 while (GetNextHashPos(List, it, &len, &Key, &vContext)) {
1131 svprintf(HKEY("ITERATE:ODDEVEN"), WCS_STRING, "%s", (oddeven)?"odd":"even");
1132 It->DoSubTemplate(SubBuf, vContext);
1133 DoTemplate(Tokens->Params[1]->Start,
1134 Tokens->Params[1]->len,
1137 StrBufAppendBuf(Target, SubBuf, 0);
1138 FlushStrBuf(SubBuf);
1139 oddeven = ~ oddeven;
1141 FreeStrBuf(&SubBuf);
1143 It->Destructor(List);
1146 int ConditionalVar(WCTemplateToken *Tokens, void *Context)
1151 if (!GetHash(WC->vars,
1152 Tokens->Params[2]->Start,
1153 Tokens->Params[2]->len,
1156 subst = (wcsubst*) vsubst;
1157 switch(subst->wcs_type) {
1159 return (subst->wcs_function!=NULL);
1161 lprintf(1, " -> Server [%s]\n", subst->wcs_value);////todo
1165 case WCS_STRBUF_REF:
1166 if (Tokens->nParameters < 4)
1168 return (strcmp(Tokens->Params[3]->Start, ChrPtr(subst->wcs_value)) == 0);
1170 if (Tokens->nParameters < 4)
1171 return (subst->lvalue != 0);
1172 return (subst->lvalue == Tokens->Params[3]->lvalue);
1174 lprintf(1," WARNING: invalid type: [%ld]!\n", subst->wcs_type);
1179 void RegisterITERATOR(const char *Name, long len,
1180 HashList *StaticList,
1181 RetrieveHashlistFunc GetHash,
1182 SubTemplFunc DoSubTempl,
1183 HashDestructorFunc Destructor)
1185 HashIterator *It = (HashIterator*)malloc(sizeof(HashIterator));
1186 It->StaticList = StaticList;
1187 It->GetHash = GetHash;
1188 It->DoSubTemplate = DoSubTempl;
1189 It->Destructor = Destructor;
1190 Put(Iterators, Name, len, It, NULL);
1193 void RegisterConditional(const char *Name, long len,
1195 WCConditionalFunc CondF)
1197 ConditionalStruct *Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
1198 Cond->nParams = nParams;
1199 Cond->CondF = CondF;
1200 Put(Contitionals, Name, len, Cond, NULL);
1203 void tmpl_do_boxed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1206 StrBuf *Headline = NewStrBuf();
1207 DoTemplate(Tokens->Params[1]->Start,
1208 Tokens->Params[1]->len,
1211 SVPutBuf("BOXTITLE", Headline, 0);
1214 DoTemplate(HKEY("beginbox"), Context, Target);
1215 DoTemplate(Tokens->Params[0]->Start,
1216 Tokens->Params[0]->len,
1219 DoTemplate(HKEY("endbox"), Context, Target);
1222 void tmpl_do_tabbed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
1225 int i, ntabs, nTabs;
1227 nTabs = ntabs = Tokens->nParameters / 2;
1228 TabNames = (StrBuf **) malloc(ntabs * sizeof(StrBuf*));
1230 for (i = 0; i < ntabs; i++) {
1231 TabNames[i] = NewStrBuf();
1232 if (Tokens->Params[i * 2]->len > 0) {
1233 DoTemplate(Tokens->Params[i * 2]->Start,
1234 Tokens->Params[i * 2]->len,
1239 /** A Tab without subject? we can't count that, add it as silent */
1244 StrTabbedDialog(Target, nTabs, TabNames);
1245 for (i = 0; i < ntabs; i++) {
1246 StrBeginTab(Target, i, nTabs);
1248 DoTemplate(Tokens->Params[i * 2 + 1]->Start,
1249 Tokens->Params[i * 2 + 1]->len,
1252 StrEndTab(Target, i, nTabs);
1260 RegisterNamespace("SERV:PID", 0, 0, tmplput_serv_ip);
1261 RegisterNamespace("SERV:NODENAME", 0, 0, tmplput_serv_nodename);
1262 RegisterNamespace("SERV:HUMANNODE", 0, 0, tmplput_serv_humannode);
1263 RegisterNamespace("SERV:FQDN", 0, 0, tmplput_serv_fqdn);
1264 RegisterNamespace("SERV:SOFTWARE", 0, 0, tmmplput_serv_software);
1265 RegisterNamespace("SERV:REV_LEVEL", 0, 0, tmplput_serv_rev_level);
1266 RegisterNamespace("SERV:BBS_CITY", 0, 0, tmmplput_serv_bbs_city);
1267 /// RegisterNamespace("SERV:LDAP_SUPP", 0, 0, tmmplput_serv_ldap_enabled);
1268 RegisterNamespace("CURRENT_USER", 0, 0, tmplput_current_user);
1269 RegisterNamespace("CURRENT_ROOM", 0, 0, tmplput_current_room);
1270 RegisterNamespace("ITERATE", 2, 4, tmpl_iterate_subtmpl);
1271 RegisterNamespace("DOBOXED", 1, 2, tmpl_do_boxed);
1272 RegisterNamespace("DOTABBED", 2, 100, tmpl_do_tabbed);
1273 RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar);