+ if (pch + 1 >= pE)
+ continue;
+ pte = pch;
+ PutNewToken(NewTemplate,
+ NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte));
+ pch ++;
+ }
+ Put(PutThere, ChrPtr(Key), StrLength(Key), NewTemplate, FreeWCTemplate);
+ return NewTemplate;
+}
+
+/**
+ * \brief Display a variable-substituted template
+ * \param templatename template file to load
+ */
+void DoTemplate(const char *templatename, long len, void *Context, StrBuf *Target)
+{
+ HashList *Static;
+ HashList *StaticLocal;
+ void *vTmpl;
+
+ if (Target == NULL)
+ Target = WC->WBuf;
+ if (WC->is_mobile) {
+ Static = WirelessTemplateCache;
+ StaticLocal = WirelessLocalTemplateCache;
+ }
+ else {
+ Static = TemplateCache;
+ StaticLocal = LocalTemplateCache;
+ }
+
+ if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
+ !GetHash(Static, templatename, len, &vTmpl)) {
+ printf ("didn't find %s\n", templatename);
+ return;
+ }
+ if (vTmpl == NULL)
+ return;
+ ProcessTemplate(vTmpl, Target, Context);
+}
+
+int LoadTemplateDir(const char *DirName, HashList *wireless, HashList *big)
+{
+ StrBuf *FileName;
+ StrBuf *Tag;
+ StrBuf *Dir;
+ DIR *filedir = NULL;
+ struct dirent *filedir_entry;
+ int d_namelen;
+ int d_without_ext;
+ int IsMobile;
+
+ Dir = NewStrBuf();
+ StrBufPrintf(Dir, "%s/t", DirName);
+ filedir = opendir (ChrPtr(Dir));
+ if (filedir == NULL) {
+ return 0;
+ }
+
+ FileName = NewStrBuf();
+ Tag = NewStrBuf();
+ while ((filedir_entry = readdir(filedir)))
+ {
+ char *MinorPtr;
+ char *PStart;
+#ifdef _DIRENT_HAVE_D_NAMELEN
+ d_namelen = filedir_entry->d_namelen;
+#else
+ d_namelen = strlen(filedir_entry->d_name);
+#endif
+ d_without_ext = d_namelen;
+ while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
+ d_without_ext --;
+ if ((d_without_ext == 0) || (d_namelen < 3))
+ continue;
+
+ IsMobile = (strstr(filedir_entry->d_name, ".m.html")!= NULL);
+ PStart = filedir_entry->d_name;
+ StrBufPrintf(FileName, "%s/%s", ChrPtr(Dir), filedir_entry->d_name);
+ MinorPtr = strchr(filedir_entry->d_name, '.');
+ if (MinorPtr != NULL)
+ *MinorPtr = '\0';
+ StrBufPlain(Tag, filedir_entry->d_name, MinorPtr - filedir_entry->d_name);
+
+
+ printf("%s %d %s\n",ChrPtr(FileName), IsMobile, ChrPtr(Tag));
+ load_template(FileName, Tag, (IsMobile)?wireless:big);
+ }
+ closedir(filedir);
+ FreeStrBuf(&FileName);
+ FreeStrBuf(&Tag);
+ FreeStrBuf(&Dir);
+ return 1;
+}
+
+void InitTemplateCache(void)
+{
+ LoadTemplateDir(static_dirs[0],
+ WirelessTemplateCache,
+ TemplateCache);
+ LoadTemplateDir(static_dirs[1],
+ WirelessLocalTemplateCache,
+ LocalTemplateCache);
+}
+
+void tmplput_serv_ip(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+{
+ StrBufAppendPrintf(Target, "%d", WC->ctdl_pid);
+}
+
+void tmplput_serv_nodename(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+{
+ StrEscAppend(Target, NULL, serv_info.serv_nodename, 0, 0);
+}
+
+void tmplput_serv_humannode(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+{
+ StrEscAppend(Target, NULL, serv_info.serv_humannode, 0, 0);
+}
+
+void tmplput_serv_fqdn(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+{
+ StrEscAppend(Target, NULL, serv_info.serv_fqdn, 0, 0);
+}
+
+void tmmplput_serv_software(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+{
+ StrEscAppend(Target, NULL, serv_info.serv_software, 0, 0);
+}
+
+void tmplput_serv_rev_level(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+{
+ StrBufAppendPrintf(Target, "%d.%02d",
+ serv_info.serv_rev_level / 100,
+ serv_info.serv_rev_level % 100);
+}
+
+void tmmplput_serv_bbs_city(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+{
+ StrEscAppend(Target, NULL, serv_info.serv_bbs_city, 0, 0);
+}
+
+void tmplput_current_user(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+{
+ StrEscAppend(Target, NULL, WC->wc_fullname, 0, 0);
+}
+
+void tmplput_current_room(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+{
+ StrEscAppend(Target, NULL, WC->wc_roomname, 0, 0);
+}
+
+
+typedef struct _HashIterator {
+ HashList *StaticList;
+ RetrieveHashlistFunc GetHash;
+ HashDestructorFunc Destructor;
+ SubTemplFunc DoSubTemplate;
+} HashIterator;
+
+void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+{
+ void *vIt;
+ HashIterator *It;
+ HashList *List;
+ HashPos *it;
+ long len;
+ const char *Key;
+ void *vContext;
+ StrBuf *SubBuf;
+ int oddeven = 0;
+
+ if (!GetHash(Iterators,
+ Tokens->Params[0]->Start,
+ Tokens->Params[0]->len,
+ &vIt))
+ return;
+ It = (HashIterator*) vIt;
+ if (It->StaticList == NULL)
+ List = It->GetHash();
+ else
+ List = It->StaticList;
+
+ SubBuf = NewStrBuf();
+ it = GetNewHashPos();
+ while (GetNextHashPos(List, it, &len, &Key, &vContext)) {
+ svprintf(HKEY("ITERATE:ODDEVEN"), WCS_STRING, "%s", (oddeven)?"odd":"even");
+ It->DoSubTemplate(SubBuf, vContext);
+ DoTemplate(Tokens->Params[1]->Start,
+ Tokens->Params[1]->len,
+ vContext, SubBuf);
+
+ StrBufAppendBuf(Target, SubBuf, 0);
+ FlushStrBuf(SubBuf);
+ oddeven = ~ oddeven;