summaryrefslogtreecommitdiff
path: root/libmailutils/mime/mimeitr.c
blob: 0deb873c168f91772d5641d333081374fe1782c4 (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
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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2020-2021 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/>. */

#include <config.h>
#include <stdlib.h>
#include <mailutils/types.h>
#include <mailutils/errno.h>
#include <mailutils/iterator.h>
#include <mailutils/message.h>
#include <mailutils/sys/iterator.h>

struct mime_part
{
  struct mime_part *up; /* Upper-level part */
  size_t nparts;  /* Number of MIME parts in this part */
  size_t index;   /* Current position in this part */
  mu_message_t mesg;
};

struct mimeitr
{
  int stop;
  struct mime_part *parent;
  struct mime_part *current;
  mu_coord_t coord;
};

static void
mime_part_list_unwind (struct mime_part **pp, struct mime_part *point)
{
  struct mime_part *p = *pp;
  while (p != point)
    {
      struct mime_part *up = p->up;
      free (p);
      p = up;
    }
  *pp = p;
}

static int
mime_part_list_alloc (struct mime_part **pp, mu_message_t msg)
{
  struct mime_part *np = malloc (sizeof (*np));
  if (!np)
    return -1;
  np->up = *pp;
  np->nparts = np->index = 0;
  np->mesg = msg;
  *pp = np;
  return 0;
}

static inline size_t
mime_part_list_len (struct mime_part *p)
{
  size_t n = 1;
  while ((p = p->up))
    n++;
  return n;
}

static int
first_plain (void *owner)
{
  struct mimeitr *itr = owner;
  itr->current->index = 0;
  itr->stop = 0;
  return mime_part_list_alloc (&itr->current, itr->current->mesg);
}

static int
next_plain (void *owner)
{
  struct mimeitr *itr = owner;
  mime_part_list_unwind (&itr->current, itr->parent);
  itr->stop = 1;
  return 0;
}

static int next_mime (void *owner);

static int
first_mime (void *owner)
{
  struct mimeitr *itr = owner;
  mime_part_list_unwind (&itr->current, itr->parent);
  itr->current->index = 0;
  itr->stop = 0;
  return next_mime (owner);
}

static int
next_mime (void *owner)
{
  struct mimeitr *itr = owner;
  
  while (itr->current->index == itr->current->nparts)
    {
      if (!itr->current->up)
	{
	  itr->stop = 1;
	  return 0;
	}
      mime_part_list_unwind (&itr->current, itr->current->up);
    }

  do
    {
      mu_message_t msg;
      int rc, ismime;
      
      itr->current->index++;

      rc = mu_message_get_part (itr->current->mesg, itr->current->index, &msg);
      if (rc)
	return -1;

      rc = mime_part_list_alloc (&itr->current, msg);
      if (rc)
	return -1;

      rc = mu_message_is_multipart (msg, &ismime);
      if (rc == 0)
	{
	  if (ismime)
	    rc = mu_message_get_num_parts (msg, &itr->current->nparts);
	}
      if (rc)
	{
	  mime_part_list_unwind (&itr->current, itr->current->up);
	  return rc;
	}
    }
  while (itr->current->nparts);
  
  return 0;
}

static int
getitem (void *owner, void **pret, const void **pkey)
{
  struct mimeitr *itr = owner;

  if (pkey)
    {
      struct mime_part *p;
      size_t n = mime_part_list_len (itr->current);
      mu_coord_t coord = realloc (itr->coord, n * sizeof (*itr->coord));

      if (!coord)
	return -1;
      itr->coord = coord;
      coord[0] = --n;
      for (p = itr->current->up; p; p = p->up, n--)
	coord[n] = p->index;
      *pkey = coord;
    }
  *pret = itr->current->mesg;
  return 0;
}

static int
finished_p (void *owner)
{
  struct mimeitr *itr = owner;
  return itr->stop;
}

static void
mimeitr_free (struct mimeitr *itr)
{
  mime_part_list_unwind (&itr->current, NULL);
  free (itr->coord);
  free (itr);
}

static int
destroy (mu_iterator_t iterator, void *data)
{
  struct mimeitr *itr = data;
  mimeitr_free (itr);
  return 0;
}

static int
itrdup (void **ptr, void *owner)
{
  struct mimeitr *orig = owner;
  struct mimeitr *itr;
  struct mime_part *pi, *po;
  size_t n;
  int rc;
  
  itr = malloc (sizeof (*itr));
  if (!itr)
    return ENOMEM;

  itr->parent = malloc (sizeof (*itr->parent));
  if (!itr->parent)
    {
      free (itr);
      return ENOMEM;
    }
  itr->current = itr->parent;
  
  for (n = mime_part_list_len (orig->current); n > 1; n--)
    {
      rc = mime_part_list_alloc (&itr->current, NULL);
      if (rc)
	break;
    }

  if (rc)
    {
      mime_part_list_unwind (&itr->current, NULL);
      free (itr->current);
      return rc;
    }

  pi = itr->current;
  po = orig->current;
  while (po)
    {
      pi->nparts = po->nparts;
      pi->index = po->index;
      pi->mesg = po->mesg;

      pi = pi->up;
      po = po->up;
    }

  rc = mu_coord_dup (orig->coord, &itr->coord);
  if (rc)
    {
      mimeitr_free (itr);
      return rc;
    }
  
  *ptr = itr;
  return 0;
}

int
mu_message_get_iterator (mu_message_t msg, mu_iterator_t *pitr)
{
  mu_iterator_t iterator;
  struct mimeitr *itr;
  int rc, ismime;
  size_t nparts = 0;
  
  rc = mu_message_is_multipart (msg, &ismime);
  if (rc == 0)
    {
      if (ismime)
	rc = mu_message_get_num_parts (msg, &nparts);
    }

  if (rc)
    return rc;
  
  itr = malloc (sizeof (*itr));
  if (!itr)
    return ENOMEM;

  itr->parent = malloc (sizeof (*itr->parent));
  if (!itr->parent)
    {
      free (itr);
      return ENOMEM;
    }

  itr->parent->up = NULL;
  itr->parent->mesg = msg;
  itr->parent->index = 0;
  itr->parent->nparts = nparts;
  itr->current = itr->parent;
  itr->stop = 0;
  itr->coord = NULL;
  
  rc = mu_iterator_create (&iterator, itr);
  if (rc)
    {
      free (itr->parent);
      free (itr);
      return rc;
    }

  mu_iterator_set_first (iterator, ismime ? first_mime : first_plain);
  mu_iterator_set_next (iterator, ismime ? next_mime : next_plain);
  mu_iterator_set_getitem (iterator, getitem);
  mu_iterator_set_finished_p (iterator, finished_p);
  mu_iterator_set_destroy (iterator, destroy);
  mu_iterator_set_dup (iterator, itrdup);

  *pitr = iterator;
  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.