mbox_hcache.c
3.62 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
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <mailutils/error.h>
#include <mailutils/sys/mbox.h>
static const char *hcache_default[] =
{
"Bcc",
"Cc",
"Content-Language",
"Content-Transfer-Encoding",
"Content-Type",
"Date",
"From",
"In-Reply-To",
"Message-ID",
"Reply-To",
"Reply-To",
"Sender",
"Subject",
"To",
"X-UIDL"
};
void
mbox_hcache_free (mbox_t mbox, unsigned int msgno)
{
struct _hcache *hc;
if (mbox == NULL)
return;
if (msgno)
hc = &mbox->umessages[msgno - 1]->hcache;
else
hc = &mbox->hcache;
if (hc->size)
{
unsigned int i;
for (i = 0; i < hc->size; i++)
{
if (hc->values[i])
free (hc->values[i]);
}
}
hc->size = 0;
if (hc->values)
free (hc->values);
hc->values = NULL;
}
int
mbox_set_hcache (mbox_t mbox, const char **array, size_t len)
{
if (mbox == NULL)
return MU_ERROR_INVALID_PARAMETER;
mbox_hcache_free (mbox, 0);
if (array && len)
{
unsigned int i;
mbox->hcache.values = calloc (len, sizeof (*(mbox->hcache.values)));
if (mbox->hcache.values == NULL)
return MU_ERROR_NO_MEMORY;
for (i = 0; i < len; i++)
{
mbox->hcache.values[i] = strdup (array[i]);
if (mbox->hcache.values[i] == NULL)
{
mbox_set_hcache (mbox, NULL, 0);
return MU_ERROR_NO_MEMORY;
}
mbox->hcache.size++;
}
}
return 0;
}
int
mbox_hcache_append (mbox_t mbox, unsigned int msgno, const char *name,
const char *value)
{
struct _hcache *hc;
size_t i;
int status = MU_ERROR_ENTRY_NOT_EXIST;
if (mbox == NULL || msgno == 0 || name == NULL || *name == '\0')
return MU_ERROR_INVALID_PARAMETER;
if (msgno > mbox->messages_count)
return MU_ERROR_INVALID_PARAMETER;
if (mbox->hcache.size == 0)
return MU_ERROR_INVALID_PARAMETER;
msgno--;
hc = &(mbox->umessages[msgno]->hcache);
if (hc->values == NULL)
{
hc->values = calloc (mbox->hcache.size, sizeof (*(mbox->hcache.values)));
if (hc->values == NULL)
return MU_ERROR_NO_MEMORY;
}
hc->size = mbox->hcache.size;
for (i = 0; i < mbox->hcache.size; i++)
{
if (strcasecmp (mbox->hcache.values[i], name) == 0)
{
if (value == NULL)
{
if (hc->values[i])
free (hc->values[i]);
hc->values[i] = NULL;
}
else
{
if (hc->values[i] == NULL)
hc->values[i] = strdup (value);
else
{
char *tmp = realloc (hc->values[i], strlen (value) +
strlen (hc->values[i]) + 1);
if (tmp)
{
hc->values[i] = tmp;
strcat (hc->values[i], value);
}
}
}
status = 0;
break;
}
}
return status;
}
int
mbox_set_hcache_default (mbox_t mbox)
{
return mbox_set_hcache (mbox, hcache_default,
sizeof (hcache_default) / sizeof (*hcache_default));
}