envelope.c
4.82 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2004, 2005, 2007 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, 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 <errno.h>
#include <stdlib.h>
#include <string.h>
#include <mailutils/errno.h>
#include <mailutils/mutil.h>
#include <envelope0.h>
int
mu_envelope_create (mu_envelope_t *penvelope, void *owner)
{
mu_envelope_t envelope;
if (penvelope == NULL)
return MU_ERR_OUT_PTR_NULL;
envelope = calloc (1, sizeof (*envelope));
if (envelope == NULL)
return ENOMEM;
envelope->owner = owner;
*penvelope = envelope;
return 0;
}
void
mu_envelope_destroy (mu_envelope_t *penvelope, void *owner)
{
if (penvelope && *penvelope)
{
mu_envelope_t envelope = *penvelope;
if (envelope->owner == owner)
{
if (envelope->_destroy)
envelope->_destroy (envelope);
free (envelope->date);
free (envelope->sender);
free (envelope);
}
*penvelope = NULL;
}
}
void *
mu_envelope_get_owner (mu_envelope_t envelope)
{
return (envelope) ? envelope->owner : NULL;
}
int
mu_envelope_set_sender (mu_envelope_t envelope,
int (*_sender) (mu_envelope_t, char *, size_t,
size_t*),
void *owner)
{
if (envelope == NULL)
return EINVAL;
if (envelope->owner != owner)
return EACCES;
envelope->_get_sender = _sender;
return 0;
}
int
mu_envelope_set_date (mu_envelope_t envelope,
int (*_date) (mu_envelope_t, char *, size_t , size_t *),
void *owner)
{
if (envelope == NULL)
return EINVAL;
if (envelope->owner != owner)
return EACCES;
envelope->_get_date = _date;
return 0;
}
/* General accessors: */
#define AC2(a,b) a ## b
#define AC4(a,b,c,d) a ## b ## c ## d
#define ACCESSOR(action,field) AC4(mu_envelope_,action,_,field)
#define DECL_SGET(field) \
int \
ACCESSOR(sget,field) (mu_envelope_t env, char const **sptr) \
{ \
if (env == NULL) \
return EINVAL; \
if (!env->field) \
{ \
if (env->AC2(_get_,field)) \
{ \
size_t n; \
char *buf; \
int status; \
\
status = env->AC2(_get_,field) (env, NULL, 0, &n); \
if (status) \
return status; \
\
buf = malloc (n + 1); \
if (!buf) \
return ENOMEM; \
\
status = env->AC2(_get_,field) (env, buf, n + 1, NULL); \
if (status) \
return status; \
\
env->field = buf; \
} \
else \
return MU_ERR_NOENT; \
} \
*sptr = env->field; \
return 0; \
}
#define DECL_GET(field) \
int \
ACCESSOR(get,field) (mu_envelope_t env, char *buf, size_t len, size_t *n) \
{ \
size_t i; \
const char *str; \
int status = ACCESSOR(sget, field) (env, &str); \
\
if (status) \
return status; \
\
i = mu_cpystr (buf, str, len); \
if (n) \
*n = i; \
return 0; \
}
#define DECL_AGET(field) \
int \
ACCESSOR(aget, field) (mu_envelope_t env, char **buf) \
{ \
const char *str; \
int status = ACCESSOR(sget, field) (env, &str); \
\
if (status) \
return status; \
\
if (str) \
{ \
*buf = strdup (str); \
if (!*buf) \
status = ENOMEM; \
} \
else \
*buf = NULL; \
return status; \
}
#define DECL_ACCESSORS(field) \
DECL_SGET(field) \
DECL_GET(field) \
DECL_AGET(field)
DECL_ACCESSORS(sender)
DECL_ACCESSORS(date)