summaryrefslogtreecommitdiff
path: root/noncore/net/mailit/popclient.cpp
Unidiff
Diffstat (limited to 'noncore/net/mailit/popclient.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/net/mailit/popclient.cpp332
1 files changed, 0 insertions, 332 deletions
diff --git a/noncore/net/mailit/popclient.cpp b/noncore/net/mailit/popclient.cpp
deleted file mode 100644
index 1df6b2b..0000000
--- a/noncore/net/mailit/popclient.cpp
+++ b/dev/null
@@ -1,332 +0,0 @@
1/**********************************************************************
2** Copyright (C) 2001 Trolltech AS. All rights reserved.
3**
4** This file is part of Qt Palmtop Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20#include "popclient.h"
21#include "emailhandler.h"
22//#define APOP_TEST
23
24extern "C" {
25#include "md5.h"
26}
27
28#include <qcstring.h>
29
30PopClient::PopClient()
31{
32
33 socket = new QSocket(this, "popClient");
34 connect(socket, SIGNAL(error(int)), this, SLOT(errorHandling(int)));
35 connect(socket, SIGNAL(connected()), this, SLOT(connectionEstablished()));
36 connect(socket, SIGNAL(readyRead()), this, SLOT(incomingData()));
37
38 stream = new QTextStream(socket);
39
40 receiving = FALSE;
41 synchronize = FALSE;
42 lastSync = 0;
43 headerLimit = 0;
44 mailList = 0;
45 preview = FALSE;
46}
47
48PopClient::~PopClient()
49{
50 delete socket;
51 delete stream;
52}
53
54void PopClient::newConnection(const QString &target, int port)
55{
56 if (receiving) {
57 qWarning("socket in use, connection refused");
58 return;
59 }
60
61 status = Init;
62
63 socket->connectToHost(target, port);
64 receiving = TRUE;
65 //selected = FALSE;
66
67 emit updateStatus(tr("DNS lookup"));
68}
69
70void PopClient::setAccount(const QString &popUser, const QString &popPasswd)
71{
72 popUserName = popUser;
73 popPassword = popPasswd;
74}
75
76void PopClient::setSynchronize(int lastCount)
77{
78 synchronize = TRUE;
79 lastSync = lastCount;
80}
81
82void PopClient::removeSynchronize()
83{
84 synchronize = FALSE;
85 lastSync = 0;
86}
87
88void PopClient::headersOnly(bool headers, int limit)
89{
90 preview = headers;
91 headerLimit = limit;
92}
93
94void PopClient::setSelectedMails(MailList *list)
95{
96 selected = TRUE;
97 mailList = list;
98}
99
100void PopClient::connectionEstablished()
101{
102 emit updateStatus(tr("Connection established"));
103}
104
105void PopClient::errorHandling(int status)
106{
107 errorHandlingWithMsg( status, QString::null );
108}
109void PopClient::errorHandlingWithMsg(int status, const QString & Msg )
110{
111 emit updateStatus(tr("Error Occured"));
112 emit errorOccurred(status, Msg);
113 socket->close();
114 receiving = FALSE;
115}
116
117void PopClient::incomingData()
118{
119 QString response, temp, temp2, timeStamp;
120 QString md5Source;
121 int start, end;
122// char *md5Digest;
123 char md5Digest[16];
124// if ( !socket->canReadLine() )
125// return;
126
127
128 response = socket->readLine();
129
130 switch(status) {
131 //logging in
132 case Init: {
133#ifdef APOP_TEST
134 start = response.find('<',0);
135 end = response.find('>', start);
136 if( start >= 0 && end > start )
137 {
138 timeStamp = response.mid( start , end - start + 1);
139 md5Source = timeStamp + popPassword;
140
141 md5_buffer( (char const *)md5Source, md5Source.length(),&md5Digest[0]);
142
143 for(int j =0;j < MD5_DIGEST_LENGTH ;j++)
144 {
145 printf("%x", md5Digest[j]);
146 }
147 printf("\n");
148// qDebug(md5Digest);
149 *stream << "APOP " << popUserName << " " << md5Digest << "\r\n";
150 // qDebug("%s", stream);
151 status = Stat;
152 }
153 else
154#endif
155 {
156 timeStamp = "";
157 *stream << "USER " << popUserName << "\r\n";
158 status = Pass;
159 }
160
161 break;
162 }
163
164 case Pass: {
165 *stream << "PASS " << popPassword << "\r\n";
166 status = Stat;
167
168 break;
169 }
170 //ask for number of messages
171 case Stat: {
172 if (response[0] == '+') {
173 *stream << "STAT" << "\r\n";
174 status = Mcnt;
175 } else errorHandlingWithMsg(ErrLoginFailed, response);
176 break;
177 }
178 //get count of messages, eg "+OK 4 900.." -> int 4
179 case Mcnt: {
180 if (response[0] == '+') {
181 temp = response.replace(0, 4, "");
182 int x = temp.find(" ", 0);
183 temp.truncate((uint) x);
184 newMessages = temp.toInt();
185 messageCount = 1;
186 status = List;
187
188 if (synchronize) {
189 //messages deleted from server, reload all
190 if (newMessages < lastSync)
191 lastSync = 0;
192 messageCount = 1;
193 }
194
195 if (selected && mailList ) {
196 int *ptr = mailList->first();
197 if (ptr != 0) {
198 newMessages++; //to ensure no early jumpout
199 messageCount = *ptr;
200 } else newMessages = 0;
201 }
202
203 } else errorHandlingWithMsg(ErrUnknownResponse, response);
204 }
205 //Read message number x, count upwards to messageCount
206 case List: {
207 if (messageCount <= newMessages) {
208 *stream << "LIST " << messageCount << "\r\n";
209 status = Size;
210 temp2.setNum(newMessages - lastSync);
211 temp.setNum(messageCount - lastSync);
212 if (!selected) {
213 emit updateStatus(tr("Retrieving ") + temp + "/" + temp2);
214 } else {
215 //completing a previously closed transfer
216 /* if ( (messageCount - lastSync) <= 0) {
217 temp.setNum(messageCount);
218 emit updateStatus(tr("Previous message ") + temp);
219 } else {*/
220 emit updateStatus(tr("Completing message ") + temp);
221 //}
222 }
223 break;
224 } else {
225 emit updateStatus(tr("No new Messages"));
226 status = Quit;
227 }
228 }
229 //get size of message, eg "500 characters in message.." -> int 500
230 case Size: {
231 if (status != Quit) { //because of idiotic switch
232 if (response[0] == '+') {
233 temp = response.replace(0, 4, "");
234 int x = temp.find(" ", 0);
235 temp = temp.right(temp.length() - ((uint) x + 1) );
236 mailSize = temp.toInt();
237 emit currentMailSize(mailSize);
238
239 status = Retr;
240 } else {
241 //qWarning(response);
242 errorHandlingWithMsg(ErrUnknownResponse, response);
243 }
244 }
245 }
246 //Read message number x, count upwards to messageCount
247 case Retr: {
248 if (status != Quit) {
249 if ((selected)||(mailSize <= headerLimit))
250 {
251 *stream << "RETR " << messageCount << "\r\n";
252 } else { //only header
253 *stream << "TOP " << messageCount << " 0\r\n";
254 }
255 messageCount++;
256 status = Ignore;
257 break;
258 } }
259 case Ignore: {
260 if (status != Quit) { //because of idiotic switch
261 if (response[0] == '+') {
262 message = "";
263 status = Read;
264 if (!socket->canReadLine()) //sync. problems
265 break;
266 response = socket->readLine();
267 } else errorHandlingWithMsg(ErrUnknownResponse, response);
268 }
269 }
270 //add all incoming lines to body. When size is reached, send
271 //message, and go back to read new message
272 case Read: {
273 if (status != Quit) { //because of idiotic switch
274 message += response;
275 while ( socket->canReadLine() ) {
276 response = socket->readLine();
277 message += response;
278 }
279 emit downloadedSize(message.length());
280 int x = message.find("\r\n.\r\n",-5);
281 if (x == -1) {
282 break;
283 } else { //message reach entire size
284 if ( (selected)||(mailSize <= headerLimit)) //mail size limit is not used if late download is active
285 {
286 emit newMessage(message, messageCount-1, mailSize, TRUE);
287 } else { //incomplete mail downloaded
288 emit newMessage(message, messageCount-1, mailSize, FALSE);
289 }
290
291 if ((messageCount > newMessages)||(selected)) //last message ?
292 {
293 status = Quit;
294 if (selected) { //grab next from queue
295 newMessages--;
296 status = Quit;
297 }
298 }
299 else
300 {
301 *stream << "LIST " << messageCount << "\r\n";
302 status = Size;
303 temp2.setNum(newMessages - lastSync);
304 temp.setNum(messageCount - lastSync);
305 emit updateStatus(tr("Retrieving ") + temp + "/" + temp2);
306
307 break;
308 }
309 }
310 }
311 if (status != Quit)
312 break;
313 }
314 case Quit: {
315 *stream << "Quit\r\n";
316 status = Done;
317 int newM = newMessages - lastSync;
318 if (newM > 0) {
319 temp.setNum(newM);
320 emit updateStatus(temp + tr(" new messages"));
321 } else {
322 emit updateStatus(tr("No new messages"));
323 }
324
325 socket->close();
326 receiving = FALSE;
327 emit mailTransfered(newM);
328 break;
329 }
330 }
331
332}