Diffstat (limited to 'noncore/net/mail/pop3wrapper.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/net/mail/pop3wrapper.cpp | 166 |
1 files changed, 65 insertions, 101 deletions
diff --git a/noncore/net/mail/pop3wrapper.cpp b/noncore/net/mail/pop3wrapper.cpp index 2e70dee..de90dd1 100644 --- a/noncore/net/mail/pop3wrapper.cpp +++ b/noncore/net/mail/pop3wrapper.cpp | |||
@@ -1,254 +1,218 @@ | |||
1 | #include <stdlib.h> | 1 | #include <stdlib.h> |
2 | #include "pop3wrapper.h" | 2 | #include "pop3wrapper.h" |
3 | #include "mailtypes.h" | 3 | #include "mailtypes.h" |
4 | #include "logindialog.h" | 4 | #include "logindialog.h" |
5 | #include <libetpan/libetpan.h> | 5 | #include <libetpan/libetpan.h> |
6 | #include <qpe/global.h> | 6 | #include <qpe/global.h> |
7 | #include <qfile.h> | 7 | #include <qfile.h> |
8 | #include <qstring.h> | ||
8 | 9 | ||
9 | /* we don't fetch messages larger than 5 MB */ | 10 | /* we don't fetch messages larger than 5 MB */ |
10 | #define HARD_MSG_SIZE_LIMIT 5242880 | 11 | #define HARD_MSG_SIZE_LIMIT 5242880 |
11 | 12 | ||
12 | POP3wrapper::POP3wrapper( POP3account *a ) | 13 | POP3wrapper::POP3wrapper( POP3account *a ) |
13 | : Genericwrapper() | 14 | : Genericwrapper() |
14 | { | 15 | { |
15 | account = a; | 16 | account = a; |
16 | m_pop3 = NULL; | 17 | m_pop3 = NULL; |
18 | m_folder = NULL; | ||
17 | msgTempName = a->getFileName()+"_msg_cache"; | 19 | msgTempName = a->getFileName()+"_msg_cache"; |
18 | last_msg_id = 0; | 20 | last_msg_id = 0; |
19 | } | 21 | } |
20 | 22 | ||
21 | POP3wrapper::~POP3wrapper() | 23 | POP3wrapper::~POP3wrapper() |
22 | { | 24 | { |
23 | logout(); | 25 | logout(); |
24 | QFile msg_cache(msgTempName); | 26 | QFile msg_cache(msgTempName); |
25 | if (msg_cache.exists()) { | 27 | if (msg_cache.exists()) { |
26 | msg_cache.remove(); | 28 | msg_cache.remove(); |
27 | } | 29 | } |
28 | } | 30 | } |
29 | 31 | ||
30 | void POP3wrapper::pop3_progress( size_t current, size_t maximum ) | 32 | void POP3wrapper::pop3_progress( size_t current, size_t maximum ) |
31 | { | 33 | { |
32 | //qDebug( "POP3: %i of %i", current, maximum ); | 34 | qDebug( "POP3: %i of %i", current, maximum ); |
33 | } | 35 | } |
34 | 36 | ||
35 | RecBody POP3wrapper::fetchBody( const RecMail &mail ) | 37 | RecBody POP3wrapper::fetchBody( const RecMail &mail ) |
36 | { | 38 | { |
37 | int err = MAILPOP3_NO_ERROR; | 39 | int err = MAILPOP3_NO_ERROR; |
38 | char *message; | 40 | char *message = 0; |
39 | size_t length = 0; | 41 | size_t length = 0; |
40 | 42 | ||
41 | login(); | 43 | login(); |
42 | if ( !m_pop3 ) { | 44 | if ( !m_pop3 ) { |
43 | return RecBody(); | 45 | return RecBody(); |
44 | } | 46 | } |
45 | 47 | ||
46 | RecBody body; | 48 | RecBody body; |
47 | mailmessage * msg = 0; | 49 | mailmessage * mailmsg; |
48 | |||
49 | QFile msg_cache(msgTempName); | ||
50 | |||
51 | if (mail.Msgsize()>HARD_MSG_SIZE_LIMIT) { | 50 | if (mail.Msgsize()>HARD_MSG_SIZE_LIMIT) { |
52 | qDebug("Message to large: %i",mail.Msgsize()); | 51 | qDebug("Message to large: %i",mail.Msgsize()); |
53 | return body; | 52 | return body; |
54 | } | 53 | } |
54 | |||
55 | QFile msg_cache(msgTempName); | ||
56 | |||
55 | cleanMimeCache(); | 57 | cleanMimeCache(); |
58 | |||
56 | if (mail.getNumber()!=last_msg_id) { | 59 | if (mail.getNumber()!=last_msg_id) { |
57 | if (msg_cache.exists()) { | 60 | if (msg_cache.exists()) { |
58 | msg_cache.remove(); | 61 | msg_cache.remove(); |
59 | } | 62 | } |
60 | msg_cache.open(IO_ReadWrite|IO_Truncate); | 63 | msg_cache.open(IO_ReadWrite|IO_Truncate); |
61 | last_msg_id = mail.getNumber(); | 64 | last_msg_id = mail.getNumber(); |
62 | err = mailpop3_retr( m_pop3, mail.getNumber(), &message, &length ); | 65 | err = mailsession_get_message(m_folder->fld_session, mail.getNumber(), &mailmsg); |
63 | if ( err != MAILPOP3_NO_ERROR ) { | 66 | err = mailmessage_fetch(mailmsg,&message,&length); |
64 | qDebug( "POP3: error retrieving body with index %i", mail.getNumber() ); | ||
65 | last_msg_id = 0; | ||
66 | return RecBody(); | ||
67 | } | ||
68 | msg_cache.writeBlock(message,length); | 67 | msg_cache.writeBlock(message,length); |
69 | } else { | 68 | } else { |
70 | QString msg=""; | 69 | QString msg=""; |
71 | msg_cache.open(IO_ReadOnly); | 70 | msg_cache.open(IO_ReadOnly); |
72 | message = new char[4096]; | 71 | message = new char[4096]; |
73 | memset(message,0,4096); | 72 | memset(message,0,4096); |
74 | while (msg_cache.readBlock(message,4095)>0) { | 73 | while (msg_cache.readBlock(message,4095)>0) { |
75 | msg+=message; | 74 | msg+=message; |
76 | memset(message,0,4096); | 75 | memset(message,0,4096); |
77 | } | 76 | } |
78 | delete message; | 77 | delete message; |
79 | message = (char*)malloc(msg.length()+1*sizeof(char)); | 78 | message = (char*)malloc(msg.length()+1*sizeof(char)); |
80 | memset(message,0,msg.length()+1); | 79 | memset(message,0,msg.length()+1); |
81 | memcpy(message,msg.latin1(),msg.length()); | 80 | memcpy(message,msg.latin1(),msg.length()); |
81 | /* transform to libetpan stuff */ | ||
82 | mailmsg = mailmessage_new(); | ||
83 | mailmessage_init(mailmsg, NULL, data_message_driver, 0, strlen(message)); | ||
84 | generic_message_t * msg_data; | ||
85 | msg_data = (generic_message_t *)mailmsg->msg_data; | ||
86 | msg_data->msg_fetched = 1; | ||
87 | msg_data->msg_message = message; | ||
88 | msg_data->msg_length = strlen(message); | ||
82 | } | 89 | } |
90 | body = parseMail(mailmsg); | ||
83 | 91 | ||
84 | /* transform to libetpan stuff */ | ||
85 | msg = mailmessage_new(); | ||
86 | mailmessage_init(msg, NULL, data_message_driver, 0, strlen(message)); | ||
87 | generic_message_t * msg_data; | ||
88 | msg_data = (generic_message_t *)msg->msg_data; | ||
89 | msg_data->msg_fetched = 1; | ||
90 | msg_data->msg_message = message; | ||
91 | msg_data->msg_length = strlen(message); | ||
92 | |||
93 | /* parse the mail */ | ||
94 | body = parseMail(msg); | ||
95 | |||
96 | /* clean up */ | 92 | /* clean up */ |
97 | mailmessage_free(msg); | 93 | if (mailmsg) mailmessage_free(mailmsg); |
98 | free(message); | 94 | if (message) free(message); |
99 | 95 | ||
100 | /* finish */ | ||
101 | return body; | 96 | return body; |
102 | } | 97 | } |
103 | 98 | ||
104 | void POP3wrapper::listMessages(const QString &, QList<RecMail> &target ) | 99 | void POP3wrapper::listMessages(const QString &, QList<RecMail> &target ) |
105 | { | 100 | { |
106 | int err = MAILPOP3_NO_ERROR; | ||
107 | char * header = 0; | ||
108 | /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ | ||
109 | size_t length = 0; | ||
110 | carray * messages = 0; | ||
111 | |||
112 | login(); | 101 | login(); |
113 | if (!m_pop3) return; | 102 | if (!m_pop3) return; |
114 | 103 | uint32_t res_messages,res_recent,res_unseen; | |
115 | mailpop3_list( m_pop3, &messages ); | 104 | mailsession_status_folder(m_folder->fld_session,"INBOX",&res_messages,&res_recent,&res_unseen); |
116 | 105 | parseList(target,m_folder->fld_session,"INBOX"); | |
117 | for (unsigned int i = 0; i < carray_count(messages);++i) { | 106 | Global::statusMessage( tr("Mailbox contains %1 mail(s)").arg(res_messages)); |
118 | mailpop3_msg_info *info; | ||
119 | err = mailpop3_get_msg_info(m_pop3,i+1,&info); | ||
120 | if (info->msg_deleted) | ||
121 | continue; | ||
122 | err = mailpop3_header( m_pop3, info->msg_index, &header, &length ); | ||
123 | if ( err != MAILPOP3_NO_ERROR ) { | ||
124 | qDebug( "POP3: error retrieving header msgid: %i", info->msg_index ); | ||
125 | free(header); | ||
126 | return; | ||
127 | } | ||
128 | RecMail *mail = parseHeader( header ); | ||
129 | mail->setNumber( info->msg_index ); | ||
130 | mail->setWrapper(this); | ||
131 | mail->setMsgsize(info->msg_size); | ||
132 | target.append( mail ); | ||
133 | free(header); | ||
134 | } | ||
135 | Global::statusMessage( tr("Mailbox contains %1 mail(s)").arg(carray_count(messages)-m_pop3->pop3_deleted_count)); | ||
136 | } | 107 | } |
137 | 108 | ||
138 | void POP3wrapper::login() | 109 | void POP3wrapper::login() |
139 | { | 110 | { |
140 | /* we'll hold the line */ | 111 | /* we'll hold the line */ |
141 | if ( m_pop3 != NULL ) return; | 112 | if ( m_pop3 != NULL ) return; |
142 | 113 | ||
143 | const char *server, *user, *pass; | 114 | const char *server, *user, *pass; |
144 | uint16_t port; | 115 | uint16_t port; |
145 | int err = MAILPOP3_NO_ERROR; | 116 | int err = MAILPOP3_NO_ERROR; |
146 | 117 | ||
147 | server = account->getServer().latin1(); | 118 | server = account->getServer().latin1(); |
148 | port = account->getPort().toUInt(); | 119 | port = account->getPort().toUInt(); |
149 | 120 | ||
150 | if ( account->getUser().isEmpty() || account->getPassword().isEmpty() ) { | 121 | if ( account->getUser().isEmpty() || account->getPassword().isEmpty() ) { |
151 | LoginDialog login( account->getUser(), account->getPassword(), NULL, 0, true ); | 122 | LoginDialog login( account->getUser(), account->getPassword(), NULL, 0, true ); |
152 | login.show(); | 123 | login.show(); |
153 | if ( QDialog::Accepted == login.exec() ) { | 124 | if ( QDialog::Accepted == login.exec() ) { |
154 | // ok | 125 | // ok |
155 | user = strdup( login.getUser().latin1() ); | 126 | user = login.getUser().latin1(); |
156 | pass = strdup( login.getPassword().latin1() ); | 127 | pass = login.getPassword().latin1(); |
157 | } else { | 128 | } else { |
158 | // cancel | 129 | // cancel |
159 | qDebug( "POP3: Login canceled" ); | 130 | qDebug( "POP3: Login canceled" ); |
160 | return; | 131 | return; |
161 | } | 132 | } |
162 | } else { | 133 | } else { |
163 | user = account->getUser().latin1(); | 134 | user = account->getUser().latin1(); |
164 | pass = account->getPassword().latin1(); | 135 | pass = account->getPassword().latin1(); |
165 | } | 136 | } |
166 | 137 | ||
167 | m_pop3 = mailpop3_new( 200, &pop3_progress ); | 138 | bool ssl = account->getSSL(); |
168 | 139 | ||
169 | // connect | 140 | m_pop3=mailstorage_new(NULL); |
170 | if (account->getSSL()) { | 141 | pop3_mailstorage_init(m_pop3,(char*)server,port,NULL,CONNECTION_TYPE_TRY_STARTTLS,POP3_AUTH_TYPE_TRY_APOP, |
171 | err = mailpop3_ssl_connect( m_pop3, (char*)server, port ); | 142 | (char*)user,(char*)pass,0,0,0); |
172 | } else { | 143 | |
173 | err = mailpop3_socket_connect( m_pop3, (char*)server, port ); | 144 | m_folder = mailfolder_new(m_pop3, NULL, NULL); |
174 | } | ||
175 | 145 | ||
176 | if ( err != MAILPOP3_NO_ERROR ) { | 146 | if (m_folder==0) { |
177 | qDebug( "pop3: error connecting to %s\n reason: %s", server, | 147 | Global::statusMessage(tr("Error initializing folder")); |
178 | m_pop3->pop3_response ); | 148 | mailstorage_free(m_pop3); |
179 | mailpop3_free( m_pop3 ); | ||
180 | m_pop3 = NULL; | 149 | m_pop3 = NULL; |
181 | return; | 150 | return; |
182 | } | 151 | } |
183 | qDebug( "POP3: connected!" ); | 152 | err = mailfolder_connect(m_folder); |
184 | 153 | if (err != MAIL_NO_ERROR) { | |
185 | // login | 154 | Global::statusMessage(tr("Error initializing folder")); |
186 | // TODO: decide if apop or plain login should be used | 155 | mailfolder_free(m_folder); |
187 | err = mailpop3_login( m_pop3, (char *) user, (char *) pass ); | 156 | m_folder = 0; |
188 | if ( err != MAILPOP3_NO_ERROR ) { | 157 | mailstorage_free(m_pop3); |
189 | qDebug( "pop3: error logging in: %s", m_pop3->pop3_response ); | 158 | m_pop3 = 0; |
190 | logout(); | ||
191 | return; | ||
192 | } | 159 | } |
193 | |||
194 | qDebug( "POP3: logged in!" ); | 160 | qDebug( "POP3: logged in!" ); |
195 | } | 161 | } |
196 | 162 | ||
197 | void POP3wrapper::logout() | 163 | void POP3wrapper::logout() |
198 | { | 164 | { |
199 | int err = MAILPOP3_NO_ERROR; | 165 | int err = MAILPOP3_NO_ERROR; |
200 | if ( m_pop3 == NULL ) return; | 166 | if ( m_pop3 == NULL ) return; |
201 | err = mailpop3_quit( m_pop3 ); | 167 | mailfolder_free(m_folder); |
202 | mailpop3_free( m_pop3 ); | 168 | m_folder = 0; |
203 | m_pop3 = NULL; | 169 | mailstorage_free(m_pop3); |
170 | m_pop3 = 0; | ||
204 | } | 171 | } |
205 | 172 | ||
206 | 173 | ||
207 | QList<Folder>* POP3wrapper::listFolders() | 174 | QList<Folder>* POP3wrapper::listFolders() |
208 | { | 175 | { |
209 | QList<Folder> * folders = new QList<Folder>(); | 176 | QList<Folder> * folders = new QList<Folder>(); |
210 | folders->setAutoDelete( false ); | 177 | folders->setAutoDelete( false ); |
211 | Folder*inb=new Folder("INBOX","/"); | 178 | Folder*inb=new Folder("INBOX","/"); |
212 | folders->append(inb); | 179 | folders->append(inb); |
213 | return folders; | 180 | return folders; |
214 | } | 181 | } |
215 | 182 | ||
216 | void POP3wrapper::deleteMail(const RecMail&mail) | 183 | void POP3wrapper::deleteMail(const RecMail&mail) |
217 | { | 184 | { |
218 | login(); | 185 | login(); |
219 | if (!m_pop3) return; | 186 | if (!m_pop3) return; |
220 | int err = mailpop3_dele(m_pop3,mail.getNumber()); | 187 | int err = mailsession_remove_message(m_folder->fld_session,mail.getNumber()); |
221 | if (err != MAILPOP3_NO_ERROR) { | 188 | if (err != MAIL_NO_ERROR) { |
222 | Global::statusMessage(tr("error deleting mail")); | 189 | Global::statusMessage(tr("error deleting mail")); |
223 | } | 190 | } |
224 | } | 191 | } |
225 | 192 | ||
226 | void POP3wrapper::answeredMail(const RecMail&) | 193 | void POP3wrapper::answeredMail(const RecMail&) |
227 | { | 194 | { |
228 | } | 195 | } |
229 | 196 | ||
230 | int POP3wrapper::deleteAllMail(const Folder*) | 197 | int POP3wrapper::deleteAllMail(const Folder*) |
231 | { | 198 | { |
232 | login(); | 199 | login(); |
233 | if (!m_pop3) return 0; | 200 | if (!m_pop3) return 0; |
234 | carray * messages = 0; | ||
235 | |||
236 | /* if connected this info is cached! */ | ||
237 | int err = 0; | ||
238 | mailpop3_list( m_pop3, &messages ); | ||
239 | |||
240 | int res = 1; | 201 | int res = 1; |
241 | for (unsigned int i = 0; messages!=0 && i < carray_count(messages);++i) { | 202 | |
242 | mailpop3_msg_info *info; | 203 | uint32_t result = 0; |
243 | err = mailpop3_get_msg_info(m_pop3,i+1,&info); | 204 | int err = mailsession_messages_number(m_folder->fld_session,NULL,&result); |
244 | if (info->msg_deleted) | 205 | if (err != MAIL_NO_ERROR) { |
245 | continue; | 206 | Global::statusMessage(tr("Error getting folder info")); |
246 | err = mailpop3_dele(m_pop3,i+1); | 207 | return 0; |
247 | if (err != MAILPOP3_NO_ERROR) { | 208 | } |
209 | for (unsigned int i = 0; i < result; ++i) { | ||
210 | err = mailsession_remove_message(m_folder->fld_session,i+1); | ||
211 | if (err != MAIL_NO_ERROR) { | ||
248 | Global::statusMessage(tr("Error deleting mail %1").arg(i+1)); | 212 | Global::statusMessage(tr("Error deleting mail %1").arg(i+1)); |
249 | res = 0; | 213 | res=0; |
250 | break; | ||
251 | } | 214 | } |
252 | } | 215 | break; |
216 | } | ||
253 | return res; | 217 | return res; |
254 | } | 218 | } |