Blame view

mailbox/filter.c 7.62 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2 3
   Copyright (C) 1999, 2000, 2001, 2004, 2005, 
   2007 Free Software Foundation, Inc.
Alain Magloire authored
4

5 6 7
   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
8
   version 3 of the License, or (at your option) any later version.
Alain Magloire authored
9

10
   This library is distributed in the hope that it will be useful,
Alain Magloire authored
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
Alain Magloire authored
14

15 16 17 18
   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, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301 USA */
Alain Magloire authored
19 20 21 22 23 24 25 26 27 28 29 30 31 32

/* Notes:
First draft: Alain Magloire.

 */

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

#include <errno.h>
#include <string.h>
#include <stdlib.h>

33 34 35 36
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif

Alain Magloire authored
37
#include <filter0.h>
38

Alain Magloire authored
39
#include <mailutils/iterator.h>
40
#include <mailutils/stream.h>
41
#include <mailutils/errno.h>
Alain Magloire authored
42 43

static void
44
filter_destroy (mu_stream_t stream)
Alain Magloire authored
45
{
46
  mu_filter_t filter = mu_stream_get_owner (stream);
Alain Magloire authored
47 48 49
  if (filter->_destroy)
    filter->_destroy (filter);
  if (filter->property)
50
    mu_property_destroy (&(filter->property), filter);
Alain Magloire authored
51 52 53 54
  free (filter);
}

static int
55
filter_read (mu_stream_t stream, char *buffer, size_t buflen, mu_off_t offset,
Alain Magloire authored
56 57
	     size_t *nbytes)
{
58
  mu_filter_t filter = mu_stream_get_owner (stream);
Alain Magloire authored
59 60 61
  if (filter->_read && (filter->direction & MU_STREAM_READ
		      || filter->direction & MU_STREAM_RDWR))
    return filter->_read (filter, buffer, buflen, offset, nbytes);
62
  return mu_stream_read (filter->stream, buffer, buflen, offset, nbytes);
Alain Magloire authored
63 64 65
}

static int
66
filter_readline (mu_stream_t stream, char *buffer, size_t buflen,
67
		 mu_off_t offset, size_t *nbytes)
Alain Magloire authored
68
{
69
  mu_filter_t filter = mu_stream_get_owner (stream);
Alain Magloire authored
70 71 72
  if (filter->_readline && (filter->direction & MU_STREAM_READ
			  || filter->direction & MU_STREAM_RDWR))
    return filter->_readline (filter, buffer, buflen, offset, nbytes);
73
  return mu_stream_readline (filter->stream, buffer, buflen, offset, nbytes);
Alain Magloire authored
74 75 76
}

static int
77
filter_write (mu_stream_t stream, const char *buffer, size_t buflen,
78
	      mu_off_t offset, size_t *nbytes)
Alain Magloire authored
79
{
80
  mu_filter_t filter = mu_stream_get_owner (stream);
Alain Magloire authored
81 82 83
  if (filter->_write && (filter->direction & MU_STREAM_WRITE
		       || filter->direction & MU_STREAM_RDWR))
    return filter->_write (filter, buffer, buflen, offset, nbytes);
84
  return mu_stream_write (filter->stream, buffer, buflen, offset, nbytes);
Alain Magloire authored
85 86 87
}

static int
88
filter_open (mu_stream_t stream)
Alain Magloire authored
89
{
90
  mu_filter_t filter = mu_stream_get_owner (stream);
91

92
  return mu_stream_open (filter->stream);
Alain Magloire authored
93 94 95
}

static int
96
filter_truncate (mu_stream_t stream, mu_off_t len)
Alain Magloire authored
97
{
98 99
  mu_filter_t filter = mu_stream_get_owner (stream);
  return mu_stream_truncate (filter->stream, len);
Alain Magloire authored
100 101 102
}

static int
103
filter_size (mu_stream_t stream, mu_off_t *psize)
Alain Magloire authored
104
{
105 106
  mu_filter_t filter = mu_stream_get_owner (stream);
  return mu_stream_size (filter->stream, psize);
Alain Magloire authored
107 108 109
}

static int
110
filter_flush (mu_stream_t stream)
Alain Magloire authored
111
{
112 113
  mu_filter_t filter = mu_stream_get_owner(stream);
  return mu_stream_flush (filter->stream);
Alain Magloire authored
114 115 116
}

static int
117
filter_get_transport2 (mu_stream_t stream, mu_transport_t *pin, mu_transport_t *pout)
Alain Magloire authored
118
{
119 120
  mu_filter_t filter = mu_stream_get_owner (stream);
  return mu_stream_get_transport2 (filter->stream, pin, pout);
Alain Magloire authored
121 122 123
}

static int
124
filter_close (mu_stream_t stream)
Alain Magloire authored
125
{
126 127
  mu_filter_t filter = mu_stream_get_owner (stream);
  return mu_stream_close (filter->stream);
Alain Magloire authored
128 129 130 131
}

/* NOTE: We will leak here since the monitor of the filter will never
   be release.  That's ok we can leave with this, it's only done once.  */
132
static mu_list_t filter_list;
133
struct mu_monitor filter_monitor = MU_MONITOR_INITIALIZER;
Alain Magloire authored
134 135

int
136
mu_filter_get_list (mu_list_t *plist)
Alain Magloire authored
137 138
{
  if (plist == NULL)
139
    return MU_ERR_OUT_PTR_NULL;
140
  mu_monitor_wrlock (&filter_monitor);
Alain Magloire authored
141 142
  if (filter_list == NULL)
    {
143
      int status = mu_list_create (&filter_list);
Alain Magloire authored
144 145 146
      if (status != 0)
	return status;
      /* Default filters.  */
147 148 149 150 151 152 153
      mu_list_append (filter_list, mu_base64_filter);
      mu_list_append (filter_list, mu_qp_filter);
      mu_list_append (filter_list, mu_binary_filter);
      mu_list_append (filter_list, mu_bit8_filter);
      mu_list_append (filter_list, mu_bit7_filter);
      mu_list_append (filter_list, mu_rfc822_filter);
      mu_list_append (filter_list, mu_rfc_2047_Q_filter);
154
      mu_list_append (filter_list, mu_rfc_2047_B_filter);
Alain Magloire authored
155 156 157
      /* FIXME: add the default encodings?  */
    }
  *plist = filter_list;
158
  mu_monitor_unlock (&filter_monitor);
Alain Magloire authored
159 160 161 162
  return 0;
}

int
163
mu_filter_create (mu_stream_t *pstream, mu_stream_t stream, const char *name,
Alain Magloire authored
164 165
	       int type, int direction)
{
166 167 168
  mu_iterator_t iterator = NULL;
  mu_filter_record_t filter_record = NULL;
  int  (*f_init)  (mu_filter_t)  = NULL;
Alain Magloire authored
169 170
  int found = 0;
  int status;
171
  mu_list_t list = NULL;
Alain Magloire authored
172

173 174 175
  if (pstream == NULL)
    return MU_ERR_OUT_PTR_NULL;
  if (stream == NULL || name == NULL)
Alain Magloire authored
176 177
    return EINVAL;

178 179
  mu_filter_get_list (&list);
  status = mu_list_get_iterator (list, &iterator);
Alain Magloire authored
180 181 182
  if (status != 0)
    return status;

183 184
  for (mu_iterator_first (iterator); !mu_iterator_is_done (iterator);
       mu_iterator_next (iterator))
Alain Magloire authored
185
    {
186
      mu_iterator_current (iterator, (void **)&filter_record);
Alain Magloire authored
187 188 189 190 191 192 193 194
      if ((filter_record->_is_filter
	   && filter_record->_is_filter (filter_record, name))
	  || (strcasecmp (filter_record->name, name) == 0))
        {
	  found = 1;
	  if (filter_record->_get_filter)
	    filter_record->_get_filter (filter_record, &f_init);
	  else
195
	    f_init = filter_record->_mu_filter;
Alain Magloire authored
196 197 198
	  break;
        }
    }
199
  mu_iterator_destroy (&iterator);
Alain Magloire authored
200 201 202 203

  if (found)
    {
      int flags = 0;
204
      mu_filter_t filter;
Alain Magloire authored
205 206 207 208 209

      filter = calloc (1, sizeof (*filter));
      if (filter == NULL)
	return ENOMEM;

210 211
      mu_stream_get_flags (stream, &flags);
      status = mu_stream_create (pstream, flags | MU_STREAM_NO_CHECK, filter);
Alain Magloire authored
212 213 214 215 216 217 218 219 220 221 222
      if (status != 0)
	{
	  free (filter);
	  return status;
	}

      filter->stream = stream;
      filter->filter_stream = *pstream;
      filter->direction = (direction == 0) ? MU_STREAM_READ : direction;
      filter->type = type;

223
      status = mu_property_create (&(filter->property), filter);
Alain Magloire authored
224 225
      if (status != 0)
	{
226
	  mu_stream_destroy (pstream, filter);
Alain Magloire authored
227 228 229
	  free (filter);
	  return status;
	}
230
      mu_property_set_value (filter->property, "DIRECTION",
231 232 233
			  ((filter->direction == MU_STREAM_WRITE) ? "WRITE":
			   (filter->direction == MU_STREAM_RDWR) ? "RDWR" :
			   "READ"), 1);
234 235
      mu_property_set_value (filter->property, "TYPE", filter_record->name, 1);
      mu_stream_set_property (*pstream, filter->property, filter);
Alain Magloire authored
236 237 238 239 240 241

      if (f_init != NULL)
	{
	  status = f_init (filter);
	  if (status != 0)
	    {
242
	      mu_stream_destroy (pstream, filter);
Alain Magloire authored
243 244 245 246 247
	      free (filter);
	      return status;
	    }
        }

248 249 250 251 252 253 254 255 256 257
      mu_stream_set_open (*pstream, filter_open, filter );
      mu_stream_set_close (*pstream, filter_close, filter );
      mu_stream_set_read (*pstream, filter_read, filter);
      mu_stream_set_readline (*pstream, filter_readline, filter);
      mu_stream_set_write (*pstream, filter_write, filter);
      mu_stream_set_get_transport2 (*pstream, filter_get_transport2, filter );
      mu_stream_set_truncate (*pstream, filter_truncate, filter );
      mu_stream_set_size (*pstream, filter_size, filter );
      mu_stream_set_flush (*pstream, filter_flush, filter );
      mu_stream_set_destroy (*pstream, filter_destroy, filter);
Alain Magloire authored
258 259
    }
  else
260
    status = MU_ERR_NOENT;
Alain Magloire authored
261 262
  return status;
}