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: pop3driver_message.c,v 1.15 2008/02/17 13:13:27 hoa Exp $
34  */
35 
36 #ifdef HAVE_CONFIG_H
37 #	include <config.h>
38 #endif
39 
40 #include "pop3driver_message.h"
41 
42 #include "mailmessage_tools.h"
43 #include "pop3driver_tools.h"
44 #include "pop3driver.h"
45 #include "mailpop3.h"
46 #include <stdlib.h>
47 #include <string.h>
48 
49 static int pop3_prefetch(mailmessage * msg_info);
50 
51 static void pop3_prefetch_free(struct generic_message_t * msg);
52 
53 static int pop3_initialize(mailmessage * msg_info);
54 
55 static int pop3_fetch_header(mailmessage * msg_info,
56 			     char ** result,
57 			     size_t * result_len);
58 
59 static int pop3_fetch_size(mailmessage * msg_info,
60 			   size_t * result);
61 
62 static mailmessage_driver local_pop3_message_driver = {
63   /* msg_name */ "pop3",
64 
65   /* msg_initialize */ pop3_initialize,
66   /* msg_uninitialize */ mailmessage_generic_uninitialize,
67 
68   /* msg_flush */ mailmessage_generic_flush,
69   /* msg_check */ NULL,
70 
71   /* msg_fetch_result_free */ mailmessage_generic_fetch_result_free,
72 
73   /* msg_fetch */ mailmessage_generic_fetch,
74   /* msg_fetch_header */ pop3_fetch_header,
75   /* msg_fetch_body */ mailmessage_generic_fetch_body,
76   /* msg_fetch_size */ pop3_fetch_size,
77   /* msg_get_bodystructure */ mailmessage_generic_get_bodystructure,
78   /* msg_fetch_section */ mailmessage_generic_fetch_section,
79   /* msg_fetch_section_header */ mailmessage_generic_fetch_section_header,
80   /* msg_fetch_section_mime */ mailmessage_generic_fetch_section_mime,
81   /* msg_fetch_section_body */ mailmessage_generic_fetch_section_body,
82   /* msg_fetch_envelope */ mailmessage_generic_fetch_envelope,
83 
84   /* msg_get_flags */ NULL
85 };
86 
87 mailmessage_driver * pop3_message_driver = &local_pop3_message_driver;
88 
89 static inline struct pop3_session_state_data *
get_data(mailsession * session)90 get_data(mailsession * session)
91 {
92   return session->sess_data;
93 }
94 
95 
get_pop3_session(mailsession * session)96 static mailpop3 * get_pop3_session(mailsession * session)
97 {
98   return get_data(session)->pop3_session;
99 }
100 
101 
pop3_prefetch(mailmessage * msg_info)102 static int pop3_prefetch(mailmessage * msg_info)
103 {
104   char * msg_content;
105   size_t msg_length;
106   struct generic_message_t * msg;
107   int r;
108 
109   r = pop3driver_retr(msg_info->msg_session, msg_info->msg_index,
110 		      &msg_content, &msg_length);
111   if (r != MAIL_NO_ERROR)
112     return r;
113 
114   msg = msg_info->msg_data;
115 
116   msg->msg_message = msg_content;
117   msg->msg_length = msg_length;
118 
119   return MAIL_NO_ERROR;
120 }
121 
pop3_prefetch_free(struct generic_message_t * msg)122 static void pop3_prefetch_free(struct generic_message_t * msg)
123 {
124   if (msg->msg_message != NULL) {
125     mmap_string_unref(msg->msg_message);
126     msg->msg_message = NULL;
127   }
128 }
129 
pop3_initialize(mailmessage * msg_info)130 static int pop3_initialize(mailmessage * msg_info)
131 {
132   struct generic_message_t * msg;
133   int r;
134   char * uid;
135   struct mailpop3_msg_info * info;
136   mailpop3 * pop3;
137 
138   pop3 = get_pop3_session(msg_info->msg_session);
139 
140   r = mailpop3_get_msg_info(pop3, msg_info->msg_index, &info);
141   switch (r) {
142   case MAILPOP3_NO_ERROR:
143     break;
144   default:
145     return pop3driver_pop3_error_to_mail_error(r);
146   }
147 
148   uid = strdup(info->msg_uidl);
149   if (uid == NULL)
150     return MAIL_ERROR_MEMORY;
151 
152   r = mailmessage_generic_initialize(msg_info);
153   if (r != MAIL_NO_ERROR) {
154     free(uid);
155     return r;
156   }
157 
158   msg = msg_info->msg_data;
159   msg->msg_prefetch = pop3_prefetch;
160   msg->msg_prefetch_free = pop3_prefetch_free;
161   msg_info->msg_uid = uid;
162 
163   return MAIL_NO_ERROR;
164 }
165 
166 
pop3_fetch_header(mailmessage * msg_info,char ** result,size_t * result_len)167 static int pop3_fetch_header(mailmessage * msg_info,
168 			     char ** result,
169 			     size_t * result_len)
170 {
171   struct generic_message_t * msg;
172   char * headers;
173   size_t headers_length;
174   int r;
175 
176   msg = msg_info->msg_data;
177 
178   if (msg->msg_message != NULL)
179     return mailmessage_generic_fetch_header(msg_info,
180         result, result_len);
181 
182   r = pop3driver_header(msg_info->msg_session, msg_info->msg_index,
183 			&headers, &headers_length);
184   if (r != MAIL_NO_ERROR)
185     return r;
186 
187   * result = headers;
188   * result_len = headers_length;
189 
190   return MAIL_NO_ERROR;
191 }
192 
pop3_fetch_size(mailmessage * msg_info,size_t * result)193 static int pop3_fetch_size(mailmessage * msg_info,
194 			   size_t * result)
195 {
196   return pop3driver_size(msg_info->msg_session, msg_info->msg_index, result);
197 }
198