summaryrefslogtreecommitdiff
path: root/libmailutils/mailbox/imapenv.c
blob: 30b8814727840503f9454a420a4c97b2987711b6 (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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2011-2012, 2014-2017 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/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <mailutils/types.h>
#include <mailutils/header.h>
#include <mailutils/envelope.h>
#include <mailutils/address.h>
#include <mailutils/message.h>
#include <mailutils/sys/message.h>
#include <mailutils/errno.h>

void
mu_message_imapenvelope_free (struct mu_imapenvelope *env)
{
  if (!env)
    return;
  free (env->subject);
  mu_address_destroy (&env->from);
  mu_address_destroy (&env->sender);
  mu_address_destroy (&env->reply_to);
  mu_address_destroy (&env->to);
  mu_address_destroy (&env->cc);
  mu_address_destroy (&env->bcc);
  free (env->in_reply_to);
  free (env->message_id);
  free (env);
}

int
mu_message_get_imapenvelope (mu_message_t msg, struct mu_imapenvelope **pimapenvelope)
{
  struct mu_imapenvelope *imapenvelope;
  int rc;
  
  if (msg == NULL)
    return EINVAL;
  if (pimapenvelope == NULL)
    return MU_ERR_OUT_PTR_NULL;
  if (msg->_imapenvelope)
    return msg->_imapenvelope (msg, pimapenvelope);

  imapenvelope = calloc (1, sizeof (imapenvelope[0]));
  if (!imapenvelope)
    return ENOMEM;
  do
    {
      mu_header_t hdr;
      mu_envelope_t env;
      const char *s;

      if ((rc = mu_message_get_envelope (msg, &env)))
	break;
      if ((rc = mu_envelope_sget_date (env, &s)))
	break;
      if ((rc = mu_scan_datetime (s, MU_DATETIME_FROM,
				  &imapenvelope->date, &imapenvelope->tz,
				  NULL)))
	break;

      if ((rc = mu_message_get_header (msg, &hdr)))
	break;

      rc = mu_header_get_address (hdr, MU_HEADER_FROM, &imapenvelope->from);
      if (rc && rc != MU_ERR_NOENT)
	break;
      
      rc = mu_header_get_address (hdr, MU_HEADER_SENDER, &imapenvelope->sender);
      if (rc && rc != MU_ERR_NOENT)
	break;

      rc = mu_header_get_address (hdr, MU_HEADER_REPLY_TO, &imapenvelope->reply_to);
      if (rc && rc != MU_ERR_NOENT)
	break;

      rc = mu_header_get_address (hdr, MU_HEADER_TO, &imapenvelope->to);
      if (rc && rc != MU_ERR_NOENT)
	break;

      rc = mu_header_get_address (hdr, MU_HEADER_CC, &imapenvelope->cc);
      if (rc && rc != MU_ERR_NOENT)
	break;

      rc = mu_header_get_address (hdr, MU_HEADER_BCC, &imapenvelope->bcc);
      if (rc && rc != MU_ERR_NOENT)
	break;
      
      rc = mu_header_aget_value_unfold (hdr, MU_HEADER_SUBJECT,
					&imapenvelope->subject);
      if (rc && rc != MU_ERR_NOENT)
	break;

      rc = mu_header_aget_value_unfold (hdr, MU_HEADER_IN_REPLY_TO,
					&imapenvelope->in_reply_to);
      if (rc && rc != MU_ERR_NOENT)
	break;

      rc = mu_header_aget_value_unfold (hdr, MU_HEADER_MESSAGE_ID,
					&imapenvelope->message_id);
    }
  while (0);
  if (rc)
    mu_message_imapenvelope_free (imapenvelope);
  else
    *pimapenvelope = imapenvelope;
  return rc;
}

int
mu_message_set_imapenvelope (mu_message_t msg,
			int (*_imapenvelope) (mu_message_t,
					 struct mu_imapenvelope **),
			void *owner)
{
  if (msg == NULL)
    return EINVAL;
  if (msg->owner != owner)
    return EACCES;
  msg->_imapenvelope = _imapenvelope;
  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.