summaryrefslogtreecommitdiff
path: root/mailbox/munre.c
blob: 60a76f2fe9594828db03a483b633bf50ed9aa895 (plain)
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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001, 2002, 2003 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 2 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, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301 USA */

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

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

#include <mailutils/error.h>
#include <mailutils/errno.h>

#include <regex.h>

static regex_t *re_prefix;

int
mu_unre_set_regex (char *str, int caseflag, char **errp)
{
  int rc;
  int flags = REG_EXTENDED;

  if (errp)
    *errp = NULL;

  if (!str)
    str = "^re: *";
  if (re_prefix)
    regfree (re_prefix);
  else
    {
      re_prefix = malloc (sizeof (*re_prefix));
      if (!re_prefix)
	return ENOMEM;
    }
  if (!caseflag)
    flags |= REG_ICASE;
  rc = regcomp (re_prefix, str, flags);
  if (rc)
    {
      if (errp)
	{
	  size_t s = regerror (rc, re_prefix, NULL, 0);
	  s++;
	  *errp = malloc (s);
	  if (*errp)
	    regerror (rc, re_prefix, *errp, s);
	}
      regfree (re_prefix);
      free (re_prefix);
      return MU_ERR_FAILURE;
    }
  return 0;
}

int
mu_unre_subject (const char *subject, const char **new_subject)
{
  int rc;
  regmatch_t rm;

  if (!subject)
    return EINVAL;
  
  if (!re_prefix)
    {
      rc = mu_unre_set_regex (NULL, 0, NULL);
      if (rc)
	return rc;
    }

  rc = regexec (re_prefix, subject, 1, &rm, 0);
  if (rc == 0 && rm.rm_eo != -1 && new_subject)
    *new_subject = subject + rm.rm_eo;
  return rc;
}

Return to:

Send suggestions and report system problems to the System administrator.