summaryrefslogtreecommitdiff
path: root/libsieve/extensions/timestamp.c
blob: 316cce38ac31afd45085ad2f609d0b57607a8868 (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
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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2003, 2005 Free Software Foundation, Inc.

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

   You should have received a copy of the GNU Lesser General Public
   License along with GNU Mailutils; if not, write to the Free
   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301 USA */

/*  Syntax:   timestamp [":before"/":after"] <header-name: string>
              <date: datestring>

    The "timestamp" test compares the value of a structured date header
    field with the given date.

    If the tagged argument is ":after" and the date from the header is
    after the specified date the result is true, otherwise, if the
    header date is before the given date, the result is false.

    If the tagged argument is ":before" and the date from the header is
    before the specified date the result is true, otherwise, if the
    header date is after the given date, the result is false.

    If no tagged argument is supplied, :after is assumed.

    Almost any date format is understood.

    Example:  timestamp :before "X-Expire-Timestamp" "now - 5 days"

    This test will return true, if the date in X-Expire-Timestamp is
    more than 5 days older than the current date. */

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

#include <stdlib.h>
#include <mailutils/libsieve.h>

/* Handler for the timestamp test */
static int
timestamp_test (sieve_machine_t mach, list_t args, list_t tags)
{
  sieve_value_t *h, *v;
  header_t hdr;
  char *val;
  time_t now = time (NULL);
  time_t tlimit, tval;
  int rc;
  
  if (sieve_get_debug_level (mach) & MU_SIEVE_DEBUG_TRACE)
    {
      sieve_locus_t locus;
      sieve_get_locus (mach, &locus);
      sieve_debug (mach, "%s:%lu: TIMESTAMP\n",
		   locus.source_file,
		   (unsigned long) locus.source_line);
    }

  /* Retrieve required arguments: */
  /* First argument: header name */
  h = sieve_value_get (args, 0);
  if (!h)
    {
      sieve_arg_error (mach, 1);
      sieve_abort (mach);
    }
  /* Second argument: date displacement */
  v = sieve_value_get (args, 1);
  if (!v)
    {
      sieve_arg_error (mach, 2);
      sieve_abort (mach);
    }

  if (mu_parse_date (v->v.string, &tlimit, &now))
    {
      sieve_error (mach, _("cannot parse date specification (%s)"),
		   v->v.string);
      sieve_abort (mach);
    }

  rc = message_get_header (sieve_get_message (mach), &hdr);
  if (rc)
    {
      sieve_error (mach, "message_get_header: %s", mu_strerror (rc));
      sieve_abort (mach);
    }
  
  if (header_aget_value (hdr, h->v.string, &val))
    return 0;

  if (mu_parse_date (val, &tval, &now))
    {
      sieve_error (mach,
		   "cannot parse header date specification (%s)",
		   val);
      free (val);
      sieve_abort (mach);
    }
  free (val);

  rc = tval > tlimit;
    
  if (sieve_tag_lookup (tags, "before", NULL))
    rc = !rc;  

  return rc;
}

/* Required arguments: */
static sieve_data_type timestamp_req_args[] = {
  SVT_STRING,
  SVT_STRING,
  SVT_VOID
};

/* Tagged arguments: */
static sieve_tag_def_t timestamp_tags[] = {
  { "after", SVT_VOID },
  { "before", SVT_VOID },
  { NULL }
};

static sieve_tag_group_t timestamp_tag_groups[] = {
  { timestamp_tags, NULL },
  { NULL }
};

/* Initialization function. It is the only function exported from this
   module. */
int
SIEVE_EXPORT(timestamp,init) (sieve_machine_t mach)
{
  return sieve_register_test (mach, "timestamp", timestamp_test,
                              timestamp_req_args, timestamp_tag_groups, 1);
}

Return to:

Send suggestions and report system problems to the System administrator.