attribute.c
9.23 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2004, 2005 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 */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <mailutils/errno.h>
#include <attribute0.h>
static int flags_to_string (int, char *, size_t, size_t *);
int
attribute_create (attribute_t *pattr, void *owner)
{
attribute_t attr;
if (pattr == NULL)
return MU_ERR_OUT_PTR_NULL;
attr = calloc (1, sizeof(*attr));
if (attr == NULL)
return ENOMEM;
attr->owner = owner;
*pattr = attr;
return 0;
}
void
attribute_destroy (attribute_t *pattr, void *owner)
{
if (pattr && *pattr)
{
attribute_t attr = *pattr;
if (attr->owner == owner)
free (*pattr);
/* Loose the link */
*pattr = NULL;
}
}
void *
attribute_get_owner (attribute_t attr)
{
return (attr) ? attr->owner : NULL;
}
int
attribute_is_modified (attribute_t attr)
{
return (attr) ? attr->flags & MU_ATTRIBUTE_MODIFIED : 0;
}
int
attribute_clear_modified (attribute_t attr)
{
if (attr)
attr->flags &= ~MU_ATTRIBUTE_MODIFIED;
return 0;
}
int
attribute_set_modified (attribute_t attr)
{
if (attr)
attr->flags |= MU_ATTRIBUTE_MODIFIED;
return 0;
}
int
attribute_get_flags (attribute_t attr, int *pflags)
{
if (attr == NULL)
return EINVAL;
if (pflags == NULL)
return MU_ERR_OUT_PTR_NULL;
if (attr->_get_flags)
return attr->_get_flags (attr, pflags);
*pflags = attr->flags;
return 0;
}
int
attribute_set_flags (attribute_t attr, int flags)
{
int status = 0;
int oflags = 0;
if (attr == NULL)
return EINVAL;
/* If the required bits are already set, do not modify anything */
attribute_get_flags (attr, &oflags);
if ((oflags & flags) == flags)
return 0;
if (attr->_set_flags)
status = attr->_set_flags (attr, flags);
else
attr->flags |= flags;
if (status == 0)
attribute_set_modified (attr);
return 0;
}
int
attribute_unset_flags (attribute_t attr, int flags)
{
int status = 0;
int oflags = 0;
if (attr == NULL)
return EINVAL;
/* If the required bits are already cleared, do not modify anything */
attribute_get_flags (attr, &oflags);
if ((oflags & flags) == 0)
return 0;
if (attr->_unset_flags)
status = attr->_unset_flags (attr, flags);
else
attr->flags &= ~flags;
if (status == 0)
attribute_set_modified (attr);
return 0;
}
int
attribute_set_get_flags (attribute_t attr, int (*_get_flags)
(attribute_t, int *), void *owner)
{
if (attr == NULL)
return EINVAL;
if (attr->owner != owner)
return EACCES;
attr->_get_flags = _get_flags;
return 0;
}
int
attribute_set_set_flags (attribute_t attr, int (*_set_flags)
(attribute_t, int), void *owner)
{
if (attr == NULL)
return EINVAL;
if (attr->owner != owner)
return EACCES;
attr->_set_flags = _set_flags;
return 0;
}
int
attribute_set_unset_flags (attribute_t attr, int (*_unset_flags)
(attribute_t, int), void *owner)
{
if (attr == NULL)
return EINVAL;
if (attr->owner != owner)
return EACCES;
attr->_unset_flags = _unset_flags;
return 0;
}
/* We add support for "USER" flag, it is a way for external objects
Not being the owner to add custom flags. */
int
attribute_set_userflag (attribute_t attr, int flag)
{
if (attr == NULL)
return EINVAL;
attr->user_flags |= flag;
return 0;
}
int
attribute_set_seen (attribute_t attr)
{
return attribute_set_flags (attr, MU_ATTRIBUTE_SEEN);
}
int
attribute_set_answered (attribute_t attr)
{
return attribute_set_flags (attr, MU_ATTRIBUTE_ANSWERED);
}
int
attribute_set_flagged (attribute_t attr)
{
return attribute_set_flags (attr, MU_ATTRIBUTE_FLAGGED);
}
int
attribute_set_read (attribute_t attr)
{
return attribute_set_flags (attr, MU_ATTRIBUTE_READ);
}
int
attribute_set_deleted (attribute_t attr)
{
return attribute_set_flags (attr, MU_ATTRIBUTE_DELETED);
}
int
attribute_set_draft (attribute_t attr)
{
return attribute_set_flags (attr, MU_ATTRIBUTE_DRAFT);
}
int
attribute_set_recent (attribute_t attr)
{
int status = attribute_unset_flags (attr, MU_ATTRIBUTE_READ);
if (status == 0)
status = attribute_unset_flags (attr, MU_ATTRIBUTE_SEEN);
return status;
}
int
attribute_is_userflag (attribute_t attr, int flag)
{
if (attr == NULL)
return 0;
return attr->user_flags & flag;
}
int
attribute_is_seen (attribute_t attr)
{
int flags = 0;
if (attribute_get_flags (attr, &flags) == 0)
return flags & MU_ATTRIBUTE_SEEN;
return 0;
}
int
attribute_is_answered (attribute_t attr)
{
int flags = 0;
if (attribute_get_flags (attr, &flags) == 0)
return flags & MU_ATTRIBUTE_ANSWERED;
return 0;
}
int
attribute_is_flagged (attribute_t attr)
{
int flags = 0;
if (attribute_get_flags (attr, &flags) == 0)
return flags & MU_ATTRIBUTE_FLAGGED;
return 0;
}
int
attribute_is_read (attribute_t attr)
{
int flags = 0;
if (attribute_get_flags (attr, &flags) == 0)
return flags & MU_ATTRIBUTE_READ;
return 0;
}
int
attribute_is_deleted (attribute_t attr)
{
int flags = 0;
if (attribute_get_flags (attr, &flags) == 0)
return flags & MU_ATTRIBUTE_DELETED;
return 0;
}
int
attribute_is_draft (attribute_t attr)
{
int flags = 0;
if (attribute_get_flags (attr, &flags) == 0)
return flags & MU_ATTRIBUTE_DRAFT;
return 0;
}
int
attribute_is_recent (attribute_t attr)
{
int flags = 0;
if (attribute_get_flags (attr, &flags) == 0)
return MU_ATTRIBUTE_IS_UNSEEN(flags);
return 0;
}
int
attribute_unset_userflag (attribute_t attr, int flag)
{
if (attr == NULL)
return 0;
attr->user_flags &= ~flag;
return 0;
}
int
attribute_unset_seen (attribute_t attr)
{
return attribute_unset_flags (attr, MU_ATTRIBUTE_SEEN);
}
int
attribute_unset_answered (attribute_t attr)
{
return attribute_unset_flags (attr, MU_ATTRIBUTE_ANSWERED);
}
int
attribute_unset_flagged (attribute_t attr)
{
return attribute_unset_flags (attr, MU_ATTRIBUTE_FLAGGED);
}
int
attribute_unset_read (attribute_t attr)
{
return attribute_unset_flags (attr, MU_ATTRIBUTE_READ);
}
int
attribute_unset_deleted (attribute_t attr)
{
return attribute_unset_flags (attr, MU_ATTRIBUTE_DELETED);
}
int
attribute_unset_draft (attribute_t attr)
{
return attribute_unset_flags (attr, MU_ATTRIBUTE_DRAFT);
}
int
attribute_unset_recent (attribute_t attr)
{
return attribute_unset_flags (attr, MU_ATTRIBUTE_SEEN);
}
int
attribute_is_equal (attribute_t attr, attribute_t attr2)
{
int flags2 = 0, flags = 0;
attribute_get_flags (attr, &flags);
attribute_get_flags (attr2, &flags2);
return flags == flags;
}
/* Miscellaneous. */
int
attribute_copy (attribute_t dest, attribute_t src)
{
if (dest == NULL || src == NULL)
return EINVAL;
/* Can not be a deep copy. */
/* memcpy (dest, src, sizeof (*dest)); */
dest->flags = src->flags;
return 0;
}
int
string_to_flags (const char *buffer, int *pflags)
{
const char *sep;
if (pflags == NULL)
return EINVAL;
/* Set the attribute */
if (strncasecmp (buffer, "Status:", 7) == 0)
{
sep = strchr(buffer, ':'); /* pass the ':' */
sep++;
}
else
sep = buffer;
while (*sep)
{
if (strchr (sep, 'R') != NULL || strchr (sep, 'r') != NULL)
*pflags |= MU_ATTRIBUTE_READ;
if (strchr (sep, 'O') != NULL || strchr (sep, 'o') != NULL)
*pflags |= MU_ATTRIBUTE_SEEN;
if (strchr (sep, 'A') != NULL || strchr (sep, 'a') != NULL)
*pflags |= MU_ATTRIBUTE_ANSWERED;
if (strchr (sep, 'F') != NULL || strchr (sep, 'f') != NULL)
*pflags |= MU_ATTRIBUTE_FLAGGED;
sep++;
}
return 0;
}
int
attribute_to_string (attribute_t attr, char *buffer, size_t len, size_t *pn)
{
int flags = 0;;
attribute_get_flags (attr, &flags);
return flags_to_string (flags, buffer, len, pn);
}
static int
flags_to_string (int flags, char *buffer, size_t len, size_t *pn)
{
char status[32];
char a[8];
size_t i;
*status = *a = '\0';
if (flags & MU_ATTRIBUTE_SEEN)
strcat (a, "O");
if (flags & MU_ATTRIBUTE_ANSWERED)
strcat (a, "A");
if (flags & MU_ATTRIBUTE_FLAGGED)
strcat (a, "F");
if (flags & MU_ATTRIBUTE_READ)
strcat (a, "R");
if (flags & MU_ATTRIBUTE_DELETED)
strcat (a, "d");
if (*a != '\0')
{
strcpy (status, "Status: ");
strcat (status, a);
strcat (status, "\n");
}
i = strlen (status);
if (buffer && len != 0)
{
strncpy (buffer, status, len - 1);
buffer[len - 1] = '\0';
i = strlen (buffer);
}
if (pn)
*pn = i;
return 0;
}