Added extensive comments.
Showing
1 changed file
with
29 additions
and
8 deletions
... | @@ -40,12 +40,20 @@ | ... | @@ -40,12 +40,20 @@ |
40 | #include <stdlib.h> | 40 | #include <stdlib.h> |
41 | #include <mailutils/libsieve.h> | 41 | #include <mailutils/libsieve.h> |
42 | 42 | ||
43 | struct val_ctr { | 43 | struct val_ctr { /* Data passed to the counter function */ |
44 | header_t hdr; | 44 | header_t hdr; /* Headers of the current message */ |
45 | size_t limit; | 45 | size_t limit; /* Limit for the number of addresses */ |
46 | size_t count; | 46 | size_t count; /* Number of addresses counted so far */ |
47 | }; | 47 | }; |
48 | 48 | ||
49 | /* Count addresses in a single header value. | ||
50 | |||
51 | Input: | ||
52 | ITEM is the name of the header to scan. | ||
53 | DATA is a pointer to the val_ctr structure */ | ||
54 | Return value: | ||
55 | non-zero if the limit on the number of addresses has been reached. */ | ||
56 | |||
49 | static int | 57 | static int |
50 | _count_items (void *item, void *data) | 58 | _count_items (void *item, void *data) |
51 | { | 59 | { |
... | @@ -66,7 +74,8 @@ _count_items (void *item, void *data) | ... | @@ -66,7 +74,8 @@ _count_items (void *item, void *data) |
66 | free (val); | 74 | free (val); |
67 | return vp->count >= vp->limit; | 75 | return vp->count >= vp->limit; |
68 | } | 76 | } |
69 | 77 | ||
78 | /* Handler for the numaddr test */ | ||
70 | static int | 79 | static int |
71 | numaddr_test (sieve_machine_t mach, list_t args, list_t tags) | 80 | numaddr_test (sieve_machine_t mach, list_t args, list_t tags) |
72 | { | 81 | { |
... | @@ -77,12 +86,15 @@ numaddr_test (sieve_machine_t mach, list_t args, list_t tags) | ... | @@ -77,12 +86,15 @@ numaddr_test (sieve_machine_t mach, list_t args, list_t tags) |
77 | if (sieve_get_debug_level (mach) & MU_SIEVE_DEBUG_TRACE) | 86 | if (sieve_get_debug_level (mach) & MU_SIEVE_DEBUG_TRACE) |
78 | sieve_debug (mach, "NUMADDR\n"); | 87 | sieve_debug (mach, "NUMADDR\n"); |
79 | 88 | ||
89 | /* Retrieve required arguments: */ | ||
90 | /* First argument: list of header names */ | ||
80 | h = sieve_value_get (args, 0); | 91 | h = sieve_value_get (args, 0); |
81 | if (!h) | 92 | if (!h) |
82 | { | 93 | { |
83 | sieve_error (mach, "numaddr: can't get argument 1"); | 94 | sieve_error (mach, "numaddr: can't get argument 1"); |
84 | sieve_abort (mach); | 95 | sieve_abort (mach); |
85 | } | 96 | } |
97 | /* Second argument: Limit on the number of addresses */ | ||
86 | v = sieve_value_get (args, 1); | 98 | v = sieve_value_get (args, 1); |
87 | if (!v) | 99 | if (!v) |
88 | { | 100 | { |
... | @@ -90,23 +102,31 @@ numaddr_test (sieve_machine_t mach, list_t args, list_t tags) | ... | @@ -90,23 +102,31 @@ numaddr_test (sieve_machine_t mach, list_t args, list_t tags) |
90 | sieve_abort (mach); | 102 | sieve_abort (mach); |
91 | } | 103 | } |
92 | 104 | ||
105 | /* Fill in the val_ctr structure */ | ||
93 | message_get_header (sieve_get_message (mach), &vc.hdr); | 106 | message_get_header (sieve_get_message (mach), &vc.hdr); |
94 | vc.count = 0; | 107 | vc.count = 0; |
95 | vc.limit = v->v.number; | 108 | vc.limit = v->v.number; |
96 | 109 | ||
110 | /* Count the addresses */ | ||
97 | rc = sieve_vlist_do (h, _count_items, &vc); | 111 | rc = sieve_vlist_do (h, _count_items, &vc); |
98 | 112 | ||
113 | /* Here rc >= 1 iff the counted number of addresses is greater or equal | ||
114 | to vc.limit. If `:under' tag was given we reverse the return value */ | ||
99 | if (sieve_tag_lookup (tags, "under", NULL)) | 115 | if (sieve_tag_lookup (tags, "under", NULL)) |
100 | rc = !rc; | 116 | rc = !rc; |
101 | return rc; | 117 | return rc; |
102 | } | 118 | } |
103 | 119 | ||
120 | /* Syntactic definitions for the numaddr test */ | ||
121 | |||
122 | /* Required arguments: */ | ||
104 | static sieve_data_type numaddr_req_args[] = { | 123 | static sieve_data_type numaddr_req_args[] = { |
105 | SVT_STRING_LIST, | 124 | SVT_STRING_LIST, |
106 | SVT_NUMBER, | 125 | SVT_NUMBER, |
107 | SVT_VOID | 126 | SVT_VOID |
108 | }; | 127 | }; |
109 | 128 | ||
129 | /* Tagged arguments: */ | ||
110 | static sieve_tag_def_t numaddr_tags[] = { | 130 | static sieve_tag_def_t numaddr_tags[] = { |
111 | { "over", SVT_VOID }, | 131 | { "over", SVT_VOID }, |
112 | { "under", SVT_VOID }, | 132 | { "under", SVT_VOID }, |
... | @@ -118,10 +138,11 @@ static sieve_tag_group_t numaddr_tag_groups[] = { | ... | @@ -118,10 +138,11 @@ static sieve_tag_group_t numaddr_tag_groups[] = { |
118 | { NULL } | 138 | { NULL } |
119 | }; | 139 | }; |
120 | 140 | ||
121 | 141 | /* Initialization function. It is the only function exported from this | |
142 | module. */ | ||
122 | int | 143 | int |
123 | SIEVE_EXPORT(numaddr,init) (sieve_machine_t mach) | 144 | SIEVE_EXPORT(numaddr,init) (sieve_machine_t mach) |
124 | { | 145 | { |
125 | return sieve_register_test (mach, "numaddr", numaddr_test, | 146 | return sieve_register_test (mach, "numaddr", numaddr_test, |
126 | numaddr_req_args, numaddr_tag_groups, 1); | 147 | numaddr_req_args, numaddr_tag_groups, 1); |
127 | } | 148 | } | ... | ... |
-
Please register or sign in to post a comment