2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 #include "gmime-filter-reply.h"
22 #include "notmuch-client.h"
25 * SECTION: gmime-filter-reply
26 * @title: GMimeFilterReply
27 * @short_description: Add/remove reply markers
29 * A #GMimeFilter for adding or removing reply markers
33 static void g_mime_filter_reply_class_init (GMimeFilterReplyClass *klass, void *class_data);
34 static void g_mime_filter_reply_init (GMimeFilterReply *filter, GMimeFilterReplyClass *klass);
35 static void g_mime_filter_reply_finalize (GObject *object);
37 static GMimeFilter *filter_copy (GMimeFilter *filter);
38 static void filter_filter (GMimeFilter *filter, char *in, size_t len, size_t prespace,
39 char **out, size_t *outlen, size_t *outprespace);
40 static void filter_complete (GMimeFilter *filter, char *in, size_t len, size_t prespace,
41 char **out, size_t *outlen, size_t *outprespace);
42 static void filter_reset (GMimeFilter *filter);
45 static GMimeFilterClass *parent_class = NULL;
46 static GType type = 0;
47 static const GTypeInfo info = {
48 .class_size = sizeof (GMimeFilterReplyClass),
50 .base_finalize = NULL,
51 .class_init = (GClassInitFunc) g_mime_filter_reply_class_init,
52 .class_finalize = NULL,
54 .instance_size = sizeof (GMimeFilterReply),
56 .instance_init = (GInstanceInitFunc) g_mime_filter_reply_init,
62 g_mime_filter_reply_module_init (void)
64 type = g_type_register_static (GMIME_TYPE_FILTER, "GMimeFilterReply", &info, (GTypeFlags) 0);
65 parent_class = (GMimeFilterClass *) g_type_class_ref (GMIME_TYPE_FILTER);
69 g_mime_filter_reply_get_type (void)
76 g_mime_filter_reply_class_init (GMimeFilterReplyClass *klass, unused (void *class_data))
78 GObjectClass *object_class = G_OBJECT_CLASS (klass);
79 GMimeFilterClass *filter_class = GMIME_FILTER_CLASS (klass);
81 object_class->finalize = g_mime_filter_reply_finalize;
83 filter_class->copy = filter_copy;
84 filter_class->filter = filter_filter;
85 filter_class->complete = filter_complete;
86 filter_class->reset = filter_reset;
90 g_mime_filter_reply_init (GMimeFilterReply *filter, GMimeFilterReplyClass *klass)
93 filter->saw_nl = true;
94 filter->saw_angle = false;
98 g_mime_filter_reply_finalize (GObject *object)
100 G_OBJECT_CLASS (parent_class)->finalize (object);
105 filter_copy (GMimeFilter *filter)
107 GMimeFilterReply *reply = (GMimeFilterReply *) filter;
109 return g_mime_filter_reply_new (reply->encode);
113 filter_filter (GMimeFilter *filter, char *inbuf, size_t inlen, size_t prespace,
114 char **outbuf, size_t *outlen, size_t *outprespace)
116 GMimeFilterReply *reply = (GMimeFilterReply *) filter;
117 const char *inptr = inbuf;
118 const char *inend = inbuf + inlen;
123 g_mime_filter_set_size (filter, 3 * inlen, false);
125 outptr = filter->outbuf;
126 while (inptr < inend) {
130 reply->saw_nl = false;
133 reply->saw_nl = true;
135 reply->saw_nl = false;
141 g_mime_filter_set_size (filter, inlen + 1, false);
143 outptr = filter->outbuf;
144 while (inptr < inend) {
147 reply->saw_angle = true;
150 reply->saw_nl = false;
151 } else if (reply->saw_angle) {
156 reply->saw_angle = false;
157 } else if (*inptr != '\r') {
159 reply->saw_nl = true;
167 *outlen = outptr - filter->outbuf;
168 *outprespace = filter->outpre;
169 *outbuf = filter->outbuf;
173 filter_complete (GMimeFilter *filter, char *inbuf, size_t inlen, size_t prespace,
174 char **outbuf, size_t *outlen, size_t *outprespace)
177 filter_filter (filter, inbuf, inlen, prespace, outbuf, outlen, outprespace);
181 filter_reset (GMimeFilter *filter)
183 GMimeFilterReply *reply = (GMimeFilterReply *) filter;
185 reply->saw_nl = true;
186 reply->saw_angle = false;
191 * g_mime_filter_reply_new:
192 * @encode: %true if the filter should encode or %false otherwise
193 * @dots: encode/decode dots (as for SMTP)
195 * Creates a new #GMimeFilterReply filter.
197 * If @encode is %true, then all lines will be prefixed by "> ",
198 * otherwise any lines starting with "> " will have that removed
200 * Returns: a new #GMimeFilterReply filter.
203 g_mime_filter_reply_new (gboolean encode)
205 GMimeFilterReply *new_reply;
207 new_reply = (GMimeFilterReply *) g_object_new (GMIME_TYPE_FILTER_REPLY, NULL);
208 new_reply->encode = encode;
210 return (GMimeFilter *) new_reply;