File: cmd_spam.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 (203 lines) | stat: -rw-r--r-- 6,620 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
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
/* $Cambridge: hermes/src/prayer/cmd/cmd_spam.c,v 1.4 2008/09/17 17:20:25 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 template_vals *tvals = session->template_vals;
    struct request *request = session->request;
    struct account *account = session->account;
    struct buffer *b = request->write_buffer;
    unsigned long threshold =
        (account->spam_threshold_valid) ? account->spam_threshold : 10;

    template_vals_ulong(tvals, "spam_threshold", threshold);

    if (account->spam_enabled)
        template_vals_ulong(tvals, "spam_enabled", 1);


    if (session->sieved_server)
        template_vals_ulong(tvals, "using_sieve", 1);

    if (account->spam_purge_enabled)
        template_vals_ulong(tvals, "spam_purge_enabled", 1);

    template_vals_ulong(tvals, "spam_purge_timeout",
                        account->spam_purge_timeout);

    if (account->spam_whitelist && account->spam_whitelist[0])
        template_vals_string(tvals, "spam_whitelist", account->spam_whitelist);

    session_seed_template(session, tvals);
    template_expand("spam", tvals, b);
    response_html(request, 200);        /* Success */
}

static void generate_error(struct session *session)
{
    struct template_vals *tvals = session->template_vals;
    struct account *account = session->account;
    struct request *request = session->request;
    char *msg = account_fetch_message(account);
    struct buffer *b = request->write_buffer;

    if (!(msg && msg[0]))
        msg = "Unable to check mail processing status";

    template_vals_string(tvals, "msg", msg);
    session_seed_template(session, tvals);
    template_expand("spam_fail", tvals, b);
    response_html(request, 200);
}

/* Remove extraneous whitespace from each line in whitelist */

static char *
normalise_whitelist(char *list, struct pool *pool)
{
    struct buffer *b = buffer_create(pool, 64);
    char *tmp = pool_strdup(pool, list);         /* Scratch copy */
    char *s;

    while ((s=string_get_line(&tmp))) {
        if ((s=string_trim_whitespace(s)) && s[0]) {
            bputs(b, s);
            bputs(b, ""CRLF);
        }
    }
    return(buffer_fetch(b, 0, buffer_size(b), NIL));
}

/* Check that addresslist consists of one valid address per line
   (wildcard characters are allowed in addresses) */

static BOOL
validate_whitelist(struct session *session, char *list, struct pool *pool)
{
    char *tmp = pool_strdup(pool, list); /* Scratch copy */
    char *s;
    ADDRESS *addr;

    while ((s=string_get_line(&tmp))) {
        if ((s=string_trim_whitespace(s)) && s[0]) {
            addr = addr_parse(pool, s, "");

            if (!addr) {
                session_message(session, "Invalid address: %s", s);
                return(NIL);
            }
            if (addr->next) {
                session_message(session,
                                "Single address on each line please: %s", s);
                mail_free_address(&addr);
                return(NIL);
            }
            mail_free_address(&addr);
        }
    }
    return(T);
}

void cmd_spam(struct session *session)
{
    struct config *config = session->config;
    struct request *request = session->request;
    struct assoc *h = NIL;
    struct account *account = session->account;
    char *s, *t;

    if (request->method != POST) {
        if (!account_mail_check(account, request->pool))
            generate_error(session);
        else
            generate_form(session);
        return;
    }

    /* Form post */
    request_decode_form(request);
    h = request->form;

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

    s = assoc_lookup(h, "spam_threshold");

    if (!(s && string_isnumber(s) && (atoi(s) > 0))) {
        session_message(session,
                        "Threshold must be a number greater than zero");
        generate_form(session);
        return;
    }

    account->spam_enabled = assoc_lookup(h, "sub_enable") ? T : NIL;
    account->spam_threshold = atoi(s);
    account->spam_threshold_valid = T;

    /* Spam whitelist and purge currently only available to sieve users */
    if (session->sieved_server) {
        if ((t = assoc_lookup(h, "spam_whitelist"))) {
            string_strdup(&account->spam_whitelist,
                          normalise_whitelist(t, request->pool));

            if (!validate_whitelist(session, account->spam_whitelist,
                                    request->pool)) {
                generate_form(session);
                return;
            }
        } else
            string_strdup(&account->spam_whitelist, "");

        t = assoc_lookup(h, "spam_purge_timeout");
        if (!(t && string_isnumber(t) && (atoi(t) >= 0))) {
            session_message(session,
                            "Purge time must be a non-negative integer");
            generate_form(session);
            return;
        }

        account->spam_purge_timeout = atoi(t);
        account->spam_purge_enabled = assoc_lookup(h, "spam_purge") ? T : NIL;

        if (account->spam_purge_enabled) {
            session_log(session, "[cmd_spam] spam_purge enabled: %d days", 
                        account->spam_purge_timeout);
        } else {
            session_log(session, "[cmd_spam] spam_purge disabled");
        }
    }

    if (!account_mail_update(account, request->pool)) {
        char *msg = account_fetch_message(account);

        session_alert(session, "Failed to update spam filter: %s", msg);
        session_log(session,
                    "[cmd_spam] Failed to update spam filter: %s", msg);
        generate_form(session);
        return;
    }

    if (account->spam_enabled &&
        !folderlist_lookup(session->folderlist, "spam")) {
        if (ml_create(session, session->stream,
                      session_mailbox(session, request->pool, "spam"))) {
            if (config->dualuse)
                folderlist_add(session->folderlist, "spam", NIL, NIL);
            else
                folderlist_add(session->folderlist, "spam", NIL, T);
        } else
            session_alert(session, "Failed to create spam folder");
    }

    session_message(session, "Updated junk email filtering");
    session_redirect(session, request, "manage");
}