Commit 217ae7a2 217ae7a2228c6f7054b4e8e1731ec5e17f0a86a4 by Sergey Poznyakoff

Split ibmailutils/mailbox/message.c into logically separated and

easily manageable parts.
1 parent 1ae9bae2
...@@ -28,7 +28,27 @@ libmailbox_la_SOURCES = \ ...@@ -28,7 +28,27 @@ libmailbox_la_SOURCES = \
28 hdrfirst.c\ 28 hdrfirst.c\
29 hdritr.c\ 29 hdritr.c\
30 header.c\ 30 header.c\
31 message.c\ 31 msgcpy.c\
32 msgscan.c 32 msgattr.c\
33 msgbody.c\
34 msgcreat.c\
35 msgheader.c\
36 msgenv.c\
37 msglines.c\
38 msgmbx.c\
39 msgmod.c\
40 msgmulti.c\
41 msgnumparts.c\
42 msgobs.c\
43 msgpart.c\
44 msgqid.c\
45 msgqlines.c\
46 msgref.c\
47 msgsave.c\
48 msgscan.c\
49 msgsize.c\
50 msgstream.c\
51 msguid.c\
52 msguidl.c
33 53
34 INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils 54 INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
3 2010, 2011 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20 #include <stdlib.h>
21
22 #include <mailutils/types.h>
23 #include <mailutils/message.h>
24 #include <mailutils/errno.h>
25 #include <mailutils/attribute.h>
26 #include <mailutils/sys/message.h>
27
28 int
29 mu_message_get_attribute (mu_message_t msg, mu_attribute_t *pattribute)
30 {
31 if (msg == NULL)
32 return EINVAL;
33 if (pattribute == NULL)
34 return MU_ERR_OUT_PTR_NULL;
35 if (msg->attribute == NULL)
36 {
37 mu_attribute_t attribute;
38 int status = mu_attribute_create (&attribute, msg);
39 if (status != 0)
40 return status;
41 msg->attribute = attribute;
42 }
43 *pattribute = msg->attribute;
44 return 0;
45 }
46
47 int
48 mu_message_set_attribute (mu_message_t msg, mu_attribute_t attribute, void *owner)
49 {
50 if (msg == NULL)
51 return EINVAL;
52 if (msg->owner != owner)
53 return EACCES;
54 if (msg->attribute)
55 mu_attribute_destroy (&msg->attribute, owner);
56 msg->attribute = attribute;
57 msg->flags |= MESSAGE_MODIFIED;
58 return 0;
59 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
3 2010, 2011 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20 #include <stdlib.h>
21
22 #include <mailutils/types.h>
23 #include <mailutils/message.h>
24 #include <mailutils/errno.h>
25 #include <mailutils/header.h>
26 #include <mailutils/body.h>
27 #include <mailutils/stream.h>
28 #include <mailutils/sys/message.h>
29
30 int
31 mu_message_get_body (mu_message_t msg, mu_body_t *pbody)
32 {
33 if (msg == NULL)
34 return EINVAL;
35 if (pbody == NULL)
36 return MU_ERR_OUT_PTR_NULL;
37
38 /* Is it a floating mesg. */
39 if (msg->body == NULL)
40 {
41 mu_body_t body;
42 int status = mu_body_create (&body, msg);
43 if (status != 0)
44 return status;
45 /* If a stream is already set, use it to create the body stream. */
46 /* FIXME: I'm not sure if the second condition is really needed */
47 if (msg->stream/* && (msg->flags & MESSAGE_INTERNAL_STREAM)*/)
48 {
49 mu_stream_t stream;
50 int flags = 0;
51
52 /* FIXME: The actual mu_header_size cannot be used as offset,
53 because the headers might have been modified in between. */
54
55 mu_stream_get_flags (msg->stream, &flags);
56 status = mu_streamref_create_abridged (&stream, msg->stream,
57 msg->orig_header_size, 0);
58 if (status)
59 {
60 mu_body_destroy (&body, msg);
61 return status;
62 }
63 mu_body_set_stream (body, stream, msg);
64 }
65 msg->body = body;
66 }
67 *pbody = msg->body;
68 return 0;
69 }
70
71 int
72 mu_message_set_body (mu_message_t msg, mu_body_t body, void *owner)
73 {
74 if (msg == NULL )
75 return EINVAL;
76 if (msg->owner != owner)
77 return EACCES;
78 /* Make sure we destroy the old if it was owned by the mesg. */
79 /* FIXME: I do not know if somebody has already a ref on this ? */
80 if (msg->body)
81 mu_body_destroy (&msg->body, msg);
82 msg->body = body;
83 msg->flags |= MESSAGE_MODIFIED;
84 return 0;
85 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
3 2010, 2011 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20 #include <stdlib.h>
21
22 #include <mailutils/types.h>
23 #include <mailutils/message.h>
24 #include <mailutils/errno.h>
25 #include <mailutils/stream.h>
26 #include <mailutils/sys/message.h>
27
28 int
29 mu_message_create_copy (mu_message_t *to, mu_message_t from)
30 {
31 int status = 0;
32 mu_stream_t fromstr = NULL;
33 mu_stream_t tmp = NULL;
34
35 if (!to)
36 return MU_ERR_OUT_PTR_NULL;
37 if (!from)
38 return EINVAL;
39
40 status = mu_memory_stream_create (&tmp, MU_STREAM_RDWR|MU_STREAM_SEEK);
41 if (status)
42 return status;
43
44 status = mu_message_get_streamref (from, &fromstr);
45 if (status)
46 {
47 mu_stream_destroy (&tmp);
48 return status;
49 }
50
51 status = mu_stream_copy (tmp, fromstr, 0, NULL);
52 if (status == 0)
53 {
54 status = mu_message_create (to, NULL);
55 if (status == 0)
56 mu_message_set_stream (*to, tmp, NULL);
57 }
58
59 if (status)
60 mu_stream_destroy (&tmp);
61 mu_stream_destroy (&fromstr);
62
63 return status;
64 }
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
3 2010, 2011 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20 #include <stdlib.h>
21
22 #include <mailutils/types.h>
23 #include <mailutils/message.h>
24 #include <mailutils/errno.h>
25 #include <mailutils/sys/message.h>
26
27 /* Allocate ressources for the mu_message_t. */
28 int
29 mu_message_create (mu_message_t *pmsg, void *owner)
30 {
31 mu_message_t msg;
32 int status;
33
34 if (pmsg == NULL)
35 return MU_ERR_OUT_PTR_NULL;
36 msg = calloc (1, sizeof (*msg));
37 if (msg == NULL)
38 return ENOMEM;
39 status = mu_monitor_create (&msg->monitor, 0, msg);
40 if (status != 0)
41 {
42 free (msg);
43 return status;
44 }
45 msg->owner = owner;
46 msg->ref_count = 1;
47 *pmsg = msg;
48 return 0;
49 }
50
51 void *
52 mu_message_get_owner (mu_message_t msg)
53 {
54 return (msg == NULL) ? NULL : msg->owner;
55 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
3 2010, 2011 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20 #include <sys/types.h>
21 #include <stdlib.h>
22 #include <time.h>
23 #include <unistd.h>
24 #include <string.h>
25
26 #include <mailutils/types.h>
27 #include <mailutils/message.h>
28 #include <mailutils/errno.h>
29 #include <mailutils/envelope.h>
30 #include <mailutils/util.h>
31 #include <mailutils/header.h>
32 #include <mailutils/mu_auth.h>
33 #include <mailutils/address.h>
34 #include <mailutils/sys/message.h>
35
36 /* Message envelope */
37 static int
38 message_envelope_date (mu_envelope_t envelope, char *buf, size_t len,
39 size_t *pnwrite)
40 {
41 mu_message_t msg = mu_envelope_get_owner (envelope);
42 time_t t;
43 size_t n;
44
45 if (msg == NULL)
46 return EINVAL;
47
48 /* FIXME: extract the time from "Date:". */
49
50 if (buf == NULL || len == 0)
51 {
52 n = MU_ENVELOPE_DATE_LENGTH;
53 }
54 else
55 {
56 char tmpbuf[MU_ENVELOPE_DATE_LENGTH+1];
57 t = time (NULL);
58 n = mu_strftime (tmpbuf, sizeof tmpbuf,
59 MU_ENVELOPE_DATE_FORMAT, localtime (&t));
60 n = mu_cpystr (buf, tmpbuf, len);
61 }
62 if (pnwrite)
63 *pnwrite = n;
64 return 0;
65 }
66
67 static int
68 message_envelope_sender (mu_envelope_t envelope, char *buf, size_t len,
69 size_t *pnwrite)
70 {
71 mu_message_t msg = mu_envelope_get_owner (envelope);
72 mu_header_t header;
73 int status;
74 const char *sender;
75 struct mu_auth_data *auth = NULL;
76 static char *hdrnames[] = {
77 "X-Envelope-Sender",
78 "X-Envelope-From",
79 "X-Original-Sender",
80 "From",
81 NULL
82 };
83 mu_address_t address = NULL;
84
85 if (msg == NULL)
86 return EINVAL;
87
88 /* First, try the header */
89 status = mu_message_get_header (msg, &header);
90 if (status)
91 return status;
92 status = mu_header_sget_firstof (header, hdrnames, &sender, NULL);
93 if (status)
94 {
95 auth = mu_get_auth_by_uid (getuid ());
96 if (!auth)
97 return MU_ERR_NOENT;
98 sender = auth->name;
99 }
100
101 status = mu_address_create (&address, sender);
102 if (status == 0)
103 {
104 status = mu_address_sget_email (address, 1, &sender);
105 if (status == 0)
106 {
107 size_t n = strlen (sender);
108 if (buf && len > 0)
109 {
110 len--; /* One for the null. */
111 n = (n < len) ? n : len;
112 memcpy (buf, sender, n);
113 buf[n] = '\0';
114 }
115 if (pnwrite)
116 *pnwrite = n;
117 }
118 mu_address_destroy (&address);
119 }
120
121 if (auth)
122 mu_auth_data_free (auth);
123
124 return status;
125 }
126
127 int
128 mu_message_get_envelope (mu_message_t msg, mu_envelope_t *penvelope)
129 {
130 if (msg == NULL)
131 return EINVAL;
132 if (penvelope == NULL)
133 return MU_ERR_OUT_PTR_NULL;
134
135 if (msg->envelope == NULL)
136 {
137 mu_envelope_t envelope;
138 int status = mu_envelope_create (&envelope, msg);
139 if (status != 0)
140 return status;
141 mu_envelope_set_sender (envelope, message_envelope_sender, msg);
142 mu_envelope_set_date (envelope, message_envelope_date, msg);
143 msg->envelope = envelope;
144 }
145 *penvelope = msg->envelope;
146 return 0;
147 }
148
149 int
150 mu_message_set_envelope (mu_message_t msg, mu_envelope_t envelope, void *owner)
151 {
152 if (msg == NULL)
153 return EINVAL;
154 if (msg->owner != owner)
155 return EACCES;
156 if (msg->envelope)
157 mu_envelope_destroy (&msg->envelope, msg);
158 msg->envelope = envelope;
159 msg->flags |= MESSAGE_MODIFIED;
160 return 0;
161 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
3 2010, 2011 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <mailutils/types.h>
24 #include <mailutils/message.h>
25 #include <mailutils/errno.h>
26 #include <mailutils/header.h>
27 #include <mailutils/stream.h>
28 #include <mailutils/sys/message.h>
29
30
31 enum eoh_state
32 {
33 eoh_no,
34 eoh_maybe,
35 eoh_yes
36 };
37
38 /* Message header stuff */
39 static enum eoh_state
40 string_find_eoh (enum eoh_state eoh, const char *str, size_t len,
41 size_t *ppos)
42 {
43 size_t pos;
44
45 if (eoh == eoh_maybe && *str == '\n')
46 {
47 *ppos = 0;
48 return eoh_yes;
49 }
50
51 for (pos = 0; pos < len - 1; pos++)
52 if (str[pos] == '\n' && str[pos + 1] == '\n')
53 {
54 *ppos = pos + 1;
55 return eoh_yes;
56 }
57
58 *ppos = pos + 1;
59 return str[pos] == '\n' ? eoh_maybe : eoh_no;
60 }
61
62 #define MIN_HEADER_BUF_SIZE 2048
63
64 static int
65 _header_fill (mu_stream_t stream, char **pbuf, size_t *plen)
66 {
67 int status = 0;
68 char *buffer = NULL;
69 size_t bufsize = 0;
70 char inbuf[MIN_HEADER_BUF_SIZE];
71 size_t nread;
72 enum eoh_state eoh = eoh_no;
73
74 status = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
75 if (status)
76 return status;
77
78 while (eoh != eoh_yes
79 && (status = mu_stream_read (stream, inbuf, sizeof (inbuf), &nread))
80 == 0
81 && nread)
82 {
83 char *nbuf;
84 size_t len;
85
86 eoh = string_find_eoh (eoh, inbuf, nread, &len);
87
88 nbuf = realloc (buffer, bufsize + len);
89 if (!nbuf)
90 {
91 status = ENOMEM;
92 break;
93 }
94 memcpy (nbuf + bufsize, inbuf, len);
95 buffer = nbuf;
96 bufsize += len;
97 }
98
99 if (status)
100 free (buffer);
101 else
102 {
103 *pbuf = buffer;
104 *plen = bufsize;
105 }
106 return status;
107 }
108
109 static int
110 message_header_fill (void *data, char **pbuf, size_t *plen)
111 {
112 int status = 0;
113 mu_message_t msg = data;
114 mu_stream_t stream;
115
116 status = mu_message_get_streamref (msg, &stream);
117 if (status == 0)
118 {
119 status = _header_fill (stream, pbuf, plen);
120 mu_stream_destroy (&stream);
121 }
122 return status;
123 }
124
125 int
126 mu_message_get_header (mu_message_t msg, mu_header_t *phdr)
127 {
128 if (msg == NULL)
129 return EINVAL;
130 if (phdr == NULL)
131 return MU_ERR_OUT_PTR_NULL;
132
133 if (msg->header == NULL)
134 {
135 mu_header_t header;
136 int status = mu_header_create (&header, NULL, 0);
137 if (status != 0)
138 return status;
139 if (msg->stream)
140 mu_header_set_fill (header, message_header_fill, msg);
141 status = mu_header_size (header, &msg->orig_header_size);
142 if (status)
143 return status;
144 msg->header = header;
145 }
146 *phdr = msg->header;
147 return 0;
148 }
149
150 /* Note: mu_message_set_header steals the reference to hdr */
151 int
152 mu_message_set_header (mu_message_t msg, mu_header_t hdr, void *owner)
153 {
154 if (msg == NULL )
155 return EINVAL;
156 if (msg->owner != owner)
157 return EACCES;
158 if (msg->header)
159 mu_header_destroy (&msg->header);
160 msg->header = hdr;
161 msg->flags |= MESSAGE_MODIFIED;
162 return 0;
163 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
3 2010, 2011 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20 #include <stdlib.h>
21
22 #include <mailutils/types.h>
23 #include <mailutils/message.h>
24 #include <mailutils/errno.h>
25 #include <mailutils/header.h>
26 #include <mailutils/body.h>
27 #include <mailutils/sys/message.h>
28
29 int
30 mu_message_lines (mu_message_t msg, size_t *plines)
31 {
32 size_t hlines, blines;
33 int ret = 0;
34
35 if (msg == NULL)
36 return EINVAL;
37 /* Overload. */
38 if (msg->_lines)
39 return msg->_lines (msg, plines, 0);
40 if (plines)
41 {
42 mu_header_t hdr = NULL;
43 mu_body_t body = NULL;
44
45 hlines = blines = 0;
46 mu_message_get_header (msg, &hdr);
47 mu_message_get_body (msg, &body);
48 if ( ( ret = mu_header_lines (hdr, &hlines) ) == 0 )
49 ret = mu_body_lines (body, &blines);
50 *plines = hlines + blines;
51 }
52 return ret;
53 }
54
55 int
56 mu_message_set_lines (mu_message_t msg, int (*_lines)
57 (mu_message_t, size_t *, int), void *owner)
58 {
59 if (msg == NULL)
60 return EINVAL;
61 if (msg->owner != owner)
62 return EACCES;
63 msg->_lines = _lines;
64 return 0;
65 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
3 2010, 2011 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20 #include <stdlib.h>
21
22 #include <mailutils/types.h>
23 #include <mailutils/message.h>
24 #include <mailutils/errno.h>
25 #include <mailutils/sys/message.h>
26
27 int
28 mu_message_get_mailbox (mu_message_t msg, mu_mailbox_t *pmailbox)
29 {
30 if (msg == NULL)
31 return EINVAL;
32 if (pmailbox == NULL)
33 return MU_ERR_OUT_PTR_NULL;
34 *pmailbox = msg->mailbox;
35 return 0;
36 }
37
38 int
39 mu_message_set_mailbox (mu_message_t msg, mu_mailbox_t mailbox, void *owner)
40 {
41 if (msg == NULL)
42 return EINVAL;
43 if (msg->owner != owner)
44 return EACCES;
45 msg->mailbox = mailbox;
46 return 0;
47 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
3 2010, 2011 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20 #include <stdlib.h>
21
22 #include <mailutils/types.h>
23 #include <mailutils/message.h>
24 #include <mailutils/errno.h>
25 #include <mailutils/header.h>
26 #include <mailutils/attribute.h>
27 #include <mailutils/body.h>
28 #include <mailutils/sys/message.h>
29
30 int
31 mu_message_is_modified (mu_message_t msg)
32 {
33 int mod = 0;
34 if (msg)
35 {
36 if (mu_header_is_modified (msg->header))
37 mod |= MU_MSG_HEADER_MODIFIED;
38 if (mu_attribute_is_modified (msg->attribute))
39 mod |= MU_MSG_ATTRIBUTE_MODIFIED;
40 if (mu_body_is_modified (msg->body))
41 mod |= MU_MSG_BODY_MODIFIED;
42 if (msg->flags & MESSAGE_MODIFIED)
43 mod |= MU_MSG_BODY_MODIFIED | MU_MSG_HEADER_MODIFIED;
44 }
45 return mod;
46 }
47
48 int
49 mu_message_clear_modified (mu_message_t msg)
50 {
51 if (msg)
52 {
53 if (msg->header)
54 mu_header_clear_modified (msg->header);
55 if (msg->attribute)
56 mu_attribute_clear_modified (msg->attribute);
57 if (msg->body)
58 mu_body_clear_modified (msg->body);
59 msg->flags &= ~MESSAGE_MODIFIED;
60 }
61 return 0;
62 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
3 2010, 2011 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20 #include <stdlib.h>
21 #include <mailutils/types.h>
22 #include <mailutils/message.h>
23 #include <mailutils/mime.h>
24 #include <mailutils/errno.h>
25 #include <mailutils/sys/message.h>
26
27 int
28 mu_message_is_multipart (mu_message_t msg, int *pmulti)
29 {
30 if (msg && pmulti)
31 {
32 if (msg->_is_multipart)
33 return msg->_is_multipart (msg, pmulti);
34 if (msg->mime == NULL)
35 {
36 int status = mu_mime_create (&msg->mime, msg, 0);
37 if (status != 0)
38 return 0;
39 msg->flags |= MESSAGE_MIME_OWNER;
40 }
41 *pmulti = mu_mime_is_multipart(msg->mime);
42 }
43 return 0;
44 }
45
46 int
47 mu_message_set_is_multipart (mu_message_t msg,
48 int (*_is_multipart) (mu_message_t, int *),
49 void *owner)
50 {
51 if (msg == NULL)
52 return EINVAL;
53 if (msg->owner != owner)
54 return EACCES;
55 msg->_is_multipart = _is_multipart;
56 return 0;
57 }
58
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
3 2010, 2011 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20 #include <stdlib.h>
21 #include <mailutils/types.h>
22 #include <mailutils/message.h>
23 #include <mailutils/mime.h>
24 #include <mailutils/errno.h>
25 #include <mailutils/sys/message.h>
26
27 int
28 mu_message_get_num_parts (mu_message_t msg, size_t *pparts)
29 {
30 if (msg == NULL || pparts == NULL)
31 return EINVAL;
32
33 if (msg->_get_num_parts)
34 return msg->_get_num_parts (msg, pparts);
35
36 if (msg->mime == NULL)
37 {
38 int status = mu_mime_create (&msg->mime, msg, 0);
39 if (status != 0)
40 return status;
41 }
42 return mu_mime_get_num_parts (msg->mime, pparts);
43 }
44
45 int
46 mu_message_set_get_num_parts (mu_message_t msg,
47 int (*_get_num_parts) (mu_message_t, size_t *),
48 void *owner)
49 {
50 if (msg == NULL)
51 return EINVAL;
52 if (msg->owner != owner)
53 return EACCES;
54 msg->_get_num_parts = _get_num_parts;
55 return 0;
56 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
3 2010, 2011 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20 #include <stdlib.h>
21 #include <mailutils/types.h>
22 #include <mailutils/message.h>
23 #include <mailutils/errno.h>
24 #include <mailutils/observer.h>
25 #include <mailutils/sys/message.h>
26
27 int
28 mu_message_get_observable (mu_message_t msg, mu_observable_t *pobservable)
29 {
30 if (msg == NULL || pobservable == NULL)
31 return EINVAL;
32
33 if (msg->observable == NULL)
34 {
35 int status = mu_observable_create (&msg->observable, msg);
36 if (status != 0)
37 return status;
38 }
39 *pobservable = msg->observable;
40 return 0;
41 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
3 2010, 2011 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20 #include <stdlib.h>
21 #include <mailutils/types.h>
22 #include <mailutils/message.h>
23 #include <mailutils/errno.h>
24 #include <mailutils/sys/message.h>
25
26 int
27 mu_message_get_part (mu_message_t msg, size_t part, mu_message_t *pmsg)
28 {
29 if (msg == NULL || pmsg == NULL)
30 return EINVAL;
31
32 /* Overload. */
33 if (msg->_get_part)
34 return msg->_get_part (msg, part, pmsg);
35
36 if (msg->mime == NULL)
37 {
38 int status = mu_mime_create (&msg->mime, msg, 0);
39 if (status != 0)
40 return status;
41 }
42 return mu_mime_get_part (msg->mime, part, pmsg);
43 }
44
45 int
46 mu_message_set_get_part (mu_message_t msg, int (*_get_part)
47 (mu_message_t, size_t, mu_message_t *),
48 void *owner)
49 {
50 if (msg == NULL)
51 return EINVAL;
52 if (msg->owner != owner)
53 return EACCES;
54 msg->_get_part = _get_part;
55 return 0;
56 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
3 2010, 2011 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20 #include <stdlib.h>
21
22 #include <mailutils/types.h>
23 #include <mailutils/message.h>
24 #include <mailutils/errno.h>
25 #include <mailutils/sys/message.h>
26
27 int
28 mu_message_get_qid (mu_message_t msg, mu_message_qid_t *pqid)
29 {
30 if (msg == NULL)
31 return EINVAL;
32 if (!msg->_get_qid)
33 return ENOSYS;
34 return msg->_get_qid (msg, pqid);
35 }
36
37 int
38 mu_message_set_qid (mu_message_t msg,
39 int (*_get_qid) (mu_message_t, mu_message_qid_t *),
40 void *owner)
41 {
42 if (msg == NULL)
43 return EINVAL;
44 if (msg->owner != owner)
45 return EACCES;
46 msg->_get_qid = _get_qid;
47 return 0;
48 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
3 2010, 2011 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20 #include <stdlib.h>
21
22 #include <mailutils/types.h>
23 #include <mailutils/message.h>
24 #include <mailutils/errno.h>
25 #include <mailutils/header.h>
26 #include <mailutils/body.h>
27 #include <mailutils/sys/message.h>
28
29 /* Return the number of lines in the message, without going into
30 excess trouble for calculating it. If obtaining the result
31 means downloading the entire message (as is the case for POP3,
32 for example), return MU_ERR_INFO_UNAVAILABLE. */
33 int
34 mu_message_quick_lines (mu_message_t msg, size_t *plines)
35 {
36 size_t hlines, blines;
37 int rc;
38
39 if (msg == NULL)
40 return EINVAL;
41 /* Overload. */
42 if (msg->_lines)
43 {
44 int rc = msg->_lines (msg, plines, 1);
45 if (rc != ENOSYS)
46 return rc;
47 }
48 if (plines)
49 {
50 mu_header_t hdr = NULL;
51 mu_body_t body = NULL;
52
53 hlines = blines = 0;
54 mu_message_get_header (msg, &hdr);
55 mu_message_get_body (msg, &body);
56 if ((rc = mu_header_lines (hdr, &hlines)) == 0)
57 rc = mu_body_lines (body, &blines);
58 if (rc == 0)
59 *plines = hlines + blines;
60 }
61 return rc;
62 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
3 2010, 2011 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20 #include <stdlib.h>
21
22 #include <mailutils/types.h>
23 #include <mailutils/message.h>
24 #include <mailutils/errno.h>
25 #include <mailutils/monitor.h>
26 #include <mailutils/observer.h>
27 #include <mailutils/envelope.h>
28 #include <mailutils/header.h>
29 #include <mailutils/body.h>
30 #include <mailutils/attribute.h>
31 #include <mailutils/stream.h>
32 #include <mailutils/sys/message.h>
33
34 void
35 mu_message_ref (mu_message_t msg)
36 {
37 if (msg)
38 {
39 mu_monitor_wrlock (msg->monitor);
40 msg->ref_count++;
41 mu_monitor_unlock (msg->monitor);
42 }
43 }
44
45 /* Free the message and all associated stuff */
46 static void
47 _mu_message_free (mu_message_t msg)
48 {
49 /* Notify the listeners. */
50 /* FIXME: to be removed since we do not support this event. */
51 if (msg->observable)
52 {
53 mu_observable_notify (msg->observable, MU_EVT_MESSAGE_DESTROY, msg);
54 mu_observable_destroy (&msg->observable, msg);
55 }
56
57 /* Envelope. */
58 if (msg->envelope)
59 mu_envelope_destroy (&msg->envelope, msg);
60
61 /* Header. */
62 if (msg->header)
63 mu_header_destroy (&msg->header);
64
65 /* Body. */
66 if (msg->body)
67 mu_body_destroy (&msg->body, msg);
68
69 /* Attribute. */
70 if (msg->attribute)
71 mu_attribute_destroy (&msg->attribute, msg);
72
73 /* Stream. */
74 if (msg->stream)
75 mu_stream_destroy (&msg->stream);
76
77 /* Mime. */
78 if (msg->flags & MESSAGE_MIME_OWNER)
79 mu_mime_destroy (&msg->mime);
80
81 /* Loose the owner. */
82 msg->owner = NULL;
83
84 free (msg);
85 }
86
87 void
88 mu_message_unref (mu_message_t msg)
89 {
90 if (msg)
91 {
92 mu_monitor_t monitor = msg->monitor;
93 mu_monitor_wrlock (monitor);
94 /* Note: msg->ref may be incremented by mu_message_ref without
95 additional checking for its owner, therefore decrementing
96 it must also occur independently of the owner checking. Due
97 to this inconsistency ref may reach negative values, which
98 is very unfortunate.
99
100 The `owner' stuff is a leftover from older mailutils versions.
101 We are heading to removing it altogether. */
102 if (msg->ref_count > 0)
103 msg->ref_count--;
104 if (msg->ref_count == 0)
105 {
106 _mu_message_free (msg);
107 mu_monitor_unlock (monitor);
108 mu_monitor_destroy (&monitor, msg);
109 }
110 else
111 mu_monitor_unlock (monitor);
112 }
113 }
114
115 void
116 mu_message_destroy (mu_message_t *pmsg, void *owner)
117 {
118 if (pmsg && *pmsg)
119 {
120 mu_message_t msg = *pmsg;
121
122 mu_monitor_t monitor = msg->monitor;
123 mu_monitor_wrlock (monitor);
124
125 if (msg->owner && msg->owner == owner)
126 {
127 _mu_message_free (msg);
128 mu_monitor_unlock (monitor);
129 mu_monitor_destroy (&monitor, msg);
130 *pmsg = NULL;
131 return;
132 }
133 mu_monitor_unlock (monitor);
134 }
135 }
136
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
3 2010, 2011 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20
21 #include <mailutils/types.h>
22 #include <mailutils/diag.h>
23 #include <mailutils/error.h>
24 #include <mailutils/errno.h>
25 #include <mailutils/mailbox.h>
26 #include <mailutils/message.h>
27 #include <mailutils/stream.h>
28
29 int
30 mu_message_save_to_mailbox (mu_message_t msg, const char *toname, int perms)
31 {
32 int rc = 0;
33 mu_mailbox_t to = 0;
34
35 if ((rc = mu_mailbox_create_default (&to, toname)))
36 {
37 mu_debug (MU_DEBCAT_MESSAGE, MU_DEBUG_ERROR,
38 ("mu_mailbox_create_default (%s) failed: %s\n", toname,
39 mu_strerror (rc)));
40 goto end;
41 }
42
43 if ((rc = mu_mailbox_open (to,
44 MU_STREAM_WRITE | MU_STREAM_CREAT
45 | (perms & MU_STREAM_IMASK))))
46 {
47 mu_debug (MU_DEBCAT_MESSAGE, MU_DEBUG_ERROR,
48 ("mu_mailbox_open (%s) failed: %s", toname,
49 mu_strerror (rc)));
50 goto end;
51 }
52
53 if ((rc = mu_mailbox_append_message (to, msg)))
54 {
55 mu_debug (MU_DEBCAT_MESSAGE, MU_DEBUG_ERROR,
56 ("mu_mailbox_append_message (%s) failed: %s", toname,
57 mu_strerror (rc)));
58 goto end;
59 }
60
61 end:
62
63 if (!rc)
64 {
65 if ((rc = mu_mailbox_close (to)))
66 mu_debug (MU_DEBCAT_MESSAGE, MU_DEBUG_ERROR,
67 ("mu_mailbox_close (%s) failed: %s", toname,
68 mu_strerror (rc)));
69 }
70 else
71 mu_mailbox_close (to);
72
73 mu_mailbox_destroy (&to);
74
75 return rc;
76 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
3 2010, 2011 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20 #include <stdlib.h>
21
22 #include <mailutils/types.h>
23 #include <mailutils/message.h>
24 #include <mailutils/errno.h>
25 #include <mailutils/header.h>
26 #include <mailutils/body.h>
27 #include <mailutils/sys/message.h>
28
29 int
30 mu_message_size (mu_message_t msg, size_t *psize)
31 {
32 size_t hsize, bsize;
33 int ret = 0;
34
35 if (msg == NULL)
36 return EINVAL;
37 /* Overload ? */
38 if (msg->_size)
39 return msg->_size (msg, psize);
40 if (psize)
41 {
42 mu_header_t hdr = NULL;
43 mu_body_t body = NULL;
44
45 hsize = bsize = 0;
46 mu_message_get_header (msg, &hdr);
47 mu_message_get_body (msg, &body);
48 if ( ( ret = mu_header_size (hdr, &hsize) ) == 0 )
49 ret = mu_body_size (body, &bsize);
50 *psize = hsize + bsize;
51 }
52 return ret;
53 }
54
55 int
56 mu_message_set_size (mu_message_t msg, int (*_size)
57 (mu_message_t, size_t *), void *owner)
58 {
59 if (msg == NULL)
60 return EINVAL;
61 if (msg->owner != owner)
62 return EACCES;
63 msg->_size = _size;
64 return 0;
65 }
66
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
3 2010, 2011 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20 #include <stdlib.h>
21
22 #include <mailutils/types.h>
23 #include <mailutils/message.h>
24 #include <mailutils/errno.h>
25 #include <mailutils/stream.h>
26 #include <mailutils/header.h>
27 #include <mailutils/body.h>
28 #include <mailutils/sys/message.h>
29 #include <mailutils/sys/stream.h>
30
31 /* Message stream */
32
33 enum _message_stream_state
34 {
35 _mss_init,
36 _mss_header,
37 _mss_body,
38 _mss_eof
39 };
40
41 struct _mu_message_stream
42 {
43 struct _mu_stream stream;
44 mu_message_t msg;
45 enum _message_stream_state state;
46 mu_stream_t transport;
47 mu_off_t limit;
48 };
49
50 static int
51 _check_stream_state (struct _mu_message_stream *str)
52 {
53 int rc = 0;
54
55 if (str->transport && mu_stream_eof (str->transport))
56 mu_stream_destroy (&str->transport);
57
58 switch (str->state)
59 {
60 case _mss_init:
61 if (!str->transport)
62 {
63 rc = mu_header_get_streamref (str->msg->header, &str->transport);
64 if (rc == 0)
65 {
66 str->state = _mss_header;
67 rc = mu_stream_seek (str->transport, 0, MU_SEEK_SET, NULL);
68 }
69 }
70 break;
71
72 case _mss_header:
73 if (!str->transport)
74 {
75 rc = mu_body_get_streamref (str->msg->body, &str->transport);
76 if (rc == 0)
77 {
78 str->state = _mss_body;
79 rc = mu_stream_seek (str->transport, 0, MU_SEEK_SET, NULL);
80 }
81 }
82 break;
83
84 case _mss_body:
85 if (!str->transport)
86 str->state = _mss_eof;
87 case _mss_eof:
88 break;
89 }
90 return rc;
91 }
92
93 static void
94 _message_stream_done (struct _mu_stream *str)
95 {
96 struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
97 mu_stream_destroy (&sp->transport);
98 }
99
100 static int
101 _message_stream_flush (struct _mu_stream *str)
102 {
103 struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
104 int rc = _check_stream_state (sp);
105 if (rc)
106 return rc;
107 return mu_stream_flush (sp->transport);
108 }
109
110 static int
111 _message_stream_size (struct _mu_stream *str, mu_off_t *psize)
112 {
113 struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
114 size_t hsize, bsize;
115 mu_header_size (sp->msg->header, &hsize);
116 mu_body_size (sp->msg->body, &bsize);
117 if (psize)
118 *psize = hsize + bsize;
119 return 0;
120 }
121
122 static int
123 _message_stream_seek (struct _mu_stream *str, mu_off_t off, mu_off_t *ppos)
124 {
125 struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
126 size_t hsize, size;
127 int rc;
128
129 rc = _check_stream_state (sp);
130 if (rc)
131 return rc;
132 mu_header_size (sp->msg->header, &hsize);
133 mu_body_size (sp->msg->body, &size);
134
135 if (off < 0 || off >= size + hsize)
136 return ESPIPE;
137
138 switch (sp->state)
139 {
140 case _mss_eof:
141 sp->state = _mss_init;
142 rc = _check_stream_state (sp);
143 if (rc)
144 return rc;
145 /* fall through */
146 case _mss_header:
147 if (off < hsize)
148 break;
149 mu_stream_destroy (&sp->transport);
150 rc = _check_stream_state (sp);
151 if (rc)
152 return rc;
153 /* fall through */
154 case _mss_body:
155 if (off > hsize)
156 off -= hsize;
157 else
158 {
159 mu_stream_destroy (&sp->transport);
160 sp->state = _mss_init;
161 rc = _check_stream_state (sp);
162 if (rc)
163 return rc;
164 }
165
166 break;
167
168 default:
169 break;
170 }
171 rc = mu_stream_seek (sp->transport, off, MU_SEEK_SET, &off);
172 if (rc == 0)
173 {
174 if (sp->state == _mss_body)
175 off += hsize;
176 *ppos = off;
177 }
178 return rc;
179 }
180
181 static int
182 _message_stream_read (struct _mu_stream *str, char *buf, size_t bufsize,
183 size_t *pnread)
184 {
185 struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
186 size_t nread = 0;
187 int rc;
188
189 while (bufsize)
190 {
191 size_t n;
192 rc = _check_stream_state (sp);
193 if (rc)
194 break;
195 if (sp->state == _mss_eof)
196 break;
197 rc = mu_stream_read (sp->transport, buf, bufsize, &n);
198 if (rc)
199 break;
200 if (n == 0)
201 continue;
202 nread += n;
203 buf += n;
204 bufsize -= n;
205 }
206 *pnread = nread;
207 return rc;
208 }
209
210 static int
211 _message_stream_readdelim (struct _mu_stream *str, char *buf, size_t bufsize,
212 int delim, size_t *pnread)
213 {
214 struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
215 size_t nread = 0;
216 int rc;
217
218 while (bufsize)
219 {
220 size_t n;
221 rc = _check_stream_state (sp);
222 if (rc)
223 break;
224 if (sp->state == _mss_eof)
225 break;
226 rc = mu_stream_readdelim (sp->transport, buf, bufsize, delim, &n);
227 if (rc)
228 break;
229 if (n == 0)
230 continue;
231 nread += n;
232 if (buf[n-1] == delim)
233 break;
234 buf += n;
235 bufsize -= n;
236 }
237 *pnread = nread;
238 return rc;
239 }
240
241 #if 0
242 static int
243 _message_stream_write (struct _mu_stream *str,
244 const char *buf, size_t bufsize,
245 size_t *pnwritten)
246 {
247 struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
248
249 /* FIXME */
250 }
251 #endif
252
253 static int
254 _message_stream_create (mu_stream_t *pmsg, mu_message_t msg, int flags)
255 {
256 struct _mu_message_stream *sp;
257
258 sp = (struct _mu_message_stream *) _mu_stream_create (sizeof (*sp),
259 flags |
260 MU_STREAM_SEEK |
261 _MU_STR_OPEN);
262 if (!sp)
263 return ENOMEM;
264
265 sp->stream.read = _message_stream_read;
266 sp->stream.readdelim = _message_stream_readdelim;
267 /* FIXME: Write is not defined */
268 /* sp->stream.write = _message_stream_write;*/
269 sp->stream.done = _message_stream_done;
270 sp->stream.flush = _message_stream_flush;
271 sp->stream.seek = _message_stream_seek;
272 sp->stream.size = _message_stream_size;
273 sp->state = _mss_init;
274 sp->msg = msg;
275 *pmsg = (mu_stream_t) sp;
276 return 0;
277 }
278
279 int
280 mu_message_set_stream (mu_message_t msg, mu_stream_t stream, void *owner)
281 {
282 if (msg == NULL)
283 return EINVAL;
284 if (msg->owner != owner)
285 return EACCES;
286 if (msg->stream)
287 mu_stream_destroy (&msg->stream);
288 msg->stream = stream;
289 msg->flags |= MESSAGE_MODIFIED;
290 msg->flags &= ~MESSAGE_INTERNAL_STREAM;
291 return 0;
292 }
293
294 static int
295 _message_get_stream (mu_message_t msg, mu_stream_t *pstream, int ref)
296 {
297 int status;
298
299 if (msg == NULL)
300 return EINVAL;
301 if (pstream == NULL)
302 return MU_ERR_OUT_PTR_NULL;
303
304 if (msg->stream == NULL)
305 {
306 if (msg->_get_stream)
307 {
308 status = msg->_get_stream (msg, &msg->stream);
309 if (status)
310 return status;
311 }
312 else
313 {
314 mu_header_t hdr;
315 mu_body_t body;
316
317 /* FIXME: Kind of a kludge: make sure the message has header
318 and body initialized. */
319 status = mu_message_get_header (msg, &hdr);
320 if (status)
321 return status;
322 status = mu_message_get_body (msg, &body);
323 if (status)
324 return status;
325
326 status = _message_stream_create (&msg->stream, msg, MU_STREAM_RDWR);
327 if (status)
328 return status;
329 msg->flags |= MESSAGE_INTERNAL_STREAM;
330 }
331 }
332
333 if (!ref)
334 {
335 *pstream = msg->stream;
336 return 0;
337 }
338 return mu_streamref_create (pstream, msg->stream);
339 }
340
341 int
342 mu_message_get_stream (mu_message_t msg, mu_stream_t *pstream)
343 {
344 /* FIXME: Deprecation warning */
345 return _message_get_stream (msg, pstream, 0);
346 }
347
348 int
349 mu_message_get_streamref (mu_message_t msg, mu_stream_t *pstream)
350 {
351 return _message_get_stream (msg, pstream, 1);
352 }
353
354 int
355 mu_message_set_get_stream (mu_message_t msg,
356 int (*_getstr) (mu_message_t, mu_stream_t *),
357 void *owner)
358 {
359 if (msg == NULL)
360 return EINVAL;
361 if (msg->owner != owner)
362 return EACCES;
363 msg->_get_stream = _getstr;
364 return 0;
365 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
3 2010, 2011 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20 #include <stdlib.h>
21
22 #include <mailutils/types.h>
23 #include <mailutils/message.h>
24 #include <mailutils/errno.h>
25 #include <mailutils/sys/message.h>
26
27 int
28 mu_message_get_uid (mu_message_t msg, size_t *puid)
29 {
30 if (msg == NULL)
31 return EINVAL;
32 if (msg->_get_uid)
33 return msg->_get_uid (msg, puid);
34 *puid = 0;
35 return 0;
36 }
37
38 int
39 mu_message_set_uid (mu_message_t msg, int (*_get_uid) (mu_message_t, size_t *),
40 void *owner)
41 {
42 if (msg == NULL)
43 return EINVAL;
44 if (msg->owner != owner)
45 return EACCES;
46 msg->_get_uid = _get_uid;
47 return 0;
48 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
3 2010, 2011 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20 #include <stdlib.h>
21 #include <time.h>
22 #include <string.h>
23 #include <mailutils/types.h>
24 #include <mailutils/message.h>
25 #include <mailutils/errno.h>
26 #include <mailutils/header.h>
27 #include <mailutils/stream.h>
28 #include <mailutils/md5.h>
29 #include <mailutils/sys/message.h>
30
31 int
32 mu_message_get_uidl (mu_message_t msg, char *buffer, size_t buflen,
33 size_t *pwriten)
34 {
35 mu_header_t header = NULL;
36 size_t n = 0;
37 int status;
38
39 if (msg == NULL || buffer == NULL || buflen == 0)
40 return EINVAL;
41
42 buffer[0] = '\0';
43 /* Try the function overload if error fallback. */
44 if (msg->_get_uidl)
45 {
46 status = msg->_get_uidl (msg, buffer, buflen, pwriten);
47 if (status == 0)
48 return status;
49 }
50
51 /* Be compatible with Qpopper ? qppoper saves the UIDL in "X-UIDL".
52 We generate a chksum and save it in the header. */
53 mu_message_get_header (msg, &header);
54 status = mu_header_get_value_unfold (header, "X-UIDL", buffer, buflen, &n);
55 if (status != 0 || n == 0)
56 {
57 size_t uid = 0;
58 struct mu_md5_ctx md5context;
59 mu_stream_t stream = NULL;
60 char buf[1024];
61 unsigned char md5digest[16];
62 char *tmp;
63 n = 0;
64 mu_message_get_uid (msg, &uid);
65 mu_message_get_streamref (msg, &stream);
66 mu_md5_init_ctx (&md5context);
67 status = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
68 if (status == 0)
69 {
70 while (mu_stream_read (stream, buf, sizeof (buf), &n) == 0
71 && n > 0)
72 mu_md5_process_bytes (buf, n, &md5context);
73 mu_md5_finish_ctx (&md5context, md5digest);
74 tmp = buf;
75 for (n = 0; n < 16; n++, tmp += 2)
76 sprintf (tmp, "%02x", md5digest[n]);
77 *tmp = '\0';
78 /* POP3 rfc says that an UID should not be longer than 70. */
79 snprintf (buf + 32, 70, ".%lu.%lu", (unsigned long)time (NULL),
80 (unsigned long) uid);
81
82 mu_header_set_value (header, "X-UIDL", buf, 1);
83 buflen--; /* leave space for the NULL. */
84 strncpy (buffer, buf, buflen)[buflen] = '\0';
85 }
86 mu_stream_destroy (&stream);
87 }
88 return status;
89 }
90
91 int
92 mu_message_set_uidl (mu_message_t msg,
93 int (* _get_uidl) (mu_message_t, char *, size_t, size_t *),
94 void *owner)
95 {
96 if (msg == NULL)
97 return EINVAL;
98 if (msg->owner != owner)
99 return EACCES;
100 msg->_get_uidl = _get_uidl;
101 return 0;
102 }