File: gmime-filter-header.c

package info (click to toggle)
balsa 2.6.5-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,576 kB
  • sloc: ansic: 99,871; xml: 4,934; makefile: 769; sh: 185; awk: 60; python: 34
file content (157 lines) | stat: -rw-r--r-- 4,829 bytes parent folder | download | duplicates (4)
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
/* -*-mode:c; c-style:k&r; c-basic-offset:4; -*- */
/* GMime message header filter
 * Written/Copyright (c) by Albrecht Dreß <albrecht.dress@arcor.de> 2017
 * This module remove RFC 822 massage headers which shall not be sent to the MTA from from a message stream, including Bcc, Status,
 * X-Status and X-Balsa-*.
 *
 * The basic structure of this file has been shamelessly stolen from the gmime-filter-* files, written by Jeffrey Stedfast.
 *
 * This library 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.1 of the License, or (at your option) any later version.
 *
 * This library 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 this library; if not, write to the Free
 * Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <string.h>
#include "gmime-filter-header.h"

static void g_mime_filter_header_finalize(GObject *object);

static GMimeFilter *filter_copy(GMimeFilter *filter);
static void filter_filter(GMimeFilter *filter, char *in, size_t len, size_t prespace, char **out, size_t *outlen,
						  size_t *outprespace);
static void filter_complete(GMimeFilter *filter, char *in, size_t len, size_t prespace, char **out, size_t *outlen,
							size_t *outprespace);
static void filter_reset(GMimeFilter *filter);

struct _GMimeFilterHeader {
	GMimeFilter parent_object;
	gboolean headers_done;
	gboolean drop_header;
};

G_DEFINE_TYPE(GMimeFilterHeader, g_mime_filter_header, GMIME_TYPE_FILTER)


static void
g_mime_filter_header_class_init(GMimeFilterHeaderClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS(klass);
	GMimeFilterClass *filter_class = GMIME_FILTER_CLASS(klass);

	object_class->finalize = g_mime_filter_header_finalize;

	filter_class->copy = filter_copy;
	filter_class->filter = filter_filter;
	filter_class->complete = filter_complete;
	filter_class->reset = filter_reset;
}

static void
g_mime_filter_header_init(GMimeFilterHeader *self)
{
	self->headers_done = FALSE;
	self->drop_header = FALSE;
}

static void
g_mime_filter_header_finalize(GObject *object)
{
	G_OBJECT_CLASS(g_mime_filter_header_parent_class)->finalize(object);
}

static GMimeFilter *
filter_copy(GMimeFilter *filter)
{
	return g_mime_filter_header_new();
}

static void
filter_filter(GMimeFilter *filter, char *inbuf, size_t inlen, size_t prespace,
	       	  char **outbuf, size_t *outlen, size_t *outprespace)
{
	GMimeFilterHeader *self = GMIME_FILTER_HEADER(filter);

	g_mime_filter_set_size(filter, inlen, FALSE);

	if (self->headers_done) {
		memcpy(filter->outbuf, inbuf, inlen);
		*outlen = inlen;
	} else {
		gchar *newline;
		gchar *outptr;

		outptr = filter->outbuf;
		*outlen = 0U;
		newline = memchr(inbuf, '\n', inlen);
		while ((newline != NULL) && !self->headers_done) {
			size_t count;

			/* number of chars in this line */
			count = (newline - inbuf) + 1U;

			/* check for for folded header continuation */
			if ((inbuf[0] == ' ') || (inbuf[0] == '\t')) {
				/* nothing to do, do not change the state */
			} else if (newline == inbuf) {
				self->headers_done = TRUE;
			} else if (((count >= 4U) && (g_ascii_strncasecmp(inbuf, "Bcc:", 4U) == 0)) ||
				   	   ((count >= 7U) && (g_ascii_strncasecmp(inbuf, "Status:", 7U) == 0)) ||
					   ((count >= 9U) && (g_ascii_strncasecmp(inbuf, "X-Status:", 9U) == 0)) ||
					   ((count >= 8U) && (g_ascii_strncasecmp(inbuf, "X-Balsa-", 8U) == 0))) {
				self->drop_header = TRUE;
			} else {
				self->drop_header = FALSE;
			}

			/* copy if we want to keep this header */
			if (!self->drop_header) {
				memcpy(outptr, inbuf, count);
				outptr = &outptr[count];
				*outlen += count;
			}

			/* adjust */
			inbuf = &inbuf[count];
			inlen -= count;
			if (!self->headers_done && (inlen > 0)) {
				newline = memchr(inbuf, '\n', inlen);
			} else {
				newline = NULL;
			}
		}

		/* back up left-over data */
		if (inlen > 0U) {
			g_mime_filter_backup(filter, inbuf, inlen);
		}
	}
	*outprespace = filter->outpre;
	*outbuf = filter->outbuf;
}

static void
filter_complete(GMimeFilter *filter, char *inbuf, size_t inlen, size_t prespace,
		 	 	char **outbuf, size_t *outlen, size_t *outprespace)
{
	filter_filter(filter, inbuf, inlen, prespace, outbuf, outlen, outprespace);
}

static void
filter_reset(GMimeFilter *filter)
{
}

GMimeFilter *
g_mime_filter_header_new()
{
	GMimeFilterHeader *header;

	header = g_object_new(GMIME_TYPE_FILTER_HEADER, NULL);

	return (GMimeFilter *) header;
}