New file. Implements sieve extension test "timestamp".
Showing
1 changed file
with
139 additions
and
0 deletions
examples/timestamp.c
0 → 100644
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | ||
2 | Copyright (C) 2003 Free Software Foundation, Inc. | ||
3 | |||
4 | GNU Mailutils is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU Lesser General Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | GNU Mailutils is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU Lesser General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Lesser General Public License | ||
15 | along with GNU Mailutils; if not, write to the Free Software | ||
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ | ||
17 | |||
18 | /* Syntax: timestamp [":before"/":after"] <header-name: string> | ||
19 | <date: datestring> | ||
20 | |||
21 | The "timestamp" test compares the value of a structured date header | ||
22 | field with the given date. | ||
23 | |||
24 | If the tagged argument is ":after" and the date from the header is | ||
25 | after the specified date the result is true, otherwise, if the | ||
26 | header date is before the given date, the result is false. | ||
27 | |||
28 | If the tagged argument is ":before" and the date from the header is | ||
29 | before the specified date the result is true, otherwise, if the | ||
30 | header date is after the given date, the result is false. | ||
31 | |||
32 | If no tagged argument is supplied, :after is assumed. | ||
33 | |||
34 | Almost any date format is understood. | ||
35 | |||
36 | Example: timestamp :before "X-Expire-Timestamp" "now - 5 days" | ||
37 | |||
38 | This test will return true, if the date in X-Expire-Timestamp is | ||
39 | more than 5 days older than the current date. */ | ||
40 | |||
41 | #ifdef HAVE_CONFIG_H | ||
42 | # include <config.h> | ||
43 | #endif | ||
44 | |||
45 | #include <stdlib.h> | ||
46 | #include <mailutils/libsieve.h> | ||
47 | |||
48 | /* Handler for the timestamp test */ | ||
49 | static int | ||
50 | timestamp_test (sieve_machine_t mach, list_t args, list_t tags) | ||
51 | { | ||
52 | sieve_value_t *h, *v; | ||
53 | header_t hdr; | ||
54 | char *val; | ||
55 | time_t now = time (NULL); | ||
56 | time_t tlimit, tval; | ||
57 | int rc; | ||
58 | |||
59 | if (sieve_get_debug_level (mach) & MU_SIEVE_DEBUG_TRACE) | ||
60 | sieve_debug (mach, "TIMESTAMP\n"); | ||
61 | |||
62 | /* Retrieve required arguments: */ | ||
63 | /* First argument: header name */ | ||
64 | h = sieve_value_get (args, 0); | ||
65 | if (!h) | ||
66 | { | ||
67 | sieve_error (mach, "timestamp: can't get argument 1"); | ||
68 | sieve_abort (mach); | ||
69 | } | ||
70 | /* Second argument: date displacement */ | ||
71 | v = sieve_value_get (args, 1); | ||
72 | if (!v) | ||
73 | { | ||
74 | sieve_error (mach, "timestamp: can't get argument 2"); | ||
75 | sieve_abort (mach); | ||
76 | } | ||
77 | |||
78 | if (mu_parse_date (v->v.string, &tlimit, &now)) | ||
79 | { | ||
80 | sieve_error (mach, "timestamp: can't parse date specification (%s)", | ||
81 | v->v.string); | ||
82 | sieve_abort (mach); | ||
83 | } | ||
84 | |||
85 | rc = message_get_header (sieve_get_message (mach), &hdr); | ||
86 | if (rc) | ||
87 | { | ||
88 | sieve_error (mach, "message_get_header: %s", mu_strerror (rc)); | ||
89 | sieve_abort (mach); | ||
90 | } | ||
91 | |||
92 | if (header_aget_value (hdr, h->v.string, &val)) | ||
93 | return 0; | ||
94 | |||
95 | if (mu_parse_date (val, &tval, &now)) | ||
96 | { | ||
97 | sieve_error (mach, | ||
98 | "timestamp: can't parse header date specification (%s)", | ||
99 | val); | ||
100 | free (val); | ||
101 | sieve_abort (mach); | ||
102 | } | ||
103 | free (val); | ||
104 | |||
105 | rc = tval > tlimit; | ||
106 | |||
107 | if (sieve_tag_lookup (tags, "before", NULL)) | ||
108 | rc = !rc; | ||
109 | |||
110 | return rc; | ||
111 | } | ||
112 | |||
113 | /* Required arguments: */ | ||
114 | static sieve_data_type timestamp_req_args[] = { | ||
115 | SVT_STRING, | ||
116 | SVT_STRING, | ||
117 | SVT_VOID | ||
118 | }; | ||
119 | |||
120 | /* Tagged arguments: */ | ||
121 | static sieve_tag_def_t timestamp_tags[] = { | ||
122 | { "after", SVT_VOID }, | ||
123 | { "before", SVT_VOID }, | ||
124 | { NULL } | ||
125 | }; | ||
126 | |||
127 | static sieve_tag_group_t timestamp_tag_groups[] = { | ||
128 | { timestamp_tags, NULL }, | ||
129 | { NULL } | ||
130 | }; | ||
131 | |||
132 | /* Initialization function. It is the only function exported from this | ||
133 | module. */ | ||
134 | int | ||
135 | SIEVE_EXPORT(timestamp,init) (sieve_machine_t mach) | ||
136 | { | ||
137 | return sieve_register_test (mach, "timestamp", timestamp_test, | ||
138 | timestamp_req_args, timestamp_tag_groups, 1); | ||
139 | } |
-
Please register or sign in to post a comment