083f8e4e03719c48ebfdbad99aac56b9fbc35922
[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[1024];
62         struct wcsubst *ptr = NULL;
63         struct wcsubst *scan;
64
65         va_start(arg_ptr, format);
66         vsprintf(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         }
84
85         ptr->wcs_type = keytype;
86         strcpy(ptr->wcs_key, keyname);
87         ptr->wcs_value = malloc(strlen(wbuf)+1);
88         strcpy(ptr->wcs_value, wbuf);
89         WC->vars = ptr;
90 }
91
92 /*
93  * Add a substitution variable (local to this session) that does a callback
94  */
95 void svcallback(char *keyname, void (*fcn_ptr)() )
96 {
97         struct wcsubst *ptr;
98
99         ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
100         ptr->next = WC->vars;
101         ptr->wcs_type = WCS_FUNCTION;
102         strcpy(ptr->wcs_key, keyname);
103         ptr->wcs_function = fcn_ptr;
104         WC->vars = ptr;
105 }
106
107
108
109 /*
110  * back end for print_value_of() ... does a server command
111  */
112 void pvo_do_cmd(char *servcmd) {
113         char buf[SIZ];
114
115         serv_puts(servcmd);
116         serv_gets(buf);
117
118         switch(buf[0]) {
119                 case '2':
120                 case '3':
121                 case '5':
122                         wprintf("%s\n", &buf[4]);
123                         break;
124                 case '1':
125                         fmout(NULL);
126                         break;
127                 case '4':
128                         wprintf("%s\n", &buf[4]);
129                         serv_puts("000");
130                         break;
131         }
132 }
133
134
135
136 /*
137  * Print the value of a variable
138  */
139 void print_value_of(char *keyname) {
140         struct wcsubst *ptr;
141         void *fcn();
142
143         if (!strcasecmp(keyname, "SERV_PID")) {
144                 wprintf("%d", serv_info.serv_pid);
145         }
146
147         else if (!strcasecmp(keyname, "SERV_NODENAME")) {
148                 wprintf("%s", serv_info.serv_nodename);
149         }
150
151         else if (!strcasecmp(keyname, "SERV_HUMANNODE")) {
152                 wprintf("%s", serv_info.serv_humannode);
153         }
154
155         else if (!strcasecmp(keyname, "SERV_FQDN")) {
156                 wprintf("%s", serv_info.serv_fqdn);
157         }
158
159         else if (!strcasecmp(keyname, "SERV_SOFTWARE")) {
160                 wprintf("%s", serv_info.serv_software);
161         }
162
163         else if (!strcasecmp(keyname, "SERV_REV_LEVEL")) {
164                 wprintf("%d.%02d",
165                         serv_info.serv_rev_level / 100,
166                         serv_info.serv_rev_level % 100
167                 );
168         }
169
170         else if (!strcasecmp(keyname, "SERV_BBS_CITY")) {
171                 wprintf("%s", serv_info.serv_bbs_city);
172         }
173
174         /* Page-local variables */
175         else for (ptr = WC->vars; ptr != NULL; ptr = ptr->next) {
176                 if (!strcasecmp(ptr->wcs_key, keyname)) {
177                         if (ptr->wcs_type == WCS_STRING) {
178                                 wprintf("%s", ptr->wcs_value);
179                         }
180                         else if (ptr->wcs_type == WCS_SERVCMD) {
181                                 pvo_do_cmd(ptr->wcs_value);
182                         }
183                         else if (ptr->wcs_type == WCS_FUNCTION) {
184                                 (*ptr->wcs_function) ();
185                         }
186                 }
187         }
188 }
189
190
191
192 /*
193  * Display a variable-substituted template
194  */
195 void do_template(void *templatename) {
196         char filename[PATH_MAX];
197         FILE *fp;
198         char inbuf[1024];
199         char outbuf[sizeof inbuf];
200         char key[sizeof inbuf];
201         int i, pos;
202
203         strcpy(filename, "static/");
204         strcat(filename, templatename);
205         if (WC->is_wap)
206                 strcat(filename, ".wml");
207         else
208                 strcat(filename, ".html");
209         
210         fp = fopen(filename, "r");
211         if (fp == NULL) {
212                 wprintf("<BLINK>ERROR</BLINK> - could not open template ");
213                 wprintf("'%s' - %s<BR>\n",
214                         templatename, strerror(errno));
215                 return;
216         }
217
218         strcpy(inbuf, "");
219
220         while (fgets(inbuf, sizeof inbuf, fp) != NULL) {
221                 strcpy(outbuf, "");
222
223                 while (strlen(inbuf) > 0) {
224                         pos = (-1);
225                         for (i=strlen(inbuf); i>=0; --i) {
226                                 if ((inbuf[i]=='<')&&(inbuf[i+1]=='?')) pos = i;
227                         }
228                         if (pos < 0) {
229                                 wprintf("%s", inbuf);
230                                 strcpy(inbuf, "");
231                         }
232                         else {
233                                 strncpy(outbuf, inbuf, pos);
234                                 outbuf[pos] = 0;
235                                 wprintf("%s", outbuf);
236                                 strcpy(inbuf, &inbuf[pos]);
237                                 pos = 1;
238                                 for (i=strlen(inbuf); i>=0; --i) {
239                                         if (inbuf[i]=='>') pos = i;
240                                 }
241                                 strncpy(key, &inbuf[2], pos-2);
242                                 key[pos-2] = 0;
243                                 print_value_of(key);
244                                 strcpy(inbuf, &inbuf[pos+1]);
245                         }
246                 }
247         }
248
249         fclose(fp);
250 }