Diffstat (limited to 'libetpan/src/driver/implementation/pop3/pop3driver_message.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libetpan/src/driver/implementation/pop3/pop3driver_message.c | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/libetpan/src/driver/implementation/pop3/pop3driver_message.c b/libetpan/src/driver/implementation/pop3/pop3driver_message.c new file mode 100644 index 0000000..6ea8979 --- a/dev/null +++ b/libetpan/src/driver/implementation/pop3/pop3driver_message.c | |||
@@ -0,0 +1,193 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #include "pop3driver_message.h" | ||
37 | |||
38 | #include "mailmessage_tools.h" | ||
39 | #include "pop3driver_tools.h" | ||
40 | #include "pop3driver.h" | ||
41 | #include "mailpop3.h" | ||
42 | #include <stdlib.h> | ||
43 | #include <string.h> | ||
44 | |||
45 | static int pop3_prefetch(mailmessage * msg_info); | ||
46 | |||
47 | static void pop3_prefetch_free(struct generic_message_t * msg); | ||
48 | |||
49 | static int pop3_initialize(mailmessage * msg_info); | ||
50 | |||
51 | static int pop3_fetch_header(mailmessage * msg_info, | ||
52 | char ** result, | ||
53 | size_t * result_len); | ||
54 | |||
55 | static int pop3_fetch_size(mailmessage * msg_info, | ||
56 | size_t * result); | ||
57 | |||
58 | static mailmessage_driver local_pop3_message_driver = { | ||
59 | .msg_name = "pop3", | ||
60 | |||
61 | .msg_initialize = pop3_initialize, | ||
62 | .msg_uninitialize = mailmessage_generic_uninitialize, | ||
63 | |||
64 | .msg_flush = mailmessage_generic_flush, | ||
65 | .msg_check = NULL, | ||
66 | |||
67 | .msg_fetch_result_free = mailmessage_generic_fetch_result_free, | ||
68 | |||
69 | .msg_fetch = mailmessage_generic_fetch, | ||
70 | .msg_fetch_header = pop3_fetch_header, | ||
71 | .msg_fetch_body = mailmessage_generic_fetch_body, | ||
72 | .msg_fetch_size = pop3_fetch_size, | ||
73 | .msg_get_bodystructure = mailmessage_generic_get_bodystructure, | ||
74 | .msg_fetch_section = mailmessage_generic_fetch_section, | ||
75 | .msg_fetch_section_header = mailmessage_generic_fetch_section_header, | ||
76 | .msg_fetch_section_mime = mailmessage_generic_fetch_section_mime, | ||
77 | .msg_fetch_section_body = mailmessage_generic_fetch_section_body, | ||
78 | .msg_fetch_envelope = mailmessage_generic_fetch_envelope, | ||
79 | |||
80 | .msg_get_flags = NULL, | ||
81 | }; | ||
82 | |||
83 | mailmessage_driver * pop3_message_driver = &local_pop3_message_driver; | ||
84 | |||
85 | static inline struct pop3_session_state_data * | ||
86 | get_data(mailsession * session) | ||
87 | { | ||
88 | return session->sess_data; | ||
89 | } | ||
90 | |||
91 | |||
92 | static mailpop3 * get_pop3_session(mailsession * session) | ||
93 | { | ||
94 | return get_data(session)->pop3_session; | ||
95 | } | ||
96 | |||
97 | |||
98 | static int pop3_prefetch(mailmessage * msg_info) | ||
99 | { | ||
100 | char * msg_content; | ||
101 | size_t msg_length; | ||
102 | struct generic_message_t * msg; | ||
103 | int r; | ||
104 | |||
105 | r = pop3driver_retr(msg_info->msg_session, msg_info->msg_index, | ||
106 | &msg_content, &msg_length); | ||
107 | if (r != MAIL_NO_ERROR) | ||
108 | return r; | ||
109 | |||
110 | msg = msg_info->msg_data; | ||
111 | |||
112 | msg->msg_message = msg_content; | ||
113 | msg->msg_length = msg_length; | ||
114 | |||
115 | return MAIL_NO_ERROR; | ||
116 | } | ||
117 | |||
118 | static void pop3_prefetch_free(struct generic_message_t * msg) | ||
119 | { | ||
120 | if (msg->msg_message != NULL) { | ||
121 | mmap_string_unref(msg->msg_message); | ||
122 | msg->msg_message = NULL; | ||
123 | } | ||
124 | } | ||
125 | |||
126 | static int pop3_initialize(mailmessage * msg_info) | ||
127 | { | ||
128 | struct generic_message_t * msg; | ||
129 | int r; | ||
130 | char * uid; | ||
131 | struct mailpop3_msg_info * info; | ||
132 | mailpop3 * pop3; | ||
133 | |||
134 | pop3 = get_pop3_session(msg_info->msg_session); | ||
135 | |||
136 | r = mailpop3_get_msg_info(pop3, msg_info->msg_index, &info); | ||
137 | switch (r) { | ||
138 | case MAILPOP3_NO_ERROR: | ||
139 | break; | ||
140 | default: | ||
141 | return pop3driver_pop3_error_to_mail_error(r); | ||
142 | } | ||
143 | |||
144 | uid = strdup(info->msg_uidl); | ||
145 | if (uid == NULL) | ||
146 | return MAIL_ERROR_MEMORY; | ||
147 | |||
148 | r = mailmessage_generic_initialize(msg_info); | ||
149 | if (r != MAIL_NO_ERROR) { | ||
150 | free(uid); | ||
151 | return r; | ||
152 | } | ||
153 | |||
154 | msg = msg_info->msg_data; | ||
155 | msg->msg_prefetch = pop3_prefetch; | ||
156 | msg->msg_prefetch_free = pop3_prefetch_free; | ||
157 | msg_info->msg_uid = uid; | ||
158 | |||
159 | return MAIL_NO_ERROR; | ||
160 | } | ||
161 | |||
162 | |||
163 | static int pop3_fetch_header(mailmessage * msg_info, | ||
164 | char ** result, | ||
165 | size_t * result_len) | ||
166 | { | ||
167 | struct generic_message_t * msg; | ||
168 | char * headers; | ||
169 | size_t headers_length; | ||
170 | int r; | ||
171 | |||
172 | msg = msg_info->msg_data; | ||
173 | |||
174 | if (msg->msg_message != NULL) | ||
175 | return mailmessage_generic_fetch_header(msg_info, | ||
176 | result, result_len); | ||
177 | |||
178 | r = pop3driver_header(msg_info->msg_session, msg_info->msg_index, | ||
179 | &headers, &headers_length); | ||
180 | if (r != MAIL_NO_ERROR) | ||
181 | return r; | ||
182 | |||
183 | * result = headers; | ||
184 | * result_len = headers_length; | ||
185 | |||
186 | return MAIL_NO_ERROR; | ||
187 | } | ||
188 | |||
189 | static int pop3_fetch_size(mailmessage * msg_info, | ||
190 | size_t * result) | ||
191 | { | ||
192 | return pop3driver_size(msg_info->msg_session, msg_info->msg_index, result); | ||
193 | } | ||