aboutsummaryrefslogtreecommitdiffstats
path: root/em-format/e-mail-stripsig-filter.c
blob: e861a13a53bee338411abf72aaa276a3a3f0d687 (plain) (blame)
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
/*
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *      Jeffrey Stedfast <fejj@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <string.h>

#include "e-mail-stripsig-filter.h"

G_DEFINE_TYPE (EMailStripSigFilter, e_mail_stripsig_filter, CAMEL_TYPE_MIME_FILTER)

static void
strip_signature (CamelMimeFilter *filter,
                 const gchar *in,
                 gsize len,
                 gsize prespace,
                 gchar **out,
                 gsize *outlen,
                 gsize *outprespace,
                 gint flush)
{
    EMailStripSigFilter *stripsig = (EMailStripSigFilter *) filter;
    register const gchar *inptr = in;
    const gchar *inend = in + len;
    const gchar *start = NULL;

    if (stripsig->midline) {
        while (inptr < inend && *inptr != '\n')
            inptr++;

        if (inptr < inend) {
            stripsig->midline = FALSE;
            inptr++;
        }
    }

    while (inptr < inend) {
        if ((inend - inptr) >= 4 && !strncmp (inptr, "-- \n", 4)) {
            start = inptr;
            inptr += 4;
        } else if (!stripsig->text_plain_only &&
                (inend - inptr) >= 7 &&
                !g_ascii_strncasecmp (inptr, "-- <BR>", 7)) {
            start = inptr;
            inptr += 7;
        } else {
            while (inptr < inend && *inptr != '\n')
                inptr++;

            if (inptr == inend) {
                stripsig->midline = TRUE;
                break;
            }

            inptr++;
        }
    }

    if (start != NULL) {
        inptr = start;
        stripsig->midline = FALSE;
    }

    if (!flush && inend > inptr)
        camel_mime_filter_backup (filter, inptr, inend - inptr);
    else if (!start)
        inptr = inend;

    *out = (gchar *)in;
    *outlen = inptr - in;
    *outprespace = prespace;
}

static void
filter_filter (CamelMimeFilter *filter,
               const gchar *in,
               gsize len,
               gsize prespace,
               gchar **out,
               gsize *outlen,
               gsize *outprespace)
{
    strip_signature (
        filter, in, len, prespace, out, outlen, outprespace, FALSE);
}

static void
filter_complete (CamelMimeFilter *filter,
                 const gchar *in,
                 gsize len,
                 gsize prespace,
                 gchar **out,
                 gsize *outlen,
                 gsize *outprespace)
{
    strip_signature (
        filter, in, len, prespace, out, outlen, outprespace, TRUE);
}

/* should this 'flush' outstanding state/data bytes? */
static void
filter_reset (CamelMimeFilter *filter)
{
    EMailStripSigFilter *stripsig = (EMailStripSigFilter *) filter;

    stripsig->midline = FALSE;
}

static void
e_mail_stripsig_filter_class_init (EMailStripSigFilterClass *class)
{
    CamelMimeFilterClass *mime_filter_class;

    mime_filter_class = CAMEL_MIME_FILTER_CLASS (class);
    mime_filter_class->filter = filter_filter;
    mime_filter_class->complete = filter_complete;
    mime_filter_class->reset = filter_reset;
}

static void
e_mail_stripsig_filter_init (EMailStripSigFilter *filter)
{
}

/**
 * e_mail_stripsig_filter_new:
 * @text_plain_only: Whether should look for a text/plain signature
 * delimiter "-- \n" only or also an HTML signature delimiter "-- <BR>".
 *
 * Creates a new stripsig filter.
 *
 * Returns a new stripsig filter.
 **/
CamelMimeFilter *
e_mail_stripsig_filter_new (gboolean text_plain_only)
{
    EMailStripSigFilter *filter = g_object_new (E_TYPE_MAIL_STRIPSIG_FILTER, NULL);

    filter->text_plain_only = text_plain_only;

    return CAMEL_MIME_FILTER (filter);
}