summaryrefslogtreecommitdiff
path: root/libproto/dotmail/message.c
blob: 1a87616489982fc651c0115ae86fa58e33a8efe1 (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
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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2019 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, 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */

#include <config.h>
#include <stdlib.h>
#include <mailutils/sys/dotmail.h>
#include <mailutils/sys/mailbox.h>
#include <mailutils/sys/message.h>
#include <mailutils/diag.h>
#include <mailutils/errno.h>
#include <mailutils/cctype.h>
#include <mailutils/stream.h>
#include <mailutils/header.h>
#include <mailutils/body.h>
#include <mailutils/filter.h>
#include <mailutils/attribute.h>
#include <mailutils/io.h>

void
mu_dotmail_message_free (struct mu_dotmail_message *dmsg)
{
  if (dmsg)
    {
      int i;
      for (i = 0; i < MU_DOTMAIL_HDR_MAX; i++)
	free (dmsg->hdr[i]);
      mu_message_destroy (&dmsg->message, dmsg);
      free (dmsg);
    }
}

static int
_msg_stream_setup (mu_message_t msg, struct mu_dotmail_message *dmsg)
{
  mu_stream_t stream;
  int rc;

  rc = mu_streamref_create_abridged (&stream,
				     dmsg->mbox->mailbox->stream,
				     dmsg->message_start,
				     dmsg->message_end - 1);
  if (rc == 0)
    rc = mu_message_set_stream (msg, stream, dmsg);
  return rc;
}

static int
dotmail_body_size (mu_body_t body, size_t *psize)
{
  mu_message_t msg = mu_body_get_owner (body);
  struct mu_dotmail_message *dmsg = mu_message_get_owner (msg);
  if (!dmsg)
    return EINVAL;
  if (psize)
    *psize = dmsg->body_size;
  return 0;
}

static int
dotmail_body_lines (mu_body_t body, size_t *plines)
{
  mu_message_t msg = mu_body_get_owner (body);
  struct mu_dotmail_message *dmsg = mu_message_get_owner (msg);
  if (!dmsg)
    return EINVAL;
  if (!dmsg->body_lines_scanned)
    {
      int rc;
      mu_stream_t str;
      char c;
      size_t n;

      rc = mu_body_get_streamref (body, &str);
      if (rc)
	return rc;
      dmsg->body_lines = 0;
      while ((rc = mu_stream_read (str, &c, 1, &n)) == 0 && n == 1)
	{
	  if (c == '\n')
	    dmsg->body_lines++;
	}
      mu_stream_unref (str);
      if (rc)
	return rc;
      dmsg->body_lines_scanned = 1;
    }
  if (plines)
    *plines = dmsg->body_lines;
  return 0;
}

static int
_msg_body_setup (mu_message_t msg, struct mu_dotmail_message *dmsg)
{
  mu_body_t body;
  mu_stream_t stream, flt;
  int rc;

  if (dmsg->body_dot_stuffed)
    {
      rc = mu_streamref_create_abridged (&stream,
					 dmsg->mbox->mailbox->stream,
					 dmsg->body_start,
					 dmsg->message_end + 1);
      if (rc)
	return rc;

      rc = mu_filter_create (&flt, stream, "DOT",
			     MU_FILTER_DECODE, MU_STREAM_READ);
      mu_stream_unref (stream);
      if (rc)
	return rc;

      rc = mu_rdcache_stream_create (&stream, flt,
				     MU_STREAM_READ|MU_STREAM_SEEK);
      mu_stream_unref (flt);
    }
  else
    {
      rc = mu_streamref_create_abridged (&stream,
					 dmsg->mbox->mailbox->stream,
					 dmsg->body_start,
					 dmsg->message_end - 1);
    }
  if (rc)
    return rc;

  rc = mu_body_create (&body, msg);
  if (rc == 0)
    {
      mu_body_set_stream (body, stream, msg);
      mu_body_set_size (body, dotmail_body_size, msg);
      mu_body_set_lines (body, dotmail_body_lines, msg);
      mu_body_clear_modified (body);
      mu_message_set_body (msg, body, dmsg);
    }
  return rc;
}

int
mu_dotmail_message_attr_load (struct mu_dotmail_message *dmsg)
{
  if (!dmsg->attr_scanned)
    {
      if (dmsg->hdr[mu_dotmail_hdr_status])
	mu_string_to_flags (dmsg->hdr[mu_dotmail_hdr_status], &dmsg->attr_flags);
      else
	dmsg->attr_flags = 0;
      dmsg->attr_scanned = 1;
    }
  return 0;
}

static int
dotmail_get_attr_flags (mu_attribute_t attr, int *pflags)
{
  mu_message_t msg = mu_attribute_get_owner (attr);
  struct mu_dotmail_message *dmsg = mu_message_get_owner (msg);

  if (dmsg == NULL)
    return EINVAL;
  mu_dotmail_message_attr_load (dmsg);
  if (pflags)
    *pflags = dmsg->attr_flags;
  return 0;
}

static int
dotmail_set_attr_flags (mu_attribute_t attr, int flags)
{
  mu_message_t msg = mu_attribute_get_owner (attr);
  struct mu_dotmail_message *dmsg = mu_message_get_owner (msg);

  mu_dotmail_message_attr_load (dmsg);
  dmsg->attr_flags |= flags;
  return 0;
}

static int
dotmail_unset_attr_flags (mu_attribute_t attr, int flags)
{
  mu_message_t msg = mu_attribute_get_owner (attr);
  struct mu_dotmail_message *dmsg = mu_message_get_owner (msg);

  mu_dotmail_message_attr_load (dmsg);
  dmsg->attr_flags &= ~flags;
  return 0;
}

static int
_msg_attribute_setup (mu_message_t msg, struct mu_dotmail_message *dmsg)
{
  mu_attribute_t attribute;
  int rc = mu_attribute_create (&attribute, msg);
  if (rc)
    return rc;

  mu_attribute_set_get_flags (attribute, dotmail_get_attr_flags, msg);
  mu_attribute_set_set_flags (attribute, dotmail_set_attr_flags, msg);
  mu_attribute_set_unset_flags (attribute, dotmail_unset_attr_flags, msg);
  mu_message_set_attribute (msg, attribute, dmsg);

  return 0;
}

static int
dotmail_message_uid (mu_message_t msg, size_t *puid)
{
  struct mu_dotmail_message *dmsg = mu_message_get_owner (msg);
  int rc = mu_dotmail_mailbox_uid_setup (dmsg->mbox);
  if (rc == 0)
    *puid = dmsg->uid;
  return rc;
}

static int
dotmail_message_qid (mu_message_t msg, mu_message_qid_t *pqid)
{
  struct mu_dotmail_message *dmsg = mu_message_get_owner (msg);
  return mu_asprintf (pqid, "%lu", (unsigned long) dmsg->message_start);
}

static int
dotmail_message_setup (mu_message_t msg)
{
  struct mu_dotmail_message *dmsg = mu_message_get_owner (msg);
  int rc;

  rc = _msg_stream_setup (msg, dmsg);
  if (rc)
    return rc;

  rc = _msg_body_setup (msg, dmsg);
  if (rc)
    return rc;

  rc = _msg_attribute_setup (msg, dmsg);
  if (rc)
    return rc;

  return 0;
}

int
mu_dotmail_message_get (struct mu_dotmail_message *dmsg, mu_message_t *mptr)
{
  if (!dmsg->message)
    {
      mu_message_t msg;

      int rc = mu_message_create (&msg, dmsg);
      if (rc)
	return rc;

      rc = dotmail_message_setup (msg);
      if (rc)
	{
	  mu_message_destroy (&msg, dmsg);
	  return rc;
	}

      /* Set the UID.  */
      mu_message_set_uid (msg, dotmail_message_uid, dmsg);
      mu_message_set_qid (msg, dotmail_message_qid, dmsg);

      /* Attach the message to the mailbox mbox data.  */
      dmsg->message = msg;
      mu_message_set_mailbox (msg, dmsg->mbox->mailbox, dmsg);
      mu_message_clear_modified (msg);

      dmsg->message = msg;
    }
  if (mptr)
    *mptr = dmsg->message;
  return 0;
}

static int
dotmail_message_uid_save (mu_stream_t dst,
			  struct mu_dotmail_message const *dmsg)
{
  struct mu_dotmail_mailbox *dmp = dmsg->mbox;
  if (dmp->uidvalidity_scanned)
    {
      if (dmsg->hdr[mu_dotmail_hdr_x_imapbase])
	mu_stream_printf (dst, "%s: %s\n",
			  MU_HEADER_X_IMAPBASE,
			  dmsg->hdr[mu_dotmail_hdr_x_imapbase]);
      mu_stream_printf (dst, "%s: %lu\n",
			MU_HEADER_X_UID,
			dmsg->uid);
      return mu_stream_err (dst) ? mu_stream_last_error (dst) : 0;
    }
  return 0;
}

/* Copy message DMSG to DST replacing the UID-related information.
   The message is unchanged otherwise.
*/
int
dotmail_message_copy_with_uid (mu_stream_t dst,
			       struct mu_dotmail_message const *dmsg,
			       struct mu_dotmail_message_ref *ref)
{
  int rc;
  mu_stream_t src;
  static char *exclude_headers[] = {
    MU_HEADER_X_IMAPBASE,
    MU_HEADER_X_UID,
    NULL
  };

  src = dmsg->mbox->mailbox->stream;

  rc = mu_stream_seek (dst, 0, MU_SEEK_CUR, &ref->message_start);
  if (rc)
    return rc;

  rc = mu_stream_seek (src, dmsg->message_start, MU_SEEK_SET, NULL);
  if (rc)
    return rc;

  rc = mu_stream_header_copy (dst, src, exclude_headers);
  if (rc)
    return rc;

  rc = dotmail_message_uid_save (dst, dmsg);
  if (rc)
    return rc;

  rc = mu_stream_write (dst, "\n", 1, NULL);
  if (rc)
    return rc;

  rc = mu_stream_seek (dst, 0, MU_SEEK_CUR, &ref->body_start);
  if (rc)
    return rc;

  rc = mu_stream_copy (dst, src,
		       dmsg->message_end - dmsg->body_start + 2,
		       NULL);
  if (rc)
    return rc;

  return mu_stream_seek (dst, 0, MU_SEEK_CUR, &ref->message_end);
}

static int
msg_header_to_stream (mu_stream_t dst, mu_stream_t src,
		      struct mu_dotmail_message *dmsg)
{
  static char *exclude_headers[] = {
    MU_HEADER_STATUS,
    MU_HEADER_X_IMAPBASE,
    MU_HEADER_X_UID,
    NULL
  };
  mu_attribute_t attr;
  int rc;

  rc = mu_stream_header_copy (dst, src, exclude_headers);
  if (rc)
    return rc;

  rc = dotmail_message_uid_save (dst, dmsg);
  if (rc)
    return rc;

  rc = mu_message_get_attribute (dmsg->message, &attr);
  if (rc)
    return rc;

  free (dmsg->hdr[mu_dotmail_hdr_status]);
  dmsg->hdr[mu_dotmail_hdr_status] = malloc (MU_STATUS_BUF_SIZE);
  if (!dmsg->hdr[mu_dotmail_hdr_status])
    return ENOMEM;
  rc = mu_attribute_to_string (attr, dmsg->hdr[mu_dotmail_hdr_status],
			       MU_STATUS_BUF_SIZE, NULL);
  if (rc)
    return rc;

  mu_stream_printf (dst, "%s: %s\n",
		    MU_HEADER_STATUS,
		    dmsg->hdr[mu_dotmail_hdr_status]);

  return mu_stream_write (dst, "\n", 1, NULL);
}

/* Write to DEST a reconstructed copy of the message DMSG. Update
   the tracking reference REF.
*/
int
mu_dotmail_message_reconstruct (mu_stream_t dest,
				struct mu_dotmail_message *dmsg,
				struct mu_dotmail_message_ref *ref)
{
  int rc;
  mu_header_t hdr;
  mu_body_t body;
  mu_stream_t str, flt;

  if (!dmsg->message)
    return dotmail_message_copy_with_uid (dest, dmsg, ref);

  rc = mu_stream_seek (dest, 0, MU_SEEK_CUR, &ref->message_start);
  if (rc)
    return rc;

  rc = mu_message_get_header (dmsg->message, &hdr);
  if (rc)
    return rc;
  rc = mu_header_get_streamref (hdr, &str);
  if (rc)
    return rc;
  rc = msg_header_to_stream (dest, str, dmsg);
  mu_stream_unref (str);
  if (rc)
    return rc;

  rc = mu_stream_seek (dest, 0, MU_SEEK_CUR, &ref->body_start);
  if (rc)
    return rc;

  /* Copy body */
  rc = mu_message_get_body (dmsg->message, &body);
  if (rc)
    return rc;
  rc = mu_body_get_streamref (body, &str);
  if (rc)
	return rc;
  rc = mu_filter_create (&flt, str, "DOT",
			 MU_FILTER_ENCODE, MU_STREAM_READ);
  mu_stream_unref (str);
  if (rc)
    return rc;

  rc = mu_stream_copy (dest, flt, 0, NULL);
  mu_stream_unref (flt);
  if (rc == 0)
    {
      rc = mu_stream_seek (dest, 0, MU_SEEK_CUR, &ref->message_end);
      if (rc)
	return rc;

      ref->message_end -= 2;
      ref->rescan = 1;
    }

  return rc;
}

Return to:

Send suggestions and report system problems to the System administrator.