File: template_expand.c

package info (click to toggle)
prayer 1.3.5-dfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,576 kB
  • sloc: ansic: 43,161; makefile: 817; sh: 445; perl: 166
file content (616 lines) | stat: -rw-r--r-- 17,318 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/* $Cambridge: hermes/src/prayer/lib/template_expand.c,v 1.5 2008/09/16 09:59:57 dpc22 Exp $ */

/************************************************
 *    Prayer - a Webmail Interface              *
 ************************************************/

/* Copyright (c) University of Cambridge 2000 - 2008 */
/* See the file NOTICE for conditions of use and distribution. */

#include "lib.h"

/* ====================================================================== */

/* Note really awful feature of GET URLs: Web server needs to quote "&" as
 * "&" to stop browser from interpreting & sequences. Browser will send
 * the URL back as "&" */

static void 
template_expand_session(struct template_vals_urlstate *urlstate,
                        struct buffer *b, char *t, BOOL seq)
{
    unsigned char *s = (unsigned char *) t;
    unsigned char c;
    BOOL use_short = (urlstate->use_short && seq) ? T : NIL;
    unsigned char src[3];
    unsigned char dst[5];
    char *v =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-";

    src[0] = (urlstate->sequence >> 16) & 255;     /* Bits 23->16 */
    src[1] = (urlstate->sequence >> 8) & 255;      /* Bits 15->8  */
    src[2] = (urlstate->sequence) & 255;           /* Bits  7->0  */

    /* Quick (URL friendly) base64 encode of three byte sequence */
    dst[0] = v[src[0] >> 2];
    dst[1] = v[((src[0] << 4) + (src[1] >> 4)) & 0x3f];
    dst[2] = v[((src[1] << 2) + (src[2] >> 6)) & 0x3f];
    dst[3] = v[src[2] & 0x3f];
    dst[4] = '\0';

    if (!s) {
        bputs(b, "(nil)");
        return;
    }

    if (urlstate->test_mode) {
        while ((c = *s++) && (Uisalnum(c) || (c == '_')))
            bputc(b, c);
        bputs(b, ".html");
        return;
    }

    if (!urlstate->use_short) {
        bputs(b, urlstate->url_prefix_bsession);
        bputc(b, '/');
    }

    if (seq) {
        bputs(b, dst);
    } else
        bputs(b, "NOSEQ");

    bputc(b, (use_short) ? '@' : '/');
    while ((c = *s++)) {
        switch (c) {
        case '/':
            bputc(b, (use_short) ? '@' : '/');
            break;
        case '&':
            bputs(b, "&amp;");   
            break;
        default:
            bputc(b, c);
            break;
        }
    }
}

static void 
template_expand_icon(struct template_vals_urlstate *urlstate,
                     struct buffer *b, char *t)
{
    unsigned char *s = (unsigned char *) t;

    bprintf(b, "%s/%s.gif", urlstate->url_prefix_icons, s);
}

/* ====================================================================== */

/* Forward decleration */

/* foreach_active_item is list of simple substititions to make:
 *
 * $i->{foo} becomes lookup on @list-0-foo, @list-1-foo , etc
 */

struct foreach_active_item {
    struct foreach_active_item *next;
    char *var_prefix;
    char *array;
};

struct template_expand_state {
    char          *name;
    struct template_vals *tvals;
    struct pool   *pool;
    struct str    *str;
    struct str    *str2;
    struct str    *str3;
    struct str    *expbuf;
    struct str    *error;
    struct buffer *buffer;
    struct assoc  *params;
    struct assoc  *vars;
    struct foreach_active_item *foreach_list;
};

static BOOL
template_expand_work(struct template_item *item,
                     struct template_expand_state *state);

static char *
template_fetch_var(char *s, struct template_expand_state *state)
{
    struct assoc *params = state->params;
    struct assoc *h = state->vars;
    struct foreach_active_item *f = state->foreach_list;
    char *result;

    while (f) {
        int len = strlen(f->var_prefix);

        if (!strncmp(s, f->var_prefix, len)) {
            char buf[64];

            snprintf(buf, sizeof(buf), "%s%s", f->array, s+len);
            if (params && (result = assoc_lookup(params, buf)))
                return(result);
            else
                return (assoc_lookup(h, buf));
        }
        f = f->next;
    }
    if (params && (result = assoc_lookup(params, s)))
        return(result);
    else
        return (assoc_lookup(h, s));
}

static BOOL
template_test_var(char *s, struct template_expand_state *state)
{
    char *val = template_fetch_var(s, state);

    return ((val) ? T : NIL);
}

static void
template_expand_var(char *s, struct str *str,
                    struct template_expand_state *state, char quote)
{
    struct pool *pool = state->pool;
    char *val = template_fetch_var(s, state);

    if (!val)
        return;

    if (quote == 'u') {
        str_encode_url(str, val);
    } else if (quote == 'c') {
        str_encode_canon(str, val);
    } else if (quote == '7') {
        str_puts(str, utf8_from_imaputf7(pool, val));
    } else {
        str_puts(str, val);
    }
}

static char *
template_expand_vars(struct str *str, char *s,
                     struct template_expand_state *state)
{
    char *var;
    char varquote = 'n';

    /* Reuse single str variable */
    str_rewind(str, 0);

    while (*s) {
        if (*s != '$') {
            str_putc(str, *s++);
        } else if ((var = template_getvar(&s, &varquote, NIL))) {
            template_expand_var(var, str, state, varquote);
        } else
            return(NIL);
    }
    return(str_fetch(str));
}

static BOOL
template_test_value(char *s, char *t, struct template_expand_state *state)
{
    char *val  = template_expand_vars(state->str2, s, state);
    char *val2 = template_expand_vars(state->str3, t, state);

    return((val && val2 && !strcmp(val, val2)) ? T : NIL);
}

static void
template_expand_vars_buffer(char *s, struct template_expand_state *state)
{
    struct buffer *b = state->buffer;
    char *value;
    char *var, *val1, *val2;
    char quoting = 'h';

    value = template_getexpr(&s, state->pool);
    value = template_expand_vars(state->str, value, state);

    if (!strncasecmp(s, "IFDEF", 5) && Uisspace(s[5])) {
        s += 6;
        if (!((var = template_getvar(&s, NIL, NIL)) &&
              template_test_var(var, state)))
            return;
    } else if (!strncasecmp(s, "IFEQ", 4) && Uisspace(s[4])) {
        s += 5;
        if (!((val1 = template_getexpr(&s, state->pool)) &&
              (val2 = template_getexpr(&s, state->pool)) &&
              (template_test_value(val1, val2, state))))
            return;
    }
    while (Uisspace(*s))
        s++;

    if (*s == '|') {
        s++;
        quoting = *s++;
    }

    switch (quoting) {
    case 'n':
        bputs(b, value);
        break;
    case 's':
        template_expand_session(state->tvals->urlstate, b, value, T);
        break;
    case 'S':
        template_expand_session(state->tvals->urlstate, b, value, NIL);
        break;
    case 'i':
        template_expand_icon(state->tvals->urlstate, b, value);
        break;
    case 'c':
        buffer_encode_canon(b, value);
        break;
    case 'u':
        buffer_encode_url(b, value);
        break;
    case 'h':
    default:
        html_quote_string(b, value);
        break;
    }
}

/* ====================================================================== */

static BOOL
template_expand_line(char *s, struct template_expand_state *state)
{
    struct buffer *b = state->buffer;
    char *t;
    char c;

    while (s && (t = strstr(s, "<%"))) {
        /* Expand a <% ... %> escape */
        while (s < t)
            bputc(b, *s++);
        s += 2;
        if ((t = strstr(s, "%>"))) {
            int   len = t-s;
            char *buf = str_reserve(state->expbuf, len+1);

            memcpy(buf, s, len);
            buf[len] = '\0';
            template_expand_vars_buffer(buf, state);
        } else
            return(NIL);
        s = (t) ? t+2 : NIL;
    }
    if (s) {
        /* Print the rest of the line as is. \<sp>+\n blocks CRLF */
        while ((c=*s++)) {
            if (c == '\\') {
                t = s ;
                while (Uisspace(*t))
                    t++;
                if (*t == '\0')
                    return(T);
            }
            bputc(b, c);
        }
    }
    bputs(b, ""CRLF);
    return(T);
}

static BOOL
template_expand_lines(struct template_lines *item,
                      struct template_expand_state *state)
{
    char **lines = item->first;
    int    count = item->count;
    int rc = T;

    while (rc && count) {
        rc = template_expand_line(*lines, state);
        lines++;
        count--;
    }
    return(rc);
}

static BOOL
template_expand_ifdef(struct template_ifdef *item,
                      struct template_expand_state *state)
{
    int rc;
    struct template_item *true_block
        = (item->positive) ? item->true_block : item->false_block;
    struct template_item *false_block
        = (item->positive) ? item->false_block : item->true_block;

    if (template_test_var(item->expr, state))
        rc = template_expand_work(true_block, state);
    else
        rc = template_expand_work(false_block, state);

    return(rc);
}

static BOOL
template_expand_ifeq(struct template_ifeq *item,
                      struct template_expand_state *state)
{
    int rc;
    struct template_item *true_block
        = (item->positive) ? item->true_block : item->false_block;
    struct template_item *false_block
        = (item->positive) ? item->false_block : item->true_block;


    if (template_test_value(item->name, item->value, state))
        rc = template_expand_work(true_block, state);
    else
        rc = template_expand_work(false_block, state);

    return(rc);
}

static BOOL
template_expand_foreach(struct template_foreach *item,
                        struct template_expand_state *state)
{
    struct pool  *pool = state->pool;
    struct assoc *h    = state->vars;
    struct foreach_active_item *oldf = state->foreach_list;
    struct foreach_active_item *f;
    char buf[64];                  /* Variables limited to 64 chars */
    int i = 0;
    BOOL ret = T;

    f = pool_alloc(pool, sizeof (struct foreach_active_item));
    f->next       = oldf;
    f->var_prefix = pool_strcat(pool, item->name, "-");

    state->foreach_list = f;

    snprintf(buf, sizeof(buf), "%s-%d", item->array, i);
    while (assoc_lookup(h, buf)) {
        strncat(buf, "-", sizeof(buf));
        f->array = buf;

        if (!(ret = template_expand_work(item->block, state)))
            break;
        i++;
        snprintf(buf, sizeof(buf), "%s-%d", item->array, i);
    }
    state->foreach_list = oldf;

    return(ret);
}

static BOOL
template_expand_loop(struct template_loop *item,
                     struct template_expand_state *state)
{
    char *s = template_fetch_var(item->var, state);
    int count = (s) ? atoi(s) : 0;
    int rc = T;

    while (rc && (count > 0)) {
        rc = template_expand_work(item->block, state);
        count--;
    }

    return(rc);
}

/* ====================================================================== */

static BOOL
template_expand_recurse(struct template_item *template,
                        struct template_vals *tvals,
                        struct buffer *b,
                        struct assoc *params,
                        struct template_expand_state *oldstate)
{
    struct template_expand_state state;
    struct pool *pool = tvals->pool;

    state.tvals        = tvals;
    state.pool         = pool;
    state.vars         = tvals->vals;
    state.params       = params;
    state.buffer       = b;
    state.foreach_list = NIL;

    /* Recursive calls can reuse scratch, which saves lots of str_create()s */
    state.str          = oldstate->str;
    state.str2         = oldstate->str2;
    state.str3         = oldstate->str3;
    state.expbuf       = oldstate->expbuf;

    return(template_expand_work(template, &state));
}

static BOOL
template_expand_call_add_param(struct assoc *params,
                               struct str *str,
                               char *s, 
                               struct template_expand_state *state)
{
    char *t;
    char *var, *value = NIL;

    while (Uisspace(*s))
        s++;

    if ((t = strstr(s, "=>"))) {
        *t = '\0';
        t += 2;
        value = template_getexpr(&t, state->pool);
        value = template_expand_vars(str, value, state);
    } else
        value = "1";  /* Default unspecified value */

    if (!(var = template_getvar(&s, NIL, NIL)))
        return(NIL);

    assoc_update(params, var, value, T);
    return(T);
}

static BOOL
template_expand_call(struct template_call *item,
                     struct template_expand_state *state)
{
    struct template_vals *tvals = state->tvals;
    struct pool   *pool   = state->pool;
    struct buffer *b      = state->buffer;
    struct assoc  *params = NIL;
    struct str    *str    = NIL;
    struct template *template;
    char *s, *t;
    char *err = NIL;

    if (tvals->use_compiled ||
        !(template = template_parse(tvals->dir, tvals->set,
                                    item->name, tvals->pool)))
        template = template_find(tvals->set, item->name, tvals->pool);

    if (!template) {
        str_printf(state->error,
                   "Template \"%s\" not found (CALL from template \"%s\")\n",
                   item->name, state->name);
        return(NIL);
    }
    /* NB: template->error only used by interpreted templates */
    if (template->error &&
        (err = str_fetch(template->error)) && err[0]) {
        if (state->error)
            str_puts(state->error, err);
        else
            fputs(err, stderr);
        return(NIL);
    }
    if (item->params && item->params[0]) {
        s      = pool_strdup(pool, item->params);   /* Scratch copy */
        str    = str_create(pool, 0);
        params = assoc_create(pool, 16, T);

        while ((t = strchr(s, ','))) {
            *t++ = '\0';
            template_expand_call_add_param(params, str, s, state);
            s = t;
        }
        template_expand_call_add_param(params, str, s, state);
    }
    return(template_expand_recurse(template->tree, tvals, b, params, state));
}

/* ====================================================================== */

static BOOL
template_expand_work(struct template_item *item,
                     struct template_expand_state *state)
{
    BOOL rc = T;

    while (rc && item) {
        switch (item->type) {
        case TEMPLATE_ITEM_LINES:
            rc = template_expand_lines((struct template_lines *)item, state);
            break;
        case TEMPLATE_ITEM_IFDEF:
            rc = template_expand_ifdef((struct template_ifdef *)item, state);
            break;
        case TEMPLATE_ITEM_IFEQ:
            rc = template_expand_ifeq((struct template_ifeq *)item, state);
            break;
        case TEMPLATE_ITEM_FOREACH:
            rc = template_expand_foreach((struct template_foreach *)item,
                                         state);
            break;
        case TEMPLATE_ITEM_LOOP:
            rc = template_expand_loop((struct template_loop *)item,
                                      state);
            break;
        case TEMPLATE_ITEM_CALL:
            rc = template_expand_call((struct template_call *)item, state);
            break;
        default:
            log_fatal("template_expand_work() with unknown type");
        }

        item = item->tree_next;
    }
    return(rc);
}

BOOL
template_expand(char *name, struct template_vals *tvals, struct buffer *b)
{
    struct template_expand_state state;
    struct template *template;
    struct pool *pool = tvals->pool;
    struct str *error = tvals->error;
    char *err;

    if (tvals->use_compiled ||
        !(template = template_parse(tvals->dir, tvals->set,
                                    name, tvals->pool)))
        template = template_find(tvals->set, name, tvals->pool);

    if (!template) {
        str_printf(tvals->error,
                   "Template %s not found (top level template_expand())\n",
                   name);
        err = str_fetch(tvals->error);
        buffer_reset(b);
        if (tvals->html_error) {
            bputs(b, "<html><pre>"CRLF);
            html_quote_string(b, err);
            bputs(b, "</pre></html>"CRLF);
        } else
            bputs(b, err);
        return(NIL);
    }
    if (template->error && (err=str_fetch(template->error)) && err[0]) {
        buffer_reset(b);
        if (tvals->html_error) {
            bputs(b, "<html><pre>"CRLF);
            html_quote_string(b, err);
            bputs(b, "</pre></html>"CRLF);
        } else
            bputs(b, err);
        return(NIL);
    }

    state.name         = name;
    state.tvals        = tvals;
    state.pool         = pool;
    state.vars         = tvals->vals;
    state.params       = NIL;
    state.buffer       = b;
    state.foreach_list = NIL;
    state.str          = str_create(pool, 0);
    state.str2         = str_create(pool, 0);
    state.str3         = str_create(pool, 0);
    state.expbuf       = str_create(pool, 64);
    state.error        = tvals->error;

    if (!template_expand_work(template->tree, &state)) {
        buffer_reset(b);
        if (tvals->html_error) {
            bputs(b, "<html><pre>"CRLF);
            html_quote_string(b, str_fetch(error));
            bputs(b, "</pre></html>"CRLF);
        } else
            bputs(b, str_fetch(error));

        return(NIL);
    }
    return(T);
}