mbxitr.c
4.83 KB
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
147
148
149
150
151
152
153
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010-2012, 2014-2017 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 3 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, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <mailutils/debug.h>
#include <mailutils/errno.h>
#include <mailutils/error.h>
#include <mailutils/iterator.h>
#include <mailutils/message.h>
#include <mailutils/attribute.h>
#include <mailutils/sys/mailbox.h>
struct mailbox_iterator
{
mu_mailbox_t mbx;
size_t idx;
int backwards;
};
static int
mbx_first (void *owner)
{
struct mailbox_iterator *itr = owner;
if (itr->backwards)
return mu_mailbox_messages_count (itr->mbx, &itr->idx);
else
itr->idx = 1;
return 0;
}
static int
mbx_next (void *owner)
{
struct mailbox_iterator *itr = owner;
if (itr->backwards)
{
if (itr->idx)
--itr->idx;
}
else
itr->idx++;
return 0;
}
static int
mbx_getitem (void *owner, void **pret, const void **pkey)
{
struct mailbox_iterator *itr = owner;
size_t count;
int rc;
rc = mu_mailbox_messages_count (itr->mbx, &count);
if (rc)
return rc;
if (itr->idx > count)
return MU_ERR_NOENT;
rc = mu_mailbox_get_message (itr->mbx, itr->idx, (mu_message_t*)pret);
if (rc == 0 && pkey)
*pkey = NULL; /* FIXME: Perhaps return UIDL or other unique id? */
return rc;
}
static int
mbx_finished_p (void *owner)
{
struct mailbox_iterator *itr = owner;
if (itr->backwards)
return itr->idx == 0;
else
{
size_t count;
if (mu_mailbox_messages_count (itr->mbx, &count))
return 1;
return itr->idx > count;
}
}
static int
mbx_destroy (mu_iterator_t iterator, void *data)
{
struct mailbox_iterator *itr = data;
mu_iterator_detach (&itr->mbx->iterator, iterator);
free (data);
return 0;
}
static int
mbx_delitem (void *owner, void *item)
{
void *ptr;
if (mbx_getitem (owner, &ptr, NULL))
return 0;
return ptr == item ? MU_ITR_DELITEM_NEXT : MU_ITR_DELITEM_NOTHING;
/* FIXME: is it ok? */
}
static int
mbx_data_dup (void **ptr, void *owner)
{
struct mailbox_iterator *itr = owner;
*ptr = malloc (sizeof (struct mailbox_iterator));
if (*ptr == NULL)
return ENOMEM;
memcpy (*ptr, owner, sizeof (struct mailbox_iterator));
mu_iterator_attach (&itr->mbx->iterator, *ptr);
return 0;
}
static int
mbx_itrctl (void *owner, enum mu_itrctl_req req, void *arg)
{
int rc;
struct mailbox_iterator *itr = owner;
mu_message_t msg;
mu_attribute_t attr;
if (itr->idx == 0)
return MU_ERR_NOENT;
switch (req)
{
case mu_itrctl_tell:
*(size_t*)arg = itr->idx;
break;
case mu_itrctl_delete:
rc = mu_mailbox_get_message (itr->mbx, itr->idx, &msg);
if (rc)
return rc;
rc = mu_message_get_attribute (msg, &attr);
if (rc)
return rc;
rc = mu_attribute_set_deleted (attr);
if (rc)
return rc;
break;
case mu_itrctl_qry_direction:
if (!arg)
return EINVAL;
else
*(int*)arg = itr->backwards;
break;
case mu_itrctl_set_direction:
if (!arg)
return EINVAL;
else
itr->backwards = !!*(int*)arg;
break;
case mu_itrctl_count:
if (!arg)
return EINVAL;
return mu_mailbox_messages_count (itr->mbx, arg);
default:
return ENOSYS;
}
return 0;
}
int
mu_mailbox_get_iterator (mu_mailbox_t mbx, mu_iterator_t *piterator)
{
mu_iterator_t iterator;
int status;
struct mailbox_iterator *itr;
if (!mbx)
return EINVAL;
itr = calloc (1, sizeof *itr);
if (!itr)
return ENOMEM;
itr->mbx = mbx;
itr->idx = 1;
itr->backwards = 0;
status = mu_iterator_create (&iterator, itr);
if (status)
{
free (itr);
return status;
}
mu_iterator_set_first (iterator, mbx_first);
mu_iterator_set_next (iterator, mbx_next);
mu_iterator_set_getitem (iterator, mbx_getitem);
mu_iterator_set_finished_p (iterator, mbx_finished_p);
mu_iterator_set_delitem (iterator, mbx_delitem);
mu_iterator_set_destroy (iterator, mbx_destroy);
mu_iterator_set_dup (iterator, mbx_data_dup);
mu_iterator_set_itrctl (iterator, mbx_itrctl);
mu_iterator_attach (&mbx->iterator, iterator);
*piterator = iterator;
return 0;
}