* Started working on a variable-substitution thing
authorArt Cancro <ajc@citadel.org>
Mon, 11 Sep 2000 03:29:50 +0000 (03:29 +0000)
committerArt Cancro <ajc@citadel.org>
Mon, 11 Sep 2000 03:29:50 +0000 (03:29 +0000)
webcit/ChangeLog
webcit/Makefile.in
webcit/subst.c [new file with mode: 0644]
webcit/webcit.h

index 942ae2fd19fc87ffcd9d5736a15b472d0ffbea5c..8d5726cd2143112d2f18b4f917fab627eb9169b0 100644 (file)
@@ -1,4 +1,7 @@
 $Log$
+Revision 212.7  2000/09/11 03:29:50  ajc
+* Started working on a variable-substitution thing
+
 Revision 212.6  2000/08/30 03:20:22  ajc
 * Put the wholist back into its own window.
 
@@ -467,4 +470,3 @@ Sun Dec  6 19:50:55 EST 1998 Art Cancro <ajc@uncnsrd.mt-kisco.ny.us>
 
 1998-12-03 Nathan Bryant <bryant@cs.usm.maine.edu>
        * webserver.c: warning fix
-
index 7bd92c894db16d1a6eb5da781bc308dbf528a22a..1be0c0305cc813d765096be1a57cdbde0b9af9a5 100644 (file)
@@ -26,11 +26,11 @@ webserver: webserver.o context_loop.o tools.o \
        cookie_conversion.o locate_host.o \
        webcit.o auth.o tcp_sockets.o mainmenu.o serv_func.o who.o \
        roomops.o messages.o userlist.o paging.o sysmsgs.o \
-       mime_parser.o graphics.o netconf.o siteconfig.o $(LIBOBJS)
+       mime_parser.o graphics.o netconf.o siteconfig.o subst.o $(LIBOBJS)
        $(CC) webserver.o context_loop.o tools.o cookie_conversion.o \
        webcit.o auth.o tcp_sockets.o mainmenu.o serv_func.o who.o \
        roomops.o messages.o userlist.o paging.o sysmsgs.o \
-       locate_host.o siteconfig.o \
+       locate_host.o siteconfig.o subst.o \
        mime_parser.o graphics.o netconf.o \
        $(LIBOBJS) $(LIBS) -o webserver
 
diff --git a/webcit/subst.c b/webcit/subst.c
new file mode 100644 (file)
index 0000000..d3b6af9
--- /dev/null
@@ -0,0 +1,149 @@
+/*
+ * $Id$
+ *
+ * Variable substitution type stuff
+ *
+ */
+
+
+
+#include <ctype.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <sys/stat.h>
+#include <limits.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <string.h>
+#include <pwd.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <pthread.h>
+#include <signal.h>
+#include "webcit.h"
+
+struct wcsubst *global_subst = NULL;
+
+
+/*
+ * Clear out the list of substitution variables local to this session
+ */
+void clear_local_substs(void) {
+       struct wcsubst *ptr;
+
+       while (WC->vars != NULL) {
+               ptr = WC->vars->next;
+
+               if (WC->vars->wcs_type == WCS_STRING) {
+                       free(WC->vars->wcs_value);
+               }
+
+               free(WC->vars);
+               WC->vars = ptr;
+       }
+}
+
+
+/*
+ * Add a substitution variable (local to this session)
+ */
+void svprintf(char *keyname, const char *format,...)
+{
+       va_list arg_ptr;
+       char wbuf[1024];
+       struct wcsubst *ptr;
+
+       va_start(arg_ptr, format);
+       vsprintf(wbuf, format, arg_ptr);
+       va_end(arg_ptr);
+
+       ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
+       ptr->next = WC->vars;
+       ptr->wcs_type = WCS_STRING;
+       strcpy(ptr->wcs_key, keyname);
+       ptr->wcs_value = malloc(strlen(wbuf)+1);
+       strcpy(ptr->wcs_value, wbuf);
+       WC->vars = ptr;
+}
+
+
+
+/*
+ * Print the value of a variable
+ */
+void print_value_of(char *keyname) {
+       struct wbsubst *ptr;
+
+       for (ptr = WC->vars; ptr != NULL; ptr = ptr->next) {
+               if (!strcasecmp(ptr->wcs_key, keyname)) {
+                       if (ptr->wcs_type == WCS_STRING) {
+                               wprintf("%s", ptr->wcs_value);
+                       }
+               }
+       }
+}
+
+
+
+/*
+ * Display a variable-substituted template
+ */
+void do_template(void *templatename) {
+       char filename[PATH_MAX];
+       FILE *fp;
+       char inbuf[1024];
+       char outbuf[sizeof inbuf];
+       char key[sizeof inbuf];
+       int i, j, pos;
+       int olen;
+
+       strcpy(filename, "static/");
+       strcat(filename, templatename);
+       
+       fp = fopen(filename, "r");
+       if (fp == NULL) {
+               wprintf("<BLINK>ERROR</BLINK> - could not open template ");
+               wprintf("'%s' - %s<BR>\n",
+                       templatename, strerror(errno));
+               return;
+       }
+
+       strcpy(inbuf, "");
+
+       while (fgets(inbuf, sizeof inbuf, fp) != NULL) {
+               strcpy(outbuf, "");
+               olen = 0;
+
+               for (i=0; i<strlen(inbuf); ++i) {
+                       if (strncmp(&inbuf[i], "<?", 2)) {
+                               outbuf[olen] = inbuf[i];
+                               outbuf[++olen] = 0;
+                       }
+                       else {
+                               pos = (-1);
+                               for (j=strlen(inbuf); j>=i;  --j)  {
+                                       if (inbuf[j]=='>') pos = j;
+                               }
+                               if (pos > 0) {
+                                       strncpy(key, &inbuf[i+2], pos-i-2);
+                                       print_value_of(key);
+                                       olen = strlen(outbuf);
+                                       i = pos;
+                               }
+                               else {
+                                       i = i + 2;
+                               }
+                       }
+               }
+               wprintf("%s", outbuf);
+       }
+
+       fclose(fp);
+}
index 84904bc0342fd6572a8d739b2c0b4e45417f400a..8a4ffd2fae66970530f37c3613dd1a563b16c0df 100644 (file)
@@ -89,9 +89,24 @@ struct roomlisting {
 
 
 
+/*
+ * Dynamic content for variable substitution in templates
+ */
+struct wcsubst {
+       struct wcsubst *next;
+       int wcs_type;
+       char wcs_key[32];
+       void *wcs_value;
+};
 
-
-
+/*
+ * Values for wcs_type
+ */
+enum {
+       WCS_STRING,
+       WCS_FUNCTION
+};
+       
 
 /*
  * One of these is kept for each active Citadel session.
@@ -126,6 +141,7 @@ struct wcsession {
        int fake_frames;
        struct urlcontent *urlstrings;
        int HaveExpressMessages;        /* Nonzero if incoming msgs exist */
+       struct wcsubst *vars;
 };
 
 
@@ -138,6 +154,8 @@ extern char floorlist[128][256];
 extern char *axdefs[];
 extern char *defaulthost, *defaultport;
 
+extern struct wcsubst *global_subst;
+
 
 void stuff_to_cookie(char *cookie, int session,
                        char *user, char *pass, char *room);
@@ -260,3 +278,6 @@ void httpdate(char *buf, time_t thetime);
 void end_webcit_session(void);
 void page_popup(void);
 void http_redirect(char *);
+void clear_local_substs(void);
+void svprintf(char *keyname, const char *format,...);
+void do_template(void *templatename);