summaryrefslogtreecommitdiff
path: root/mailbox2/mbox/mbox_hcache.c
blob: 0e9981ad3e180773b82f304a3c27200428b0038f (plain)
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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.

   GNU Mailutils 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.

   GNU Mailutils 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 GNU Mailutils; 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 <ctype.h>

#include <mailutils/error.h>
#include <mailutils/sys/mbox.h>

#undef min
#define min(a,b) ((a) < (b) ? (a) : (b))


static const char *hcache_default[] =
{
  "Bcc",
  "Cc",
  "Content-Language",
  "Content-Transfer-Encoding",
  "Content-Type",
  "Date",
  "From",
  "In-Reply-To",
  "Mail-Followup-To",
  "Message-ID",
  "Reply-To",
  "Return-Path",
  "Sender",
  "Subject",
  "To",
  "X-UIDL"
};

void
mbox_release_hcache (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)
{
  int status = 0;

  if (mbox == NULL)
    return MU_ERROR_INVALID_PARAMETER;

  mbox_release_hcache (mbox, 0);

  if (array && len)
    status = mbox_add_hcache (mbox, array, len);

  return status;
}

int
mbox_add_hcache (mbox_t mbox, const char **array, size_t len)
{
  size_t i;
  int status = 0;

  if (mbox == NULL || array == NULL || len == 0)
    return MU_ERROR_INVALID_PARAMETER;

  for (i = 0; i < len; i++)
    {
      char **values;
      if (array[i] == NULL || array[i][0] == '\0')
	continue;
      values = realloc (mbox->hcache.values,
			(mbox->hcache.size + 1) * sizeof (*values));
      if (values == NULL)
	{
	  status = MU_ERROR_NO_MEMORY;
	  break;
	}
      mbox->hcache.values = values;
      mbox->hcache.values[mbox->hcache.size] = strdup (array[i]);
      mbox->hcache.values[mbox->hcache.size][0] =
	toupper (mbox->hcache.values[mbox->hcache.size][0]);
      mbox->hcache.size++;
    }
  return status;
}

int
mbox_append_hcache (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 (mbox->hcache.values[i]
	  && toupper (*name) == mbox->hcache.values[i][0]
	  && 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_default_hcache (mbox_t mbox)
{
  return mbox_set_hcache (mbox, hcache_default,
			  (sizeof hcache_default) / sizeof (*hcache_default));
}

int
mbox_value_hcache (mbox_t mbox, unsigned int msgno, const char *name,
		   char *buf, size_t buflen, size_t *pn)
{
  mbox_message_t mum;
  size_t i;
  size_t n = 0;
  int status = MU_ERROR_ENTRY_NOT_EXIST;

  mbox_debug_print (mbox, "value_hcache(%d)", msgno);

  if (mbox == NULL || msgno == 0 || name == NULL || *name == '\0')
    return MU_ERROR_INVALID_PARAMETER;

  if (msgno > mbox->umessages_count)
    return MU_ERROR_INVALID_PARAMETER;

  mum = mbox->umessages[msgno - 1];
  for (i = 0; i < mbox->hcache.size; i++)
    {
      if (mbox->hcache.values[i]
	  && toupper (*name) == mbox->hcache.values[i][0]
	  && strcasecmp (mbox->hcache.values[i], name) == 0)
	{
	  if (mum->hcache.size == mbox->hcache.size)
	    {
	      n = strlen (mum->hcache.values[i]);
	      if (buf && buflen)
		{
		  buflen--;
		  n = min (buflen, n);
		  memcpy (buf, mum->hcache.values[i], n);
		  buf[n] = '\0';
		}
	      status = 0;
	    }
	}
    }

  if (pn)
    *pn = n;
  return status;
}

Return to:

Send suggestions and report system problems to the System administrator.