summaryrefslogtreecommitdiff
path: root/noncore/unsupported/mailit/emailhandler.cpp
Unidiff
Diffstat (limited to 'noncore/unsupported/mailit/emailhandler.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/unsupported/mailit/emailhandler.cpp589
1 files changed, 589 insertions, 0 deletions
diff --git a/noncore/unsupported/mailit/emailhandler.cpp b/noncore/unsupported/mailit/emailhandler.cpp
new file mode 100644
index 0000000..a086dfc
--- a/dev/null
+++ b/noncore/unsupported/mailit/emailhandler.cpp
@@ -0,0 +1,589 @@
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 <qfileinfo.h>
21#include <stdlib.h>
22#include <qapplication.h>
23#include <qmessagebox.h>
24#include <qcstring.h>
25#include "emailhandler.h"
26#include <qpe/applnk.h>
27#include <qpe/filemanager.h>
28
29QCollection::Item EnclosureList::newItem(QCollection::Item d)
30{
31 return dupl( (Enclosure *) d);
32}
33
34Enclosure* EnclosureList::dupl(Enclosure *in)
35{
36 ac = new Enclosure(*in);
37 return ac;
38}
39
40EmailHandler::EmailHandler()
41{
42 smtpClient = new SmtpClient();
43 popClient = new PopClient();
44
45 connect(smtpClient, SIGNAL(errorOccurred(int)), this,
46 SIGNAL(smtpError(int)) );
47 connect(smtpClient, SIGNAL(mailSent()), this, SIGNAL(mailSent()) );
48 connect(smtpClient, SIGNAL(updateStatus(const QString &)), this,
49 SIGNAL(updateSmtpStatus(const QString &)) );
50
51 connect(popClient, SIGNAL(errorOccurred(int)), this,
52 SIGNAL(popError(int)) );
53 connect(popClient, SIGNAL(newMessage(const QString &, int, uint, bool)),
54 this, SLOT(messageArrived(const QString &, int, uint, bool)) );
55 connect(popClient, SIGNAL(updateStatus(const QString &)), this,
56 SIGNAL(updatePopStatus(const QString &)) );
57 connect(popClient, SIGNAL(mailTransfered(int)), this,
58 SIGNAL(mailTransfered(int)) );
59
60
61 //relaying size information
62 connect(popClient, SIGNAL(currentMailSize(int)),
63 this, SIGNAL(currentMailSize(int)) );
64 connect(popClient, SIGNAL(downloadedSize(int)),
65 this, SIGNAL(downloadedSize(int)) );
66}
67
68void EmailHandler::sendMail(QList<Email> *mailList)
69{
70 Email *currentMail;
71 QString temp;
72 QString userName = mailAccount.name;
73 userName += " <" + mailAccount.emailAddress + ">";
74
75 for (currentMail = mailList->first(); currentMail != 0;
76 currentMail = mailList->next()) {
77
78 if (encodeMime(currentMail) == 0) {
79 smtpClient->addMail(userName, currentMail->subject,
80 currentMail->recipients, currentMail->rawMail);
81 } else { //error
82 temp = tr("Could not locate all files in \nmail with subject: ") +
83 currentMail->subject;
84 temp += tr("\nMail has NOT been sent");
85 QMessageBox::warning(qApp->activeWindow(), tr("Attachment error"), temp, tr("OK\n"));
86
87 }
88 }
89 smtpClient->newConnection(mailAccount.smtpServer, 25);
90}
91
92void EmailHandler::setAccount(MailAccount account)
93{
94 mailAccount = account;
95}
96
97void EmailHandler::getMail()
98{
99 popClient->setAccount(mailAccount.popUserName, mailAccount.popPasswd);
100 if (mailAccount.synchronize) {
101 popClient->setSynchronize(mailAccount.lastServerMailCount);
102 } else {
103 popClient->removeSynchronize();
104 }
105
106 headers = FALSE;
107 popClient->headersOnly(headers, 0);
108 popClient->newConnection(mailAccount.popServer, 110);
109}
110
111void EmailHandler::getMailHeaders()
112{
113 popClient->setAccount(mailAccount.popUserName, mailAccount.popPasswd);
114 if (mailAccount.synchronize) {
115 popClient->setSynchronize(mailAccount.lastServerMailCount);
116 } else {
117 popClient->removeSynchronize();
118 }
119
120 headers = TRUE;
121 popClient->headersOnly(headers, 2000); //less than 2000, download all
122 popClient->newConnection(mailAccount.popServer, 110);
123}
124
125void EmailHandler::getMailByList(MailList *mailList)
126{
127 if (mailList->count() == 0) { //should not occur though
128 emit mailTransfered(0);
129 return;
130 }
131
132 headers = FALSE;
133 popClient->headersOnly(FALSE, 0);
134 popClient->newConnection(mailAccount.popServer, 110);
135 popClient->setSelectedMails(mailList);
136}
137
138void EmailHandler::messageArrived(const QString &message, int id, uint size, bool complete)
139{
140 Email mail;
141
142 mail.rawMail = message;
143 mail.serverId = id;
144 mail.size = size;
145 mail.downloaded = complete;
146
147 emit mailArrived(mail, FALSE);
148}
149
150bool EmailHandler::parse(QString in, QString lineShift, Email *mail)
151{
152 QString temp, boundary;
153 int pos;
154 QString delimiter, header, body, mimeHeader, mimeBody;
155 QString content, contentType, contentAttribute, id, encoding;
156 QString fileName, storedName;
157 int enclosureId = 0;
158
159 mail->rawMail = in;
160 mail->received = TRUE;
161 mail->files.setAutoDelete(TRUE);
162
163 temp = lineShift + "." + lineShift;
164
165 if (in.right(temp.length()) != temp) {
166 qWarning(in.right(temp.length()));
167 qWarning(" . added at end of email as separator");
168 mail->rawMail += temp;
169 }
170
171
172 delimiter = lineShift + lineShift; // "\n\n" or "\r\n\r\n"
173 pos = in.find(delimiter, 0, FALSE);
174 header = in.left(pos);
175 body = in.right(in.length() - pos - delimiter.length());
176 if ((body.at(body.length()-2) == '.') && (body.at(body.length()-3) == '\n'))
177 body.truncate(body.length()-2);
178
179 TextParser p(header, lineShift);
180
181 if ((pos = p.find("FROM",':', 0, TRUE)) != -1) {
182 pos++;
183 if (p.separatorAt(pos) == ' ') {
184 mail->from = p.getString(&pos, '<');
185 mail->from = mail->from.stripWhiteSpace();
186 if ( (mail->from.length() > 2) && (mail->from[0] == '"') ) {
187 mail->from = mail->from.left(mail->from.length() - 1);
188 mail->from = mail->from.right(mail->from.length() - 1);
189 }
190 pos++;
191 mail->fromMail = p.getString(&pos, '>');
192 } else {
193 if ((p.separatorAt(pos) == '<')
194 || (p.separatorAt(pos) == ' ')) //No name.. nasty
195 pos++;
196 pos++;
197 mail->fromMail = p.getString(&pos, 'z', TRUE);
198 if (mail->fromMail.at(mail->fromMail.length()-1) == '>')
199 mail->fromMail.truncate(mail->fromMail.length() - 1);
200 mail->from=mail->fromMail;
201 }
202 }
203 if ((pos = p.find("SUBJECT",':', 0, TRUE)) != -1) {
204 pos++;
205 mail->subject = p.getString(&pos, 'z', TRUE);
206 }
207 if ((pos = p.find("DATE",':', 0, TRUE)) != -1) {
208 pos++;
209 mail->date = p.getString(&pos, 'z', true);
210 }
211 if ((pos = p.find("TO",':', 0, TRUE)) != -1) {
212 pos++;
213 mail->recipients.append (p.getString(&pos, 'z', TRUE) );
214 }
215 if ((pos = p.find("MESSAGE",'-', 0, TRUE)) != -1) {
216 pos++;
217 if ( (p.wordAt(pos).upper() == "ID") &&
218 (p.separatorAt(pos) == ':') ) {
219
220 id = p.getString(&pos, 'z', TRUE);
221 mail->id = id;
222 }
223 }
224
225 pos = 0;
226 while ( ((pos = p.find("MIME",'-', pos, TRUE)) != -1) ) {
227 pos++;
228 if ( (p.wordAt(pos).upper() == "VERSION") &&
229 (p.separatorAt(pos) == ':') ) {
230 pos++;
231 if (p.getString(&pos, 'z', true) == "1.0") {
232 mail->mimeType = 1;
233 }
234 }
235 }
236
237 if (mail->mimeType == 1) {
238 boundary = "";
239 if ((pos = p.find("BOUNDARY", '=', 0, TRUE)) != -1) {
240 pos++;
241 boundary = p.getString(&pos, 'z', true);
242 if (boundary[0] == '"') {
243 boundary = boundary.left(boundary.length() - 1); //strip "
244 boundary = boundary.right(boundary.length() - 1); //strip "
245 }
246 boundary = "--" + boundary; //create boundary field
247 }
248
249 if (boundary == "") { //fooled by Mime-Version
250 mail->body = body;
251 mail->bodyPlain = body;
252 return mail;
253 }
254
255 while (body.length() > 0) {
256 pos = body.find(boundary, 0, FALSE);
257 pos = body.find(delimiter, pos, FALSE);
258 mimeHeader = body.left(pos);
259 mimeBody = body.right(body.length() - pos - delimiter.length());
260 TextParser bp(mimeHeader, lineShift);
261
262 contentType = "";
263 contentAttribute = "";
264 fileName = "";
265 if ((pos = bp.find("CONTENT",'-', 0, TRUE)) != -1) {
266 pos++;
267 if ( (bp.wordAt(pos).upper() == "TYPE") &&
268 (bp.separatorAt(pos) == ':') ) {
269 contentType = bp.nextWord().upper();
270 if (bp.nextSeparator() == '/')
271 contentAttribute = bp.nextWord().upper();
272 content = contentType + "/" + contentAttribute;
273 }
274 if ((pos = bp.find("ENCODING",':', 0, TRUE)) != -1) {
275 pos++;
276 encoding = bp.getString(&pos, 'z', TRUE);
277 }
278
279 if ( (pos = bp.find("FILENAME",'=', 0, TRUE)) != -1) {
280 pos++;
281 fileName = bp.getString(&pos, 'z', TRUE);
282 fileName = fileName.right(fileName.length() - 1);
283 fileName = fileName.left(fileName.length() - 1);
284 }
285
286 }
287 pos = mimeBody.find(boundary, 0, FALSE);
288 if (pos == -1) //should not occur, malformed mail
289 pos = mimeBody.length();
290 body = mimeBody.right(mimeBody.length() - pos);
291 mimeBody = mimeBody.left(pos);
292
293 if (fileName != "") { //attatchments of some type, audio, image etc.
294
295 Enclosure e;
296 e.id = enclosureId;
297 e.originalName = fileName;
298 e.contentType = contentType;
299 e.contentAttribute = contentAttribute;
300 e.encoding = encoding;
301 e.body = mimeBody;
302 e.saved = FALSE;
303 mail->addEnclosure(&e);
304 enclosureId++;
305
306 } else if (contentType == "TEXT") {
307 if (contentAttribute == "PLAIN") {
308 mail->body = mimeBody;
309 mail->bodyPlain = mimeBody;
310 }
311 if (contentAttribute == "HTML") {
312 mail->body = mimeBody;
313 }
314 }
315 }
316 } else {
317 mail->bodyPlain = body;
318 mail->body = body;
319 }
320 return TRUE;
321}
322
323bool EmailHandler::getEnclosure(Enclosure *ePtr)
324{
325 QFile f(ePtr->path + ePtr->name);
326 char src[4];
327 char *destPtr;
328 QByteArray buffer;
329 uint bufCount, pos, decodedCount, size, x;
330
331 if (! f.open(IO_WriteOnly) ) {
332 qWarning("could not save: " + ePtr->path + ePtr->name);
333 return FALSE;
334 }
335
336 if (ePtr->encoding.upper() == "BASE64") {
337 size = (ePtr->body.length() * 3 / 4); //approximate size (always above)
338 buffer.resize(size);
339 bufCount = 0;
340 pos = 0;
341 destPtr = buffer.data();
342
343 while (pos < ePtr->body.length()) {
344 decodedCount = 4;
345 x = 0;
346 while ( (x < 4) && (pos < ePtr->body.length()) ) {
347 src[x] = ePtr->body[pos].latin1();
348 pos++;
349 if (src[x] == '\r' || src[x] == '\n' || src[x] == ' ')
350 x--;
351 x++;
352 }
353 if (x > 1) {
354 decodedCount = parse64base(src, destPtr);
355 destPtr += decodedCount;
356 bufCount += decodedCount;
357 }
358 }
359
360 buffer.resize(bufCount); //set correct length of file
361 f.writeBlock(buffer);
362 } else {
363 QTextStream t(&f);
364 t << ePtr->body;
365 }
366 return TRUE;
367}
368
369int EmailHandler::parse64base(char *src, char *bufOut) {
370
371 char c, z;
372 char li[4];
373 int processed;
374
375 //conversion table withouth table...
376 for (int x = 0; x < 4; x++) {
377 c = src[x];
378
379 if ( (int) c >= 'A' && (int) c <= 'Z')
380 li[x] = (int) c - (int) 'A';
381 if ( (int) c >= 'a' && (int) c <= 'z')
382 li[x] = (int) c - (int) 'a' + 26;
383 if ( (int) c >= '0' && (int) c <= '9')
384 li[x] = (int) c - (int) '0' + 52;
385 if (c == '+')
386 li[x] = 62;
387 if (c == '/')
388 li[x] = 63;
389 }
390
391 processed = 1;
392 bufOut[0] = (char) li[0] & (32+16+8+4+2+1); //mask out top 2 bits
393 bufOut[0] <<= 2;
394 z = li[1] >> 4;
395 bufOut[0] = bufOut[0] | z; //first byte retrived
396
397 if (src[2] != '=') {
398 bufOut[1] = (char) li[1] & (8+4+2+1); //mask out top 4 bits
399 bufOut[1] <<= 4;
400 z = li[2] >> 2;
401 bufOut[1] = bufOut[1] | z; //second byte retrived
402 processed++;
403
404 if (src[3] != '=') {
405 bufOut[2] = (char) li[2] & (2+1); //mask out top 6 bits
406 bufOut[2] <<= 6;
407 z = li[3];
408 bufOut[2] = bufOut[2] | z; //third byte retrieved
409 processed++;
410 }
411 }
412 return processed;
413}
414
415int EmailHandler::encodeMime(Email *mail) {
416 QString fileName, fileType, contentType, newBody, boundary;
417 Enclosure *ePtr;
418
419 QString userName = mailAccount.name;
420 userName += " <" + mailAccount.emailAddress + ">";
421
422 //add standard headers
423 newBody = "From: " + userName + "\r\nTo: ";
424 for (QStringList::Iterator it = mail->recipients.begin(); it != mail->recipients.end(); ++it ) {
425 newBody += *it + " ";
426 }
427 newBody += "\r\nSubject: " + mail->subject + "\r\n";
428
429 if (mail->files.count() == 0) { //just a simple mail
430 newBody += "\r\n" + mail->body;
431 mail->rawMail = newBody;
432 return 0;
433 }
434
435 //Build mime encoded mail
436 boundary = "-----4345=next_bound=0495----";
437
438 newBody += "Mime-Version: 1.0\r\n";
439 newBody += "Content-Type: multipart/mixed; boundary=\"" +
440 boundary + "\"\r\n\r\n";
441
442 newBody += "This is a multipart message in Mime 1.0 format\r\n\r\n";
443 newBody += "--" + boundary + "\r\nContent-Type: text/plain\r\n\r\n";
444 newBody += mail->body;
445
446 for ( ePtr=mail->files.first(); ePtr != 0; ePtr=mail->files.next() ) {
447 fileName = ePtr->originalName;
448 fileType = ePtr->contentType;
449 QFileInfo fi(fileName);
450
451 // This specification of contentType is temporary
452 contentType = "";
453 if (fileType == "Picture") {
454 contentType = "image/x-image";
455 } else if (fileType == "Document") {
456 contentType = "text/plain";
457 } else if (fileType == "Sound") {
458 contentType = "audio/x-wav";
459 } else if (fileType == "Movie") {
460 contentType = "video/mpeg";
461 } else {
462 contentType = "application/octet-stream";
463 }
464
465 newBody += "\r\n\r\n--" + boundary + "\r\n";
466 newBody += "Content-Type: " + contentType + "; name=\"" +
467 fi.fileName() + "\"\r\n";
468 newBody += "Content-Transfer-Encoding: base64\r\n";
469 newBody += "Content-Disposition: inline; filename=\"" +
470 fi.fileName() + "\"\r\n\r\n";
471
472 if (encodeFile(fileName, &newBody) == -1) //file not found?
473 return -1;
474 }
475
476 newBody += "\r\n\r\n--" + boundary + "--";
477 mail->rawMail = newBody;
478
479 return 0;
480}
481
482int EmailHandler::encodeFile(QString fileName, QString *toBody)
483{
484 char *fileData;
485 char *dataPtr;
486 QString temp;
487 uint dataSize, count;
488 QFile f(fileName);
489
490 if (! f.open(IO_ReadOnly) ) {
491 qWarning("could not open file: " + fileName);
492 return -1;
493 }
494 QTextStream s(&f);
495 dataSize = f.size();
496 fileData = (char *) malloc(dataSize + 3);
497 s.readRawBytes(fileData, dataSize);
498
499 temp = "";
500 dataPtr = fileData;
501 count = 0;
502 while (dataSize > 0) {
503 if (dataSize < 3) {
504 encode64base(dataPtr, &temp, dataSize);
505 dataSize = 0;
506 } else {
507 encode64base(dataPtr, &temp, 3);
508 dataSize -= 3;
509 dataPtr += 3;
510 count += 4;
511 }
512 if (count > 72) {
513 count = 0;
514 temp += "\r\n";
515 }
516 }
517 toBody->append(temp);
518
519 delete(fileData);
520 f.close();
521 return 0;
522}
523
524void EmailHandler::encode64base(char *src, QString *dest, int len)
525{
526 QString temp;
527 uchar c;
528 uchar bufOut[4];
529
530 bufOut[0] = src[0];
531 bufOut[0] >>= 2; //Done byte 0
532
533 bufOut[1] = src[0];
534 bufOut[1] = bufOut[1] & (1 + 2); //mask out top 6 bits
535 bufOut[1] <<= 4; //copy up 4 places
536 if (len > 1) {
537 c = src[1];
538 } else {
539 c = 0;
540 }
541
542 c = c & (16 + 32 + 64 + 128);
543 c >>= 4;
544 bufOut[1] = bufOut[1] | c; //Done byte 1
545
546 bufOut[2] = src[1];
547 bufOut[2] = bufOut[2] & (1 + 2 + 4 + 8);
548 bufOut[2] <<= 2;
549 if (len > 2) {
550 c = src[2];
551 } else {
552 c = 0;
553 }
554 c >>= 6;
555 bufOut[2] = bufOut[2] | c;
556
557 bufOut[3] = src[2];
558 bufOut[3] = bufOut[3] & (1 + 2 + 4 + 8 + 16 + 32);
559
560 if (len == 1) {
561 bufOut[2] = 64;
562 bufOut[3] = 64;
563 }
564 if (len == 2) {
565 bufOut[3] = 64;
566 }
567 for (int x = 0; x < 4; x++) {
568 if (bufOut[x] <= 25)
569 bufOut[x] += (uint) 'A';
570 else if (bufOut[x] >= 26 && bufOut[x] <= 51)
571 bufOut[x] += (uint) 'a' - 26;
572 else if (bufOut[x] >= 52 && bufOut[x] <= 61)
573 bufOut[x] += (uint) '0' - 52;
574 else if (bufOut[x] == 62)
575 bufOut[x] = '+';
576 else if (bufOut[x] == 63)
577 bufOut[x] = '/';
578 else if (bufOut[x] == 64)
579 bufOut[x] = '=';
580
581 dest->append(bufOut[x]);
582 }
583}
584
585void EmailHandler::cancel()
586{
587 popClient->errorHandling(ErrCancel);
588 smtpClient->errorHandling(ErrCancel);
589}