summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/net/mailit/emailhandler.cpp13
-rw-r--r--noncore/unsupported/mailit/emailhandler.cpp13
2 files changed, 2 insertions, 24 deletions
diff --git a/noncore/net/mailit/emailhandler.cpp b/noncore/net/mailit/emailhandler.cpp
index c7b27a0..62fa64f 100644
--- a/noncore/net/mailit/emailhandler.cpp
+++ b/noncore/net/mailit/emailhandler.cpp
@@ -1,480 +1,469 @@
1/********************************************************************** 1/**********************************************************************
2** Copyright (C) 2001 Trolltech AS. All rights reserved. 2** Copyright (C) 2001 Trolltech AS. All rights reserved.
3** 3**
4** This file is part of Qt Palmtop Environment. 4** This file is part of Qt Palmtop Environment.
5** 5**
6** This file may be distributed and/or modified under the terms of the 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 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 8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file. 9** packaging of this file.
10** 10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 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. 12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13** 13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information. 14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15** 15**
16** Contact info@trolltech.com if any conditions of this licensing are 16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you. 17** not clear to you.
18** 18**
19**********************************************************************/ 19**********************************************************************/
20#include <qfileinfo.h> 20#include <qfileinfo.h>
21#include <stdlib.h> 21#include <stdlib.h>
22#include <qapplication.h> 22#include <qapplication.h>
23#include <qmessagebox.h> 23#include <qmessagebox.h>
24#include <qcstring.h> 24#include <qcstring.h>
25#include "emailhandler.h" 25#include "emailhandler.h"
26#include <qpe/applnk.h> 26#include <qpe/applnk.h>
27#include <qpe/filemanager.h> 27#include <qpe/filemanager.h>
28 28
29QCollection::Item EnclosureList::newItem(QCollection::Item d) 29QCollection::Item EnclosureList::newItem(QCollection::Item d)
30{ 30{
31 return dupl( (Enclosure *) d); 31 return dupl( (Enclosure *) d);
32} 32}
33 33
34Enclosure* EnclosureList::dupl(Enclosure *in) 34Enclosure* EnclosureList::dupl(Enclosure *in)
35{ 35{
36 ac = new Enclosure(*in); 36 ac = new Enclosure(*in);
37 return ac; 37 return ac;
38} 38}
39 39
40EmailHandler::EmailHandler() 40EmailHandler::EmailHandler()
41{ 41{
42 qDebug("EMailHandler::EmailHandler"); 42 qDebug("EMailHandler::EmailHandler");
43 43
44 smtpClient = new SmtpClient(); 44 smtpClient = new SmtpClient();
45 popClient = new PopClient(); 45 popClient = new PopClient();
46 46
47 connect(smtpClient, SIGNAL(errorOccurred(int)), this, 47 connect(smtpClient, SIGNAL(errorOccurred(int)), this,
48 SIGNAL(smtpError(int)) ); 48 SIGNAL(smtpError(int)) );
49 connect(smtpClient, SIGNAL(mailSent()), this, SIGNAL(mailSent()) ); 49 connect(smtpClient, SIGNAL(mailSent()), this, SIGNAL(mailSent()) );
50 connect(smtpClient, SIGNAL(updateStatus(const QString &)), this, 50 connect(smtpClient, SIGNAL(updateStatus(const QString &)), this,
51 SIGNAL(updateSmtpStatus(const QString &)) ); 51 SIGNAL(updateSmtpStatus(const QString &)) );
52 52
53 connect(popClient, SIGNAL(errorOccurred(int)), this, 53 connect(popClient, SIGNAL(errorOccurred(int)), this,
54 SIGNAL(popError(int)) ); 54 SIGNAL(popError(int)) );
55 connect(popClient, SIGNAL(newMessage(const QString &, int, uint, bool)), 55 connect(popClient, SIGNAL(newMessage(const QString &, int, uint, bool)),
56 this, SLOT(messageArrived(const QString &, int, uint, bool)) ); 56 this, SLOT(messageArrived(const QString &, int, uint, bool)) );
57 connect(popClient, SIGNAL(updateStatus(const QString &)), this, 57 connect(popClient, SIGNAL(updateStatus(const QString &)), this,
58 SIGNAL(updatePopStatus(const QString &)) ); 58 SIGNAL(updatePopStatus(const QString &)) );
59 connect(popClient, SIGNAL(mailTransfered(int)), this, 59 connect(popClient, SIGNAL(mailTransfered(int)), this,
60 SIGNAL(mailTransfered(int)) ); 60 SIGNAL(mailTransfered(int)) );
61 61
62 62
63 //relaying size information 63 //relaying size information
64 connect(popClient, SIGNAL(currentMailSize(int)), 64 connect(popClient, SIGNAL(currentMailSize(int)),
65 this, SIGNAL(currentMailSize(int)) ); 65 this, SIGNAL(currentMailSize(int)) );
66 connect(popClient, SIGNAL(downloadedSize(int)), 66 connect(popClient, SIGNAL(downloadedSize(int)),
67 this, SIGNAL(downloadedSize(int)) ); 67 this, SIGNAL(downloadedSize(int)) );
68} 68}
69 69
70void EmailHandler::sendMail(QList<Email> *mailList) 70void EmailHandler::sendMail(QList<Email> *mailList)
71{ 71{
72 Email *currentMail; 72 Email *currentMail;
73 QString temp; 73 QString temp;
74 QString userName = mailAccount.name; 74 QString userName = mailAccount.name;
75 userName += " <" + mailAccount.emailAddress + ">"; 75 userName += " <" + mailAccount.emailAddress + ">";
76 76
77 for (currentMail = mailList->first(); currentMail != 0; 77 for (currentMail = mailList->first(); currentMail != 0;
78 currentMail = mailList->next()) { 78 currentMail = mailList->next()) {
79 79
80 if (encodeMime(currentMail) == 0) { 80 if (encodeMime(currentMail) == 0) {
81 smtpClient->addMail(userName, currentMail->subject, 81 smtpClient->addMail(userName, currentMail->subject,
82 currentMail->recipients, currentMail->rawMail); 82 currentMail->recipients, currentMail->rawMail);
83 } else { //error 83 } else { //error
84 temp = tr("Could not locate all files in \nmail with subject: ") + 84 temp = tr("Could not locate all files in \nmail with subject: ") +
85 currentMail->subject; 85 currentMail->subject;
86 temp += tr("\nMail has NOT been sent"); 86 temp += tr("\nMail has NOT been sent");
87 QMessageBox::warning(qApp->activeWindow(), tr("Attachment error"), temp, tr("OK\n")); 87 QMessageBox::warning(qApp->activeWindow(), tr("Attachment error"), temp, tr("OK\n"));
88 88
89 } 89 }
90 } 90 }
91 smtpClient->newConnection(mailAccount.smtpServer, 25); 91 smtpClient->newConnection(mailAccount.smtpServer, 25);
92} 92}
93 93
94void EmailHandler::setAccount(MailAccount account) 94void EmailHandler::setAccount(MailAccount account)
95{ 95{
96 mailAccount = account; 96 mailAccount = account;
97} 97}
98 98
99void EmailHandler::getMail() 99void EmailHandler::getMail()
100{ 100{
101 popClient->setAccount(mailAccount.popUserName, mailAccount.popPasswd); 101 popClient->setAccount(mailAccount.popUserName, mailAccount.popPasswd);
102 if (mailAccount.synchronize) { 102 if (mailAccount.synchronize) {
103 popClient->setSynchronize(mailAccount.lastServerMailCount); 103 popClient->setSynchronize(mailAccount.lastServerMailCount);
104 } else { 104 } else {
105 popClient->removeSynchronize(); 105 popClient->removeSynchronize();
106 } 106 }
107 107
108 headers = FALSE; 108 headers = FALSE;
109 //popClient->headersOnly(headers, 0); 109 //popClient->headersOnly(headers, 0);
110 popClient->newConnection(mailAccount.popServer, 110); 110 popClient->newConnection(mailAccount.popServer, 110);
111} 111}
112 112
113void EmailHandler::getMailHeaders() 113void EmailHandler::getMailHeaders()
114{ 114{
115 popClient->setAccount(mailAccount.popUserName, mailAccount.popPasswd); 115 popClient->setAccount(mailAccount.popUserName, mailAccount.popPasswd);
116 mailAccount.synchronize ? popClient->setSynchronize(mailAccount.lastServerMailCount): popClient->removeSynchronize(); 116 mailAccount.synchronize ? popClient->setSynchronize(mailAccount.lastServerMailCount): popClient->removeSynchronize();
117 117
118 headers = TRUE; 118 headers = TRUE;
119 popClient->headersOnly(headers, mailAccount.syncLimit); //less than requested syncLimit, download all 119 popClient->headersOnly(headers, mailAccount.syncLimit); //less than requested syncLimit, download all
120 qDebug("Initiating connection"); 120 qDebug("Initiating connection");
121 popClient->newConnection(mailAccount.popServer, 110); 121 popClient->newConnection(mailAccount.popServer, 110);
122} 122}
123 123
124void EmailHandler::getMailByList(MailList *mailList) 124void EmailHandler::getMailByList(MailList *mailList)
125{ 125{
126 if (mailList->count() == 0) { //should not occur though 126 if (mailList->count() == 0) { //should not occur though
127 emit mailTransfered(0); 127 emit mailTransfered(0);
128 return; 128 return;
129 } 129 }
130 130
131 headers = FALSE; 131 headers = FALSE;
132 popClient->headersOnly(FALSE, 0); 132 popClient->headersOnly(FALSE, 0);
133 133
134 popClient->setAccount(mailAccount.popUserName,mailAccount.popPasswd); 134 popClient->setAccount(mailAccount.popUserName,mailAccount.popPasswd);
135 popClient->setSelectedMails(mailList); 135 popClient->setSelectedMails(mailList);
136 popClient->newConnection(mailAccount.popServer, 110); 136 popClient->newConnection(mailAccount.popServer, 110);
137 } 137 }
138 138
139void EmailHandler::messageArrived(const QString &message, int id, uint size, bool complete) 139void EmailHandler::messageArrived(const QString &message, int id, uint size, bool complete)
140{ 140{
141 Email mail; 141 Email mail;
142 142
143 mail.rawMail = message; 143 mail.rawMail = message;
144 mail.serverId = id; 144 mail.serverId = id;
145 mail.size = size; 145 mail.size = size;
146 mail.downloaded = complete; 146 mail.downloaded = complete;
147 147
148 emit mailArrived(mail, FALSE); 148 emit mailArrived(mail, FALSE);
149} 149}
150 150
151bool EmailHandler::parse(QString in, QString lineShift, Email *mail) 151bool EmailHandler::parse(QString in, QString lineShift, Email *mail)
152{ 152{
153 QString temp, boundary; 153 QString temp, boundary;
154 int pos; 154 int pos;
155 QString delimiter, header, body, mimeHeader, mimeBody; 155 QString delimiter, header, body, mimeHeader, mimeBody;
156 QString content, contentType, contentAttribute, id, encoding; 156 QString content, contentType, contentAttribute, id, encoding;
157 QString fileName, storedName; 157 QString fileName, storedName;
158 int enclosureId = 0; 158 int enclosureId = 0;
159 159
160 mail->rawMail = in; 160 mail->rawMail = in;
161 mail->received = TRUE; 161 mail->received = TRUE;
162 mail->files.setAutoDelete(TRUE); 162 mail->files.setAutoDelete(TRUE);
163 163
164 temp = lineShift + "." + lineShift; 164 temp = lineShift + "." + lineShift;
165 165
166 if (in.right(temp.length()) != temp) { 166 if (in.right(temp.length()) != temp) {
167 mail->rawMail += temp; 167 mail->rawMail += temp;
168 } 168 }
169 169
170 170
171 delimiter = lineShift + lineShift; // "\n\n" or "\r\n\r\n" 171 delimiter = lineShift + lineShift; // "\n\n" or "\r\n\r\n"
172 pos = in.find(delimiter, 0, FALSE); 172 pos = in.find(delimiter, 0, FALSE);
173 header = in.left(pos); 173 header = in.left(pos);
174 body = in.right(in.length() - pos - delimiter.length()); 174 body = in.right(in.length() - pos - delimiter.length());
175 if ((body.at(body.length()-2) == '.') && (body.at(body.length()-3) == '\n')) 175 if ((body.at(body.length()-2) == '.') && (body.at(body.length()-3) == '\n'))
176 body.truncate(body.length()-2); 176 body.truncate(body.length()-2);
177 177
178 TextParser p(header, lineShift); 178 TextParser p(header, lineShift);
179 179
180 if ((pos = p.find("FROM",':', 0, TRUE)) != -1) { 180 if ((pos = p.find("FROM",':', 0, TRUE)) != -1) {
181 pos++; 181 pos++;
182 if (p.separatorAt(pos) == ' ') { 182 if (p.separatorAt(pos) == ' ') {
183 mail->from = p.getString(&pos, '<', false); 183 mail->from = p.getString(&pos, '<', false);
184 mail->from = mail->from.stripWhiteSpace(); 184 mail->from = mail->from.stripWhiteSpace();
185 if ( (mail->from.length() > 2) && (mail->from[0] == '"') ) { 185 if ( (mail->from.length() > 2) && (mail->from[0] == '"') ) {
186 mail->from = mail->from.left(mail->from.length() - 1); 186 mail->from = mail->from.left(mail->from.length() - 1);
187 mail->from = mail->from.right(mail->from.length() - 1); 187 mail->from = mail->from.right(mail->from.length() - 1);
188 } 188 }
189 pos++; 189 pos++;
190 mail->fromMail = p.getString(&pos, '>', false); 190 mail->fromMail = p.getString(&pos, '>', false);
191 } else { 191 } else {
192 if (p.separatorAt(pos) == '<') //No name.. nasty 192 if (p.separatorAt(pos) == '<') //No name.. nasty
193 pos++; 193 pos++;
194 //pos++; 194 //pos++;
195 mail->fromMail = p.getString(&pos, 'z', TRUE); 195 mail->fromMail = p.getString(&pos, 'z', TRUE);
196 if (mail->fromMail.at(mail->fromMail.length()-1) == '>') 196 if (mail->fromMail.at(mail->fromMail.length()-1) == '>')
197 mail->fromMail.truncate(mail->fromMail.length() - 1); 197 mail->fromMail.truncate(mail->fromMail.length() - 1);
198 mail->from=mail->fromMail; 198 mail->from=mail->fromMail;
199 } 199 }
200 } 200 }
201 201
202 pos=0; 202 pos=0;
203 203
204 //Search for To: after the FROM: attribute to prevent hitting the Delivered-To: 204 //Search for To: after the FROM: attribute to prevent hitting the Delivered-To:
205 while((pos = p.find("TO",':', pos+1, TRUE))!=-1) 205 while((pos = p.find("TO",':', pos+1, TRUE))!=-1)
206 { 206 {
207 QString rec; 207 QString rec;
208 208
209 if (p.separatorAt(pos-1)!='-') 209 if (p.separatorAt(pos-1)!='-')//The - separator means that this is a Delivered-To: or Reply-To:
210 { 210 {
211 pos++; 211 pos++;
212 mail->recipients.append(p.getString(&pos, '\r', TRUE)); 212 mail->recipients.append(p.getString(&pos, '\r', TRUE));
213 } 213 }
214 /*else {
215 if ((p.separatorAt(pos) == '<')|| (p.separatorAt(pos) == ' ')) //No name.. nasty
216 pos++;
217 pos++;
218 mail->fromMail = p.getString(&pos, 'z', TRUE);
219 if (mail->fromMail.at(mail->fromMail.length()-1) == '>')
220 mail->fromMail.truncate(mail->fromMail.length() - 1);
221 mail->from=mail->fromMail;
222 }
223 mail->recipients.append (p.getString(&pos, 'z', TRUE) );
224 }*/
225 } 214 }
226 // 215 //
227 //if (pos==-1) mail->recipients.append (tr("undisclosed recipients") ); 216 //if (pos==-1) mail->recipients.append (tr("undisclosed recipients") );
228 217
229 if ((pos = p.find("CC",':', 0, TRUE)) != -1) 218 if ((pos = p.find("CC",':', 0, TRUE)) != -1)
230 { 219 {
231 pos++; 220 pos++;
232 mail->carbonCopies.append (p.getString(&pos, 'z', TRUE) ); 221 mail->carbonCopies.append (p.getString(&pos, 'z', TRUE) );
233 } 222 }
234 223
235 if ((pos = p.find("SUBJECT",':', 0, TRUE)) != -1) { 224 if ((pos = p.find("SUBJECT",':', 0, TRUE)) != -1) {
236 pos++; 225 pos++;
237 mail->subject = p.getString(&pos, 'z', TRUE); 226 mail->subject = p.getString(&pos, 'z', TRUE);
238 } 227 }
239 228
240 if ((pos = p.find("DATE",':', 0, TRUE)) != -1) { 229 if ((pos = p.find("DATE",':', 0, TRUE)) != -1) {
241 pos++; 230 pos++;
242 mail->date = p.getString(&pos, 'z', TRUE); 231 mail->date = p.getString(&pos, 'z', TRUE);
243 } 232 }
244 233
245 234
246 235
247 if ((pos = p.find("MESSAGE",'-', 0, TRUE)) != -1) { 236 if ((pos = p.find("MESSAGE",'-', 0, TRUE)) != -1) {
248 pos++; 237 pos++;
249 if ( (p.wordAt(pos).upper() == "ID") && 238 if ( (p.wordAt(pos).upper() == "ID") &&
250 (p.separatorAt(pos) == ':') ) { 239 (p.separatorAt(pos) == ':') ) {
251 240
252 id = p.getString(&pos, 'z', TRUE); 241 id = p.getString(&pos, 'z', TRUE);
253 mail->id = id; 242 mail->id = id;
254 } 243 }
255 } 244 }
256 245
257 pos = 0; 246 pos = 0;
258 while ( ((pos = p.find("MIME",'-', pos, TRUE)) != -1) ) { 247 while ( ((pos = p.find("MIME",'-', pos, TRUE)) != -1) ) {
259 pos++; 248 pos++;
260 if ( (p.wordAt(pos).upper() == "VERSION") && 249 if ( (p.wordAt(pos).upper() == "VERSION") &&
261 (p.separatorAt(pos) == ':') ) { 250 (p.separatorAt(pos) == ':') ) {
262 pos++; 251 pos++;
263 if (p.getString(&pos, 'z', true) == "1.0") { 252 if (p.getString(&pos, 'z', true) == "1.0") {
264 mail->mimeType = 1; 253 mail->mimeType = 1;
265 } 254 }
266 } 255 }
267 } 256 }
268 257
269 if (mail->mimeType == 1) { 258 if (mail->mimeType == 1) {
270 boundary = ""; 259 boundary = "";
271 if ((pos = p.find("BOUNDARY", '=', 0, TRUE)) != -1) { 260 if ((pos = p.find("BOUNDARY", '=', 0, TRUE)) != -1) {
272 pos++; 261 pos++;
273 boundary = p.getString(&pos, 'z', true); 262 boundary = p.getString(&pos, 'z', true);
274 if (boundary[0] == '"') { 263 if (boundary[0] == '"') {
275 boundary = boundary.left(boundary.length() - 1); //strip " 264 boundary = boundary.left(boundary.length() - 1); //strip "
276 boundary = boundary.right(boundary.length() - 1); //strip " 265 boundary = boundary.right(boundary.length() - 1); //strip "
277 } 266 }
278 boundary = "--" + boundary; //create boundary field 267 boundary = "--" + boundary; //create boundary field
279 } 268 }
280 269
281 if (boundary == "") { //fooled by Mime-Version 270 if (boundary == "") { //fooled by Mime-Version
282 mail->body = body; 271 mail->body = body;
283 mail->bodyPlain = body; 272 mail->bodyPlain = body;
284 return mail; 273 return mail;
285 } 274 }
286 275
287 while (body.length() > 0) { 276 while (body.length() > 0) {
288 pos = body.find(boundary, 0, FALSE); 277 pos = body.find(boundary, 0, FALSE);
289 pos = body.find(delimiter, pos, FALSE); 278 pos = body.find(delimiter, pos, FALSE);
290 mimeHeader = body.left(pos); 279 mimeHeader = body.left(pos);
291 mimeBody = body.right(body.length() - pos - delimiter.length()); 280 mimeBody = body.right(body.length() - pos - delimiter.length());
292 TextParser bp(mimeHeader, lineShift); 281 TextParser bp(mimeHeader, lineShift);
293 282
294 contentType = ""; 283 contentType = "";
295 contentAttribute = ""; 284 contentAttribute = "";
296 fileName = ""; 285 fileName = "";
297 if ((pos = bp.find("CONTENT",'-', 0, TRUE)) != -1) { 286 if ((pos = bp.find("CONTENT",'-', 0, TRUE)) != -1) {
298 pos++; 287 pos++;
299 if ( (bp.wordAt(pos).upper() == "TYPE") && 288 if ( (bp.wordAt(pos).upper() == "TYPE") &&
300 (bp.separatorAt(pos) == ':') ) { 289 (bp.separatorAt(pos) == ':') ) {
301 contentType = bp.nextWord().upper(); 290 contentType = bp.nextWord().upper();
302 if (bp.nextSeparator() == '/') 291 if (bp.nextSeparator() == '/')
303 contentAttribute = bp.nextWord().upper(); 292 contentAttribute = bp.nextWord().upper();
304 content = contentType + "/" + contentAttribute; 293 content = contentType + "/" + contentAttribute;
305 } 294 }
306 if ((pos = bp.find("ENCODING",':', 0, TRUE)) != -1) { 295 if ((pos = bp.find("ENCODING",':', 0, TRUE)) != -1) {
307 pos++; 296 pos++;
308 encoding = bp.getString(&pos, 'z', TRUE); 297 encoding = bp.getString(&pos, 'z', TRUE);
309 } 298 }
310 299
311 if ( (pos = bp.find("FILENAME",'=', 0, TRUE)) != -1) { 300 if ( (pos = bp.find("FILENAME",'=', 0, TRUE)) != -1) {
312 pos++; 301 pos++;
313 fileName = bp.getString(&pos, 'z', TRUE); 302 fileName = bp.getString(&pos, 'z', TRUE);
314 fileName = fileName.right(fileName.length() - 1); 303 fileName = fileName.right(fileName.length() - 1);
315 fileName = fileName.left(fileName.length() - 1); 304 fileName = fileName.left(fileName.length() - 1);
316 } 305 }
317 306
318 } 307 }
319 pos = mimeBody.find(boundary, 0, FALSE); 308 pos = mimeBody.find(boundary, 0, FALSE);
320 if (pos == -1) //should not occur, malformed mail 309 if (pos == -1) //should not occur, malformed mail
321 pos = mimeBody.length(); 310 pos = mimeBody.length();
322 body = mimeBody.right(mimeBody.length() - pos); 311 body = mimeBody.right(mimeBody.length() - pos);
323 mimeBody = mimeBody.left(pos); 312 mimeBody = mimeBody.left(pos);
324 313
325 if (fileName != "") { //attatchments of some type, audio, image etc. 314 if (fileName != "") { //attatchments of some type, audio, image etc.
326 315
327 Enclosure e; 316 Enclosure e;
328 e.id = enclosureId; 317 e.id = enclosureId;
329 e.originalName = fileName; 318 e.originalName = fileName;
330 e.contentType = contentType; 319 e.contentType = contentType;
331 e.contentAttribute = contentAttribute; 320 e.contentAttribute = contentAttribute;
332 e.encoding = encoding; 321 e.encoding = encoding;
333 e.body = mimeBody; 322 e.body = mimeBody;
334 e.saved = FALSE; 323 e.saved = FALSE;
335 mail->addEnclosure(&e); 324 mail->addEnclosure(&e);
336 enclosureId++; 325 enclosureId++;
337 326
338 } else if (contentType == "TEXT") { 327 } else if (contentType == "TEXT") {
339 if (contentAttribute == "PLAIN") { 328 if (contentAttribute == "PLAIN") {
340 mail->body = mimeBody; 329 mail->body = mimeBody;
341 mail->bodyPlain = mimeBody; 330 mail->bodyPlain = mimeBody;
342 } 331 }
343 if (contentAttribute == "HTML") { 332 if (contentAttribute == "HTML") {
344 mail->body = mimeBody; 333 mail->body = mimeBody;
345 } 334 }
346 } 335 }
347 } 336 }
348 } else { 337 } else {
349 mail->bodyPlain = body; 338 mail->bodyPlain = body;
350 mail->body = body; 339 mail->body = body;
351 } 340 }
352 return TRUE; 341 return TRUE;
353} 342}
354 343
355bool EmailHandler::getEnclosure(Enclosure *ePtr) 344bool EmailHandler::getEnclosure(Enclosure *ePtr)
356{ 345{
357 QFile f(ePtr->path + ePtr->name); 346 QFile f(ePtr->path + ePtr->name);
358 char src[4]; 347 char src[4];
359 char *destPtr; 348 char *destPtr;
360 QByteArray buffer; 349 QByteArray buffer;
361 uint bufCount, pos, decodedCount, size, x; 350 uint bufCount, pos, decodedCount, size, x;
362 351
363 if (! f.open(IO_WriteOnly) ) { 352 if (! f.open(IO_WriteOnly) ) {
364 qWarning("could not save: " + ePtr->path + ePtr->name); 353 qWarning("could not save: " + ePtr->path + ePtr->name);
365 return FALSE; 354 return FALSE;
366 } 355 }
367 356
368 if (ePtr->encoding.upper() == "BASE64") { 357 if (ePtr->encoding.upper() == "BASE64") {
369 size = (ePtr->body.length() * 3 / 4); //approximate size (always above) 358 size = (ePtr->body.length() * 3 / 4); //approximate size (always above)
370 buffer.resize(size); 359 buffer.resize(size);
371 bufCount = 0; 360 bufCount = 0;
372 pos = 0; 361 pos = 0;
373 destPtr = buffer.data(); 362 destPtr = buffer.data();
374 363
375 while (pos < ePtr->body.length()) { 364 while (pos < ePtr->body.length()) {
376 decodedCount = 4; 365 decodedCount = 4;
377 x = 0; 366 x = 0;
378 while ( (x < 4) && (pos < ePtr->body.length()) ) { 367 while ( (x < 4) && (pos < ePtr->body.length()) ) {
379 src[x] = ePtr->body[pos].latin1(); 368 src[x] = ePtr->body[pos].latin1();
380 pos++; 369 pos++;
381 if (src[x] == '\r' || src[x] == '\n' || src[x] == ' ') 370 if (src[x] == '\r' || src[x] == '\n' || src[x] == ' ')
382 x--; 371 x--;
383 x++; 372 x++;
384 } 373 }
385 if (x > 1) { 374 if (x > 1) {
386 decodedCount = parse64base(src, destPtr); 375 decodedCount = parse64base(src, destPtr);
387 destPtr += decodedCount; 376 destPtr += decodedCount;
388 bufCount += decodedCount; 377 bufCount += decodedCount;
389 } 378 }
390 } 379 }
391 380
392 buffer.resize(bufCount); //set correct length of file 381 buffer.resize(bufCount); //set correct length of file
393 f.writeBlock(buffer); 382 f.writeBlock(buffer);
394 } else { 383 } else {
395 QTextStream t(&f); 384 QTextStream t(&f);
396 t << ePtr->body; 385 t << ePtr->body;
397 } 386 }
398 return TRUE; 387 return TRUE;
399} 388}
400 389
401int EmailHandler::parse64base(char *src, char *bufOut) { 390int EmailHandler::parse64base(char *src, char *bufOut) {
402 391
403 char c, z; 392 char c, z;
404 char li[4]; 393 char li[4];
405 int processed; 394 int processed;
406 395
407 //conversion table withouth table... 396 //conversion table withouth table...
408 for (int x = 0; x < 4; x++) { 397 for (int x = 0; x < 4; x++) {
409 c = src[x]; 398 c = src[x];
410 399
411 if ( (int) c >= 'A' && (int) c <= 'Z') 400 if ( (int) c >= 'A' && (int) c <= 'Z')
412 li[x] = (int) c - (int) 'A'; 401 li[x] = (int) c - (int) 'A';
413 if ( (int) c >= 'a' && (int) c <= 'z') 402 if ( (int) c >= 'a' && (int) c <= 'z')
414 li[x] = (int) c - (int) 'a' + 26; 403 li[x] = (int) c - (int) 'a' + 26;
415 if ( (int) c >= '0' && (int) c <= '9') 404 if ( (int) c >= '0' && (int) c <= '9')
416 li[x] = (int) c - (int) '0' + 52; 405 li[x] = (int) c - (int) '0' + 52;
417 if (c == '+') 406 if (c == '+')
418 li[x] = 62; 407 li[x] = 62;
419 if (c == '/') 408 if (c == '/')
420 li[x] = 63; 409 li[x] = 63;
421 } 410 }
422 411
423 processed = 1; 412 processed = 1;
424 bufOut[0] = (char) li[0] & (32+16+8+4+2+1); //mask out top 2 bits 413 bufOut[0] = (char) li[0] & (32+16+8+4+2+1); //mask out top 2 bits
425 bufOut[0] <<= 2; 414 bufOut[0] <<= 2;
426 z = li[1] >> 4; 415 z = li[1] >> 4;
427 bufOut[0] = bufOut[0] | z; //first byte retrived 416 bufOut[0] = bufOut[0] | z; //first byte retrived
428 417
429 if (src[2] != '=') { 418 if (src[2] != '=') {
430 bufOut[1] = (char) li[1] & (8+4+2+1); //mask out top 4 bits 419 bufOut[1] = (char) li[1] & (8+4+2+1); //mask out top 4 bits
431 bufOut[1] <<= 4; 420 bufOut[1] <<= 4;
432 z = li[2] >> 2; 421 z = li[2] >> 2;
433 bufOut[1] = bufOut[1] | z; //second byte retrived 422 bufOut[1] = bufOut[1] | z; //second byte retrived
434 processed++; 423 processed++;
435 424
436 if (src[3] != '=') { 425 if (src[3] != '=') {
437 bufOut[2] = (char) li[2] & (2+1); //mask out top 6 bits 426 bufOut[2] = (char) li[2] & (2+1); //mask out top 6 bits
438 bufOut[2] <<= 6; 427 bufOut[2] <<= 6;
439 z = li[3]; 428 z = li[3];
440 bufOut[2] = bufOut[2] | z; //third byte retrieved 429 bufOut[2] = bufOut[2] | z; //third byte retrieved
441 processed++; 430 processed++;
442 } 431 }
443 } 432 }
444 return processed; 433 return processed;
445} 434}
446 435
447int EmailHandler::encodeMime(Email *mail) 436int EmailHandler::encodeMime(Email *mail)
448{ 437{
449 438
450 QString fileName, fileType, contentType, newBody, boundary; 439 QString fileName, fileType, contentType, newBody, boundary;
451 Enclosure *ePtr; 440 Enclosure *ePtr;
452 441
453 QString userName = mailAccount.name; 442 QString userName = mailAccount.name;
454 if (userName.length()>0)//only embrace it if there is a user name 443 if (userName.length()>0)//only embrace it if there is a user name
455 userName += " <" + mailAccount.emailAddress + ">"; 444 userName += " <" + mailAccount.emailAddress + ">";
456 445
457 //add standard headers 446 //add standard headers
458 newBody = "From: " + userName + "\r\nTo: "; 447 newBody = "From: " + userName + "\r\nTo: ";
459 for (QStringList::Iterator it = mail->recipients.begin(); it != mail->recipients.end(); ++it ) { 448 for (QStringList::Iterator it = mail->recipients.begin(); it != mail->recipients.end(); ++it ) {
460 newBody += *it + " "; 449 newBody += *it + " ";
461 } 450 }
462 451
463 newBody += "\r\nCC: "; 452 newBody += "\r\nCC: ";
464 453
465 for (QStringList::Iterator it = mail->carbonCopies.begin(); it != mail->carbonCopies.end(); ++it ) { 454 for (QStringList::Iterator it = mail->carbonCopies.begin(); it != mail->carbonCopies.end(); ++it ) {
466 newBody += *it + " "; 455 newBody += *it + " ";
467 } 456 }
468 457
469 newBody += "\r\nSubject: " + mail->subject + "\r\n"; 458 newBody += "\r\nSubject: " + mail->subject + "\r\n";
470 459
471 if (mail->files.count() == 0) { //just a simple mail 460 if (mail->files.count() == 0) { //just a simple mail
472 newBody += "\r\n" + mail->body; 461 newBody += "\r\n" + mail->body;
473 mail->rawMail = newBody; 462 mail->rawMail = newBody;
474 return 0; 463 return 0;
475 } 464 }
476 465
477 //Build mime encoded mail 466 //Build mime encoded mail
478 boundary = "-----4345=next_bound=0495----"; 467 boundary = "-----4345=next_bound=0495----";
479 468
480 newBody += "Mime-Version: 1.0\r\n"; 469 newBody += "Mime-Version: 1.0\r\n";
diff --git a/noncore/unsupported/mailit/emailhandler.cpp b/noncore/unsupported/mailit/emailhandler.cpp
index c7b27a0..62fa64f 100644
--- a/noncore/unsupported/mailit/emailhandler.cpp
+++ b/noncore/unsupported/mailit/emailhandler.cpp
@@ -1,480 +1,469 @@
1/********************************************************************** 1/**********************************************************************
2** Copyright (C) 2001 Trolltech AS. All rights reserved. 2** Copyright (C) 2001 Trolltech AS. All rights reserved.
3** 3**
4** This file is part of Qt Palmtop Environment. 4** This file is part of Qt Palmtop Environment.
5** 5**
6** This file may be distributed and/or modified under the terms of the 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 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 8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file. 9** packaging of this file.
10** 10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 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. 12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13** 13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information. 14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15** 15**
16** Contact info@trolltech.com if any conditions of this licensing are 16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you. 17** not clear to you.
18** 18**
19**********************************************************************/ 19**********************************************************************/
20#include <qfileinfo.h> 20#include <qfileinfo.h>
21#include <stdlib.h> 21#include <stdlib.h>
22#include <qapplication.h> 22#include <qapplication.h>
23#include <qmessagebox.h> 23#include <qmessagebox.h>
24#include <qcstring.h> 24#include <qcstring.h>
25#include "emailhandler.h" 25#include "emailhandler.h"
26#include <qpe/applnk.h> 26#include <qpe/applnk.h>
27#include <qpe/filemanager.h> 27#include <qpe/filemanager.h>
28 28
29QCollection::Item EnclosureList::newItem(QCollection::Item d) 29QCollection::Item EnclosureList::newItem(QCollection::Item d)
30{ 30{
31 return dupl( (Enclosure *) d); 31 return dupl( (Enclosure *) d);
32} 32}
33 33
34Enclosure* EnclosureList::dupl(Enclosure *in) 34Enclosure* EnclosureList::dupl(Enclosure *in)
35{ 35{
36 ac = new Enclosure(*in); 36 ac = new Enclosure(*in);
37 return ac; 37 return ac;
38} 38}
39 39
40EmailHandler::EmailHandler() 40EmailHandler::EmailHandler()
41{ 41{
42 qDebug("EMailHandler::EmailHandler"); 42 qDebug("EMailHandler::EmailHandler");
43 43
44 smtpClient = new SmtpClient(); 44 smtpClient = new SmtpClient();
45 popClient = new PopClient(); 45 popClient = new PopClient();
46 46
47 connect(smtpClient, SIGNAL(errorOccurred(int)), this, 47 connect(smtpClient, SIGNAL(errorOccurred(int)), this,
48 SIGNAL(smtpError(int)) ); 48 SIGNAL(smtpError(int)) );
49 connect(smtpClient, SIGNAL(mailSent()), this, SIGNAL(mailSent()) ); 49 connect(smtpClient, SIGNAL(mailSent()), this, SIGNAL(mailSent()) );
50 connect(smtpClient, SIGNAL(updateStatus(const QString &)), this, 50 connect(smtpClient, SIGNAL(updateStatus(const QString &)), this,
51 SIGNAL(updateSmtpStatus(const QString &)) ); 51 SIGNAL(updateSmtpStatus(const QString &)) );
52 52
53 connect(popClient, SIGNAL(errorOccurred(int)), this, 53 connect(popClient, SIGNAL(errorOccurred(int)), this,
54 SIGNAL(popError(int)) ); 54 SIGNAL(popError(int)) );
55 connect(popClient, SIGNAL(newMessage(const QString &, int, uint, bool)), 55 connect(popClient, SIGNAL(newMessage(const QString &, int, uint, bool)),
56 this, SLOT(messageArrived(const QString &, int, uint, bool)) ); 56 this, SLOT(messageArrived(const QString &, int, uint, bool)) );
57 connect(popClient, SIGNAL(updateStatus(const QString &)), this, 57 connect(popClient, SIGNAL(updateStatus(const QString &)), this,
58 SIGNAL(updatePopStatus(const QString &)) ); 58 SIGNAL(updatePopStatus(const QString &)) );
59 connect(popClient, SIGNAL(mailTransfered(int)), this, 59 connect(popClient, SIGNAL(mailTransfered(int)), this,
60 SIGNAL(mailTransfered(int)) ); 60 SIGNAL(mailTransfered(int)) );
61 61
62 62
63 //relaying size information 63 //relaying size information
64 connect(popClient, SIGNAL(currentMailSize(int)), 64 connect(popClient, SIGNAL(currentMailSize(int)),
65 this, SIGNAL(currentMailSize(int)) ); 65 this, SIGNAL(currentMailSize(int)) );
66 connect(popClient, SIGNAL(downloadedSize(int)), 66 connect(popClient, SIGNAL(downloadedSize(int)),
67 this, SIGNAL(downloadedSize(int)) ); 67 this, SIGNAL(downloadedSize(int)) );
68} 68}
69 69
70void EmailHandler::sendMail(QList<Email> *mailList) 70void EmailHandler::sendMail(QList<Email> *mailList)
71{ 71{
72 Email *currentMail; 72 Email *currentMail;
73 QString temp; 73 QString temp;
74 QString userName = mailAccount.name; 74 QString userName = mailAccount.name;
75 userName += " <" + mailAccount.emailAddress + ">"; 75 userName += " <" + mailAccount.emailAddress + ">";
76 76
77 for (currentMail = mailList->first(); currentMail != 0; 77 for (currentMail = mailList->first(); currentMail != 0;
78 currentMail = mailList->next()) { 78 currentMail = mailList->next()) {
79 79
80 if (encodeMime(currentMail) == 0) { 80 if (encodeMime(currentMail) == 0) {
81 smtpClient->addMail(userName, currentMail->subject, 81 smtpClient->addMail(userName, currentMail->subject,
82 currentMail->recipients, currentMail->rawMail); 82 currentMail->recipients, currentMail->rawMail);
83 } else { //error 83 } else { //error
84 temp = tr("Could not locate all files in \nmail with subject: ") + 84 temp = tr("Could not locate all files in \nmail with subject: ") +
85 currentMail->subject; 85 currentMail->subject;
86 temp += tr("\nMail has NOT been sent"); 86 temp += tr("\nMail has NOT been sent");
87 QMessageBox::warning(qApp->activeWindow(), tr("Attachment error"), temp, tr("OK\n")); 87 QMessageBox::warning(qApp->activeWindow(), tr("Attachment error"), temp, tr("OK\n"));
88 88
89 } 89 }
90 } 90 }
91 smtpClient->newConnection(mailAccount.smtpServer, 25); 91 smtpClient->newConnection(mailAccount.smtpServer, 25);
92} 92}
93 93
94void EmailHandler::setAccount(MailAccount account) 94void EmailHandler::setAccount(MailAccount account)
95{ 95{
96 mailAccount = account; 96 mailAccount = account;
97} 97}
98 98
99void EmailHandler::getMail() 99void EmailHandler::getMail()
100{ 100{
101 popClient->setAccount(mailAccount.popUserName, mailAccount.popPasswd); 101 popClient->setAccount(mailAccount.popUserName, mailAccount.popPasswd);
102 if (mailAccount.synchronize) { 102 if (mailAccount.synchronize) {
103 popClient->setSynchronize(mailAccount.lastServerMailCount); 103 popClient->setSynchronize(mailAccount.lastServerMailCount);
104 } else { 104 } else {
105 popClient->removeSynchronize(); 105 popClient->removeSynchronize();
106 } 106 }
107 107
108 headers = FALSE; 108 headers = FALSE;
109 //popClient->headersOnly(headers, 0); 109 //popClient->headersOnly(headers, 0);
110 popClient->newConnection(mailAccount.popServer, 110); 110 popClient->newConnection(mailAccount.popServer, 110);
111} 111}
112 112
113void EmailHandler::getMailHeaders() 113void EmailHandler::getMailHeaders()
114{ 114{
115 popClient->setAccount(mailAccount.popUserName, mailAccount.popPasswd); 115 popClient->setAccount(mailAccount.popUserName, mailAccount.popPasswd);
116 mailAccount.synchronize ? popClient->setSynchronize(mailAccount.lastServerMailCount): popClient->removeSynchronize(); 116 mailAccount.synchronize ? popClient->setSynchronize(mailAccount.lastServerMailCount): popClient->removeSynchronize();
117 117
118 headers = TRUE; 118 headers = TRUE;
119 popClient->headersOnly(headers, mailAccount.syncLimit); //less than requested syncLimit, download all 119 popClient->headersOnly(headers, mailAccount.syncLimit); //less than requested syncLimit, download all
120 qDebug("Initiating connection"); 120 qDebug("Initiating connection");
121 popClient->newConnection(mailAccount.popServer, 110); 121 popClient->newConnection(mailAccount.popServer, 110);
122} 122}
123 123
124void EmailHandler::getMailByList(MailList *mailList) 124void EmailHandler::getMailByList(MailList *mailList)
125{ 125{
126 if (mailList->count() == 0) { //should not occur though 126 if (mailList->count() == 0) { //should not occur though
127 emit mailTransfered(0); 127 emit mailTransfered(0);
128 return; 128 return;
129 } 129 }
130 130
131 headers = FALSE; 131 headers = FALSE;
132 popClient->headersOnly(FALSE, 0); 132 popClient->headersOnly(FALSE, 0);
133 133
134 popClient->setAccount(mailAccount.popUserName,mailAccount.popPasswd); 134 popClient->setAccount(mailAccount.popUserName,mailAccount.popPasswd);
135 popClient->setSelectedMails(mailList); 135 popClient->setSelectedMails(mailList);
136 popClient->newConnection(mailAccount.popServer, 110); 136 popClient->newConnection(mailAccount.popServer, 110);
137 } 137 }
138 138
139void EmailHandler::messageArrived(const QString &message, int id, uint size, bool complete) 139void EmailHandler::messageArrived(const QString &message, int id, uint size, bool complete)
140{ 140{
141 Email mail; 141 Email mail;
142 142
143 mail.rawMail = message; 143 mail.rawMail = message;
144 mail.serverId = id; 144 mail.serverId = id;
145 mail.size = size; 145 mail.size = size;
146 mail.downloaded = complete; 146 mail.downloaded = complete;
147 147
148 emit mailArrived(mail, FALSE); 148 emit mailArrived(mail, FALSE);
149} 149}
150 150
151bool EmailHandler::parse(QString in, QString lineShift, Email *mail) 151bool EmailHandler::parse(QString in, QString lineShift, Email *mail)
152{ 152{
153 QString temp, boundary; 153 QString temp, boundary;
154 int pos; 154 int pos;
155 QString delimiter, header, body, mimeHeader, mimeBody; 155 QString delimiter, header, body, mimeHeader, mimeBody;
156 QString content, contentType, contentAttribute, id, encoding; 156 QString content, contentType, contentAttribute, id, encoding;
157 QString fileName, storedName; 157 QString fileName, storedName;
158 int enclosureId = 0; 158 int enclosureId = 0;
159 159
160 mail->rawMail = in; 160 mail->rawMail = in;
161 mail->received = TRUE; 161 mail->received = TRUE;
162 mail->files.setAutoDelete(TRUE); 162 mail->files.setAutoDelete(TRUE);
163 163
164 temp = lineShift + "." + lineShift; 164 temp = lineShift + "." + lineShift;
165 165
166 if (in.right(temp.length()) != temp) { 166 if (in.right(temp.length()) != temp) {
167 mail->rawMail += temp; 167 mail->rawMail += temp;
168 } 168 }
169 169
170 170
171 delimiter = lineShift + lineShift; // "\n\n" or "\r\n\r\n" 171 delimiter = lineShift + lineShift; // "\n\n" or "\r\n\r\n"
172 pos = in.find(delimiter, 0, FALSE); 172 pos = in.find(delimiter, 0, FALSE);
173 header = in.left(pos); 173 header = in.left(pos);
174 body = in.right(in.length() - pos - delimiter.length()); 174 body = in.right(in.length() - pos - delimiter.length());
175 if ((body.at(body.length()-2) == '.') && (body.at(body.length()-3) == '\n')) 175 if ((body.at(body.length()-2) == '.') && (body.at(body.length()-3) == '\n'))
176 body.truncate(body.length()-2); 176 body.truncate(body.length()-2);
177 177
178 TextParser p(header, lineShift); 178 TextParser p(header, lineShift);
179 179
180 if ((pos = p.find("FROM",':', 0, TRUE)) != -1) { 180 if ((pos = p.find("FROM",':', 0, TRUE)) != -1) {
181 pos++; 181 pos++;
182 if (p.separatorAt(pos) == ' ') { 182 if (p.separatorAt(pos) == ' ') {
183 mail->from = p.getString(&pos, '<', false); 183 mail->from = p.getString(&pos, '<', false);
184 mail->from = mail->from.stripWhiteSpace(); 184 mail->from = mail->from.stripWhiteSpace();
185 if ( (mail->from.length() > 2) && (mail->from[0] == '"') ) { 185 if ( (mail->from.length() > 2) && (mail->from[0] == '"') ) {
186 mail->from = mail->from.left(mail->from.length() - 1); 186 mail->from = mail->from.left(mail->from.length() - 1);
187 mail->from = mail->from.right(mail->from.length() - 1); 187 mail->from = mail->from.right(mail->from.length() - 1);
188 } 188 }
189 pos++; 189 pos++;
190 mail->fromMail = p.getString(&pos, '>', false); 190 mail->fromMail = p.getString(&pos, '>', false);
191 } else { 191 } else {
192 if (p.separatorAt(pos) == '<') //No name.. nasty 192 if (p.separatorAt(pos) == '<') //No name.. nasty
193 pos++; 193 pos++;
194 //pos++; 194 //pos++;
195 mail->fromMail = p.getString(&pos, 'z', TRUE); 195 mail->fromMail = p.getString(&pos, 'z', TRUE);
196 if (mail->fromMail.at(mail->fromMail.length()-1) == '>') 196 if (mail->fromMail.at(mail->fromMail.length()-1) == '>')
197 mail->fromMail.truncate(mail->fromMail.length() - 1); 197 mail->fromMail.truncate(mail->fromMail.length() - 1);
198 mail->from=mail->fromMail; 198 mail->from=mail->fromMail;
199 } 199 }
200 } 200 }
201 201
202 pos=0; 202 pos=0;
203 203
204 //Search for To: after the FROM: attribute to prevent hitting the Delivered-To: 204 //Search for To: after the FROM: attribute to prevent hitting the Delivered-To:
205 while((pos = p.find("TO",':', pos+1, TRUE))!=-1) 205 while((pos = p.find("TO",':', pos+1, TRUE))!=-1)
206 { 206 {
207 QString rec; 207 QString rec;
208 208
209 if (p.separatorAt(pos-1)!='-') 209 if (p.separatorAt(pos-1)!='-')//The - separator means that this is a Delivered-To: or Reply-To:
210 { 210 {
211 pos++; 211 pos++;
212 mail->recipients.append(p.getString(&pos, '\r', TRUE)); 212 mail->recipients.append(p.getString(&pos, '\r', TRUE));
213 } 213 }
214 /*else {
215 if ((p.separatorAt(pos) == '<')|| (p.separatorAt(pos) == ' ')) //No name.. nasty
216 pos++;
217 pos++;
218 mail->fromMail = p.getString(&pos, 'z', TRUE);
219 if (mail->fromMail.at(mail->fromMail.length()-1) == '>')
220 mail->fromMail.truncate(mail->fromMail.length() - 1);
221 mail->from=mail->fromMail;
222 }
223 mail->recipients.append (p.getString(&pos, 'z', TRUE) );
224 }*/
225 } 214 }
226 // 215 //
227 //if (pos==-1) mail->recipients.append (tr("undisclosed recipients") ); 216 //if (pos==-1) mail->recipients.append (tr("undisclosed recipients") );
228 217
229 if ((pos = p.find("CC",':', 0, TRUE)) != -1) 218 if ((pos = p.find("CC",':', 0, TRUE)) != -1)
230 { 219 {
231 pos++; 220 pos++;
232 mail->carbonCopies.append (p.getString(&pos, 'z', TRUE) ); 221 mail->carbonCopies.append (p.getString(&pos, 'z', TRUE) );
233 } 222 }
234 223
235 if ((pos = p.find("SUBJECT",':', 0, TRUE)) != -1) { 224 if ((pos = p.find("SUBJECT",':', 0, TRUE)) != -1) {
236 pos++; 225 pos++;
237 mail->subject = p.getString(&pos, 'z', TRUE); 226 mail->subject = p.getString(&pos, 'z', TRUE);
238 } 227 }
239 228
240 if ((pos = p.find("DATE",':', 0, TRUE)) != -1) { 229 if ((pos = p.find("DATE",':', 0, TRUE)) != -1) {
241 pos++; 230 pos++;
242 mail->date = p.getString(&pos, 'z', TRUE); 231 mail->date = p.getString(&pos, 'z', TRUE);
243 } 232 }
244 233
245 234
246 235
247 if ((pos = p.find("MESSAGE",'-', 0, TRUE)) != -1) { 236 if ((pos = p.find("MESSAGE",'-', 0, TRUE)) != -1) {
248 pos++; 237 pos++;
249 if ( (p.wordAt(pos).upper() == "ID") && 238 if ( (p.wordAt(pos).upper() == "ID") &&
250 (p.separatorAt(pos) == ':') ) { 239 (p.separatorAt(pos) == ':') ) {
251 240
252 id = p.getString(&pos, 'z', TRUE); 241 id = p.getString(&pos, 'z', TRUE);
253 mail->id = id; 242 mail->id = id;
254 } 243 }
255 } 244 }
256 245
257 pos = 0; 246 pos = 0;
258 while ( ((pos = p.find("MIME",'-', pos, TRUE)) != -1) ) { 247 while ( ((pos = p.find("MIME",'-', pos, TRUE)) != -1) ) {
259 pos++; 248 pos++;
260 if ( (p.wordAt(pos).upper() == "VERSION") && 249 if ( (p.wordAt(pos).upper() == "VERSION") &&
261 (p.separatorAt(pos) == ':') ) { 250 (p.separatorAt(pos) == ':') ) {
262 pos++; 251 pos++;
263 if (p.getString(&pos, 'z', true) == "1.0") { 252 if (p.getString(&pos, 'z', true) == "1.0") {
264 mail->mimeType = 1; 253 mail->mimeType = 1;
265 } 254 }
266 } 255 }
267 } 256 }
268 257
269 if (mail->mimeType == 1) { 258 if (mail->mimeType == 1) {
270 boundary = ""; 259 boundary = "";
271 if ((pos = p.find("BOUNDARY", '=', 0, TRUE)) != -1) { 260 if ((pos = p.find("BOUNDARY", '=', 0, TRUE)) != -1) {
272 pos++; 261 pos++;
273 boundary = p.getString(&pos, 'z', true); 262 boundary = p.getString(&pos, 'z', true);
274 if (boundary[0] == '"') { 263 if (boundary[0] == '"') {
275 boundary = boundary.left(boundary.length() - 1); //strip " 264 boundary = boundary.left(boundary.length() - 1); //strip "
276 boundary = boundary.right(boundary.length() - 1); //strip " 265 boundary = boundary.right(boundary.length() - 1); //strip "
277 } 266 }
278 boundary = "--" + boundary; //create boundary field 267 boundary = "--" + boundary; //create boundary field
279 } 268 }
280 269
281 if (boundary == "") { //fooled by Mime-Version 270 if (boundary == "") { //fooled by Mime-Version
282 mail->body = body; 271 mail->body = body;
283 mail->bodyPlain = body; 272 mail->bodyPlain = body;
284 return mail; 273 return mail;
285 } 274 }
286 275
287 while (body.length() > 0) { 276 while (body.length() > 0) {
288 pos = body.find(boundary, 0, FALSE); 277 pos = body.find(boundary, 0, FALSE);
289 pos = body.find(delimiter, pos, FALSE); 278 pos = body.find(delimiter, pos, FALSE);
290 mimeHeader = body.left(pos); 279 mimeHeader = body.left(pos);
291 mimeBody = body.right(body.length() - pos - delimiter.length()); 280 mimeBody = body.right(body.length() - pos - delimiter.length());
292 TextParser bp(mimeHeader, lineShift); 281 TextParser bp(mimeHeader, lineShift);
293 282
294 contentType = ""; 283 contentType = "";
295 contentAttribute = ""; 284 contentAttribute = "";
296 fileName = ""; 285 fileName = "";
297 if ((pos = bp.find("CONTENT",'-', 0, TRUE)) != -1) { 286 if ((pos = bp.find("CONTENT",'-', 0, TRUE)) != -1) {
298 pos++; 287 pos++;
299 if ( (bp.wordAt(pos).upper() == "TYPE") && 288 if ( (bp.wordAt(pos).upper() == "TYPE") &&
300 (bp.separatorAt(pos) == ':') ) { 289 (bp.separatorAt(pos) == ':') ) {
301 contentType = bp.nextWord().upper(); 290 contentType = bp.nextWord().upper();
302 if (bp.nextSeparator() == '/') 291 if (bp.nextSeparator() == '/')
303 contentAttribute = bp.nextWord().upper(); 292 contentAttribute = bp.nextWord().upper();
304 content = contentType + "/" + contentAttribute; 293 content = contentType + "/" + contentAttribute;
305 } 294 }
306 if ((pos = bp.find("ENCODING",':', 0, TRUE)) != -1) { 295 if ((pos = bp.find("ENCODING",':', 0, TRUE)) != -1) {
307 pos++; 296 pos++;
308 encoding = bp.getString(&pos, 'z', TRUE); 297 encoding = bp.getString(&pos, 'z', TRUE);
309 } 298 }
310 299
311 if ( (pos = bp.find("FILENAME",'=', 0, TRUE)) != -1) { 300 if ( (pos = bp.find("FILENAME",'=', 0, TRUE)) != -1) {
312 pos++; 301 pos++;
313 fileName = bp.getString(&pos, 'z', TRUE); 302 fileName = bp.getString(&pos, 'z', TRUE);
314 fileName = fileName.right(fileName.length() - 1); 303 fileName = fileName.right(fileName.length() - 1);
315 fileName = fileName.left(fileName.length() - 1); 304 fileName = fileName.left(fileName.length() - 1);
316 } 305 }
317 306
318 } 307 }
319 pos = mimeBody.find(boundary, 0, FALSE); 308 pos = mimeBody.find(boundary, 0, FALSE);
320 if (pos == -1) //should not occur, malformed mail 309 if (pos == -1) //should not occur, malformed mail
321 pos = mimeBody.length(); 310 pos = mimeBody.length();
322 body = mimeBody.right(mimeBody.length() - pos); 311 body = mimeBody.right(mimeBody.length() - pos);
323 mimeBody = mimeBody.left(pos); 312 mimeBody = mimeBody.left(pos);
324 313
325 if (fileName != "") { //attatchments of some type, audio, image etc. 314 if (fileName != "") { //attatchments of some type, audio, image etc.
326 315
327 Enclosure e; 316 Enclosure e;
328 e.id = enclosureId; 317 e.id = enclosureId;
329 e.originalName = fileName; 318 e.originalName = fileName;
330 e.contentType = contentType; 319 e.contentType = contentType;
331 e.contentAttribute = contentAttribute; 320 e.contentAttribute = contentAttribute;
332 e.encoding = encoding; 321 e.encoding = encoding;
333 e.body = mimeBody; 322 e.body = mimeBody;
334 e.saved = FALSE; 323 e.saved = FALSE;
335 mail->addEnclosure(&e); 324 mail->addEnclosure(&e);
336 enclosureId++; 325 enclosureId++;
337 326
338 } else if (contentType == "TEXT") { 327 } else if (contentType == "TEXT") {
339 if (contentAttribute == "PLAIN") { 328 if (contentAttribute == "PLAIN") {
340 mail->body = mimeBody; 329 mail->body = mimeBody;
341 mail->bodyPlain = mimeBody; 330 mail->bodyPlain = mimeBody;
342 } 331 }
343 if (contentAttribute == "HTML") { 332 if (contentAttribute == "HTML") {
344 mail->body = mimeBody; 333 mail->body = mimeBody;
345 } 334 }
346 } 335 }
347 } 336 }
348 } else { 337 } else {
349 mail->bodyPlain = body; 338 mail->bodyPlain = body;
350 mail->body = body; 339 mail->body = body;
351 } 340 }
352 return TRUE; 341 return TRUE;
353} 342}
354 343
355bool EmailHandler::getEnclosure(Enclosure *ePtr) 344bool EmailHandler::getEnclosure(Enclosure *ePtr)
356{ 345{
357 QFile f(ePtr->path + ePtr->name); 346 QFile f(ePtr->path + ePtr->name);
358 char src[4]; 347 char src[4];
359 char *destPtr; 348 char *destPtr;
360 QByteArray buffer; 349 QByteArray buffer;
361 uint bufCount, pos, decodedCount, size, x; 350 uint bufCount, pos, decodedCount, size, x;
362 351
363 if (! f.open(IO_WriteOnly) ) { 352 if (! f.open(IO_WriteOnly) ) {
364 qWarning("could not save: " + ePtr->path + ePtr->name); 353 qWarning("could not save: " + ePtr->path + ePtr->name);
365 return FALSE; 354 return FALSE;
366 } 355 }
367 356
368 if (ePtr->encoding.upper() == "BASE64") { 357 if (ePtr->encoding.upper() == "BASE64") {
369 size = (ePtr->body.length() * 3 / 4); //approximate size (always above) 358 size = (ePtr->body.length() * 3 / 4); //approximate size (always above)
370 buffer.resize(size); 359 buffer.resize(size);
371 bufCount = 0; 360 bufCount = 0;
372 pos = 0; 361 pos = 0;
373 destPtr = buffer.data(); 362 destPtr = buffer.data();
374 363
375 while (pos < ePtr->body.length()) { 364 while (pos < ePtr->body.length()) {
376 decodedCount = 4; 365 decodedCount = 4;
377 x = 0; 366 x = 0;
378 while ( (x < 4) && (pos < ePtr->body.length()) ) { 367 while ( (x < 4) && (pos < ePtr->body.length()) ) {
379 src[x] = ePtr->body[pos].latin1(); 368 src[x] = ePtr->body[pos].latin1();
380 pos++; 369 pos++;
381 if (src[x] == '\r' || src[x] == '\n' || src[x] == ' ') 370 if (src[x] == '\r' || src[x] == '\n' || src[x] == ' ')
382 x--; 371 x--;
383 x++; 372 x++;
384 } 373 }
385 if (x > 1) { 374 if (x > 1) {
386 decodedCount = parse64base(src, destPtr); 375 decodedCount = parse64base(src, destPtr);
387 destPtr += decodedCount; 376 destPtr += decodedCount;
388 bufCount += decodedCount; 377 bufCount += decodedCount;
389 } 378 }
390 } 379 }
391 380
392 buffer.resize(bufCount); //set correct length of file 381 buffer.resize(bufCount); //set correct length of file
393 f.writeBlock(buffer); 382 f.writeBlock(buffer);
394 } else { 383 } else {
395 QTextStream t(&f); 384 QTextStream t(&f);
396 t << ePtr->body; 385 t << ePtr->body;
397 } 386 }
398 return TRUE; 387 return TRUE;
399} 388}
400 389
401int EmailHandler::parse64base(char *src, char *bufOut) { 390int EmailHandler::parse64base(char *src, char *bufOut) {
402 391
403 char c, z; 392 char c, z;
404 char li[4]; 393 char li[4];
405 int processed; 394 int processed;
406 395
407 //conversion table withouth table... 396 //conversion table withouth table...
408 for (int x = 0; x < 4; x++) { 397 for (int x = 0; x < 4; x++) {
409 c = src[x]; 398 c = src[x];
410 399
411 if ( (int) c >= 'A' && (int) c <= 'Z') 400 if ( (int) c >= 'A' && (int) c <= 'Z')
412 li[x] = (int) c - (int) 'A'; 401 li[x] = (int) c - (int) 'A';
413 if ( (int) c >= 'a' && (int) c <= 'z') 402 if ( (int) c >= 'a' && (int) c <= 'z')
414 li[x] = (int) c - (int) 'a' + 26; 403 li[x] = (int) c - (int) 'a' + 26;
415 if ( (int) c >= '0' && (int) c <= '9') 404 if ( (int) c >= '0' && (int) c <= '9')
416 li[x] = (int) c - (int) '0' + 52; 405 li[x] = (int) c - (int) '0' + 52;
417 if (c == '+') 406 if (c == '+')
418 li[x] = 62; 407 li[x] = 62;
419 if (c == '/') 408 if (c == '/')
420 li[x] = 63; 409 li[x] = 63;
421 } 410 }
422 411
423 processed = 1; 412 processed = 1;
424 bufOut[0] = (char) li[0] & (32+16+8+4+2+1); //mask out top 2 bits 413 bufOut[0] = (char) li[0] & (32+16+8+4+2+1); //mask out top 2 bits
425 bufOut[0] <<= 2; 414 bufOut[0] <<= 2;
426 z = li[1] >> 4; 415 z = li[1] >> 4;
427 bufOut[0] = bufOut[0] | z; //first byte retrived 416 bufOut[0] = bufOut[0] | z; //first byte retrived
428 417
429 if (src[2] != '=') { 418 if (src[2] != '=') {
430 bufOut[1] = (char) li[1] & (8+4+2+1); //mask out top 4 bits 419 bufOut[1] = (char) li[1] & (8+4+2+1); //mask out top 4 bits
431 bufOut[1] <<= 4; 420 bufOut[1] <<= 4;
432 z = li[2] >> 2; 421 z = li[2] >> 2;
433 bufOut[1] = bufOut[1] | z; //second byte retrived 422 bufOut[1] = bufOut[1] | z; //second byte retrived
434 processed++; 423 processed++;
435 424
436 if (src[3] != '=') { 425 if (src[3] != '=') {
437 bufOut[2] = (char) li[2] & (2+1); //mask out top 6 bits 426 bufOut[2] = (char) li[2] & (2+1); //mask out top 6 bits
438 bufOut[2] <<= 6; 427 bufOut[2] <<= 6;
439 z = li[3]; 428 z = li[3];
440 bufOut[2] = bufOut[2] | z; //third byte retrieved 429 bufOut[2] = bufOut[2] | z; //third byte retrieved
441 processed++; 430 processed++;
442 } 431 }
443 } 432 }
444 return processed; 433 return processed;
445} 434}
446 435
447int EmailHandler::encodeMime(Email *mail) 436int EmailHandler::encodeMime(Email *mail)
448{ 437{
449 438
450 QString fileName, fileType, contentType, newBody, boundary; 439 QString fileName, fileType, contentType, newBody, boundary;
451 Enclosure *ePtr; 440 Enclosure *ePtr;
452 441
453 QString userName = mailAccount.name; 442 QString userName = mailAccount.name;
454 if (userName.length()>0)//only embrace it if there is a user name 443 if (userName.length()>0)//only embrace it if there is a user name
455 userName += " <" + mailAccount.emailAddress + ">"; 444 userName += " <" + mailAccount.emailAddress + ">";
456 445
457 //add standard headers 446 //add standard headers
458 newBody = "From: " + userName + "\r\nTo: "; 447 newBody = "From: " + userName + "\r\nTo: ";
459 for (QStringList::Iterator it = mail->recipients.begin(); it != mail->recipients.end(); ++it ) { 448 for (QStringList::Iterator it = mail->recipients.begin(); it != mail->recipients.end(); ++it ) {
460 newBody += *it + " "; 449 newBody += *it + " ";
461 } 450 }
462 451
463 newBody += "\r\nCC: "; 452 newBody += "\r\nCC: ";
464 453
465 for (QStringList::Iterator it = mail->carbonCopies.begin(); it != mail->carbonCopies.end(); ++it ) { 454 for (QStringList::Iterator it = mail->carbonCopies.begin(); it != mail->carbonCopies.end(); ++it ) {
466 newBody += *it + " "; 455 newBody += *it + " ";
467 } 456 }
468 457
469 newBody += "\r\nSubject: " + mail->subject + "\r\n"; 458 newBody += "\r\nSubject: " + mail->subject + "\r\n";
470 459
471 if (mail->files.count() == 0) { //just a simple mail 460 if (mail->files.count() == 0) { //just a simple mail
472 newBody += "\r\n" + mail->body; 461 newBody += "\r\n" + mail->body;
473 mail->rawMail = newBody; 462 mail->rawMail = newBody;
474 return 0; 463 return 0;
475 } 464 }
476 465
477 //Build mime encoded mail 466 //Build mime encoded mail
478 boundary = "-----4345=next_bound=0495----"; 467 boundary = "-----4345=next_bound=0495----";
479 468
480 newBody += "Mime-Version: 1.0\r\n"; 469 newBody += "Mime-Version: 1.0\r\n";