Diffstat (limited to 'noncore/net/mail/libmailwrapper/nntpwrapper.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/net/mail/libmailwrapper/nntpwrapper.cpp | 241 |
1 files changed, 241 insertions, 0 deletions
diff --git a/noncore/net/mail/libmailwrapper/nntpwrapper.cpp b/noncore/net/mail/libmailwrapper/nntpwrapper.cpp new file mode 100644 index 0000000..e73a890 --- a/dev/null +++ b/noncore/net/mail/libmailwrapper/nntpwrapper.cpp | |||
@@ -0,0 +1,241 @@ | |||
1 | #include "nntpwrapper.h" | ||
2 | #include "logindialog.h" | ||
3 | #include "mailtypes.h" | ||
4 | |||
5 | #include <qfile.h> | ||
6 | |||
7 | #include <stdlib.h> | ||
8 | |||
9 | #include <libetpan/libetpan.h> | ||
10 | #include <libetpan/nntpdriver.h> | ||
11 | |||
12 | |||
13 | |||
14 | #define HARD_MSG_SIZE_LIMIT 5242880 | ||
15 | |||
16 | NNTPwrapper::NNTPwrapper( NNTPaccount *a ) | ||
17 | : Genericwrapper() { | ||
18 | account = a; | ||
19 | m_nntp = NULL; | ||
20 | msgTempName = a->getFileName()+"_msg_cache"; | ||
21 | last_msg_id = 0; | ||
22 | } | ||
23 | |||
24 | NNTPwrapper::~NNTPwrapper() { | ||
25 | logout(); | ||
26 | QFile msg_cache(msgTempName); | ||
27 | if (msg_cache.exists()) { | ||
28 | msg_cache.remove(); | ||
29 | } | ||
30 | } | ||
31 | |||
32 | void NNTPwrapper::nntp_progress( size_t current, size_t maximum ) { | ||
33 | qDebug( "NNTP: %i of %i", current, maximum ); | ||
34 | } | ||
35 | |||
36 | |||
37 | RecBody NNTPwrapper::fetchBody( const RecMail &mail ) { | ||
38 | int err = NEWSNNTP_NO_ERROR; | ||
39 | char *message = 0; | ||
40 | size_t length = 0; | ||
41 | |||
42 | login(); | ||
43 | if ( !m_nntp ) { | ||
44 | return RecBody(); | ||
45 | } | ||
46 | |||
47 | RecBody body; | ||
48 | mailmessage * mailmsg; | ||
49 | if (mail.Msgsize()>HARD_MSG_SIZE_LIMIT) { | ||
50 | qDebug("Message to large: %i",mail.Msgsize()); | ||
51 | return body; | ||
52 | } | ||
53 | |||
54 | QFile msg_cache(msgTempName); | ||
55 | |||
56 | cleanMimeCache(); | ||
57 | |||
58 | if (mail.getNumber()!=last_msg_id) { | ||
59 | if (msg_cache.exists()) { | ||
60 | msg_cache.remove(); | ||
61 | } | ||
62 | msg_cache.open(IO_ReadWrite|IO_Truncate); | ||
63 | last_msg_id = mail.getNumber(); | ||
64 | err = mailsession_get_message(m_nntp->sto_session, mail.getNumber(), &mailmsg); | ||
65 | err = mailmessage_fetch(mailmsg,&message,&length); | ||
66 | msg_cache.writeBlock(message,length); | ||
67 | } else { | ||
68 | QString msg=""; | ||
69 | msg_cache.open(IO_ReadOnly); | ||
70 | message = new char[4096]; | ||
71 | memset(message,0,4096); | ||
72 | while (msg_cache.readBlock(message,4095)>0) { | ||
73 | msg+=message; | ||
74 | memset(message,0,4096); | ||
75 | } | ||
76 | delete message; | ||
77 | message = (char*)malloc(msg.length()+1*sizeof(char)); | ||
78 | memset(message,0,msg.length()+1); | ||
79 | memcpy(message,msg.latin1(),msg.length()); | ||
80 | /* transform to libetpan stuff */ | ||
81 | mailmsg = mailmessage_new(); | ||
82 | mailmessage_init(mailmsg, NULL, data_message_driver, 0, strlen(message)); | ||
83 | generic_message_t * msg_data; | ||
84 | msg_data = (generic_message_t *)mailmsg->msg_data; | ||
85 | msg_data->msg_fetched = 1; | ||
86 | msg_data->msg_message = message; | ||
87 | msg_data->msg_length = strlen(message); | ||
88 | } | ||
89 | body = parseMail(mailmsg); | ||
90 | |||
91 | /* clean up */ | ||
92 | if (mailmsg) | ||
93 | mailmessage_free(mailmsg); | ||
94 | if (message) | ||
95 | free(message); | ||
96 | |||
97 | return body; | ||
98 | } | ||
99 | |||
100 | |||
101 | void NNTPwrapper::listMessages(const QString &, QList<RecMail> &target ) | ||
102 | { | ||
103 | login(); | ||
104 | if (!m_nntp) | ||
105 | return; | ||
106 | uint32_t res_messages,res_recent,res_unseen; | ||
107 | mailsession_status_folder(m_nntp->sto_session,"INBOX",&res_messages,&res_recent,&res_unseen); | ||
108 | parseList(target,m_nntp->sto_session,"INBOX"); | ||
109 | } | ||
110 | |||
111 | void NNTPwrapper::login() | ||
112 | { | ||
113 | if (account->getOffline()) | ||
114 | return; | ||
115 | /* we'll hold the line */ | ||
116 | if ( m_nntp != NULL ) | ||
117 | return; | ||
118 | |||
119 | const char *server, *user, *pass; | ||
120 | uint16_t port; | ||
121 | int err = NEWSNNTP_NO_ERROR; | ||
122 | |||
123 | server = account->getServer().latin1(); | ||
124 | port = account->getPort().toUInt(); | ||
125 | |||
126 | if ( account->getUser().isEmpty() || account->getPassword().isEmpty() ) { | ||
127 | LoginDialog login( account->getUser(), account->getPassword(), NULL, 0, true ); | ||
128 | login.show(); | ||
129 | if ( QDialog::Accepted == login.exec() ) { | ||
130 | // ok | ||
131 | user = login.getUser().latin1(); | ||
132 | pass = login.getPassword().latin1(); | ||
133 | } else { | ||
134 | // cancel | ||
135 | qDebug( "NNTP: Login canceled" ); | ||
136 | return; | ||
137 | } | ||
138 | } else { | ||
139 | user = account->getUser().latin1(); | ||
140 | pass = account->getPassword().latin1(); | ||
141 | } | ||
142 | |||
143 | // bool ssl = account->getSSL(); | ||
144 | |||
145 | m_nntp=mailstorage_new(NULL); | ||
146 | |||
147 | int conntypeset = account->ConnectionType(); | ||
148 | int conntype = 0; | ||
149 | if ( conntypeset == 3 ) { | ||
150 | conntype = CONNECTION_TYPE_COMMAND; | ||
151 | } else if ( conntypeset == 2 ) { | ||
152 | conntype = CONNECTION_TYPE_TLS; | ||
153 | } else if ( conntypeset == 1 ) { | ||
154 | conntype = CONNECTION_TYPE_STARTTLS; | ||
155 | } else if ( conntypeset == 0 ) { | ||
156 | conntype = CONNECTION_TYPE_TRY_STARTTLS; | ||
157 | } | ||
158 | |||
159 | nntp_mailstorage_init(m_nntp,(char*)server, port, NULL, conntype, NNTP_AUTH_TYPE_PLAIN, | ||
160 | (char*)user,(char*)pass,0,0,0); | ||
161 | |||
162 | err = mailstorage_connect(m_nntp); | ||
163 | |||
164 | if (err != NEWSNNTP_NO_ERROR) { | ||
165 | qDebug( QString( "FEHLERNUMMER %1" ).arg( err ) ); | ||
166 | // Global::statusMessage(tr("Error initializing folder")); | ||
167 | mailstorage_free(m_nntp); | ||
168 | m_nntp = 0; | ||
169 | } | ||
170 | } | ||
171 | |||
172 | void NNTPwrapper::logout() | ||
173 | { | ||
174 | int err = NEWSNNTP_NO_ERROR; | ||
175 | if ( m_nntp == NULL ) | ||
176 | return; | ||
177 | mailstorage_free(m_nntp); | ||
178 | m_nntp = 0; | ||
179 | } | ||
180 | |||
181 | QList<Folder>* NNTPwrapper::listFolders() { | ||
182 | QList<Folder> * folders = new QList<Folder>(); | ||
183 | folders->setAutoDelete( false ); | ||
184 | clist *result = 0; | ||
185 | |||
186 | // int err = | ||
187 | // if ( err == _NO_ERROR ) { | ||
188 | // current = result->first; | ||
189 | // for ( current=clist_begin(result);current!=NULL;current=clist_next(current)) { | ||
190 | |||
191 | |||
192 | // Folder*inb=new Folder("INBOX","/"); | ||
193 | |||
194 | |||
195 | // folders->append(inb); | ||
196 | return folders; | ||
197 | } | ||
198 | |||
199 | |||
200 | void NNTPwrapper::answeredMail(const RecMail&) {} | ||
201 | |||
202 | void NNTPwrapper::statusFolder(folderStat&target_stat,const QString&) { | ||
203 | login(); | ||
204 | target_stat.message_count = 0; | ||
205 | target_stat.message_unseen = 0; | ||
206 | target_stat.message_recent = 0; | ||
207 | if (!m_nntp) | ||
208 | return; | ||
209 | int r = mailsession_status_folder(m_nntp->sto_session,0,&target_stat.message_count, | ||
210 | &target_stat.message_recent,&target_stat.message_unseen); | ||
211 | } | ||
212 | |||
213 | |||
214 | encodedString* NNTPwrapper::fetchRawBody(const RecMail&mail) { | ||
215 | char*target=0; | ||
216 | size_t length=0; | ||
217 | encodedString*res = 0; | ||
218 | mailmessage * mailmsg = 0; | ||
219 | int err = mailsession_get_message(m_nntp->sto_session, mail.getNumber(), &mailmsg); | ||
220 | err = mailmessage_fetch(mailmsg,&target,&length); | ||
221 | if (mailmsg) | ||
222 | mailmessage_free(mailmsg); | ||
223 | if (target) { | ||
224 | res = new encodedString(target,length); | ||
225 | } | ||
226 | return res; | ||
227 | } | ||
228 | |||
229 | const QString&NNTPwrapper::getType()const { | ||
230 | return account->getType(); | ||
231 | } | ||
232 | |||
233 | const QString&NNTPwrapper::getName()const{ | ||
234 | return account->getAccountName(); | ||
235 | } | ||
236 | |||
237 | void NNTPwrapper::deleteMail(const RecMail&mail) { | ||
238 | } | ||
239 | |||
240 | int NNTPwrapper::deleteAllMail(const Folder*) { | ||
241 | } | ||