Diffstat (limited to 'noncore/net/mail/libmailwrapper') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/net/mail/libmailwrapper/imapwrapper.cpp | 411 | ||||
-rw-r--r-- | noncore/net/mail/libmailwrapper/logindialog.cpp | 26 | ||||
-rw-r--r-- | noncore/net/mail/libmailwrapper/logindialog.h | 23 | ||||
-rw-r--r-- | noncore/net/mail/libmailwrapper/logindialogui.ui | 83 | ||||
-rw-r--r-- | noncore/net/mail/libmailwrapper/mailwrapper.cpp | 643 | ||||
-rw-r--r-- | noncore/net/mail/libmailwrapper/mailwrapper.h | 183 | ||||
-rw-r--r-- | noncore/net/mail/libmailwrapper/settings.cpp | 432 | ||||
-rw-r--r-- | noncore/net/mail/libmailwrapper/settings.h | 166 |
8 files changed, 1967 insertions, 0 deletions
diff --git a/noncore/net/mail/libmailwrapper/imapwrapper.cpp b/noncore/net/mail/libmailwrapper/imapwrapper.cpp new file mode 100644 index 0000000..16f4565 --- a/dev/null +++ b/noncore/net/mail/libmailwrapper/imapwrapper.cpp | |||
@@ -0,0 +1,411 @@ | |||
1 | |||
2 | #include <stdlib.h> | ||
3 | |||
4 | #include "mailwrapper.h" | ||
5 | |||
6 | |||
7 | IMAPwrapper::IMAPwrapper( IMAPaccount *a ) | ||
8 | { | ||
9 | account = a; | ||
10 | } | ||
11 | |||
12 | void imap_progress( size_t current, size_t maximum ) | ||
13 | { | ||
14 | qDebug( "IMAP: %i of %i", current, maximum ); | ||
15 | } | ||
16 | |||
17 | void IMAPwrapper::listMessages(const QString&mailbox,Maillist&target ) | ||
18 | { | ||
19 | const char *server, *user, *pass, *mb; | ||
20 | uint16_t port; | ||
21 | int err = MAILIMAP_NO_ERROR; | ||
22 | clist *result; | ||
23 | clistcell *current; | ||
24 | mailimap_fetch_att *fetchAtt,*fetchAttFlags; | ||
25 | mailimap_fetch_type *fetchType; | ||
26 | mailimap_set *set; | ||
27 | |||
28 | mb = mailbox.latin1(); | ||
29 | server = account->getServer().latin1(); | ||
30 | port = account->getPort().toUInt(); | ||
31 | user = account->getUser().latin1(); | ||
32 | pass = account->getPassword().latin1(); | ||
33 | |||
34 | mailimap *imap = mailimap_new( 20, &imap_progress ); | ||
35 | if ( imap == NULL ) { | ||
36 | qDebug("IMAP Memory error"); | ||
37 | return; | ||
38 | } | ||
39 | |||
40 | /* connect */ | ||
41 | err = mailimap_socket_connect( imap, (char*)server, port ); | ||
42 | if ( err != MAILIMAP_NO_ERROR && | ||
43 | err != MAILIMAP_NO_ERROR_AUTHENTICATED && | ||
44 | err != MAILIMAP_NO_ERROR_NON_AUTHENTICATED ) { | ||
45 | qDebug("error connecting server: %s",imap->response); | ||
46 | mailimap_free( imap ); | ||
47 | return; | ||
48 | } | ||
49 | |||
50 | /* login */ | ||
51 | err = mailimap_login_simple( imap, (char*)user, (char*)pass ); | ||
52 | if ( err != MAILIMAP_NO_ERROR ) { | ||
53 | qDebug("error logging in imap: %s",imap->response); | ||
54 | err = mailimap_close( imap ); | ||
55 | mailimap_free( imap ); | ||
56 | return; | ||
57 | } | ||
58 | |||
59 | /* select mailbox READONLY for operations */ | ||
60 | err = mailimap_examine( imap, (char*)mb); | ||
61 | if ( err != MAILIMAP_NO_ERROR ) { | ||
62 | qDebug("error selecting mailbox: %s",imap->response); | ||
63 | err = mailimap_logout( imap ); | ||
64 | err = mailimap_close( imap ); | ||
65 | mailimap_free( imap ); | ||
66 | return; | ||
67 | } | ||
68 | |||
69 | int last = imap->selection_info->exists; | ||
70 | if (last == 0) { | ||
71 | qDebug("mailbox has no mails"); | ||
72 | err = mailimap_logout( imap ); | ||
73 | err = mailimap_close( imap ); | ||
74 | mailimap_free( imap ); | ||
75 | return; | ||
76 | } | ||
77 | |||
78 | |||
79 | result = clist_new(); | ||
80 | /* the range has to start at 1!!! not with 0!!!! */ | ||
81 | set = mailimap_set_new_interval( 1, last ); | ||
82 | fetchAtt = mailimap_fetch_att_new_envelope(); | ||
83 | fetchAttFlags = mailimap_fetch_att_new_flags(); | ||
84 | |||
85 | //fetchType = mailimap_fetch_type_new_fetch_att(fetchAtt); | ||
86 | fetchType = mailimap_fetch_type_new_fetch_att_list_empty(); | ||
87 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,fetchAtt); | ||
88 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,fetchAttFlags); | ||
89 | |||
90 | err = mailimap_fetch( imap, set, fetchType, &result ); | ||
91 | mailimap_set_free( set ); | ||
92 | mailimap_fetch_type_free( fetchType ); | ||
93 | |||
94 | QString date,subject,from; | ||
95 | |||
96 | if ( err == MAILIMAP_NO_ERROR ) { | ||
97 | current = clist_begin(result); | ||
98 | mailimap_msg_att * msg_att; | ||
99 | int i = 0; | ||
100 | while ( current != 0 ) { | ||
101 | ++i; | ||
102 | msg_att = (mailimap_msg_att*)current->data; | ||
103 | RecMail*m = parse_list_result(msg_att); | ||
104 | if (m) { | ||
105 | m->setNumber(i); | ||
106 | target.append(m); | ||
107 | } | ||
108 | current = current->next; | ||
109 | } | ||
110 | } else { | ||
111 | qDebug("Error fetching headers: %s",imap->response); | ||
112 | } | ||
113 | |||
114 | err = mailimap_logout( imap ); | ||
115 | err = mailimap_close( imap ); | ||
116 | clist_free(result); | ||
117 | mailimap_free( imap ); | ||
118 | } | ||
119 | |||
120 | QList<IMAPFolder>* IMAPwrapper::listFolders() | ||
121 | { | ||
122 | const char *server, *user, *pass, *path, *mask; | ||
123 | uint16_t port; | ||
124 | int err = MAILIMAP_NO_ERROR; | ||
125 | clist *result; | ||
126 | clistcell *current; | ||
127 | |||
128 | QList<IMAPFolder> * folders = new QList<IMAPFolder>(); | ||
129 | folders->setAutoDelete( true ); | ||
130 | |||
131 | server = account->getServer().latin1(); | ||
132 | port = account->getPort().toUInt(); | ||
133 | user = account->getUser().latin1(); | ||
134 | pass = account->getPassword().latin1(); | ||
135 | path = account->getPrefix().latin1(); | ||
136 | |||
137 | mailimap *imap = mailimap_new( 20, &imap_progress ); | ||
138 | if ( imap == NULL ) { | ||
139 | qDebug("error mailimap_new"); | ||
140 | return folders; | ||
141 | } | ||
142 | |||
143 | err = mailimap_socket_connect( imap, (char*)server, port ); | ||
144 | if ( err != MAILIMAP_NO_ERROR && | ||
145 | err != MAILIMAP_NO_ERROR_AUTHENTICATED && | ||
146 | err != MAILIMAP_NO_ERROR_NON_AUTHENTICATED ) { | ||
147 | mailimap_free(imap); | ||
148 | qDebug("error imap_socket_connect: %s",imap->response); | ||
149 | return folders; | ||
150 | } | ||
151 | |||
152 | err = mailimap_login_simple( imap, (char*)user, (char*)pass ); | ||
153 | if ( err != MAILIMAP_NO_ERROR ) { | ||
154 | mailimap_free(imap); | ||
155 | qDebug("error logging in: %s",imap->response); | ||
156 | return folders; | ||
157 | } | ||
158 | /* | ||
159 | * First we have to check for INBOX 'cause it sometimes it's not inside the path. | ||
160 | * We must not forget to filter them out in next loop! | ||
161 | * it seems like ugly code. and yes - it is ugly code. but the best way. | ||
162 | */ | ||
163 | QString temp; | ||
164 | mask = "INBOX" ; | ||
165 | result = clist_new(); | ||
166 | mailimap_mailbox_list *list; | ||
167 | err = mailimap_list( imap, (char*)"", (char*)mask, &result ); | ||
168 | if ( err == MAILIMAP_NO_ERROR ) { | ||
169 | current = result->first; | ||
170 | for ( int i = result->count; i > 0; i-- ) { | ||
171 | list = (mailimap_mailbox_list *) current->data; | ||
172 | // it is better use the deep copy mechanism of qt itself | ||
173 | // instead of using strdup! | ||
174 | temp = list->mb; | ||
175 | folders->append( new IMAPFolder(temp)); | ||
176 | current = current->next; | ||
177 | } | ||
178 | } else { | ||
179 | qDebug("error fetching folders: %s",imap->response); | ||
180 | } | ||
181 | mailimap_list_result_free( result ); | ||
182 | |||
183 | /* | ||
184 | * second stage - get the other then inbox folders | ||
185 | */ | ||
186 | mask = "*" ; | ||
187 | result = clist_new(); | ||
188 | err = mailimap_list( imap, (char*)path, (char*)mask, &result ); | ||
189 | if ( err == MAILIMAP_NO_ERROR ) { | ||
190 | current = result->first; | ||
191 | for ( int i = result->count; i > 0; i-- ) { | ||
192 | list = (mailimap_mailbox_list *) current->data; | ||
193 | // it is better use the deep copy mechanism of qt itself | ||
194 | // instead of using strdup! | ||
195 | temp = list->mb; | ||
196 | current = current->next; | ||
197 | if (temp.lower()=="inbox") | ||
198 | continue; | ||
199 | folders->append(new IMAPFolder(temp)); | ||
200 | |||
201 | } | ||
202 | } else { | ||
203 | qDebug("error fetching folders"); | ||
204 | } | ||
205 | mailimap_list_result_free( result ); | ||
206 | err = mailimap_logout( imap ); | ||
207 | err = mailimap_close( imap ); | ||
208 | mailimap_free( imap ); | ||
209 | return folders; | ||
210 | } | ||
211 | |||
212 | RecMail*IMAPwrapper::parse_list_result(mailimap_msg_att* m_att) | ||
213 | { | ||
214 | RecMail * m = 0; | ||
215 | mailimap_msg_att_item *item=0; | ||
216 | bool named_from = false; | ||
217 | QString from,date,subject; | ||
218 | date = from = subject = ""; | ||
219 | clistcell *current,*c,*cf, *current_from = NULL; | ||
220 | mailimap_address * current_address = NULL; | ||
221 | mailimap_msg_att_dynamic*flist; | ||
222 | mailimap_flag_fetch*cflag; | ||
223 | QBitArray mFlags(7); | ||
224 | |||
225 | if (!m_att) { | ||
226 | return m; | ||
227 | } | ||
228 | |||
229 | #if 0 | ||
230 | MAILIMAP_FLAG_KEYWORD, /* keyword flag */ | ||
231 | MAILIMAP_FLAG_EXTENSION, /* \extension flag */ | ||
232 | #endif | ||
233 | c = clist_begin(m_att->list); | ||
234 | while ( c ) { | ||
235 | current = c; | ||
236 | c = c->next; | ||
237 | item = (mailimap_msg_att_item*)current->data; | ||
238 | if (item->type!=MAILIMAP_MSG_ATT_ITEM_STATIC) { | ||
239 | flist = (mailimap_msg_att_dynamic*)item->msg_att_dyn; | ||
240 | if (!flist->list) { | ||
241 | continue; | ||
242 | } | ||
243 | cf = flist->list->first; | ||
244 | while (cf) { | ||
245 | cflag = (mailimap_flag_fetch*)cf->data; | ||
246 | if (cflag->type==MAILIMAP_FLAG_FETCH_OTHER && cflag->flag!=0) { | ||
247 | switch (cflag->flag->type) { | ||
248 | case MAILIMAP_FLAG_ANSWERED: /* \Answered flag */ | ||
249 | mFlags.setBit(FLAG_ANSWERED); | ||
250 | break; | ||
251 | case MAILIMAP_FLAG_FLAGGED: /* \Flagged flag */ | ||
252 | mFlags.setBit(FLAG_FLAGGED); | ||
253 | break; | ||
254 | case MAILIMAP_FLAG_DELETED: /* \Deleted flag */ | ||
255 | mFlags.setBit(FLAG_DELETED); | ||
256 | break; | ||
257 | case MAILIMAP_FLAG_SEEN: /* \Seen flag */ | ||
258 | mFlags.setBit(FLAG_SEEN); | ||
259 | break; | ||
260 | case MAILIMAP_FLAG_DRAFT: /* \Draft flag */ | ||
261 | mFlags.setBit(FLAG_DRAFT); | ||
262 | break; | ||
263 | case MAILIMAP_FLAG_KEYWORD: /* keyword flag */ | ||
264 | break; | ||
265 | case MAILIMAP_FLAG_EXTENSION: /* \extension flag */ | ||
266 | break; | ||
267 | default: | ||
268 | break; | ||
269 | } | ||
270 | } else if (cflag->type==MAILIMAP_FLAG_FETCH_RECENT) { | ||
271 | mFlags.setBit(FLAG_RECENT); | ||
272 | } | ||
273 | |||
274 | cf = cf->next; | ||
275 | } | ||
276 | continue; | ||
277 | } | ||
278 | if ( item->msg_att_static->type == MAILIMAP_MSG_ATT_RFC822_HEADER ) { | ||
279 | qDebug( "header: \n%s", item->msg_att_static->rfc822_header ); | ||
280 | } else if (item->msg_att_static->type==MAILIMAP_MSG_ATT_ENVELOPE) { | ||
281 | mailimap_envelope * head = item->msg_att_static->env; | ||
282 | date = head->date; | ||
283 | subject = head->subject; | ||
284 | if (head->from!=NULL) | ||
285 | current_from = head->from->list->first; | ||
286 | while (current_from != NULL) { | ||
287 | from = ""; | ||
288 | named_from = false; | ||
289 | current_address=(mailimap_address*)current_from->data; | ||
290 | current_from = current_from->next; | ||
291 | if (current_address->personal_name){ | ||
292 | from+=QString(current_address->personal_name); | ||
293 | from+=" "; | ||
294 | named_from = true; | ||
295 | } | ||
296 | if (named_from && (current_address->mailbox_name || current_address->host_name)) { | ||
297 | from+="<"; | ||
298 | } | ||
299 | if (current_address->mailbox_name) { | ||
300 | from+=QString(current_address->mailbox_name); | ||
301 | from+="@"; | ||
302 | } | ||
303 | if (current_address->host_name) { | ||
304 | from+=QString(current_address->host_name); | ||
305 | } | ||
306 | if (named_from && (current_address->mailbox_name || current_address->host_name)) { | ||
307 | from+=">"; | ||
308 | } | ||
309 | } | ||
310 | qDebug("header: \nFrom: %s\nSubject: %s\nDate: %s", | ||
311 | from.latin1(), | ||
312 | subject.latin1(),date.latin1()); | ||
313 | m = new RecMail(); | ||
314 | m->setSubject(subject); | ||
315 | m->setFrom(from); | ||
316 | m->setDate(date); | ||
317 | } else { | ||
318 | qDebug("Another type"); | ||
319 | } | ||
320 | } | ||
321 | /* msg is already deleted */ | ||
322 | if (mFlags.testBit(FLAG_DELETED) && m) { | ||
323 | delete m; | ||
324 | m = 0; | ||
325 | } | ||
326 | if (m) { | ||
327 | m->setFlags(mFlags); | ||
328 | } | ||
329 | return m; | ||
330 | } | ||
331 | |||
332 | QString IMAPwrapper::fetchBody(const QString & mailbox,const RecMail&mail) | ||
333 | { | ||
334 | QString body = ""; | ||
335 | const char *server, *user, *pass, *mb; | ||
336 | uint16_t port; | ||
337 | int err = MAILIMAP_NO_ERROR; | ||
338 | clist *result; | ||
339 | clistcell *current; | ||
340 | mailimap_fetch_att *fetchAtt; | ||
341 | mailimap_fetch_type *fetchType; | ||
342 | mailimap_set *set; | ||
343 | |||
344 | mb = mailbox.latin1(); | ||
345 | server = account->getServer().latin1(); | ||
346 | port = account->getPort().toUInt(); | ||
347 | user = account->getUser().latin1(); | ||
348 | pass = account->getPassword().latin1(); | ||
349 | |||
350 | mailimap *imap = mailimap_new( 20, &imap_progress ); | ||
351 | if ( imap == NULL ) { | ||
352 | qDebug("IMAP Memory error"); | ||
353 | return body; | ||
354 | } | ||
355 | |||
356 | /* connect */ | ||
357 | err = mailimap_socket_connect( imap, (char*)server, port ); | ||
358 | if ( err != MAILIMAP_NO_ERROR && | ||
359 | err != MAILIMAP_NO_ERROR_AUTHENTICATED && | ||
360 | err != MAILIMAP_NO_ERROR_NON_AUTHENTICATED ) { | ||
361 | qDebug("error connecting server: %s",imap->response); | ||
362 | mailimap_free( imap ); | ||
363 | return body; | ||
364 | } | ||
365 | |||
366 | /* login */ | ||
367 | err = mailimap_login_simple( imap, (char*)user, (char*)pass ); | ||
368 | if ( err != MAILIMAP_NO_ERROR ) { | ||
369 | qDebug("error logging in imap: %s",imap->response); | ||
370 | err = mailimap_close( imap ); | ||
371 | mailimap_free( imap ); | ||
372 | return body; | ||
373 | } | ||
374 | |||
375 | /* select mailbox READONLY for operations */ | ||
376 | err = mailimap_examine( imap, (char*)mb); | ||
377 | if ( err != MAILIMAP_NO_ERROR ) { | ||
378 | qDebug("error selecting mailbox: %s",imap->response); | ||
379 | err = mailimap_logout( imap ); | ||
380 | err = mailimap_close( imap ); | ||
381 | mailimap_free( imap ); | ||
382 | return body; | ||
383 | } | ||
384 | result = clist_new(); | ||
385 | /* the range has to start at 1!!! not with 0!!!! */ | ||
386 | set = mailimap_set_new_interval( mail.getNumber(),mail.getNumber() ); | ||
387 | fetchAtt = mailimap_fetch_att_new_rfc822_text(); | ||
388 | fetchType = mailimap_fetch_type_new_fetch_att(fetchAtt); | ||
389 | err = mailimap_fetch( imap, set, fetchType, &result ); | ||
390 | mailimap_set_free( set ); | ||
391 | mailimap_fetch_type_free( fetchType ); | ||
392 | |||
393 | if (err == MAILIMAP_NO_ERROR && (current=clist_begin(result)) ) { | ||
394 | mailimap_msg_att * msg_att; | ||
395 | msg_att = (mailimap_msg_att*)current->data; | ||
396 | mailimap_msg_att_item*item = (mailimap_msg_att_item*)msg_att->list->first->data; | ||
397 | |||
398 | if (item->msg_att_static && item->msg_att_static->rfc822_text) { | ||
399 | body = item->msg_att_static->rfc822_text; | ||
400 | } | ||
401 | } else { | ||
402 | qDebug("error fetching text: %s",imap->response); | ||
403 | } | ||
404 | |||
405 | err = mailimap_logout( imap ); | ||
406 | err = mailimap_close( imap ); | ||
407 | mailimap_free( imap ); | ||
408 | clist_free(result); | ||
409 | |||
410 | return body; | ||
411 | } | ||
diff --git a/noncore/net/mail/libmailwrapper/logindialog.cpp b/noncore/net/mail/libmailwrapper/logindialog.cpp new file mode 100644 index 0000000..d2c6a07 --- a/dev/null +++ b/noncore/net/mail/libmailwrapper/logindialog.cpp | |||
@@ -0,0 +1,26 @@ | |||
1 | #include <qlineedit.h> | ||
2 | |||
3 | #include "logindialog.h" | ||
4 | |||
5 | LoginDialog::LoginDialog( QString user, QString pass, QWidget *parent, const char *name, bool modal, WFlags flags ) | ||
6 | : LoginDialogUI( parent, name, modal, flags ) | ||
7 | { | ||
8 | userLine->setText( user ); | ||
9 | passLine->setText( pass ); | ||
10 | _user = user; | ||
11 | _pass = pass; | ||
12 | |||
13 | if ( user.isEmpty() ) { | ||
14 | userLine->setFocus(); | ||
15 | } else { | ||
16 | passLine->setFocus(); | ||
17 | } | ||
18 | } | ||
19 | |||
20 | void LoginDialog::accept() | ||
21 | { | ||
22 | _user.replace( 0, _user.length(), userLine->text() ); | ||
23 | _pass.replace( 0, _pass.length(), passLine->text() ); | ||
24 | |||
25 | QDialog::accept(); | ||
26 | } | ||
diff --git a/noncore/net/mail/libmailwrapper/logindialog.h b/noncore/net/mail/libmailwrapper/logindialog.h new file mode 100644 index 0000000..7a0d1b0 --- a/dev/null +++ b/noncore/net/mail/libmailwrapper/logindialog.h | |||
@@ -0,0 +1,23 @@ | |||
1 | #ifndef LOGINDIALOG_H | ||
2 | #define LOGINDIALOG_H | ||
3 | |||
4 | #include "logindialogui.h" | ||
5 | |||
6 | class LoginDialog : public LoginDialogUI | ||
7 | { | ||
8 | Q_OBJECT | ||
9 | |||
10 | public: | ||
11 | LoginDialog( QString user, QString pass, QWidget *parent = 0, const char *name = 0, bool modal = false, WFlags flags = 0 ); | ||
12 | QString getUser() { return _user; } | ||
13 | QString getPassword() { return _pass; } | ||
14 | |||
15 | protected slots: | ||
16 | void accept(); | ||
17 | |||
18 | private: | ||
19 | QString _user, _pass; | ||
20 | |||
21 | }; | ||
22 | |||
23 | #endif | ||
diff --git a/noncore/net/mail/libmailwrapper/logindialogui.ui b/noncore/net/mail/libmailwrapper/logindialogui.ui new file mode 100644 index 0000000..565f777 --- a/dev/null +++ b/noncore/net/mail/libmailwrapper/logindialogui.ui | |||
@@ -0,0 +1,83 @@ | |||
1 | <!DOCTYPE UI><UI> | ||
2 | <class>LoginDialogUI</class> | ||
3 | <widget> | ||
4 | <class>QDialog</class> | ||
5 | <property stdset="1"> | ||
6 | <name>name</name> | ||
7 | <cstring>LoginDialogUI</cstring> | ||
8 | </property> | ||
9 | <property stdset="1"> | ||
10 | <name>geometry</name> | ||
11 | <rect> | ||
12 | <x>0</x> | ||
13 | <y>0</y> | ||
14 | <width>196</width> | ||
15 | <height>110</height> | ||
16 | </rect> | ||
17 | </property> | ||
18 | <property stdset="1"> | ||
19 | <name>caption</name> | ||
20 | <string>Login</string> | ||
21 | </property> | ||
22 | <property> | ||
23 | <name>layoutMargin</name> | ||
24 | </property> | ||
25 | <property> | ||
26 | <name>layoutSpacing</name> | ||
27 | </property> | ||
28 | <grid> | ||
29 | <property stdset="1"> | ||
30 | <name>margin</name> | ||
31 | <number>4</number> | ||
32 | </property> | ||
33 | <property stdset="1"> | ||
34 | <name>spacing</name> | ||
35 | <number>3</number> | ||
36 | </property> | ||
37 | <widget row="0" column="0" > | ||
38 | <class>QLabel</class> | ||
39 | <property stdset="1"> | ||
40 | <name>name</name> | ||
41 | <cstring>userLabel</cstring> | ||
42 | </property> | ||
43 | <property stdset="1"> | ||
44 | <name>text</name> | ||
45 | <string>User</string> | ||
46 | </property> | ||
47 | </widget> | ||
48 | <widget row="1" column="0" > | ||
49 | <class>QLineEdit</class> | ||
50 | <property stdset="1"> | ||
51 | <name>name</name> | ||
52 | <cstring>userLine</cstring> | ||
53 | </property> | ||
54 | </widget> | ||
55 | <widget row="3" column="0" > | ||
56 | <class>QLineEdit</class> | ||
57 | <property stdset="1"> | ||
58 | <name>name</name> | ||
59 | <cstring>passLine</cstring> | ||
60 | </property> | ||
61 | <property stdset="1"> | ||
62 | <name>echoMode</name> | ||
63 | <enum>Password</enum> | ||
64 | </property> | ||
65 | </widget> | ||
66 | <widget row="2" column="0" > | ||
67 | <class>QLabel</class> | ||
68 | <property stdset="1"> | ||
69 | <name>name</name> | ||
70 | <cstring>passLabel</cstring> | ||
71 | </property> | ||
72 | <property stdset="1"> | ||
73 | <name>text</name> | ||
74 | <string>Password</string> | ||
75 | </property> | ||
76 | </widget> | ||
77 | </grid> | ||
78 | </widget> | ||
79 | <tabstops> | ||
80 | <tabstop>userLine</tabstop> | ||
81 | <tabstop>passLine</tabstop> | ||
82 | </tabstops> | ||
83 | </UI> | ||
diff --git a/noncore/net/mail/libmailwrapper/mailwrapper.cpp b/noncore/net/mail/libmailwrapper/mailwrapper.cpp new file mode 100644 index 0000000..17bed65 --- a/dev/null +++ b/noncore/net/mail/libmailwrapper/mailwrapper.cpp | |||
@@ -0,0 +1,643 @@ | |||
1 | #include <qfileinfo.h> | ||
2 | #include <stdlib.h> | ||
3 | #include <sys/stat.h> | ||
4 | #include <sys/types.h> | ||
5 | #include <unistd.h> | ||
6 | #include <fcntl.h> | ||
7 | #include <string.h> | ||
8 | #include <qdir.h> | ||
9 | #include <qtextstream.h> | ||
10 | |||
11 | #include "mailwrapper.h" | ||
12 | #include "logindialog.h" | ||
13 | #include "mail.h" | ||
14 | #include "defines.h" | ||
15 | |||
16 | Attachment::Attachment( DocLnk lnk ) | ||
17 | { | ||
18 | doc = lnk; | ||
19 | size = QFileInfo( doc.file() ).size(); | ||
20 | } | ||
21 | |||
22 | Folder::Folder(const QString&tmp_name ) | ||
23 | { | ||
24 | name = tmp_name; | ||
25 | nameDisplay = name; | ||
26 | |||
27 | for ( int pos = nameDisplay.find( '&' ); pos != -1; | ||
28 | pos = nameDisplay.find( '&' ) ) { | ||
29 | int end = nameDisplay.find( '-' ); | ||
30 | if ( end == -1 || end <= pos ) break; | ||
31 | QString str64 = nameDisplay.mid( pos + 1, end - pos - 1 ); | ||
32 | // TODO: do real base64 decoding here ! | ||
33 | if ( str64.compare( "APw" ) == 0 ) { | ||
34 | nameDisplay = nameDisplay.replace( pos, end - pos + 1, "ue" ); | ||
35 | } else if ( str64.compare( "APY" ) == 0 ) { | ||
36 | nameDisplay = nameDisplay.replace( pos, end - pos + 1, "oe" ); | ||
37 | } | ||
38 | } | ||
39 | |||
40 | qDebug( "folder " + name + " - displayed as " + nameDisplay ); | ||
41 | } | ||
42 | |||
43 | MailWrapper::MailWrapper( Settings *s ) | ||
44 | : QObject() | ||
45 | { | ||
46 | settings = s; | ||
47 | } | ||
48 | |||
49 | QString MailWrapper::mailsmtpError( int errnum ) | ||
50 | { | ||
51 | switch ( errnum ) { | ||
52 | case MAILSMTP_NO_ERROR: | ||
53 | return tr( "No error" ); | ||
54 | case MAILSMTP_ERROR_UNEXPECTED_CODE: | ||
55 | return tr( "Unexpected error code" ); | ||
56 | case MAILSMTP_ERROR_SERVICE_NOT_AVAILABLE: | ||
57 | return tr( "Service not available" ); | ||
58 | case MAILSMTP_ERROR_STREAM: | ||
59 | return tr( "Stream error" ); | ||
60 | case MAILSMTP_ERROR_HOSTNAME: | ||
61 | return tr( "gethostname() failed" ); | ||
62 | case MAILSMTP_ERROR_NOT_IMPLEMENTED: | ||
63 | return tr( "Not implemented" ); | ||
64 | case MAILSMTP_ERROR_ACTION_NOT_TAKEN: | ||
65 | return tr( "Error, action not taken" ); | ||
66 | case MAILSMTP_ERROR_EXCEED_STORAGE_ALLOCATION: | ||
67 | return tr( "Data exceeds storage allocation" ); | ||
68 | case MAILSMTP_ERROR_IN_PROCESSING: | ||
69 | return tr( "Error in processing" ); | ||
70 | // case MAILSMTP_ERROR_INSUFFISANT_SYSTEM_STORAGE: | ||
71 | // return tr( "Insufficient system storage" ); | ||
72 | case MAILSMTP_ERROR_MAILBOX_UNAVAILABLE: | ||
73 | return tr( "Mailbox unavailable" ); | ||
74 | case MAILSMTP_ERROR_MAILBOX_NAME_NOT_ALLOWED: | ||
75 | return tr( "Mailbox name not allowed" ); | ||
76 | case MAILSMTP_ERROR_BAD_SEQUENCE_OF_COMMAND: | ||
77 | return tr( "Bad command sequence" ); | ||
78 | case MAILSMTP_ERROR_USER_NOT_LOCAL: | ||
79 | return tr( "User not local" ); | ||
80 | case MAILSMTP_ERROR_TRANSACTION_FAILED: | ||
81 | return tr( "Transaction failed" ); | ||
82 | case MAILSMTP_ERROR_MEMORY: | ||
83 | return tr( "Memory error" ); | ||
84 | case MAILSMTP_ERROR_CONNECTION_REFUSED: | ||
85 | return tr( "Connection refused" ); | ||
86 | default: | ||
87 | return tr( "Unknown error code" ); | ||
88 | } | ||
89 | } | ||
90 | |||
91 | mailimf_mailbox *MailWrapper::newMailbox(const QString&name, const QString&mail ) | ||
92 | { | ||
93 | return mailimf_mailbox_new( strdup( name.latin1() ), | ||
94 | strdup( mail.latin1() ) ); | ||
95 | } | ||
96 | |||
97 | mailimf_address_list *MailWrapper::parseAddresses(const QString&addr ) | ||
98 | { | ||
99 | mailimf_address_list *addresses; | ||
100 | |||
101 | if ( addr.isEmpty() ) return NULL; | ||
102 | |||
103 | addresses = mailimf_address_list_new_empty(); | ||
104 | |||
105 | QStringList list = QStringList::split( ',', addr ); | ||
106 | QStringList::Iterator it; | ||
107 | for ( it = list.begin(); it != list.end(); it++ ) { | ||
108 | char *str = strdup( (*it).latin1() ); | ||
109 | int err = mailimf_address_list_add_parse( addresses, str ); | ||
110 | if ( err != MAILIMF_NO_ERROR ) { | ||
111 | qDebug( "Error parsing" ); | ||
112 | qDebug( *it ); | ||
113 | free( str ); | ||
114 | } else { | ||
115 | qDebug( "Parse success! :)" ); | ||
116 | } | ||
117 | } | ||
118 | |||
119 | return addresses; | ||
120 | } | ||
121 | |||
122 | mailimf_fields *MailWrapper::createImfFields( Mail *mail ) | ||
123 | { | ||
124 | mailimf_fields *fields; | ||
125 | mailimf_field *xmailer; | ||
126 | mailimf_mailbox *sender, *fromBox; | ||
127 | mailimf_mailbox_list *from; | ||
128 | mailimf_address_list *to, *cc, *bcc, *reply; | ||
129 | char *subject = strdup( mail->getSubject().latin1() ); | ||
130 | int err; | ||
131 | |||
132 | sender = newMailbox( mail->getName(), mail->getMail() ); | ||
133 | if ( sender == NULL ) goto err_free; | ||
134 | |||
135 | fromBox = newMailbox( mail->getName(), mail->getMail() ); | ||
136 | if ( fromBox == NULL ) goto err_free_sender; | ||
137 | |||
138 | from = mailimf_mailbox_list_new_empty(); | ||
139 | if ( from == NULL ) goto err_free_fromBox; | ||
140 | |||
141 | err = mailimf_mailbox_list_add( from, fromBox ); | ||
142 | if ( err != MAILIMF_NO_ERROR ) goto err_free_from; | ||
143 | |||
144 | to = parseAddresses( mail->getTo() ); | ||
145 | if ( to == NULL ) goto err_free_from; | ||
146 | |||
147 | cc = parseAddresses( mail->getCC() ); | ||
148 | bcc = parseAddresses( mail->getBCC() ); | ||
149 | reply = parseAddresses( mail->getReply() ); | ||
150 | |||
151 | fields = mailimf_fields_new_with_data( from, sender, reply, to, cc, bcc, | ||
152 | NULL, NULL, subject ); | ||
153 | if ( fields == NULL ) goto err_free_reply; | ||
154 | |||
155 | xmailer = mailimf_field_new_custom( strdup( "User-Agent" ), | ||
156 | strdup( USER_AGENT ) ); | ||
157 | if ( xmailer == NULL ) goto err_free_fields; | ||
158 | |||
159 | err = mailimf_fields_add( fields, xmailer ); | ||
160 | if ( err != MAILIMF_NO_ERROR ) goto err_free_xmailer; | ||
161 | |||
162 | return fields; // Success :) | ||
163 | |||
164 | err_free_xmailer: | ||
165 | mailimf_field_free( xmailer ); | ||
166 | err_free_fields: | ||
167 | mailimf_fields_free( fields ); | ||
168 | err_free_reply: | ||
169 | mailimf_address_list_free( reply ); | ||
170 | mailimf_address_list_free( bcc ); | ||
171 | mailimf_address_list_free( cc ); | ||
172 | mailimf_address_list_free( to ); | ||
173 | err_free_from: | ||
174 | mailimf_mailbox_list_free( from ); | ||
175 | err_free_fromBox: | ||
176 | mailimf_mailbox_free( fromBox ); | ||
177 | err_free_sender: | ||
178 | mailimf_mailbox_free( sender ); | ||
179 | err_free: | ||
180 | free( subject ); | ||
181 | qDebug( "createImfFields - error" ); | ||
182 | |||
183 | return NULL; // Error :( | ||
184 | } | ||
185 | |||
186 | mailmime *MailWrapper::buildTxtPart( QString str ) | ||
187 | { | ||
188 | mailmime *txtPart; | ||
189 | mailmime_fields *fields; | ||
190 | mailmime_content *content; | ||
191 | mailmime_parameter *param; | ||
192 | char *txt = strdup( str.latin1() ); | ||
193 | int err; | ||
194 | |||
195 | param = mailmime_parameter_new( strdup( "charset" ), | ||
196 | strdup( "iso-8859-1" ) ); | ||
197 | if ( param == NULL ) goto err_free; | ||
198 | |||
199 | content = mailmime_content_new_with_str( "text/plain" ); | ||
200 | if ( content == NULL ) goto err_free_param; | ||
201 | |||
202 | err = clist_append( content->parameters, param ); | ||
203 | if ( err != MAILIMF_NO_ERROR ) goto err_free_content; | ||
204 | |||
205 | fields = mailmime_fields_new_encoding( MAILMIME_MECHANISM_8BIT ); | ||
206 | if ( fields == NULL ) goto err_free_content; | ||
207 | |||
208 | txtPart = mailmime_new_empty( content, fields ); | ||
209 | if ( txtPart == NULL ) goto err_free_fields; | ||
210 | |||
211 | err = mailmime_set_body_text( txtPart, txt, strlen( txt ) ); | ||
212 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; | ||
213 | |||
214 | return txtPart; // Success :) | ||
215 | |||
216 | err_free_txtPart: | ||
217 | mailmime_free( txtPart ); | ||
218 | err_free_fields: | ||
219 | mailmime_fields_free( fields ); | ||
220 | err_free_content: | ||
221 | mailmime_content_free( content ); | ||
222 | err_free_param: | ||
223 | mailmime_parameter_free( param ); | ||
224 | err_free: | ||
225 | free( txt ); | ||
226 | qDebug( "buildTxtPart - error" ); | ||
227 | |||
228 | return NULL; // Error :( | ||
229 | } | ||
230 | |||
231 | mailmime *MailWrapper::buildFilePart( QString filename, QString mimetype ) | ||
232 | { | ||
233 | mailmime * filePart; | ||
234 | mailmime_fields * fields; | ||
235 | mailmime_content * content; | ||
236 | mailmime_parameter * param = NULL; | ||
237 | int err; | ||
238 | |||
239 | int pos = filename.findRev( '/' ); | ||
240 | QString tmp = filename.right( filename.length() - ( pos + 1 ) ); | ||
241 | char *name = strdup( tmp.latin1() ); // just filename | ||
242 | char *file = strdup( filename.latin1() ); // full name with path | ||
243 | char *mime = strdup( mimetype.latin1() ); // mimetype -e.g. text/plain | ||
244 | |||
245 | fields = mailmime_fields_new_filename( | ||
246 | MAILMIME_DISPOSITION_TYPE_ATTACHMENT, name, | ||
247 | MAILMIME_MECHANISM_BASE64 ); | ||
248 | if ( fields == NULL ) goto err_free; | ||
249 | |||
250 | content = mailmime_content_new_with_str( mime ); | ||
251 | if ( content == NULL ) goto err_free_fields; | ||
252 | |||
253 | if ( mimetype.compare( "text/plain" ) == 0 ) { | ||
254 | param = mailmime_parameter_new( strdup( "charset" ), | ||
255 | strdup( "iso-8859-1" ) ); | ||
256 | if ( param == NULL ) goto err_free_content; | ||
257 | |||
258 | err = clist_append( content->parameters, param ); | ||
259 | if ( err != MAILIMF_NO_ERROR ) goto err_free_param; | ||
260 | } | ||
261 | |||
262 | filePart = mailmime_new_empty( content, fields ); | ||
263 | if ( filePart == NULL ) goto err_free_param; | ||
264 | |||
265 | err = mailmime_set_body_file( filePart, file ); | ||
266 | if ( err != MAILIMF_NO_ERROR ) goto err_free_filePart; | ||
267 | |||
268 | return filePart; // Success :) | ||
269 | |||
270 | err_free_filePart: | ||
271 | mailmime_free( filePart ); | ||
272 | err_free_param: | ||
273 | if ( param != NULL ) mailmime_parameter_free( param ); | ||
274 | err_free_content: | ||
275 | mailmime_content_free( content ); | ||
276 | err_free_fields: | ||
277 | mailmime_fields_free( fields ); | ||
278 | err_free: | ||
279 | free( name ); | ||
280 | free( mime ); | ||
281 | free( file ); | ||
282 | qDebug( "buildFilePart - error" ); | ||
283 | |||
284 | return NULL; // Error :( | ||
285 | } | ||
286 | |||
287 | void MailWrapper::addFileParts( mailmime *message, QList<Attachment> files ) | ||
288 | { | ||
289 | Attachment *it; | ||
290 | for ( it = files.first(); it; it = files.next() ) { | ||
291 | qDebug( "Adding file" ); | ||
292 | mailmime *filePart; | ||
293 | int err; | ||
294 | |||
295 | filePart = buildFilePart( it->getFileName(), it->getMimeType() ); | ||
296 | if ( filePart == NULL ) goto err_free; | ||
297 | |||
298 | err = mailmime_smart_add_part( message, filePart ); | ||
299 | if ( err != MAILIMF_NO_ERROR ) goto err_free_filePart; | ||
300 | |||
301 | continue; // Success :) | ||
302 | |||
303 | err_free_filePart: | ||
304 | mailmime_free( filePart ); | ||
305 | err_free: | ||
306 | qDebug( "addFileParts: error adding file:" ); | ||
307 | qDebug( it->getFileName() ); | ||
308 | } | ||
309 | } | ||
310 | |||
311 | mailmime *MailWrapper::createMimeMail( Mail *mail ) | ||
312 | { | ||
313 | mailmime *message, *txtPart; | ||
314 | mailimf_fields *fields; | ||
315 | int err; | ||
316 | |||
317 | fields = createImfFields( mail ); | ||
318 | if ( fields == NULL ) goto err_free; | ||
319 | |||
320 | message = mailmime_new_message_data( NULL ); | ||
321 | if ( message == NULL ) goto err_free_fields; | ||
322 | |||
323 | mailmime_set_imf_fields( message, fields ); | ||
324 | |||
325 | txtPart = buildTxtPart( mail->getMessage() ); | ||
326 | if ( txtPart == NULL ) goto err_free_message; | ||
327 | |||
328 | err = mailmime_smart_add_part( message, txtPart ); | ||
329 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; | ||
330 | |||
331 | addFileParts( message, mail->getAttachments() ); | ||
332 | |||
333 | return message; // Success :) | ||
334 | |||
335 | err_free_txtPart: | ||
336 | mailmime_free( txtPart ); | ||
337 | err_free_message: | ||
338 | mailmime_free( message ); | ||
339 | err_free_fields: | ||
340 | mailimf_fields_free( fields ); | ||
341 | err_free: | ||
342 | qDebug( "createMimeMail: error" ); | ||
343 | |||
344 | return NULL; // Error :( | ||
345 | } | ||
346 | |||
347 | mailimf_field *MailWrapper::getField( mailimf_fields *fields, int type ) | ||
348 | { | ||
349 | mailimf_field *field; | ||
350 | clistiter *it; | ||
351 | |||
352 | it = clist_begin( fields->list ); | ||
353 | while ( it ) { | ||
354 | field = (mailimf_field *) it->data; | ||
355 | if ( field->type == type ) { | ||
356 | return field; | ||
357 | } | ||
358 | it = it->next; | ||
359 | } | ||
360 | |||
361 | return NULL; | ||
362 | } | ||
363 | |||
364 | static void addRcpts( clist *list, mailimf_address_list *addr_list ) | ||
365 | { | ||
366 | clistiter *it, *it2; | ||
367 | |||
368 | for ( it = clist_begin( addr_list->list ); it; it = it->next ) { | ||
369 | mailimf_address *addr; | ||
370 | addr = (mailimf_address *) it->data; | ||
371 | |||
372 | if ( addr->type == MAILIMF_ADDRESS_MAILBOX ) { | ||
373 | esmtp_address_list_add( list, addr->mailbox->addr_spec, 0, NULL ); | ||
374 | } else if ( addr->type == MAILIMF_ADDRESS_GROUP ) { | ||
375 | clist *l = addr->group->mb_list->list; | ||
376 | for ( it2 = clist_begin( l ); it2; it2 = it2->next ) { | ||
377 | mailimf_mailbox *mbox; | ||
378 | mbox = (mailimf_mailbox *) it2->data; | ||
379 | esmtp_address_list_add( list, mbox->addr_spec, 0, NULL ); | ||
380 | } | ||
381 | } | ||
382 | } | ||
383 | } | ||
384 | |||
385 | clist *MailWrapper::createRcptList( mailimf_fields *fields ) | ||
386 | { | ||
387 | clist *rcptList; | ||
388 | mailimf_field *field; | ||
389 | |||
390 | rcptList = esmtp_address_list_new(); | ||
391 | |||
392 | field = getField( fields, MAILIMF_FIELD_TO ); | ||
393 | if ( field && (field->type == MAILIMF_FIELD_TO) | ||
394 | && field->field.to->addr_list ) { | ||
395 | addRcpts( rcptList, field->field.to->addr_list ); | ||
396 | } | ||
397 | |||
398 | field = getField( fields, MAILIMF_FIELD_CC ); | ||
399 | if ( field && (field->type == MAILIMF_FIELD_CC) | ||
400 | && field->field.cc->addr_list ) { | ||
401 | addRcpts( rcptList, field->field.cc->addr_list ); | ||
402 | } | ||
403 | |||
404 | field = getField( fields, MAILIMF_FIELD_BCC ); | ||
405 | if ( field && (field->type == MAILIMF_FIELD_BCC) | ||
406 | && field->field.bcc->addr_list ) { | ||
407 | addRcpts( rcptList, field->field.bcc->addr_list ); | ||
408 | } | ||
409 | |||
410 | return rcptList; | ||
411 | } | ||
412 | |||
413 | char *MailWrapper::getFrom( mailmime *mail ) | ||
414 | { | ||
415 | char *from = NULL; | ||
416 | |||
417 | mailimf_field *ffrom; | ||
418 | ffrom = getField( mail->fields, MAILIMF_FIELD_FROM ); | ||
419 | if ( ffrom && (ffrom->type == MAILIMF_FIELD_FROM) | ||
420 | && ffrom->field.from->mb_list && ffrom->field.from->mb_list->list ) { | ||
421 | clist *cl = ffrom->field.from->mb_list->list; | ||
422 | clistiter *it; | ||
423 | for ( it = clist_begin( cl ); it; it = it->next ) { | ||
424 | mailimf_mailbox *mb = (mailimf_mailbox *) it->data; | ||
425 | from = strdup( mb->addr_spec ); | ||
426 | } | ||
427 | } | ||
428 | |||
429 | return from; | ||
430 | } | ||
431 | |||
432 | SMTPaccount *MailWrapper::getAccount( QString from ) | ||
433 | { | ||
434 | SMTPaccount *smtp; | ||
435 | |||
436 | QList<Account> list = settings->getAccounts(); | ||
437 | Account *it; | ||
438 | for ( it = list.first(); it; it = list.next() ) { | ||
439 | if ( it->getType().compare( "SMTP" ) == 0 ) { | ||
440 | smtp = static_cast<SMTPaccount *>(it); | ||
441 | if ( smtp->getMail().compare( from ) == 0 ) { | ||
442 | qDebug( "SMTPaccount found for" ); | ||
443 | qDebug( from ); | ||
444 | return smtp; | ||
445 | } | ||
446 | } | ||
447 | } | ||
448 | |||
449 | return NULL; | ||
450 | } | ||
451 | |||
452 | QString MailWrapper::getTmpFile() { | ||
453 | int num = 0; | ||
454 | QString unique; | ||
455 | |||
456 | QDir dir( "/tmp" ); | ||
457 | QStringList::Iterator it; | ||
458 | |||
459 | QStringList list = dir.entryList( "opiemail-tmp-*" ); | ||
460 | do { | ||
461 | unique.setNum( num++ ); | ||
462 | } while ( list.contains( "opiemail-tmp-" + unique ) > 0 ); | ||
463 | |||
464 | return "/tmp/opiemail-tmp-" + unique; | ||
465 | } | ||
466 | |||
467 | void MailWrapper::writeToFile( QString file, mailmime *mail ) | ||
468 | { | ||
469 | FILE *f; | ||
470 | int err, col = 0; | ||
471 | |||
472 | f = fopen( file.latin1(), "w" ); | ||
473 | if ( f == NULL ) { | ||
474 | qDebug( "writeToFile: error opening file" ); | ||
475 | return; | ||
476 | } | ||
477 | |||
478 | err = mailmime_write( f, &col, mail ); | ||
479 | if ( err != MAILIMF_NO_ERROR ) { | ||
480 | fclose( f ); | ||
481 | qDebug( "writeToFile: error writing mailmime" ); | ||
482 | return; | ||
483 | } | ||
484 | |||
485 | fclose( f ); | ||
486 | } | ||
487 | |||
488 | void MailWrapper::readFromFile( QString file, char **data, size_t *size ) | ||
489 | { | ||
490 | char *buf; | ||
491 | struct stat st; | ||
492 | int fd, count = 0, total = 0; | ||
493 | |||
494 | fd = open( file.latin1(), O_RDONLY, 0 ); | ||
495 | if ( fd == -1 ) return; | ||
496 | |||
497 | if ( fstat( fd, &st ) != 0 ) goto err_close; | ||
498 | if ( !st.st_size ) goto err_close; | ||
499 | |||
500 | buf = (char *) malloc( st.st_size ); | ||
501 | if ( !buf ) goto err_close; | ||
502 | |||
503 | while ( ( total < st.st_size ) && ( count >= 0 ) ) { | ||
504 | count = read( fd, buf + total, st.st_size - total ); | ||
505 | total += count; | ||
506 | } | ||
507 | if ( count < 0 ) goto err_free; | ||
508 | |||
509 | *data = buf; | ||
510 | *size = st.st_size; | ||
511 | |||
512 | close( fd ); | ||
513 | |||
514 | return; // Success :) | ||
515 | |||
516 | err_free: | ||
517 | free( buf ); | ||
518 | err_close: | ||
519 | close( fd ); | ||
520 | } | ||
521 | |||
522 | void progress( size_t current, size_t maximum ) | ||
523 | { | ||
524 | qDebug( "Current: %i of %i", current, maximum ); | ||
525 | } | ||
526 | |||
527 | void MailWrapper::smtpSend( mailmime *mail ) | ||
528 | { | ||
529 | mailsmtp *session; | ||
530 | clist *rcpts; | ||
531 | char *from, *data, *server, *user = NULL, *pass = NULL; | ||
532 | size_t size; | ||
533 | int err; | ||
534 | bool ssl; | ||
535 | uint16_t port; | ||
536 | |||
537 | |||
538 | from = getFrom( mail ); | ||
539 | SMTPaccount *smtp = getAccount( from ); | ||
540 | if ( smtp == NULL ) { | ||
541 | free(from); | ||
542 | return; | ||
543 | } | ||
544 | server = strdup( smtp->getServer().latin1() ); | ||
545 | ssl = smtp->getSSL(); | ||
546 | port = smtp->getPort().toUInt(); | ||
547 | rcpts = createRcptList( mail->fields ); | ||
548 | |||
549 | QString file = getTmpFile(); | ||
550 | writeToFile( file, mail ); | ||
551 | readFromFile( file, &data, &size ); | ||
552 | QFile f( file ); | ||
553 | f.remove(); | ||
554 | |||
555 | session = mailsmtp_new( 20, &progress ); | ||
556 | if ( session == NULL ) goto free_mem; | ||
557 | |||
558 | qDebug( "Servername %s at port %i", server, port ); | ||
559 | if ( ssl ) { | ||
560 | qDebug( "SSL session" ); | ||
561 | err = mailsmtp_ssl_connect( session, server, port ); | ||
562 | } else { | ||
563 | qDebug( "No SSL session" ); | ||
564 | err = mailsmtp_socket_connect( session, server, port ); | ||
565 | } | ||
566 | if ( err != MAILSMTP_NO_ERROR ) goto free_mem_session; | ||
567 | |||
568 | err = mailsmtp_init( session ); | ||
569 | if ( err != MAILSMTP_NO_ERROR ) goto free_con_session; | ||
570 | |||
571 | qDebug( "INIT OK" ); | ||
572 | |||
573 | if ( smtp->getLogin() ) { | ||
574 | if ( smtp->getUser().isEmpty() || smtp->getPassword().isEmpty() ) { | ||
575 | // get'em | ||
576 | LoginDialog login( smtp->getUser(), smtp->getPassword(), NULL, 0, true ); | ||
577 | login.show(); | ||
578 | if ( QDialog::Accepted == login.exec() ) { | ||
579 | // ok | ||
580 | user = strdup( login.getUser().latin1() ); | ||
581 | pass = strdup( login.getPassword().latin1() ); | ||
582 | } else { | ||
583 | goto free_con_session; | ||
584 | } | ||
585 | } else { | ||
586 | user = strdup( smtp->getUser().latin1() ); | ||
587 | pass = strdup( smtp->getPassword().latin1() ); | ||
588 | } | ||
589 | qDebug( "session->auth: %i", session->auth); | ||
590 | err = mailsmtp_auth( session, user, pass ); | ||
591 | if ( err == MAILSMTP_NO_ERROR ) qDebug("auth ok"); | ||
592 | qDebug( "Done auth!" ); | ||
593 | } | ||
594 | |||
595 | err = mailsmtp_send( session, from, rcpts, data, size ); | ||
596 | if ( err != MAILSMTP_NO_ERROR ) goto free_con_session; | ||
597 | |||
598 | qDebug( "Mail sent." ); | ||
599 | |||
600 | free_con_session: | ||
601 | mailsmtp_quit( session ); | ||
602 | free_mem_session: | ||
603 | mailsmtp_free( session ); | ||
604 | free_mem: | ||
605 | smtp_address_list_free( rcpts ); | ||
606 | free( data ); | ||
607 | free( server ); | ||
608 | if ( smtp->getLogin() ) { | ||
609 | free( user ); | ||
610 | free( pass ); | ||
611 | } | ||
612 | free( from ); | ||
613 | } | ||
614 | |||
615 | void MailWrapper::sendMail( Mail mail ) | ||
616 | { | ||
617 | mailmime *mimeMail; | ||
618 | |||
619 | mimeMail = createMimeMail( &mail ); | ||
620 | if ( mimeMail == NULL ) { | ||
621 | qDebug( "sendMail: error creating mime mail" ); | ||
622 | } else { | ||
623 | smtpSend( mimeMail ); | ||
624 | mailmime_free( mimeMail ); | ||
625 | } | ||
626 | } | ||
627 | |||
628 | Mail::Mail() | ||
629 | :name(""), mail(""), to(""), cc(""), bcc(""), reply(""), subject(""), message("") | ||
630 | { | ||
631 | } | ||
632 | |||
633 | RecMail::RecMail() | ||
634 | :subject(""),date(""),msg_number(0),msg_flags(7) | ||
635 | { | ||
636 | } | ||
637 | |||
638 | #if 0 | ||
639 | void RecMail::setDate(const QString&aDate) | ||
640 | { | ||
641 | mDate = QDateTime::fromString(aDate); | ||
642 | } | ||
643 | #endif | ||
diff --git a/noncore/net/mail/libmailwrapper/mailwrapper.h b/noncore/net/mail/libmailwrapper/mailwrapper.h new file mode 100644 index 0000000..3de28a0 --- a/dev/null +++ b/noncore/net/mail/libmailwrapper/mailwrapper.h | |||
@@ -0,0 +1,183 @@ | |||
1 | #ifndef MAILWRAPPER_H | ||
2 | #define MAILWRAPPER_H | ||
3 | |||
4 | #include <qpe/applnk.h> | ||
5 | |||
6 | #include <mailmime.h> | ||
7 | #include <mailimf.h> | ||
8 | #include <mailsmtp.h> | ||
9 | #include <mailimap.h> | ||
10 | #include <mailstorage.h> | ||
11 | #include <maildriver.h> | ||
12 | #include <qbitarray.h> | ||
13 | #include <qdatetime.h> | ||
14 | |||
15 | #include "settings.h" | ||
16 | |||
17 | class Attachment | ||
18 | { | ||
19 | public: | ||
20 | Attachment( DocLnk lnk ); | ||
21 | virtual ~Attachment(){} | ||
22 | const QString getFileName()const{ return doc.file(); } | ||
23 | const QString getName()const{ return doc.name(); } | ||
24 | const QString getMimeType()const{ return doc.type(); } | ||
25 | const QPixmap getPixmap()const{ return doc.pixmap(); } | ||
26 | const int getSize()const { return size; } | ||
27 | DocLnk getDocLnk() { return doc; } | ||
28 | |||
29 | protected: | ||
30 | DocLnk doc; | ||
31 | int size; | ||
32 | |||
33 | }; | ||
34 | |||
35 | #define FLAG_ANSWERED 0 | ||
36 | #define FLAG_FLAGGED 1 | ||
37 | #define FLAG_DELETED 2 | ||
38 | #define FLAG_SEEN 3 | ||
39 | #define FLAG_DRAFT 4 | ||
40 | #define FLAG_RECENT 5 | ||
41 | |||
42 | /* a class to describe mails in a mailbox */ | ||
43 | class RecMail | ||
44 | { | ||
45 | public: | ||
46 | RecMail(); | ||
47 | virtual ~RecMail(){} | ||
48 | |||
49 | const int getNumber()const{return msg_number;} | ||
50 | void setNumber(int number){msg_number=number;} | ||
51 | const QString&getDate()const{ return date; } | ||
52 | void setDate( const QString&a ) { date = a; } | ||
53 | const QString&getFrom()const{ return from; } | ||
54 | void setFrom( const QString&a ) { from = a; } | ||
55 | const QString&getSubject()const { return subject; } | ||
56 | void setSubject( const QString&s ) { subject = s; } | ||
57 | void setFlags(const QBitArray&flags){msg_flags = flags;} | ||
58 | const QBitArray&getFlags()const{return msg_flags;} | ||
59 | |||
60 | #if 0 | ||
61 | void setDate(const QString&dstring); | ||
62 | void setDate(const QDateTime&date){mDate = date;} | ||
63 | QString getDate()const{return mDate.toString();} | ||
64 | #endif | ||
65 | protected: | ||
66 | QString subject,date,from; | ||
67 | int msg_number; | ||
68 | QBitArray msg_flags; | ||
69 | #if 0 | ||
70 | QDateTime mDate; | ||
71 | #endif | ||
72 | }; | ||
73 | |||
74 | typedef QList<RecMail> Maillist; | ||
75 | |||
76 | class Mail | ||
77 | { | ||
78 | public: | ||
79 | Mail(); | ||
80 | /* Possible that this destructor must not be declared virtual | ||
81 | * 'cause it seems that it will never have some child classes. | ||
82 | * in this case this object will not get a virtual table -> memory and | ||
83 | * speed will be a little bit better? | ||
84 | */ | ||
85 | virtual ~Mail(){} | ||
86 | void addAttachment( Attachment *att ) { attList.append( att ); } | ||
87 | const QList<Attachment>& getAttachments()const { return attList; } | ||
88 | void removeAttachment( Attachment *att ) { attList.remove( att ); } | ||
89 | const QString&getName()const { return name; } | ||
90 | void setName( QString s ) { name = s; } | ||
91 | const QString&getMail()const{ return mail; } | ||
92 | void setMail( const QString&s ) { mail = s; } | ||
93 | const QString&getTo()const{ return to; } | ||
94 | void setTo( const QString&s ) { to = s; } | ||
95 | const QString&getCC()const{ return cc; } | ||
96 | void setCC( const QString&s ) { cc = s; } | ||
97 | const QString&getBCC()const { return bcc; } | ||
98 | void setBCC( const QString&s ) { bcc = s; } | ||
99 | const QString&getMessage()const { return message; } | ||
100 | void setMessage( const QString&s ) { message = s; } | ||
101 | const QString&getSubject()const { return subject; } | ||
102 | void setSubject( const QString&s ) { subject = s; } | ||
103 | const QString&getReply()const{ return reply; } | ||
104 | void setReply( const QString&a ) { reply = a; } | ||
105 | |||
106 | private: | ||
107 | QList<Attachment> attList; | ||
108 | QString name, mail, to, cc, bcc, reply, subject, message; | ||
109 | }; | ||
110 | |||
111 | class Folder : public QObject | ||
112 | { | ||
113 | Q_OBJECT | ||
114 | |||
115 | public: | ||
116 | Folder( const QString&init_name ); | ||
117 | const QString&getDisplayName()const { return nameDisplay; } | ||
118 | const QString&getName()const { return name; } | ||
119 | virtual bool may_select()const{return true;}; | ||
120 | |||
121 | private: | ||
122 | QString nameDisplay, name; | ||
123 | |||
124 | }; | ||
125 | |||
126 | class IMAPFolder : public Folder | ||
127 | { | ||
128 | public: | ||
129 | IMAPFolder( QString name,bool select=true ) : Folder( name ),m_MaySelect(select) {} | ||
130 | virtual bool may_select()const{return m_MaySelect;} | ||
131 | private: | ||
132 | bool m_MaySelect; | ||
133 | }; | ||
134 | |||
135 | class MailWrapper : public QObject | ||
136 | { | ||
137 | Q_OBJECT | ||
138 | |||
139 | public: | ||
140 | MailWrapper( Settings *s ); | ||
141 | void sendMail( Mail mail ); | ||
142 | |||
143 | private: | ||
144 | mailimf_mailbox *newMailbox(const QString&name,const QString&mail ); | ||
145 | mailimf_address_list *parseAddresses(const QString&addr ); | ||
146 | mailimf_fields *createImfFields( Mail *mail ); | ||
147 | mailmime *buildTxtPart( QString str ); | ||
148 | mailmime *buildFilePart( QString filename, QString mimetype ); | ||
149 | void addFileParts( mailmime *message, QList<Attachment> files ); | ||
150 | mailmime *createMimeMail( Mail *mail ); | ||
151 | void smtpSend( mailmime *mail ); | ||
152 | mailimf_field *getField( mailimf_fields *fields, int type ); | ||
153 | clist *createRcptList( mailimf_fields *fields ); | ||
154 | char *getFrom( mailmime *mail ); | ||
155 | SMTPaccount *getAccount( QString from ); | ||
156 | void writeToFile( QString file, mailmime *mail ); | ||
157 | void readFromFile( QString file, char **data, size_t *size ); | ||
158 | static QString mailsmtpError( int err ); | ||
159 | static QString getTmpFile(); | ||
160 | |||
161 | Settings *settings; | ||
162 | |||
163 | }; | ||
164 | |||
165 | class IMAPwrapper : public QObject | ||
166 | { | ||
167 | Q_OBJECT | ||
168 | |||
169 | public: | ||
170 | IMAPwrapper( IMAPaccount *a ); | ||
171 | QList<IMAPFolder>* listFolders(); | ||
172 | void listMessages(const QString & mailbox,Maillist&target ); | ||
173 | QString fetchBody(const QString & mailbox,const RecMail&mail); | ||
174 | |||
175 | protected: | ||
176 | RecMail*parse_list_result(mailimap_msg_att*); | ||
177 | |||
178 | private: | ||
179 | IMAPaccount *account; | ||
180 | |||
181 | }; | ||
182 | |||
183 | #endif | ||
diff --git a/noncore/net/mail/libmailwrapper/settings.cpp b/noncore/net/mail/libmailwrapper/settings.cpp new file mode 100644 index 0000000..9632301 --- a/dev/null +++ b/noncore/net/mail/libmailwrapper/settings.cpp | |||
@@ -0,0 +1,432 @@ | |||
1 | #include <stdlib.h> | ||
2 | #include <qdir.h> | ||
3 | |||
4 | #include <qpe/config.h> | ||
5 | |||
6 | #include "settings.h" | ||
7 | #include "defines.h" | ||
8 | |||
9 | Settings::Settings() | ||
10 | : QObject() | ||
11 | { | ||
12 | updateAccounts(); | ||
13 | } | ||
14 | |||
15 | void Settings::checkDirectory() | ||
16 | { | ||
17 | if ( !QDir( (QString) getenv( "HOME" ) + "/Applications/opiemail/" ).exists() ) { | ||
18 | system( "mkdir -p $HOME/Applications/opiemail" ); | ||
19 | qDebug( "$HOME/Applications/opiemail created" ); | ||
20 | } | ||
21 | } | ||
22 | |||
23 | QList<Account> Settings::getAccounts() | ||
24 | { | ||
25 | return accounts; | ||
26 | } | ||
27 | |||
28 | void Settings::addAccount( Account *account ) | ||
29 | { | ||
30 | accounts.append( account ); | ||
31 | } | ||
32 | |||
33 | void Settings::delAccount( Account *account ) | ||
34 | { | ||
35 | accounts.remove( account ); | ||
36 | account->remove(); | ||
37 | } | ||
38 | |||
39 | void Settings::updateAccounts() | ||
40 | { | ||
41 | accounts.clear(); | ||
42 | QDir dir( (QString) getenv( "HOME" ) + "/Applications/opiemail" ); | ||
43 | QStringList::Iterator it; | ||
44 | |||
45 | QStringList imap = dir.entryList( "imap-*" ); | ||
46 | for ( it = imap.begin(); it != imap.end(); it++ ) { | ||
47 | qDebug( "Added IMAP account" ); | ||
48 | IMAPaccount *account = new IMAPaccount( (*it).replace(0, 5, "") ); | ||
49 | accounts.append( account ); | ||
50 | } | ||
51 | |||
52 | QStringList pop3 = dir.entryList( "pop3-*" ); | ||
53 | for ( it = pop3.begin(); it != pop3.end(); it++ ) { | ||
54 | qDebug( "Added POP account" ); | ||
55 | POP3account *account = new POP3account( (*it).replace(0, 5, "") ); | ||
56 | accounts.append( account ); | ||
57 | } | ||
58 | |||
59 | QStringList smtp = dir.entryList( "smtp-*" ); | ||
60 | for ( it = smtp.begin(); it != smtp.end(); it++ ) { | ||
61 | qDebug( "Added SMTP account" ); | ||
62 | SMTPaccount *account = new SMTPaccount( (*it).replace(0, 5, "") ); | ||
63 | accounts.append( account ); | ||
64 | } | ||
65 | |||
66 | QStringList nntp = dir.entryList( "nntp-*" ); | ||
67 | for ( it = nntp.begin(); it != nntp.end(); it++ ) { | ||
68 | qDebug( "Added NNTP account" ); | ||
69 | NNTPaccount *account = new NNTPaccount( (*it).replace(0, 5, "") ); | ||
70 | accounts.append( account ); | ||
71 | } | ||
72 | |||
73 | readAccounts(); | ||
74 | } | ||
75 | |||
76 | void Settings::saveAccounts() | ||
77 | { | ||
78 | checkDirectory(); | ||
79 | Account *it; | ||
80 | |||
81 | for ( it = accounts.first(); it; it = accounts.next() ) { | ||
82 | it->save(); | ||
83 | } | ||
84 | } | ||
85 | |||
86 | void Settings::readAccounts() | ||
87 | { | ||
88 | checkDirectory(); | ||
89 | Account *it; | ||
90 | |||
91 | for ( it = accounts.first(); it; it = accounts.next() ) { | ||
92 | it->read(); | ||
93 | } | ||
94 | } | ||
95 | |||
96 | Account::Account() | ||
97 | { | ||
98 | accountName = "changeMe"; | ||
99 | type = "changeMe"; | ||
100 | ssl = false; | ||
101 | } | ||
102 | |||
103 | void Account::remove() | ||
104 | { | ||
105 | QFile file( getFileName() ); | ||
106 | file.remove(); | ||
107 | } | ||
108 | |||
109 | IMAPaccount::IMAPaccount() | ||
110 | : Account() | ||
111 | { | ||
112 | file = IMAPaccount::getUniqueFileName(); | ||
113 | accountName = "New IMAP Account"; | ||
114 | ssl = false; | ||
115 | type = "IMAP"; | ||
116 | port = IMAP_PORT; | ||
117 | } | ||
118 | |||
119 | IMAPaccount::IMAPaccount( QString filename ) | ||
120 | : Account() | ||
121 | { | ||
122 | file = filename; | ||
123 | accountName = "New IMAP Account"; | ||
124 | ssl = false; | ||
125 | type = "IMAP"; | ||
126 | port = IMAP_PORT; | ||
127 | } | ||
128 | |||
129 | QString IMAPaccount::getUniqueFileName() | ||
130 | { | ||
131 | int num = 0; | ||
132 | QString unique; | ||
133 | |||
134 | QDir dir( (QString) getenv( "HOME" ) + "/Applications/opiemail" ); | ||
135 | QStringList::Iterator it; | ||
136 | |||
137 | QStringList imap = dir.entryList( "imap-*" ); | ||
138 | do { | ||
139 | unique.setNum( num++ ); | ||
140 | } while ( imap.contains( "imap-" + unique ) > 0 ); | ||
141 | |||
142 | return unique; | ||
143 | } | ||
144 | |||
145 | void IMAPaccount::read() | ||
146 | { | ||
147 | Config *conf = new Config( getFileName(), Config::File ); | ||
148 | conf->setGroup( "IMAP Account" ); | ||
149 | accountName = conf->readEntry( "Account","" ); | ||
150 | if (accountName.isNull()) accountName = ""; | ||
151 | server = conf->readEntry( "Server","" ); | ||
152 | if (server.isNull()) server=""; | ||
153 | port = conf->readEntry( "Port","" ); | ||
154 | if (port.isNull()) port="143"; | ||
155 | ssl = conf->readBoolEntry( "SSL",false ); | ||
156 | user = conf->readEntry( "User","" ); | ||
157 | if (user.isNull()) user = ""; | ||
158 | password = conf->readEntryCrypt( "Password","" ); | ||
159 | if (password.isNull()) password = ""; | ||
160 | prefix = conf->readEntry("MailPrefix",""); | ||
161 | if (prefix.isNull()) prefix = ""; | ||
162 | } | ||
163 | |||
164 | void IMAPaccount::save() | ||
165 | { | ||
166 | qDebug( "saving " + getFileName() ); | ||
167 | Settings::checkDirectory(); | ||
168 | |||
169 | Config *conf = new Config( getFileName(), Config::File ); | ||
170 | conf->setGroup( "IMAP Account" ); | ||
171 | conf->writeEntry( "Account", accountName ); | ||
172 | conf->writeEntry( "Server", server ); | ||
173 | conf->writeEntry( "Port", port ); | ||
174 | conf->writeEntry( "SSL", ssl ); | ||
175 | conf->writeEntry( "User", user ); | ||
176 | conf->writeEntryCrypt( "Password", password ); | ||
177 | conf->writeEntry( "MailPrefix",prefix); | ||
178 | conf->write(); | ||
179 | } | ||
180 | |||
181 | |||
182 | QString IMAPaccount::getFileName() | ||
183 | { | ||
184 | return (QString) getenv( "HOME" ) + "/Applications/opiemail/imap-" + file; | ||
185 | } | ||
186 | |||
187 | POP3account::POP3account() | ||
188 | : Account() | ||
189 | { | ||
190 | file = POP3account::getUniqueFileName(); | ||
191 | accountName = "New POP3 Account"; | ||
192 | ssl = false; | ||
193 | type = "POP3"; | ||
194 | port = POP3_PORT; | ||
195 | } | ||
196 | |||
197 | POP3account::POP3account( QString filename ) | ||
198 | : Account() | ||
199 | { | ||
200 | file = filename; | ||
201 | accountName = "New POP3 Account"; | ||
202 | ssl = false; | ||
203 | type = "POP3"; | ||
204 | port = POP3_PORT; | ||
205 | } | ||
206 | |||
207 | QString POP3account::getUniqueFileName() | ||
208 | { | ||
209 | int num = 0; | ||
210 | QString unique; | ||
211 | |||
212 | QDir dir( (QString) getenv( "HOME" ) + "/Applications/opiemail" ); | ||
213 | QStringList::Iterator it; | ||
214 | |||
215 | QStringList imap = dir.entryList( "pop3-*" ); | ||
216 | do { | ||
217 | unique.setNum( num++ ); | ||
218 | } while ( imap.contains( "pop3-" + unique ) > 0 ); | ||
219 | |||
220 | return unique; | ||
221 | } | ||
222 | |||
223 | void POP3account::read() | ||
224 | { | ||
225 | Config *conf = new Config( getFileName(), Config::File ); | ||
226 | conf->setGroup( "POP3 Account" ); | ||
227 | accountName = conf->readEntry( "Account" ); | ||
228 | server = conf->readEntry( "Server" ); | ||
229 | port = conf->readEntry( "Port" ); | ||
230 | ssl = conf->readBoolEntry( "SSL" ); | ||
231 | user = conf->readEntry( "User" ); | ||
232 | password = conf->readEntryCrypt( "Password" ); | ||
233 | } | ||
234 | |||
235 | void POP3account::save() | ||
236 | { | ||
237 | qDebug( "saving " + getFileName() ); | ||
238 | Settings::checkDirectory(); | ||
239 | |||
240 | Config *conf = new Config( getFileName(), Config::File ); | ||
241 | conf->setGroup( "POP3 Account" ); | ||
242 | conf->writeEntry( "Account", accountName ); | ||
243 | conf->writeEntry( "Server", server ); | ||
244 | conf->writeEntry( "Port", port ); | ||
245 | conf->writeEntry( "SSL", ssl ); | ||
246 | conf->writeEntry( "User", user ); | ||
247 | conf->writeEntryCrypt( "Password", password ); | ||
248 | conf->write(); | ||
249 | } | ||
250 | |||
251 | |||
252 | QString POP3account::getFileName() | ||
253 | { | ||
254 | return (QString) getenv( "HOME" ) + "/Applications/opiemail/pop3-" + file; | ||
255 | } | ||
256 | |||
257 | SMTPaccount::SMTPaccount() | ||
258 | : Account() | ||
259 | { | ||
260 | file = SMTPaccount::getUniqueFileName(); | ||
261 | accountName = "New SMTP Account"; | ||
262 | ssl = false; | ||
263 | login = false; | ||
264 | useCC = false; | ||
265 | useBCC = false; | ||
266 | useReply = false; | ||
267 | type = "SMTP"; | ||
268 | port = SMTP_PORT; | ||
269 | } | ||
270 | |||
271 | SMTPaccount::SMTPaccount( QString filename ) | ||
272 | : Account() | ||
273 | { | ||
274 | file = filename; | ||
275 | accountName = "New SMTP Account"; | ||
276 | ssl = false; | ||
277 | login = false; | ||
278 | useCC = false; | ||
279 | useBCC = false; | ||
280 | useReply = false; | ||
281 | type = "SMTP"; | ||
282 | port = SMTP_PORT; | ||
283 | } | ||
284 | |||
285 | QString SMTPaccount::getUniqueFileName() | ||
286 | { | ||
287 | int num = 0; | ||
288 | QString unique; | ||
289 | |||
290 | QDir dir( (QString) getenv( "HOME" ) + "/Applications/opiemail" ); | ||
291 | QStringList::Iterator it; | ||
292 | |||
293 | QStringList imap = dir.entryList( "smtp-*" ); | ||
294 | do { | ||
295 | unique.setNum( num++ ); | ||
296 | } while ( imap.contains( "smtp-" + unique ) > 0 ); | ||
297 | |||
298 | return unique; | ||
299 | } | ||
300 | |||
301 | void SMTPaccount::read() | ||
302 | { | ||
303 | Config *conf = new Config( getFileName(), Config::File ); | ||
304 | conf->setGroup( "SMTP Account" ); | ||
305 | accountName = conf->readEntry( "Account" ); | ||
306 | server = conf->readEntry( "Server" ); | ||
307 | port = conf->readEntry( "Port" ); | ||
308 | ssl = conf->readBoolEntry( "SSL" ); | ||
309 | login = conf->readBoolEntry( "Login" ); | ||
310 | user = conf->readEntry( "User" ); | ||
311 | password = conf->readEntryCrypt( "Password" ); | ||
312 | useCC = conf->readBoolEntry( "useCC" ); | ||
313 | useBCC = conf->readBoolEntry( "useBCC" ); | ||
314 | useReply = conf->readBoolEntry( "useReply" ); | ||
315 | name = conf->readEntry( "Name" ); | ||
316 | mail = conf->readEntry( "Mail" ); | ||
317 | org = conf->readEntry( "Org" ); | ||
318 | cc = conf->readEntry( "CC" ); | ||
319 | bcc = conf->readEntry( "BCC" ); | ||
320 | reply = conf->readEntry( "Reply" ); | ||
321 | signature = conf->readEntry( "Signature" ); | ||
322 | signature = signature.replace( QRegExp( "<br>" ), "\n" ); | ||
323 | } | ||
324 | |||
325 | void SMTPaccount::save() | ||
326 | { | ||
327 | qDebug( "saving " + getFileName() ); | ||
328 | Settings::checkDirectory(); | ||
329 | |||
330 | Config *conf = new Config( getFileName(), Config::File ); | ||
331 | conf->setGroup( "SMTP Account" ); | ||
332 | conf->writeEntry( "Account", accountName ); | ||
333 | conf->writeEntry( "Server", server ); | ||
334 | conf->writeEntry( "Port", port ); | ||
335 | conf->writeEntry( "SSL", ssl ); | ||
336 | conf->writeEntry( "Login", login ); | ||
337 | conf->writeEntry( "User", user ); | ||
338 | conf->writeEntryCrypt( "Password", password ); | ||
339 | conf->writeEntry( "useCC", useCC ); | ||
340 | conf->writeEntry( "useBCC", useBCC ); | ||
341 | conf->writeEntry( "useReply", useReply ); | ||
342 | conf->writeEntry( "Name", name ); | ||
343 | conf->writeEntry( "Mail", mail ); | ||
344 | conf->writeEntry( "Org", org ); | ||
345 | conf->writeEntry( "CC", cc ); | ||
346 | conf->writeEntry( "BCC", bcc ); | ||
347 | conf->writeEntry( "Reply", reply ); | ||
348 | conf->writeEntry( "Signature", | ||
349 | signature.replace( QRegExp( "\\n" ), "<br>" ) ); | ||
350 | conf->write(); | ||
351 | } | ||
352 | |||
353 | |||
354 | QString SMTPaccount::getFileName() | ||
355 | { | ||
356 | return (QString) getenv( "HOME" ) + "/Applications/opiemail/smtp-" + file; | ||
357 | } | ||
358 | |||
359 | NNTPaccount::NNTPaccount() | ||
360 | : Account() | ||
361 | { | ||
362 | file = NNTPaccount::getUniqueFileName(); | ||
363 | accountName = "New NNTP Account"; | ||
364 | ssl = false; | ||
365 | login = false; | ||
366 | type = "NNTP"; | ||
367 | port = NNTP_PORT; | ||
368 | } | ||
369 | |||
370 | NNTPaccount::NNTPaccount( QString filename ) | ||
371 | : Account() | ||
372 | { | ||
373 | file = filename; | ||
374 | accountName = "New NNTP Account"; | ||
375 | ssl = false; | ||
376 | login = false; | ||
377 | type = "NNTP"; | ||
378 | port = NNTP_PORT; | ||
379 | } | ||
380 | |||
381 | QString NNTPaccount::getUniqueFileName() | ||
382 | { | ||
383 | int num = 0; | ||
384 | QString unique; | ||
385 | |||
386 | QDir dir( (QString) getenv( "HOME" ) + "/Applications/opiemail" ); | ||
387 | QStringList::Iterator it; | ||
388 | |||
389 | QStringList imap = dir.entryList( "nntp-*" ); | ||
390 | do { | ||
391 | unique.setNum( num++ ); | ||
392 | } while ( imap.contains( "nntp-" + unique ) > 0 ); | ||
393 | |||
394 | return unique; | ||
395 | } | ||
396 | |||
397 | void NNTPaccount::read() | ||
398 | { | ||
399 | Config *conf = new Config( getFileName(), Config::File ); | ||
400 | conf->setGroup( "NNTP Account" ); | ||
401 | accountName = conf->readEntry( "Account" ); | ||
402 | server = conf->readEntry( "Server" ); | ||
403 | port = conf->readEntry( "Port" ); | ||
404 | ssl = conf->readBoolEntry( "SSL" ); | ||
405 | login = conf->readBoolEntry( "Login" ); | ||
406 | user = conf->readEntry( "User" ); | ||
407 | password = conf->readEntryCrypt( "Password" ); | ||
408 | } | ||
409 | |||
410 | void NNTPaccount::save() | ||
411 | { | ||
412 | qDebug( "saving " + getFileName() ); | ||
413 | Settings::checkDirectory(); | ||
414 | |||
415 | Config *conf = new Config( getFileName(), Config::File ); | ||
416 | conf->setGroup( "NNTP Account" ); | ||
417 | conf->writeEntry( "Account", accountName ); | ||
418 | conf->writeEntry( "Server", server ); | ||
419 | conf->writeEntry( "Port", port ); | ||
420 | conf->writeEntry( "SSL", ssl ); | ||
421 | conf->writeEntry( "Login", login ); | ||
422 | conf->writeEntry( "User", user ); | ||
423 | conf->writeEntryCrypt( "Password", password ); | ||
424 | conf->write(); | ||
425 | } | ||
426 | |||
427 | |||
428 | QString NNTPaccount::getFileName() | ||
429 | { | ||
430 | return (QString) getenv( "HOME" ) + "/Applications/opiemail/nntp-" + file; | ||
431 | } | ||
432 | |||
diff --git a/noncore/net/mail/libmailwrapper/settings.h b/noncore/net/mail/libmailwrapper/settings.h new file mode 100644 index 0000000..22184a5 --- a/dev/null +++ b/noncore/net/mail/libmailwrapper/settings.h | |||
@@ -0,0 +1,166 @@ | |||
1 | #ifndef SETTINGS_H | ||
2 | #define SETTINGS_H | ||
3 | |||
4 | #include <qobject.h> | ||
5 | #include <qlist.h> | ||
6 | |||
7 | class Account | ||
8 | { | ||
9 | |||
10 | public: | ||
11 | Account(); | ||
12 | virtual ~Account() {} | ||
13 | |||
14 | void remove(); | ||
15 | void setAccountName( QString name ) { accountName = name; } | ||
16 | const QString&getAccountName()const{ return accountName; } | ||
17 | const QString&getType()const{ return type; } | ||
18 | |||
19 | void setServer(const QString&str){ server = str; } | ||
20 | const QString&getServer()const{ return server; } | ||
21 | |||
22 | void setPort(const QString&str) { port = str; } | ||
23 | const QString&getPort()const{ return port; } | ||
24 | |||
25 | void setUser(const QString&str){ user = str; } | ||
26 | const QString&getUser()const{ return user; } | ||
27 | |||
28 | void setPassword(const QString&str) { password = str; } | ||
29 | const QString&getPassword()const { return password; } | ||
30 | |||
31 | void setSSL( bool b ) { ssl = b; } | ||
32 | bool getSSL() { return ssl; } | ||
33 | |||
34 | virtual QString getFileName() { return accountName; } | ||
35 | virtual void read() { qDebug( "base reading..." ); } | ||
36 | virtual void save() { qDebug( "base saving..." ); } | ||
37 | |||
38 | protected: | ||
39 | QString accountName, type, server, port, user, password; | ||
40 | bool ssl; | ||
41 | |||
42 | }; | ||
43 | |||
44 | class IMAPaccount : public Account | ||
45 | { | ||
46 | |||
47 | public: | ||
48 | IMAPaccount(); | ||
49 | IMAPaccount( QString filename ); | ||
50 | |||
51 | static QString getUniqueFileName(); | ||
52 | |||
53 | virtual void read(); | ||
54 | virtual void save(); | ||
55 | virtual QString getFileName(); | ||
56 | |||
57 | void setPrefix(const QString&str) {prefix=str;} | ||
58 | const QString&getPrefix()const{return prefix;} | ||
59 | |||
60 | private: | ||
61 | QString file,prefix; | ||
62 | |||
63 | }; | ||
64 | |||
65 | class POP3account : public Account | ||
66 | { | ||
67 | |||
68 | public: | ||
69 | POP3account(); | ||
70 | POP3account( QString filename ); | ||
71 | |||
72 | static QString getUniqueFileName(); | ||
73 | |||
74 | virtual void read(); | ||
75 | virtual void save(); | ||
76 | virtual QString getFileName(); | ||
77 | |||
78 | private: | ||
79 | QString file; | ||
80 | |||
81 | }; | ||
82 | |||
83 | class SMTPaccount : public Account | ||
84 | { | ||
85 | |||
86 | public: | ||
87 | SMTPaccount(); | ||
88 | SMTPaccount( QString filename ); | ||
89 | |||
90 | static QString getUniqueFileName(); | ||
91 | |||
92 | virtual void read(); | ||
93 | virtual void save(); | ||
94 | virtual QString getFileName(); | ||
95 | |||
96 | void setName( QString str ) { name = str; } | ||
97 | QString getName() { return name; } | ||
98 | void setMail( QString str ) { mail = str; } | ||
99 | QString getMail() { return mail; } | ||
100 | void setOrg( QString str ) { org = str; } | ||
101 | QString getOrg() { return org; } | ||
102 | void setUseCC( bool b ) { useCC = b; } | ||
103 | bool getUseCC() { return useCC; } | ||
104 | void setCC( QString str ) { cc = str; } | ||
105 | QString getCC() { return cc; } | ||
106 | void setUseBCC( bool b ) { useBCC = b; } | ||
107 | bool getUseBCC() { return useBCC; } | ||
108 | void setBCC( QString str ) { bcc = str; } | ||
109 | QString getBCC() { return bcc; } | ||
110 | void setUseReply( bool b ) { useReply = b; } | ||
111 | bool getUseReply() { return useReply; } | ||
112 | void setReply( QString str ) { reply = str; } | ||
113 | QString getReply() { return reply; } | ||
114 | void setSignature( QString str ) { signature = str; } | ||
115 | QString getSignature() { return signature; } | ||
116 | void setLogin( bool b ) { login = b; } | ||
117 | bool getLogin() { return login; } | ||
118 | |||
119 | private: | ||
120 | QString file, name, mail, org, cc, bcc, reply, signature; | ||
121 | bool useCC, useBCC, useReply, login; | ||
122 | |||
123 | }; | ||
124 | |||
125 | class NNTPaccount : public Account | ||
126 | { | ||
127 | |||
128 | public: | ||
129 | NNTPaccount(); | ||
130 | NNTPaccount( QString filename ); | ||
131 | |||
132 | static QString getUniqueFileName(); | ||
133 | |||
134 | virtual void read(); | ||
135 | virtual void save(); | ||
136 | virtual QString getFileName(); | ||
137 | |||
138 | void setLogin( bool b ) { login = b; } | ||
139 | bool getLogin() { return login; } | ||
140 | |||
141 | private: | ||
142 | QString file; | ||
143 | bool login; | ||
144 | |||
145 | }; | ||
146 | |||
147 | class Settings : public QObject | ||
148 | { | ||
149 | Q_OBJECT | ||
150 | |||
151 | public: | ||
152 | Settings(); | ||
153 | QList<Account> getAccounts(); | ||
154 | void addAccount(Account *account); | ||
155 | void delAccount(Account *account); | ||
156 | void saveAccounts(); | ||
157 | void readAccounts(); | ||
158 | static void checkDirectory(); | ||
159 | |||
160 | private: | ||
161 | void updateAccounts(); | ||
162 | QList<Account> accounts; | ||
163 | |||
164 | }; | ||
165 | |||
166 | #endif | ||