File: cmd_dictionary.c

package info (click to toggle)
prayer 1.3.5-dfsg1-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,596 kB
  • sloc: ansic: 43,163; makefile: 817; sh: 445; perl: 166
file content (99 lines) | stat: -rw-r--r-- 2,975 bytes parent folder | download | duplicates (6)
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
/* $Cambridge: hermes/src/prayer/cmd/cmd_dictionary.c,v 1.3 2008/09/16 09:59:55 dpc22 Exp $ */
/************************************************
 *    Prayer - a Webmail Interface              *
 ************************************************/

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

#include "prayer_session.h"

static void generate_form(struct session *session, struct buffer *b)
{
    struct template_vals *tvals = session->template_vals;
    struct options *options = session->options;
    struct dictionary *dictionary = options->dictionary;
    struct list_item *li;
    unsigned long i, len, max, perline, count;

    /* Find maximum length of list item */
    count = 0;
    max = 1;
    for (li = dictionary->list->head; li; li = li->next) {
        if ((len = strlen(li->name)) > max)
            max = len;
        count++;
    }

    if ((max > 0) && (max < 38))
        perline = 80 / (max + 5);
    else
        perline = 1;

    if (count > 0) {
        for (i = 0, li = dictionary->list->head; li; li = li->next, i++) {
            template_vals_foreach_init(tvals, "@words", i);
            template_vals_foreach_string(tvals, "@words", i, "word", li->name);

            if ((i % perline) == (perline - 1))
                template_vals_foreach_ulong(tvals, "@words", i, "break", 1);
        }
    }

    session_seed_template(session, tvals);
    template_expand("dictionary", tvals, b);
}

void cmd_dictionary(struct session *session)
{
    struct request *request = session->request;
    struct buffer *b = request->write_buffer;
    struct assoc *h = NIL;
    struct options *options = session->options;
    struct dictionary *dictionary = options->dictionary;
    char *s, *line, *word;

    if (request->method != POST) {
        request_decode_form(request);
        h = request->form;
        if ((s = assoc_lookup(h, "remove")) && s[0]) {
            dictionary_remove(dictionary, s);
            options->save = T;
        }

        generate_form(session, b);
        response_html(request, 200);
        return;
    }

    request_decode_form(request);
    h = request->form;

    if (assoc_lookup(h, "sub_cancel")) {
        session_redirect(session, request, "manage");
        return;
    }

    /* Add or remove words from dictionary */

    if (assoc_lookup(h, "sub_add")) {
        line = assoc_lookup(h, "add");
        while ((word = string_get_token(&line))) {
            if (word[0]) {
                dictionary_add(dictionary, word);
                options->save = T;
            }
        }
    } else if (assoc_lookup(h, "sub_remove")) {
        line = assoc_lookup(h, "remove");
        while ((word = string_get_token(&line))) {
            if (word[0]) {
                dictionary_remove(dictionary, word);
                options->save = T;
            }
        }
    }

    generate_form(session, b);
    response_html(request, 200);
}