File: mu_filter.c

package info (click to toggle)
mailutils 1%3A3.20-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,912 kB
  • sloc: ansic: 187,772; sh: 111,430; yacc: 7,463; cpp: 3,834; makefile: 3,166; lex: 1,972; python: 1,617; exp: 1,563; awk: 152; lisp: 132; sed: 31
file content (248 lines) | stat: -rw-r--r-- 7,335 bytes parent folder | download | duplicates (2)
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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2018-2025 Free Software Foundation, Inc.

   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 3 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, see
   <http://www.gnu.org/licenses/>. */

#include "mu_scm.h"
#include <mailutils/filter.h>
#include <mailutils/argcv.h>

static void
argv_free (void *p)
{
  mu_argv_free ((char**)p);
}

static SCM
make_filter_port (SCM port, SCM name, SCM args,
		  int filter_mode, char const *func_name)
/* The FUNC_NAME macro is used by SCM_VALIDATE_REST_ARGUMENT */
#define FUNC_NAME func_name
{
  char *fltname;
  mu_stream_t filter;
  mu_stream_t instr;
  size_t argc = 0;
  char **argv = NULL;
  int rc;
  int flags = 0;
  char *port_mode;
  
  SCM_ASSERT (scm_port_p (port), port, SCM_ARG1, FUNC_NAME);
  SCM_ASSERT (scm_is_string (name), name, SCM_ARG2, FUNC_NAME);
  SCM_VALIDATE_REST_ARGUMENT (args);

  port_mode = scm_to_locale_string (scm_port_mode (port));
  if (strchr (port_mode, 'r'))
    flags |= MU_STREAM_READ;
  if (strchr (port_mode, 'w'))
    flags |= MU_STREAM_WRITE;
  free (port_mode);
  if (!flags
      || ((flags & (MU_STREAM_READ|MU_STREAM_WRITE))
	  == (MU_STREAM_READ|MU_STREAM_WRITE)))
    scm_out_of_range (FUNC_NAME, port); //FIXME
  
  scm_dynwind_begin (0);
  
  fltname = scm_to_locale_string (name);
  scm_dynwind_free (fltname);
  
  rc = mu_scm_port_stream_create (&instr, port);
  if (rc)
    {
      mu_scm_error (FUNC_NAME, rc,
		    "Failed to convert transport port ~A",
		    scm_list_1 (port));
    }
  
  if (!scm_is_null (args))
    {
      size_t n;

      argc = scm_to_size_t (scm_length (args)) + 1;
      argv = calloc (argc + 1, sizeof (argv[0]));
      if (!argv)
	mu_scm_error (FUNC_NAME, ENOMEM, "Cannot allocate memory", SCM_BOOL_F);

      argv[0] = strdup (fltname);
      n = 1;
      for (; !scm_is_null (args); args = SCM_CDR (args))
	{
	  SCM arg = SCM_CAR (args);
	  SCM_ASSERT (scm_is_string (arg), arg, SCM_ARGn, FUNC_NAME);
	  argv[n] = scm_to_locale_string (arg);
	  n++;
	}
      argv[n] = NULL;
      scm_dynwind_unwind_handler (argv_free, argv, SCM_F_WIND_EXPLICITLY);
    }

  rc = mu_filter_create_args (&filter, instr,
			      fltname, argc, (const char**) argv,
			      filter_mode, flags);
  if (rc)
    {
      mu_scm_error (FUNC_NAME, rc,
		    "Failed to create filter ~A",
		    scm_list_1 (name));
    }

  scm_dynwind_end ();
  return mu_port_make_from_stream (filter,
				   flags == MU_STREAM_READ
				     ? SCM_RDNG
				     : SCM_WRTNG);
}
#undef FUNC_NAME

SCM_DEFINE_PUBLIC (scm_mu_encoder_port, "mu-encoder-port", 2, 0, 1,
		   (SCM port, SCM name, SCM args),
"Create encoding port using Mailutils filter @var{name} with optional arguments\n"
"@var{args}. The @var{port} argument must be a port opened either for\n"
"writing or for reading, but not both. The returned port will have the same\n"
"mode as @var{port}."
"\n\n"
"If @var{port} is open for reading, data will be read from it, passed through the\n"
"filter and returned. If it is open for writing, data written to the returned\n"
"port will be passed through filter and its output will be written to @var{port}.\n")		   
#define FUNC_NAME s_scm_mu_encoder_port
{
  return make_filter_port (port, name, args, MU_FILTER_ENCODE, FUNC_NAME);
}
#undef FUNC_NAME

SCM_DEFINE_PUBLIC (scm_mu_decoder_port, "mu-decoder-port", 2, 0, 1,
		   (SCM port, SCM name, SCM args),
"Create a decoding port using Mailutils filter @var{name} with optional arguments\n"
"@var{args}. The @var{port} argument must be a port opened either for\n"
"writing or for reading, but not both. The returned port will have the same\n"
"mode as @var{port}."
"\n\n"
"If @var{port} is open for reading, data will be read from it, passed through the\n"
"filter and returned. If it is open for writing, data written to the returned\n"
"port will be passed through filter and its output will be written to @var{port}.\n")		   
#define FUNC_NAME s_scm_mu_decoder_port
{
  return make_filter_port (port, name, args, MU_FILTER_DECODE, FUNC_NAME);
}
#undef FUNC_NAME

SCM_DEFINE_PUBLIC (scm_mu_header_decode, "mu-header-decode", 1, 1, 0,
		   (SCM hdr, SCM charset),
"Decode the header value @var{hdr}, encoded as per RFC 2047.\n"
"Optional @var{charset} defaults to @samp{utf-8}.\n")
#define FUNC_NAME s_scm_mu_header_decode
{
  char *c_hdr, *c_charset, *c_res;
  int rc;
  SCM res;
  
  SCM_ASSERT (scm_is_string (hdr), hdr, SCM_ARG1, FUNC_NAME);

  scm_dynwind_begin (0);
  if (SCM_UNBNDP (charset))
    c_charset = "utf-8";
  else
    {
      SCM_ASSERT (scm_is_string (charset), charset, SCM_ARG2, FUNC_NAME);
      c_charset = scm_to_locale_string (charset);
      scm_dynwind_free (c_charset);
    }
  c_hdr = scm_to_locale_string (hdr);
  scm_dynwind_free (c_hdr);

  rc = mu_rfc2047_decode (c_charset, c_hdr, &c_res);
  if (rc)
    mu_scm_error (FUNC_NAME, rc,
		  "Can't convert header value", SCM_BOOL_F);
  
  scm_dynwind_end ();

  res = scm_from_locale_string (c_res);
  free (c_res);

  return res;
}
#undef FUNC_NAME

SCM_DEFINE_PUBLIC (scm_mu_header_encode, "mu-header-encode", 1, 2, 0,
		   (SCM hdr, SCM encoding, SCM charset),
"Encode the string @var{hdr} as per RFC 2047.\n"
"Both @var{encoding} and @var{charset} are optional.\n"		   
"Allowed values for @var{encoding} are @samp{base64} and @samp{quoted-printable}.\n"
"Default is selected depending on number of printable characters in @var{hdr}.\n"
"Optional @var{charset} defaults to @samp{utf-8}.\n")
#define FUNC_NAME s_scm_mu_header_encode
{
  char *c_hdr, *c_charset, *c_encoding, *c_res;
  int rc;
  SCM res;
  
  SCM_ASSERT (scm_is_string (hdr), hdr, SCM_ARG1, FUNC_NAME);

  scm_dynwind_begin (0);
  
  if (SCM_UNBNDP (encoding))
    c_encoding = NULL;
  else
    {
      SCM_ASSERT (scm_is_string (encoding), encoding, SCM_ARG2, FUNC_NAME);
      c_encoding = scm_to_locale_string (encoding);
      scm_dynwind_free (c_encoding);
    }

  if (SCM_UNBNDP (charset))
    c_charset = "utf-8";
  else
    {
      SCM_ASSERT (scm_is_string (charset), charset, SCM_ARG3, FUNC_NAME);
      c_charset = scm_to_locale_string (charset);
      scm_dynwind_free (c_charset);
    }
  c_hdr = scm_to_locale_string (hdr);
  scm_dynwind_free (c_hdr);

  if (!c_encoding)
    {
      size_t len = strlen (c_hdr);
      size_t i, enc;

      enc = 0;
      for (i = 0; i < len; i++)
	if (!mu_isprint (c_hdr[i]))
	  enc++;
      c_encoding = (enc > len / 2) ? "base64" : "quoted-printable";
    }

  rc = mu_rfc2047_encode (c_charset, c_encoding, c_hdr, &c_res);
  if (rc)
    mu_scm_error (FUNC_NAME, rc,
		  "Can't encode header value", SCM_BOOL_F);
  
  scm_dynwind_end ();

  res = scm_from_locale_string (c_res);
  free (c_res);

  return res;
}
#undef FUNC_NAME

void
mu_scm_filter_init (void)
{
#include "mu_filter.x"
}