File: cmd_reply2.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 (329 lines) | stat: -rw-r--r-- 10,710 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
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
/* $Cambridge: hermes/src/prayer/cmd/cmd_reply2.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 BOOL string_check_sigdash(char *sig)
{
    char *s;

    /* Check whether signature already contains sigdash line */
    for (s = sig; *s; s++) {
        if ((s[0] == '-') && (s[1] == '-') && (s[2] == ' ') &&
            ((s[3] == '\015') || (s[3] == '\012'))) {
            return (NIL);
        }
    }

    return (T);
}

static char *string_decode_mime(struct pool *tpool, char *s)
{
    unsigned long len = strlen(s) + 20;
    char *buffer = pool_alloc(tpool, len);
    char *d;

    d = (char *) rfc1522_decode((unsigned char *) buffer, len, s, NIL);

    return (d);
}

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

/* Extract text from relevant message (go fishing around inside multipart
 * messages) and put in into the specified buffer as quoted (and possibly
 * line wrapped) text */

static BOOL
add_text(struct session *session, struct buffer *b, struct pool *pool,
         MAILSTREAM *stream, unsigned long msgno)

{
    struct prefs *prefs = session->options->prefs;
    BODY *body = NIL;
    char *section = NIL;
    char *init_msg, *decode_msg, *type;
    unsigned long len;
    char *text, *s;
    PARAMETER *parameter;
    char *charset = "ISO-8859-1";

    if ((body = ml_body(session, stream, msgno, "1")) == NIL)
        return(NIL);

    if (body->type == TYPEMULTIPART) {
        PART *part = body->nested.part;
        int   i = 1, body_plain = 0, body_html = 0, subsection;

        for (i = 1; part != NIL; part = part->next, i++) {
            if (!(body = &part->body))
                continue;

            if ((body->type != TYPETEXT) || !body->subtype)
                continue;

            if (!strcasecmp(body->subtype, "plain")) {
                if (!body_plain) body_plain = i;
            } else if (!strcasecmp(body->subtype, "html")) {
                if (!body_html) body_html = i;
            }
        }

        subsection = (body_plain) ? body_plain : body_html;

        if (subsection) {
            section = pool_printf(pool, "1.%lu", subsection);
            if ((body = ml_body(session, stream, msgno, section)) == NIL)
                return (NIL);
        } else
            section = NIL;
    } else if (body->type == TYPETEXT) {
        section = "1";
    } else
        section = NIL;

    if (!section) {
        bputs(b, "> (Message body was not text: suppressed)" CRLF);
        return(T);
    }

    for (parameter = body->parameter; parameter; parameter = parameter->next) {
        if (strcasecmp(parameter->attribute, "charset") == 0) {
            charset = parameter->value;
            break;
        }
    }

    /* Got a valid text section to display */
    if (!(init_msg=ml_fetchbody(session, stream, msgno, section, &len)))
        return(NIL);

    /* Strip off encoding */
    switch (body->encoding) {
    case ENCBASE64:
        if (!(decode_msg =
              (char *) rfc822_base64((unsigned char *) init_msg,
                                     body->size.bytes, &len))) {
            /* Decode failed */
            decode_msg = init_msg;
            len = body->size.bytes;
        }
        break;
    case ENCQUOTEDPRINTABLE:
        if (!(decode_msg =
              (char *) rfc822_qprint((unsigned char *) init_msg,
                                     body->size.bytes, &len))) {
            /* Decode failed */
            decode_msg = init_msg;
            len = body->size.bytes;
        }
        break;
    case ENC7BIT:
    case ENC8BIT:
    case ENCBINARY:
    case ENCOTHER:
    default:
        decode_msg = init_msg;
        len = body->size.bytes;
    }

    type = pool_strcat3(pool, body_types[body->type], "/", body->subtype);
    string_lcase(type);

    if ((!strcasecmp(type, "text/html") && prefs->html_inline) ||
        (!strncasecmp(decode_msg, "<html>", strlen("<html>")) &&
         prefs->html_inline_auto)) {
        struct buffer *b = buffer_create(pool, 8192);
        if (decode_msg == init_msg)
            decode_msg = strdup(init_msg);
        html_secure_strip_all(b, utf8_from_string(pool, charset, decode_msg, len));
        
        text = buffer_fetch(b, 0, buffer_size(b), NIL);
    } else
        text = utf8_from_string(pool, charset, decode_msg, len);

    bputs(b, ">");
    for (s = text; *s;) {
        if ((s[0] == '\015') || (s[0] == '\012')) {
            if ((s[0] == '\015') && (s[1] == '\012'))
                s += 2;     /* CRLF */
            else
                s++;        /* CR or LF */
            bputs(b, CRLF ">");

            /* Check for sigdash at start of line */
            if ((s[0] == '-') && (s[1] == '-') && (s[2] == ' ')  &&
                ((s[3] == '\015') || (s[3] == '\012')))
                break;
        } else
            bputc(b, *s++);
    }

    if (decode_msg != init_msg)
        fs_give((void **) &decode_msg);

    return(T);
}

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

void cmd_reply2(struct session *session)
{
    struct request *request = session->request;
    struct draft *draft = session->draft;
    struct options *options = session->options;
    struct prefs *prefs = options->prefs;
    unsigned long msgno;
    unsigned long msgno_uid;
    char *s, *opt_cc;
    MAILSTREAM *stream = session->stream;
    MESSAGECACHE *elt;
    ENVELOPE *env;
    ADDRESS *sender;
    struct buffer *b = request->write_buffer;
    char *postponed_text = "";

    request_decode_form(request);
    if ((s = assoc_lookup(request->form, "role")))
        draft_role_set(draft, role_find(options->role_list, s));

    msgno = session->current;
    msgno_uid = ml_uid(session, stream, msgno);

    if (!(msgno = stream_check_uid(session, stream, msgno, msgno_uid))) {
        session_redirect(session, request, "list");
        return;
    }

    if (!((elt = ml_elt(session, stream, msgno)) &&
          (env = ml_fetch_structure(session, stream, msgno, NIL, 0)))) {
        session_redirect(session, request, "restart");
        return;
    }

    sender = (env->reply_to) ? (env->reply_to) : (env->from);

    /* Calculate possible Cc value from message recipients */
    if (env->to) {
        opt_cc = pool_strdup(request->pool,
                             addr_text_exclude(session, request->pool,
                                               env->to, sender));
        if (env->cc) {
            s = addr_text_exclude(session, request->pool, env->cc, sender);
            if (s && s[0]) {
                if (opt_cc && opt_cc[0])
                    opt_cc = pool_strcat3(request->pool, opt_cc, ", ", s);
                else
                    opt_cc = pool_strdup(request->pool, s);
            }
        }
    } else if (env->cc) {
        opt_cc = pool_strdup(request->pool,
                             addr_text_exclude(session, request->pool,
                                               env->cc, sender));
    } else
        opt_cc = NIL;

    /* Write out exiting draft */
    if (draft->have_draft) {
        if (!draft_write(draft)) {
            session_message_clear(session);
            session_alert(session,
                          "Failed to postpone existing draft before reply");
            session_log(session,
                        "[cmd_reply2] Failed to postponed draft message: %s",
                        ml_errmsg());
            session_redirect(session, session->request, "compose");
            return;
        }
        draft_free(draft);
        postponed_text = " Postponed existing draft.";
    }

    draft_init(draft);
    draft_init_rich_headers(draft);

    /* Record message that we are responding to here */
    draft_set_reply(draft, stream, session->foldername, msgno);

    /* Set To address */
    if (sender && sender->mailbox && sender->host) {
        char *s = string_decode_mime(request->pool,
                                     addr_text(request->pool, sender));

        draft->to = pool_strdup(draft->pool, s);
    }

    /* Set Cc address */
    if (opt_cc && opt_cc[0] && (session->reply_all)) {
        char *s = string_decode_mime(request->pool, opt_cc);

        draft->cc = pool_strdup(draft->pool, s);
    }

    /* Create temporary scratch buffer */
    b = buffer_create(draft->pool, 0);

    if ((sender = env->from)) {
        bprintf(b, "On %s, ", mc_date_to_string(elt));

        /* XXX Factor out rfc1522_decode calls */
        if (sender->personal) {
            unsigned long len = strlen(sender->personal) + 20;
            char *buffer = pool_alloc(b->pool, len);    /* Decoded form smaller */
            char *d =
                (char *) rfc1522_decode((unsigned char *) buffer, len,
                                        sender->personal, NIL);
            bprintf(b, "%s wrote:" CRLF "" CRLF, d);
        } else if (sender->mailbox && sender->host)
            bprintf(b, "%s@%s wrote:" CRLF "" CRLF, sender->mailbox,
                    sender->host);
    }

    if (!add_text(session, b, request->pool, stream, msgno)) {
        session_redirect(session, request, "restart");
        return;
    }

    if (draft->role && draft->role->signature && draft->role->signature[0]) {
        bputs(b, "" CRLF "" CRLF);
        if (string_check_sigdash(draft->role->signature))
            bputs(b, "-- " CRLF);
        bprintf(b, "%s" CRLF, draft->role->signature);
    } else if (prefs->signature && prefs->signature[0]) {
        bputs(b, "" CRLF "" CRLF);
        if (string_check_sigdash(prefs->signature))
            bputs(b, "-- " CRLF);
        bprintf(b, "%s" CRLF, prefs->signature);
    }

    /* Get full copy of buffer */
    draft->body = buffer_fetch(b, 0, buffer_size(b), NIL);

    if (prefs->line_wrap_on_reply)
        draft_line_wrap_body(draft);

    if (!(env = ml_fetch_structure(session, stream, msgno, NIL, 0))) {
        session_redirect(session, request, "restart");
        return;
    }

    if (env->subject && env->subject) {
        char *s = string_decode_mime(request->pool, env->subject);

        if (!strncasecmp(s, "Re: ", strlen("Re: ")))
            draft->subject = pool_strdup(draft->pool, s);
        else
            draft->subject = pool_strcat(draft->pool, "Re: ", s);
    }

    session_message(session, "Replying to message %d.%s", msgno,
                    postponed_text);
    session_redirect(session, request, "compose");
}