Blame view

mu/filter.c 4.1 KB
Sergey Poznyakoff authored
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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2010 Free Software Foundation, Inc.

   GNU Mailutils is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GNU Mailutils 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */

#if defined(HAVE_CONFIG_H)
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <mailutils/mailutils.h>
#include "argp.h"
#include "mu.h"

26 27 28
static char filter_doc[] = N_("mu filter - apply a chain of filters to the input");
char filter_docstring[] = N_("apply a chain of filters to the input");
static char filter_args_doc[] = N_("[~]NAME [ARGS] [+ [~]NAME [ARGS]...]");
Sergey Poznyakoff authored
29 30 31 32 33

static struct argp_option filter_options[] = {
  { "encode", 'e', NULL, 0, N_("encode the input (default)") },
  { "decode", 'd', NULL, 0, N_("decode the input") },
  { "newline", 'n', NULL, 0, N_("print additional newline") },
34
  { "list", 'L', NULL, 0, N_("list supported filters") },
Sergey Poznyakoff authored
35 36 37 38 39
  { NULL }
};

static int filter_mode = MU_FILTER_ENCODE;
static int newline_option = 0;
40
static int list_option;
Sergey Poznyakoff authored
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

static error_t
filter_parse_opt (int key, char *arg, struct argp_state *state)
{
  switch (key)
    {
    case 'e':
      filter_mode = MU_FILTER_ENCODE;
      break;

    case 'd':
      filter_mode = MU_FILTER_DECODE;
      break;

    case 'n':
      newline_option = 1;
      break;

59 60 61
    case 'L':
      list_option = 1;
      break;
Sergey Poznyakoff authored
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
      
    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

static struct argp filter_argp = {
  filter_options,
  filter_parse_opt,
  filter_args_doc,
  filter_doc,
  NULL,
  NULL,
  NULL
};

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
static int
filter_printer (void *item, void *data)
{
  mu_filter_record_t rec = item;
  printf ("%s\n", rec->name);
  return 0;
}

static int
list_filters ()
{
  mu_list_t list;
  int rc = mu_filter_get_list (&list);

  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_filter_get_list", NULL, rc);
      return 1;
    }
  return mu_list_do (list, filter_printer, NULL);
}

101 102 103 104 105 106 107 108 109 110
static int
negate_filter_mode (int mode)
{
  if (mode == MU_FILTER_DECODE)
    return MU_FILTER_ENCODE;
  else if (mode == MU_FILTER_ENCODE)
    return MU_FILTER_DECODE;
  abort ();
}

Sergey Poznyakoff authored
111 112 113 114
int
mutool_filter (int argc, char **argv)
{
  int rc, index;
115
  mu_stream_t flt, prev_stream;
Sergey Poznyakoff authored
116
  const char *fltname;
117
  int mode;
Sergey Poznyakoff authored
118 119 120 121 122 123 124
  
  if (argp_parse (&filter_argp, argc, argv, ARGP_IN_ORDER, &index, NULL))
    return 1;

  argc -= index;
  argv += index;

125 126 127 128 129 130 131 132 133 134 135
  if (list_option)
    {
      if (argc)
	{
	  mu_error (_("excess arguments"));
	  return 1;
	}
      return list_filters ();
    }
  
  if (argc == 0)
Sergey Poznyakoff authored
136 137 138 139 140
    {
      mu_error (_("what filter do you want?"));
      return 1;
    }

141 142
  prev_stream = mu_strin;
  do
Sergey Poznyakoff authored
143
    {
144 145 146 147 148 149 150 151 152 153
      int i;
      
      fltname = argv[0];
      if (fltname[0] == '~')
	{
	  mode = negate_filter_mode (filter_mode);
	  fltname++;
	}
      else
	mode = filter_mode;
Sergey Poznyakoff authored
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
      for (i = 1; i < argc; i++)
	if (strcmp (argv[i], "+") == 0)
	  break;
      
      rc = mu_filter_create_args (&flt, prev_stream, fltname,
				  i, (const char **)argv,
				  mode, MU_STREAM_READ);
      mu_stream_unref (prev_stream);
      if (rc)
	{
	  mu_error (_("cannot open filter stream: %s"), mu_strerror (rc));
	  return 1;
	}
      prev_stream = flt;
      argc -= i;
      argv += i;
      if (argc)
	{
	  argc--;
	  argv++;
	}
    }
  while (argc);
  
179
  rc = mu_stream_copy (mu_strout, flt, 0, NULL);
Sergey Poznyakoff authored
180 181 182 183 184 185 186 187

  if (rc)
    {
      mu_error ("%s", mu_strerror (rc));
      return 1;
    }

  if (newline_option)
188
    mu_stream_write (mu_strout, "\n", 1, NULL);
Sergey Poznyakoff authored
189

190 191
  mu_stream_destroy (&flt);
  mu_stream_flush (mu_strout);
Sergey Poznyakoff authored
192 193 194
  
  return 0;
}
195 196 197 198 199 200 201

/*
  MU Setup: filter
  mu-handler: mutool_filter
  mu-docstring: filter_docstring
  End MU Setup:
*/