* Better alignment of system messages
[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, "CENTER");
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 (keyname[0] == '=') {
144                 do_template(&keyname[1]);
145         }
146
147         if (!strcasecmp(keyname, "SERV_PID")) {
148                 wprintf("%d", serv_info.serv_pid);
149         }
150
151         else if (!strcasecmp(keyname, "SERV_NODENAME")) {
152                 wprintf("%s", serv_info.serv_nodename);
153         }
154
155         else if (!strcasecmp(keyname, "SERV_HUMANNODE")) {
156                 wprintf("%s", serv_info.serv_humannode);
157         }
158
159         else if (!strcasecmp(keyname, "SERV_FQDN")) {
160                 wprintf("%s", serv_info.serv_fqdn);
161         }
162
163         else if (!strcasecmp(keyname, "SERV_SOFTWARE")) {
164                 wprintf("%s", serv_info.serv_software);
165         }
166
167         else if (!strcasecmp(keyname, "SERV_REV_LEVEL")) {
168                 wprintf("%d.%02d",
169                         serv_info.serv_rev_level / 100,
170                         serv_info.serv_rev_level % 100
171                 );
172         }
173
174         else if (!strcasecmp(keyname, "SERV_BBS_CITY")) {
175                 wprintf("%s", serv_info.serv_bbs_city);
176         }
177
178         /* Page-local variables */
179         else for (ptr = WC->vars; ptr != NULL; ptr = ptr->next) {
180                 if (!strcasecmp(ptr->wcs_key, keyname)) {
181                         if (ptr->wcs_type == WCS_STRING) {
182                                 wprintf("%s", ptr->wcs_value);
183                         }
184                         else if (ptr->wcs_type == WCS_SERVCMD) {
185                                 pvo_do_cmd(ptr->wcs_value);
186                         }
187                         else if (ptr->wcs_type == WCS_FUNCTION) {
188                                 (*ptr->wcs_function) ();
189                         }
190                 }
191         }
192 }
193
194
195
196 /*
197  * Display a variable-substituted template
198  */
199 void do_template(void *templatename) {
200         char filename[PATH_MAX];
201         FILE *fp;
202         char inbuf[1024];
203         char outbuf[sizeof inbuf];
204         char key[sizeof inbuf];
205         int i, pos;
206
207         strcpy(filename, "static/");
208         strcat(filename, templatename);
209         if (WC->is_wap)
210                 strcat(filename, ".wml");
211         else
212                 strcat(filename, ".html");
213         
214         fp = fopen(filename, "r");
215         if (fp == NULL) {
216                 wprintf("<BLINK>ERROR</BLINK> - could not open template ");
217                 wprintf("'%s' - %s<BR>\n",
218                         templatename, strerror(errno));
219                 return;
220         }
221
222         strcpy(inbuf, "");
223
224         while (fgets(inbuf, sizeof inbuf, fp) != NULL) {
225                 strcpy(outbuf, "");
226
227                 while (strlen(inbuf) > 0) {
228                         pos = (-1);
229                         for (i=strlen(inbuf); i>=0; --i) {
230                                 if ((inbuf[i]=='<')&&(inbuf[i+1]=='?')) pos = i;
231                         }
232                         if (pos < 0) {
233                                 wprintf("%s", inbuf);
234                                 strcpy(inbuf, "");
235                         }
236                         else {
237                                 strncpy(outbuf, inbuf, pos);
238                                 outbuf[pos] = 0;
239                                 wprintf("%s", outbuf);
240                                 strcpy(inbuf, &inbuf[pos]);
241                                 pos = 1;
242                                 for (i=strlen(inbuf); i>=0; --i) {
243                                         if (inbuf[i]=='>') pos = i;
244                                 }
245                                 strncpy(key, &inbuf[2], pos-2);
246                                 key[pos-2] = 0;
247                                 print_value_of(key);
248                                 strcpy(inbuf, &inbuf[pos+1]);
249                         }
250                 }
251         }
252
253         fclose(fp);
254 }