c7047771339dc617d5e7699ced56a85141f74bd3
[citadel.git] / webcit / subst.c
1 /*
2  * $Id$
3  *
4  * Variable substitution type stuff
5  *
6  */
7
8
9
10 #include <ctype.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <signal.h>
16 #include <sys/types.h>
17 #include <sys/wait.h>
18 #include <sys/socket.h>
19 #include <sys/time.h>
20 #include <sys/stat.h>
21 #include <limits.h>
22 #include <netinet/in.h>
23 #include <netdb.h>
24 #include <string.h>
25 #include <pwd.h>
26 #include <errno.h>
27 #include <stdarg.h>
28 #include <pthread.h>
29 #include <signal.h>
30 #include "webcit.h"
31
32 struct wcsubst *global_subst = NULL;
33
34
35 /*
36  * Clear out the list of substitution variables local to this session
37  */
38 void clear_local_substs(void) {
39         struct wcsubst *ptr;
40
41         while (WC->vars != NULL) {
42                 ptr = WC->vars->next;
43
44                 if ((WC->vars->wcs_type == WCS_STRING)
45                    || (WC->vars->wcs_type == WCS_SERVCMD)) {
46                         free(WC->vars->wcs_value);
47                 }
48
49                 free(WC->vars);
50                 WC->vars = ptr;
51         }
52 }
53
54
55 /*
56  * Add a substitution variable (local to this session)
57  */
58 void svprintf(char *keyname, int keytype, const char *format,...)
59 {
60         va_list arg_ptr;
61         char wbuf[4096];
62         struct wcsubst *ptr = NULL;
63         struct wcsubst *scan;
64
65         va_start(arg_ptr, format);
66         vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
67         va_end(arg_ptr);
68
69         /* First scan through to see if we're doing a replacement of
70          * an existing key
71          */
72         for (scan=WC->vars; scan!=NULL; scan=scan->next) {
73                 if (!strcasecmp(scan->wcs_key, keyname)) {
74                         ptr = scan;
75                         free(ptr->wcs_value);
76                 }
77         }
78
79         /* Otherwise allocate a new one */
80         if (ptr == NULL) {
81                 ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
82                 ptr->next = WC->vars;
83                 strcpy(ptr->wcs_key, keyname);
84                 WC->vars = ptr;
85         }
86
87         ptr->wcs_type = keytype;
88         ptr->wcs_value = strdup(wbuf);
89 }
90
91 /*
92  * Add a substitution variable (local to this session) that does a callback
93  */
94 void svcallback(char *keyname, void (*fcn_ptr)() )
95 {
96         struct wcsubst *ptr;
97
98         ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
99         ptr->next = WC->vars;
100         ptr->wcs_type = WCS_FUNCTION;
101         strcpy(ptr->wcs_key, keyname);
102         ptr->wcs_function = fcn_ptr;
103         WC->vars = ptr;
104 }
105
106
107
108 /*
109  * back end for print_value_of() ... does a server command
110  */
111 void pvo_do_cmd(char *servcmd) {
112         char buf[SIZ];
113
114         serv_puts(servcmd);
115         serv_gets(buf);
116
117         switch(buf[0]) {
118                 case '2':
119                 case '3':
120                 case '5':
121                         wprintf("%s\n", &buf[4]);
122                         break;
123                 case '1':
124                         fmout(NULL, "CENTER");
125                         break;
126                 case '4':
127                         wprintf("%s\n", &buf[4]);
128                         serv_puts("000");
129                         break;
130         }
131 }
132
133
134
135 /*
136  * Print the value of a variable
137  */
138 void print_value_of(char *keyname) {
139         struct wcsubst *ptr;
140         void *fcn();
141
142         if (keyname[0] == '=') {
143                 do_template(&keyname[1]);
144         }
145
146         if (!strcasecmp(keyname, "SERV_PID")) {
147                 wprintf("%d", serv_info.serv_pid);
148         }
149
150         else if (!strcasecmp(keyname, "SERV_NODENAME")) {
151                 escputs(serv_info.serv_nodename);
152         }
153
154         else if (!strcasecmp(keyname, "SERV_HUMANNODE")) {
155                 escputs(serv_info.serv_humannode);
156         }
157
158         else if (!strcasecmp(keyname, "SERV_FQDN")) {
159                 escputs(serv_info.serv_fqdn);
160         }
161
162         else if (!strcasecmp(keyname, "SERV_SOFTWARE")) {
163                 escputs(serv_info.serv_software);
164         }
165
166         else if (!strcasecmp(keyname, "SERV_REV_LEVEL")) {
167                 wprintf("%d.%02d",
168                         serv_info.serv_rev_level / 100,
169                         serv_info.serv_rev_level % 100
170                 );
171         }
172
173         else if (!strcasecmp(keyname, "SERV_BBS_CITY")) {
174                 escputs(serv_info.serv_bbs_city);
175         }
176
177         else if (!strcasecmp(keyname, "CURRENT_USER")) {
178                 escputs(WC->wc_username);
179         }
180
181         else if (!strcasecmp(keyname, "CURRENT_ROOM")) {
182                 escputs(WC->wc_roomname);
183         }
184
185         /* Page-local variables */
186         else for (ptr = WC->vars; ptr != NULL; ptr = ptr->next) {
187                 if (!strcasecmp(ptr->wcs_key, keyname)) {
188                         if (ptr->wcs_type == WCS_STRING) {
189                                 wprintf("%s", ptr->wcs_value);
190                         }
191                         else if (ptr->wcs_type == WCS_SERVCMD) {
192                                 pvo_do_cmd(ptr->wcs_value);
193                         }
194                         else if (ptr->wcs_type == WCS_FUNCTION) {
195                                 (*ptr->wcs_function) ();
196                         }
197                 }
198         }
199 }
200
201
202
203 /*
204  * Display a variable-substituted template
205  */
206 void do_template(void *templatename) {
207         char filename[PATH_MAX];
208         FILE *fp;
209         char inbuf[1024];
210         char outbuf[sizeof inbuf];
211         char key[sizeof inbuf];
212         int i, pos;
213
214         strcpy(filename, "static/");
215         strcat(filename, templatename);
216         if (WC->is_wap)
217                 strcat(filename, ".wml");
218         else
219                 strcat(filename, ".html");
220         
221         fp = fopen(filename, "r");
222         if (fp == NULL) {
223                 wprintf("<BLINK>ERROR</BLINK> - could not open template ");
224                 wprintf("'%s' - %s<BR>\n",
225                         templatename, strerror(errno));
226                 return;
227         }
228
229         strcpy(inbuf, "");
230
231         while (fgets(inbuf, sizeof inbuf, fp) != NULL) {
232                 strcpy(outbuf, "");
233
234                 while (strlen(inbuf) > 0) {
235                         pos = (-1);
236                         for (i=strlen(inbuf); i>=0; --i) {
237                                 if ((inbuf[i]=='<')&&(inbuf[i+1]=='?')) pos = i;
238                         }
239                         if (pos < 0) {
240                                 wprintf("%s", inbuf);
241                                 strcpy(inbuf, "");
242                         }
243                         else {
244                                 strncpy(outbuf, inbuf, pos);
245                                 outbuf[pos] = 0;
246                                 wprintf("%s", outbuf);
247                                 strcpy(inbuf, &inbuf[pos]);
248                                 pos = 1;
249                                 for (i=strlen(inbuf); i>=0; --i) {
250                                         if (inbuf[i]=='>') pos = i;
251                                 }
252                                 strncpy(key, &inbuf[2], pos-2);
253                                 key[pos-2] = 0;
254                                 print_value_of(key);
255                                 strcpy(inbuf, &inbuf[pos+1]);
256                         }
257                 }
258         }
259
260         fclose(fp);
261 }