summaryrefslogtreecommitdiff
path: root/noncore/net/mail/libmailwrapper/pop3wrapper.cpp
Unidiff
Diffstat (limited to 'noncore/net/mail/libmailwrapper/pop3wrapper.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/net/mail/libmailwrapper/pop3wrapper.cpp7
1 files changed, 4 insertions, 3 deletions
diff --git a/noncore/net/mail/libmailwrapper/pop3wrapper.cpp b/noncore/net/mail/libmailwrapper/pop3wrapper.cpp
index efd83ba..b85fbf6 100644
--- a/noncore/net/mail/libmailwrapper/pop3wrapper.cpp
+++ b/noncore/net/mail/libmailwrapper/pop3wrapper.cpp
@@ -1,55 +1,54 @@
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/mailpop3.h> 5#include <libetpan/libetpan.h>
6#include <libetpan/mailmime.h> 6#include <qpe/global.h>
7#include <libetpan/data_message_driver.h>
8#include <qfile.h> 7#include <qfile.h>
9 8
10/* we don't fetch messages larger than 5 MB */ 9/* we don't fetch messages larger than 5 MB */
11#define HARD_MSG_SIZE_LIMIT 5242880 10#define HARD_MSG_SIZE_LIMIT 5242880
12 11
13POP3wrapper::POP3wrapper( POP3account *a ) 12POP3wrapper::POP3wrapper( POP3account *a )
14 : Genericwrapper() 13 : Genericwrapper()
15{ 14{
16 account = a; 15 account = a;
17 m_pop3 = NULL; 16 m_pop3 = NULL;
18 msgTempName = a->getFileName()+"_msg_cache"; 17 msgTempName = a->getFileName()+"_msg_cache";
19 last_msg_id = 0; 18 last_msg_id = 0;
20} 19}
21 20
22POP3wrapper::~POP3wrapper() 21POP3wrapper::~POP3wrapper()
23{ 22{
24 logout(); 23 logout();
25 QFile msg_cache(msgTempName); 24 QFile msg_cache(msgTempName);
26 if (msg_cache.exists()) { 25 if (msg_cache.exists()) {
27 msg_cache.remove(); 26 msg_cache.remove();
28 } 27 }
29} 28}
30 29
31void POP3wrapper::pop3_progress( size_t current, size_t maximum ) 30void POP3wrapper::pop3_progress( size_t current, size_t maximum )
32{ 31{
33 //qDebug( "POP3: %i of %i", current, maximum ); 32 //qDebug( "POP3: %i of %i", current, maximum );
34} 33}
35 34
36RecBody POP3wrapper::fetchBody( const RecMail &mail ) 35RecBody POP3wrapper::fetchBody( const RecMail &mail )
37{ 36{
38 int err = MAILPOP3_NO_ERROR; 37 int err = MAILPOP3_NO_ERROR;
39 char *message; 38 char *message;
40 size_t length = 0; 39 size_t length = 0;
41 40
42 login(); 41 login();
43 if ( !m_pop3 ) { 42 if ( !m_pop3 ) {
44 return RecBody(); 43 return RecBody();
45 } 44 }
46 45
47 RecBody body; 46 RecBody body;
48 mailmessage * msg = 0; 47 mailmessage * msg = 0;
49 48
50 QFile msg_cache(msgTempName); 49 QFile msg_cache(msgTempName);
51 50
52 if (mail.Msgsize()>HARD_MSG_SIZE_LIMIT) { 51 if (mail.Msgsize()>HARD_MSG_SIZE_LIMIT) {
53 qDebug("Message to large: %i",mail.Msgsize()); 52 qDebug("Message to large: %i",mail.Msgsize());
54 return body; 53 return body;
55 } 54 }
@@ -67,116 +66,118 @@ RecBody POP3wrapper::fetchBody( const RecMail &mail )
67 return RecBody(); 66 return RecBody();
68 } 67 }
69 msg_cache.writeBlock(message,length); 68 msg_cache.writeBlock(message,length);
70 } else { 69 } else {
71 QString msg=""; 70 QString msg="";
72 msg_cache.open(IO_ReadOnly); 71 msg_cache.open(IO_ReadOnly);
73 message = new char[4096]; 72 message = new char[4096];
74 memset(message,0,4096); 73 memset(message,0,4096);
75 while (msg_cache.readBlock(message,4095)>0) { 74 while (msg_cache.readBlock(message,4095)>0) {
76 msg+=message; 75 msg+=message;
77 memset(message,0,4096); 76 memset(message,0,4096);
78 } 77 }
79 delete message; 78 delete message;
80 message = (char*)malloc(msg.length()+1*sizeof(char)); 79 message = (char*)malloc(msg.length()+1*sizeof(char));
81 memset(message,0,msg.length()+1); 80 memset(message,0,msg.length()+1);
82 memcpy(message,msg.latin1(),msg.length()); 81 memcpy(message,msg.latin1(),msg.length());
83 } 82 }
84 83
85 /* transform to libetpan stuff */ 84 /* transform to libetpan stuff */
86 msg = mailmessage_new(); 85 msg = mailmessage_new();
87 mailmessage_init(msg, NULL, data_message_driver, 0, strlen(message)); 86 mailmessage_init(msg, NULL, data_message_driver, 0, strlen(message));
88 generic_message_t * msg_data; 87 generic_message_t * msg_data;
89 msg_data = (generic_message_t *)msg->msg_data; 88 msg_data = (generic_message_t *)msg->msg_data;
90 msg_data->msg_fetched = 1; 89 msg_data->msg_fetched = 1;
91 msg_data->msg_message = message; 90 msg_data->msg_message = message;
92 msg_data->msg_length = strlen(message); 91 msg_data->msg_length = strlen(message);
93 92
94 /* parse the mail */ 93 /* parse the mail */
95 body = parseMail(msg); 94 body = parseMail(msg);
96 95
97 /* clean up */ 96 /* clean up */
98 mailmessage_free(msg); 97 mailmessage_free(msg);
99 free(message); 98 free(message);
100 99
101 /* finish */ 100 /* finish */
102 return body; 101 return body;
103} 102}
104 103
105void POP3wrapper::listMessages(const QString &, QList<RecMail> &target ) 104void POP3wrapper::listMessages(const QString &, QList<RecMail> &target )
106{ 105{
107 int err = MAILPOP3_NO_ERROR; 106 int err = MAILPOP3_NO_ERROR;
108 char * header = 0; 107 char * header = 0;
109 /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ 108 /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */
110 size_t length = 0; 109 size_t length = 0;
111 carray * messages = 0; 110 carray * messages = 0;
112 111
113 login(); 112 login();
114 if (!m_pop3) return; 113 if (!m_pop3) return;
114
115 mailpop3_list( m_pop3, &messages ); 115 mailpop3_list( m_pop3, &messages );
116 116
117 for (unsigned int i = 0; i < carray_count(messages);++i) { 117 for (unsigned int i = 0; i < carray_count(messages);++i) {
118 mailpop3_msg_info *info; 118 mailpop3_msg_info *info;
119 err = mailpop3_get_msg_info(m_pop3,i+1,&info); 119 err = mailpop3_get_msg_info(m_pop3,i+1,&info);
120 if (info->msg_deleted) 120 if (info->msg_deleted)
121 continue; 121 continue;
122 err = mailpop3_header( m_pop3, info->msg_index, &header, &length ); 122 err = mailpop3_header( m_pop3, info->msg_index, &header, &length );
123 if ( err != MAILPOP3_NO_ERROR ) { 123 if ( err != MAILPOP3_NO_ERROR ) {
124 qDebug( "POP3: error retrieving header msgid: %i", info->msg_index ); 124 qDebug( "POP3: error retrieving header msgid: %i", info->msg_index );
125 free(header); 125 free(header);
126 return; 126 return;
127 } 127 }
128 RecMail *mail = parseHeader( header ); 128 RecMail *mail = parseHeader( header );
129 mail->setNumber( info->msg_index ); 129 mail->setNumber( info->msg_index );
130 mail->setWrapper(this); 130 mail->setWrapper(this);
131 mail->setMsgsize(info->msg_size); 131 mail->setMsgsize(info->msg_size);
132 target.append( mail ); 132 target.append( mail );
133 free(header); 133 free(header);
134 } 134 }
135 Global::statusMessage( tr("Mailbox contains %1 mail(s)").arg(carray_count(messages)-m_pop3->pop3_deleted_count));
135} 136}
136 137
137void POP3wrapper::login() 138void POP3wrapper::login()
138{ 139{
139 /* we'll hold the line */ 140 /* we'll hold the line */
140 if ( m_pop3 != NULL ) return; 141 if ( m_pop3 != NULL ) return;
141 142
142 const char *server, *user, *pass; 143 const char *server, *user, *pass;
143 uint16_t port; 144 uint16_t port;
144 int err = MAILPOP3_NO_ERROR; 145 int err = MAILPOP3_NO_ERROR;
145 146
146 server = account->getServer().latin1(); 147 server = account->getServer().latin1();
147 port = account->getPort().toUInt(); 148 port = account->getPort().toUInt();
148 149
149 if ( account->getUser().isEmpty() || account->getPassword().isEmpty() ) { 150 if ( account->getUser().isEmpty() || account->getPassword().isEmpty() ) {
150 LoginDialog login( account->getUser(), account->getPassword(), NULL, 0, true ); 151 LoginDialog login( account->getUser(), account->getPassword(), NULL, 0, true );
151 login.show(); 152 login.show();
152 if ( QDialog::Accepted == login.exec() ) { 153 if ( QDialog::Accepted == login.exec() ) {
153 // ok 154 // ok
154 user = strdup( login.getUser().latin1() ); 155 user = strdup( login.getUser().latin1() );
155 pass = strdup( login.getPassword().latin1() ); 156 pass = strdup( login.getPassword().latin1() );
156 } else { 157 } else {
157 // cancel 158 // cancel
158 qDebug( "POP3: Login canceled" ); 159 qDebug( "POP3: Login canceled" );
159 return; 160 return;
160 } 161 }
161 } else { 162 } else {
162 user = account->getUser().latin1(); 163 user = account->getUser().latin1();
163 pass = account->getPassword().latin1(); 164 pass = account->getPassword().latin1();
164 } 165 }
165 166
166 m_pop3 = mailpop3_new( 200, &pop3_progress ); 167 m_pop3 = mailpop3_new( 200, &pop3_progress );
167 168
168 // connect 169 // connect
169 if (account->getSSL()) { 170 if (account->getSSL()) {
170 err = mailpop3_ssl_connect( m_pop3, (char*)server, port ); 171 err = mailpop3_ssl_connect( m_pop3, (char*)server, port );
171 } else { 172 } else {
172 err = mailpop3_socket_connect( m_pop3, (char*)server, port ); 173 err = mailpop3_socket_connect( m_pop3, (char*)server, port );
173 } 174 }
174 175
175 if ( err != MAILPOP3_NO_ERROR ) { 176 if ( err != MAILPOP3_NO_ERROR ) {
176 qDebug( "pop3: error connecting to %s\n reason: %s", server, 177 qDebug( "pop3: error connecting to %s\n reason: %s", server,
177 m_pop3->pop3_response ); 178 m_pop3->pop3_response );
178 mailpop3_free( m_pop3 ); 179 mailpop3_free( m_pop3 );
179 m_pop3 = NULL; 180 m_pop3 = NULL;
180 return; 181 return;
181 } 182 }
182 qDebug( "POP3: connected!" ); 183 qDebug( "POP3: connected!" );