Diffstat (limited to 'kmicromail/libmailwrapper/pop3wrapper.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | kmicromail/libmailwrapper/pop3wrapper.cpp | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/kmicromail/libmailwrapper/pop3wrapper.cpp b/kmicromail/libmailwrapper/pop3wrapper.cpp index e5d083a..7a84b30 100644 --- a/kmicromail/libmailwrapper/pop3wrapper.cpp +++ b/kmicromail/libmailwrapper/pop3wrapper.cpp | |||
@@ -1,48 +1,49 @@ | |||
1 | // CHANGED 2004-09-31 Lutz Rogowski | ||
1 | #include <stdlib.h> | 2 | #include <stdlib.h> |
2 | #include "pop3wrapper.h" | 3 | #include "pop3wrapper.h" |
3 | #include "mailtypes.h" | 4 | #include "mailtypes.h" |
4 | #include "logindialog.h" | 5 | #include "logindialog.h" |
5 | #include <libetpan/libetpan.h> | 6 | #include <libetpan/libetpan.h> |
6 | 7 | ||
7 | 8 | ||
8 | #include <qpe/global.h> | 9 | #include <qpe/global.h> |
9 | #include <qfile.h> | 10 | #include <qfile.h> |
10 | 11 | ||
11 | /* we don't fetch messages larger than 5 MB */ | 12 | /* we don't fetch messages larger than 5 MB */ |
12 | #define HARD_MSG_SIZE_LIMIT 5242880 | 13 | #define HARD_MSG_SIZE_LIMIT 5242880 |
13 | 14 | ||
14 | using namespace Opie::Core; | 15 | using namespace Opie::Core; |
15 | POP3wrapper::POP3wrapper( POP3account *a ) | 16 | POP3wrapper::POP3wrapper( POP3account *a ) |
16 | : Genericwrapper() { | 17 | : Genericwrapper() { |
17 | account = a; | 18 | account = a; |
18 | m_pop3 = NULL; | 19 | m_pop3 = NULL; |
19 | msgTempName = a->getFileName()+"_msg_cache"; | 20 | msgTempName = a->getFileName()+"_msg_cache"; |
20 | last_msg_id = 0; | 21 | last_msg_id = 0; |
21 | } | 22 | } |
22 | 23 | ||
23 | POP3wrapper::~POP3wrapper() { | 24 | POP3wrapper::~POP3wrapper() { |
24 | logout(); | 25 | logout(); |
25 | QFile msg_cache(msgTempName); | 26 | QFile msg_cache(msgTempName); |
26 | if (msg_cache.exists()) { | 27 | if (msg_cache.exists()) { |
27 | msg_cache.remove(); | 28 | msg_cache.remove(); |
28 | } | 29 | } |
29 | } | 30 | } |
30 | 31 | ||
31 | void POP3wrapper::pop3_progress( size_t current, size_t maximum ) { | 32 | void POP3wrapper::pop3_progress( size_t current, size_t maximum ) { |
32 | ; // odebug << "POP3: " << current << " of " << maximum << "" << oendl; | 33 | ; // odebug << "POP3: " << current << " of " << maximum << "" << oendl; |
33 | } | 34 | } |
34 | 35 | ||
35 | RecBodyP POP3wrapper::fetchBody( const RecMailP &mail ) { | 36 | RecBodyP POP3wrapper::fetchBody( const RecMailP &mail ) { |
36 | int err = MAILPOP3_NO_ERROR; | 37 | int err = MAILPOP3_NO_ERROR; |
37 | char *message = 0; | 38 | char *message = 0; |
38 | size_t length = 0; | 39 | size_t length = 0; |
39 | 40 | ||
40 | RecBodyP body = new RecBody(); | 41 | RecBodyP body = new RecBody(); |
41 | 42 | ||
42 | login(); | 43 | login(); |
43 | if ( !m_pop3 ) { | 44 | if ( !m_pop3 ) { |
44 | return body; | 45 | return body; |
45 | } | 46 | } |
46 | 47 | ||
47 | mailmessage * mailmsg; | 48 | mailmessage * mailmsg; |
48 | if (mail->Msgsize()>HARD_MSG_SIZE_LIMIT) { | 49 | if (mail->Msgsize()>HARD_MSG_SIZE_LIMIT) { |
@@ -51,192 +52,208 @@ RecBodyP POP3wrapper::fetchBody( const RecMailP &mail ) { | |||
51 | } | 52 | } |
52 | 53 | ||
53 | QFile msg_cache(msgTempName); | 54 | QFile msg_cache(msgTempName); |
54 | 55 | ||
55 | cleanMimeCache(); | 56 | cleanMimeCache(); |
56 | 57 | ||
57 | if (mail->getNumber()!=last_msg_id) { | 58 | if (mail->getNumber()!=last_msg_id) { |
58 | if (msg_cache.exists()) { | 59 | if (msg_cache.exists()) { |
59 | msg_cache.remove(); | 60 | msg_cache.remove(); |
60 | } | 61 | } |
61 | msg_cache.open(IO_ReadWrite|IO_Truncate); | 62 | msg_cache.open(IO_ReadWrite|IO_Truncate); |
62 | last_msg_id = mail->getNumber(); | 63 | last_msg_id = mail->getNumber(); |
63 | err = mailsession_get_message(m_pop3->sto_session, mail->getNumber(), &mailmsg); | 64 | err = mailsession_get_message(m_pop3->sto_session, mail->getNumber(), &mailmsg); |
64 | err = mailmessage_fetch(mailmsg,&message,&length); | 65 | err = mailmessage_fetch(mailmsg,&message,&length); |
65 | msg_cache.writeBlock(message,length); | 66 | msg_cache.writeBlock(message,length); |
66 | } else { | 67 | } else { |
67 | QString msg=""; | 68 | QString msg=""; |
68 | msg_cache.open(IO_ReadOnly); | 69 | msg_cache.open(IO_ReadOnly); |
69 | message = new char[4096]; | 70 | message = new char[4096]; |
70 | memset(message,0,4096); | 71 | memset(message,0,4096); |
71 | while (msg_cache.readBlock(message,4095)>0) { | 72 | while (msg_cache.readBlock(message,4095)>0) { |
72 | msg+=message; | 73 | msg+=message; |
73 | memset(message,0,4096); | 74 | memset(message,0,4096); |
74 | } | 75 | } |
75 | delete message; | 76 | delete message; |
76 | message = (char*)malloc(msg.length()+1*sizeof(char)); | 77 | message = (char*)malloc(msg.length()+1*sizeof(char)); |
77 | memset(message,0,msg.length()+1); | 78 | memset(message,0,msg.length()+1); |
78 | memcpy(message,msg.latin1(),msg.length()); | 79 | memcpy(message,msg.latin1(),msg.length()); |
79 | /* transform to libetpan stuff */ | 80 | /* transform to libetpan stuff */ |
80 | mailmsg = mailmessage_new(); | 81 | mailmsg = mailmessage_new(); |
81 | mailmessage_init(mailmsg, NULL, data_message_driver, 0, strlen(message)); | 82 | mailmessage_init(mailmsg, NULL, data_message_driver, 0, strlen(message)); |
82 | generic_message_t * msg_data; | 83 | generic_message_t * msg_data; |
83 | msg_data = (generic_message_t *)mailmsg->msg_data; | 84 | msg_data = (generic_message_t *)mailmsg->msg_data; |
84 | msg_data->msg_fetched = 1; | 85 | msg_data->msg_fetched = 1; |
85 | msg_data->msg_message = message; | 86 | msg_data->msg_message = message; |
86 | msg_data->msg_length = strlen(message); | 87 | msg_data->msg_length = strlen(message); |
87 | } | 88 | } |
88 | body = parseMail(mailmsg); | 89 | body = parseMail(mailmsg); |
89 | 90 | ||
90 | /* clean up */ | 91 | /* clean up */ |
91 | if (mailmsg) | 92 | if (mailmsg) |
92 | mailmessage_free(mailmsg); | 93 | mailmessage_free(mailmsg); |
93 | if (message) | 94 | if (message) |
94 | free(message); | 95 | free(message); |
95 | 96 | ||
96 | return body; | 97 | return body; |
97 | } | 98 | } |
98 | 99 | ||
99 | void POP3wrapper::listMessages(const QString &, QValueList<Opie::Core::OSmartPointer<RecMail> > &target ) | 100 | void POP3wrapper::listMessages(const QString &, QValueList<Opie::Core::OSmartPointer<RecMail> > &target, int maxSizeInKb ) |
100 | { | 101 | { |
101 | login(); | 102 | login(); |
102 | if (!m_pop3) | 103 | if (!m_pop3) |
103 | return; | 104 | return; |
104 | uint32_t res_messages,res_recent,res_unseen; | 105 | uint32_t res_messages,res_recent,res_unseen; |
105 | mailsession_status_folder(m_pop3->sto_session,"INBOX",&res_messages,&res_recent,&res_unseen); | 106 | mailsession_status_folder(m_pop3->sto_session,"INBOX",&res_messages,&res_recent,&res_unseen); |
106 | parseList(target,m_pop3->sto_session,"INBOX"); | 107 | parseList(target,m_pop3->sto_session,"INBOX", false, maxSizeInKb); |
107 | Global::statusMessage( tr("Mailbox contains %1 mail(s)").arg(res_messages)); | 108 | Global::statusMessage( tr("Mailbox contains %1 mail(s)").arg(res_messages)); |
108 | } | 109 | } |
109 | 110 | ||
110 | void POP3wrapper::login() | 111 | void POP3wrapper::login() |
111 | { | 112 | { |
112 | if (account->getOffline()) | 113 | if (account->getOffline()) |
113 | return; | 114 | return; |
114 | /* we'll hold the line */ | 115 | /* we'll hold the line */ |
115 | if ( m_pop3 != NULL ) | 116 | if ( m_pop3 != NULL ) |
116 | return; | 117 | return; |
117 | 118 | ||
118 | const char *server, *user, *pass; | 119 | const char *server, *user, *pass; |
119 | uint16_t port; | 120 | uint16_t port; |
120 | int err = MAILPOP3_NO_ERROR; | 121 | int err = MAILPOP3_NO_ERROR; |
121 | 122 | ||
122 | server = account->getServer().latin1(); | 123 | server = account->getServer().latin1(); |
123 | port = account->getPort().toUInt(); | 124 | port = account->getPort().toUInt(); |
124 | 125 | ||
125 | if ( account->getUser().isEmpty() || account->getPassword().isEmpty() ) { | 126 | if ( account->getUser().isEmpty() || account->getPassword().isEmpty() ) { |
126 | LoginDialog login( account->getUser(), account->getPassword(), NULL, 0, true ); | 127 | LoginDialog login( account->getUser(), account->getPassword(), NULL, 0, true ); |
127 | login.show(); | 128 | login.show(); |
128 | if ( QDialog::Accepted == login.exec() ) { | 129 | if ( QDialog::Accepted == login.exec() ) { |
129 | // ok | 130 | // ok |
130 | user = login.getUser().latin1(); | 131 | user = login.getUser().latin1(); |
131 | pass = login.getPassword().latin1(); | 132 | pass = login.getPassword().latin1(); |
132 | } else { | 133 | } else { |
133 | // cancel | 134 | // cancel |
134 | ; // odebug << "POP3: Login canceled" << oendl; | 135 | ; // odebug << "POP3: Login canceled" << oendl; |
135 | return; | 136 | return; |
136 | } | 137 | } |
137 | } else { | 138 | } else { |
138 | user = account->getUser().latin1(); | 139 | user = account->getUser().latin1(); |
139 | pass = account->getPassword().latin1(); | 140 | pass = account->getPassword().latin1(); |
140 | } | 141 | } |
141 | 142 | ||
142 | // bool ssl = account->getSSL(); | 143 | // bool ssl = account->getSSL(); |
143 | 144 | ||
144 | m_pop3=mailstorage_new(NULL); | 145 | m_pop3=mailstorage_new(NULL); |
145 | 146 | ||
146 | int conntypeset = account->ConnectionType(); | 147 | int conntypeset = account->ConnectionType(); |
147 | int conntype = 0; | 148 | int conntype = 0; |
148 | if ( conntypeset == 3 ) { | 149 | if ( conntypeset == 3 ) { |
149 | conntype = CONNECTION_TYPE_COMMAND; | 150 | conntype = CONNECTION_TYPE_COMMAND; |
150 | } else if ( conntypeset == 2 ) { | 151 | } else if ( conntypeset == 2 ) { |
151 | conntype = CONNECTION_TYPE_TLS; | 152 | conntype = CONNECTION_TYPE_TLS; |
152 | } else if ( conntypeset == 1 ) { | 153 | } else if ( conntypeset == 1 ) { |
153 | conntype = CONNECTION_TYPE_STARTTLS; | 154 | conntype = CONNECTION_TYPE_STARTTLS; |
154 | } else if ( conntypeset == 0 ) { | 155 | } else if ( conntypeset == 0 ) { |
155 | conntype = CONNECTION_TYPE_TRY_STARTTLS; | 156 | conntype = CONNECTION_TYPE_TRY_STARTTLS; |
156 | } | 157 | } |
157 | 158 | ||
158 | //(ssl?CONNECTION_TYPE_TLS:CONNECTION_TYPE_PLAIN); | 159 | //(ssl?CONNECTION_TYPE_TLS:CONNECTION_TYPE_PLAIN); |
159 | 160 | ||
160 | pop3_mailstorage_init(m_pop3,(char*)server, port, NULL, conntype, POP3_AUTH_TYPE_PLAIN, | 161 | pop3_mailstorage_init(m_pop3,(char*)server, port, NULL, conntype, POP3_AUTH_TYPE_PLAIN, |
161 | (char*)user,(char*)pass,0,0,0); | 162 | (char*)user,(char*)pass,0,0,0); |
162 | 163 | ||
163 | 164 | ||
164 | err = mailstorage_connect(m_pop3); | 165 | err = mailstorage_connect(m_pop3); |
165 | if (err != MAIL_NO_ERROR) { | 166 | if (err != MAIL_NO_ERROR) { |
166 | ; // odebug << QString( "FEHLERNUMMER %1" ).arg( err ) << oendl; | 167 | ; // odebug << QString( "FEHLERNUMMER %1" ).arg( err ) << oendl; |
167 | Global::statusMessage(tr("Error initializing folder")); | 168 | Global::statusMessage(tr("Error initializing folder")); |
168 | mailstorage_free(m_pop3); | 169 | mailstorage_free(m_pop3); |
169 | m_pop3 = 0; | 170 | m_pop3 = 0; |
170 | } else { | 171 | } else { |
171 | mailsession * session = m_pop3->sto_session; | 172 | mailsession * session = m_pop3->sto_session; |
172 | mailpop3 * mail = ( ( pop3_session_state_data * )session->sess_data )->pop3_session; | 173 | mailpop3 * mail = ( ( pop3_session_state_data * )session->sess_data )->pop3_session; |
173 | if (mail) { | 174 | if (mail) { |
174 | mail->pop3_progr_fun = &pop3_progress; | 175 | mail->pop3_progr_fun = &pop3_progress; |
175 | } | 176 | } |
176 | } | 177 | } |
177 | } | 178 | } |
178 | 179 | ||
179 | void POP3wrapper::logout() | 180 | void POP3wrapper::logout() |
180 | { | 181 | { |
181 | if ( m_pop3 == NULL ) | 182 | if ( m_pop3 == NULL ) |
182 | return; | 183 | return; |
183 | mailstorage_free(m_pop3); | 184 | mailstorage_free(m_pop3); |
184 | m_pop3 = 0; | 185 | m_pop3 = 0; |
185 | } | 186 | } |
186 | 187 | ||
187 | 188 | ||
188 | QValueList<Opie::Core::OSmartPointer<Folder> >* POP3wrapper::listFolders() { | 189 | QValueList<Opie::Core::OSmartPointer<Folder> >* POP3wrapper::listFolders() { |
189 | QValueList<Opie::Core::OSmartPointer<Folder> >* folders = new QValueList<FolderP>(); | 190 | QValueList<Opie::Core::OSmartPointer<Folder> >* folders = new QValueList<FolderP>(); |
190 | FolderP inb=new Folder("INBOX","/"); | 191 | FolderP inb=new Folder("INBOX","/"); |
191 | folders->append(inb); | 192 | folders->append(inb); |
192 | return folders; | 193 | return folders; |
193 | } | 194 | } |
194 | 195 | ||
196 | void POP3wrapper::deleteMailList(QValueList<RecMailP>&target) | ||
197 | { | ||
198 | login(); | ||
199 | if (!m_pop3) | ||
200 | return; | ||
201 | int iii = 0; | ||
202 | int count = target.count(); | ||
203 | while (iii < count ) { | ||
204 | RecMailP mail = (*target.at( iii )); | ||
205 | int err = mailsession_remove_message(m_pop3->sto_session,mail->getNumber()); | ||
206 | if (err != MAIL_NO_ERROR) { | ||
207 | Global::statusMessage(tr("error deleting mail")); | ||
208 | } | ||
209 | ++iii; | ||
210 | } | ||
211 | } | ||
195 | void POP3wrapper::deleteMail(const RecMailP&mail) { | 212 | void POP3wrapper::deleteMail(const RecMailP&mail) { |
196 | login(); | 213 | login(); |
197 | if (!m_pop3) | 214 | if (!m_pop3) |
198 | return; | 215 | return; |
199 | int err = mailsession_remove_message(m_pop3->sto_session,mail->getNumber()); | 216 | int err = mailsession_remove_message(m_pop3->sto_session,mail->getNumber()); |
200 | if (err != MAIL_NO_ERROR) { | 217 | if (err != MAIL_NO_ERROR) { |
201 | Global::statusMessage(tr("error deleting mail")); | 218 | Global::statusMessage(tr("error deleting mail")); |
202 | } | 219 | } |
203 | } | 220 | } |
204 | 221 | ||
205 | void POP3wrapper::answeredMail(const RecMailP&) {} | 222 | void POP3wrapper::answeredMail(const RecMailP&) {} |
206 | 223 | ||
207 | int POP3wrapper::deleteAllMail(const FolderP&) { | 224 | int POP3wrapper::deleteAllMail(const FolderP&) { |
208 | login(); | 225 | login(); |
209 | if (!m_pop3) | 226 | if (!m_pop3) |
210 | return 0; | 227 | return 0; |
211 | int res = 1; | 228 | int res = 1; |
212 | 229 | ||
213 | uint32_t result = 0; | 230 | uint32_t result = 0; |
214 | int err = mailsession_messages_number(m_pop3->sto_session,NULL,&result); | 231 | int err = mailsession_messages_number(m_pop3->sto_session,NULL,&result); |
215 | if (err != MAIL_NO_ERROR) { | 232 | if (err != MAIL_NO_ERROR) { |
216 | Global::statusMessage(tr("Error getting folder info")); | 233 | Global::statusMessage(tr("Error getting folder info")); |
217 | return 0; | 234 | return 0; |
218 | } | 235 | } |
219 | for (unsigned int i = 0; i < result; ++i) { | 236 | for (unsigned int i = 0; i < result; ++i) { |
220 | err = mailsession_remove_message(m_pop3->sto_session,i+1); | 237 | err = mailsession_remove_message(m_pop3->sto_session,i+1); |
221 | if (err != MAIL_NO_ERROR) { | 238 | if (err != MAIL_NO_ERROR) { |
222 | Global::statusMessage(tr("Error deleting mail %1").arg(i+1)); | 239 | Global::statusMessage(tr("Error deleting mail %1").arg(i+1)); |
223 | res=0; | 240 | res=0; |
224 | } | 241 | } |
225 | break; | 242 | break; |
226 | } | 243 | } |
227 | return res; | 244 | return res; |
228 | } | 245 | } |
229 | 246 | ||
230 | void POP3wrapper::statusFolder(folderStat&target_stat,const QString&) { | 247 | void POP3wrapper::statusFolder(folderStat&target_stat,const QString&) { |
231 | login(); | 248 | login(); |
232 | target_stat.message_count = 0; | 249 | target_stat.message_count = 0; |
233 | target_stat.message_unseen = 0; | 250 | target_stat.message_unseen = 0; |
234 | target_stat.message_recent = 0; | 251 | target_stat.message_recent = 0; |
235 | if (!m_pop3) | 252 | if (!m_pop3) |
236 | return; | 253 | return; |
237 | int r = mailsession_status_folder(m_pop3->sto_session,0,&target_stat.message_count, | 254 | int r = mailsession_status_folder(m_pop3->sto_session,0,&target_stat.message_count, |
238 | &target_stat.message_recent,&target_stat.message_unseen); | 255 | &target_stat.message_recent,&target_stat.message_unseen); |
239 | if (r != MAIL_NO_ERROR) { | 256 | if (r != MAIL_NO_ERROR) { |
240 | ; // odebug << "error getting folter status." << oendl; | 257 | ; // odebug << "error getting folter status." << oendl; |
241 | } | 258 | } |
242 | } | 259 | } |