summaryrefslogtreecommitdiffabout
path: root/kmicromail/libmailwrapper/nntpwrapper.cpp
Unidiff
Diffstat (limited to 'kmicromail/libmailwrapper/nntpwrapper.cpp') (more/less context) (show whitespace changes)
-rw-r--r--kmicromail/libmailwrapper/nntpwrapper.cpp289
1 files changed, 289 insertions, 0 deletions
diff --git a/kmicromail/libmailwrapper/nntpwrapper.cpp b/kmicromail/libmailwrapper/nntpwrapper.cpp
new file mode 100644
index 0000000..daa128e
--- a/dev/null
+++ b/kmicromail/libmailwrapper/nntpwrapper.cpp
@@ -0,0 +1,289 @@
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
11
12
13#define HARD_MSG_SIZE_LIMIT 5242880
14
15using namespace Opie::Core;
16NNTPwrapper::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
24NNTPwrapper::~NNTPwrapper() {
25 logout();
26 QFile msg_cache(msgTempName);
27 if (msg_cache.exists()) {
28 msg_cache.remove();
29 }
30}
31
32void NNTPwrapper::nntp_progress( size_t current, size_t maximum ) {
33 ; // << "NNTP: " << current << " of " << maximum << "" << oendl;
34}
35
36
37RecBodyP NNTPwrapper::fetchBody( const RecMailP &mail ) {
38 int err = NEWSNNTP_NO_ERROR;
39 char *message = 0;
40 size_t length = 0;
41
42 RecBodyP body = new RecBody();
43 login();
44 if ( !m_nntp ) {
45 return body;
46 }
47
48 mailmessage * mailmsg;
49 if (mail->Msgsize()>HARD_MSG_SIZE_LIMIT) {
50 ; // << "Message to large: " << mail->Msgsize() << "" << oendl;
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
101void NNTPwrapper::listMessages(const QString & which, QValueList<Opie::Core::OSmartPointer<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,(char*)which.latin1(),&res_messages,&res_recent,&res_unseen);
108 parseList(target,m_nntp->sto_session,which,true);
109}
110
111void 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 QString User,Pass;
121 uint16_t port;
122 int err = NEWSNNTP_NO_ERROR;
123
124 server = account->getServer().latin1();
125 port = account->getPort().toUInt();
126
127 user = pass = 0;
128
129 if ( ( account->getUser().isEmpty() || account->getPassword().isEmpty() ) && account->getLogin() ) {
130 LoginDialog login( account->getUser(), account->getPassword(), NULL, 0, true );
131 login.show();
132 if ( QDialog::Accepted == login.exec() ) {
133 // ok
134 User = login.getUser().latin1();
135 Pass = login.getPassword().latin1();
136 } else {
137 // cancel
138 ; // << "NNTP: Login canceled" << oendl;
139 return;
140 }
141 } else {
142 User = account->getUser().latin1();
143 Pass = account->getPassword().latin1();
144 }
145
146 if (User.isEmpty()) {
147 user=0;
148 pass = 0;
149 } else {
150 user=User.latin1();
151 pass=Pass.latin1();
152 }
153 // bool ssl = account->getSSL();
154
155 m_nntp=mailstorage_new(NULL);
156
157 int conntypeset = account->ConnectionType();
158 int conntype = 0;
159 if ( conntypeset == 3 ) {
160 conntype = CONNECTION_TYPE_COMMAND;
161 } else if ( conntypeset == 2 ) {
162 conntype = CONNECTION_TYPE_TLS;
163 } else if ( conntypeset == 1 ) {
164 conntype = CONNECTION_TYPE_STARTTLS;
165 } else if ( conntypeset == 0 ) {
166 conntype = CONNECTION_TYPE_TRY_STARTTLS;
167 }
168
169 nntp_mailstorage_init(m_nntp,(char*)server, port, NULL, CONNECTION_TYPE_PLAIN, NNTP_AUTH_TYPE_PLAIN,
170 (char*)user,(char*)pass,0,0,0);
171
172 err = mailstorage_connect( m_nntp );
173
174 if (err != NEWSNNTP_NO_ERROR) {
175 ; // << QString( "FEHLERNUMMER %1" ).arg( err ) << oendl;
176 // Global::statusMessage(tr("Error initializing folder"));
177 mailstorage_free(m_nntp);
178 m_nntp = 0;
179
180 } else {
181 mailsession * session = m_nntp->sto_session;
182 newsnntp * news = ( ( nntp_session_state_data * )session->sess_data )->nntp_session;
183 news->nntp_progr_fun = &nntp_progress;
184 }
185
186}
187
188void NNTPwrapper::logout()
189{
190 int err = NEWSNNTP_NO_ERROR;
191 if ( m_nntp == NULL )
192 return;
193 mailstorage_free(m_nntp);
194 m_nntp = 0;
195}
196
197QValueList<Opie::Core::OSmartPointer<Folder> >* NNTPwrapper::listFolders() {
198
199 QValueList<Opie::Core::OSmartPointer<Folder> >* folders = new QValueList<Opie::Core::OSmartPointer<Folder> >();
200 QStringList groups;
201 if (account) {
202 groups = account->getGroups();
203 }
204 for ( QStringList::Iterator it = groups.begin(); it != groups.end(); ++it ) {
205 folders->append(new Folder((*it),"."));
206 }
207 return folders;
208}
209
210/* we made this method in raw nntp access of etpan and not via generic interface
211 * 'cause in that case there will be doubled copy operations. eg. the etpan would
212 * copy that stuff into its own structures and we must copy it into useable c++
213 * structures for our frontend. this would not make sense, so it is better to reimplement
214 * the stuff from generic interface of etpan but copy it direct to qt classes.
215 */
216QStringList NNTPwrapper::listAllNewsgroups(const QString&mask) {
217 login();
218 QStringList res;
219 clist *result = 0;
220 clistcell *current = 0;
221 newsnntp_group_description *group;
222
223 if ( m_nntp ) {
224 mailsession * session = m_nntp->sto_session;
225 newsnntp * news = ( ( nntp_session_state_data * )session->sess_data )->nntp_session;
226 int err = NEWSNNTP_NO_ERROR;
227 if (mask.isEmpty()) {
228 err = newsnntp_list(news, &result);
229 } else {
230 /* taken from generic wrapper of etpan */
231 QString nmask = mask+".*";
232 err = newsnntp_list_active(news, nmask.latin1(), &result);
233 }
234 if ( err == NEWSNNTP_NO_ERROR && result) {
235 for ( current=clist_begin(result);current!=NULL;current=clist_next(current) ) {
236 group = ( newsnntp_group_description* ) current->data;
237 if (!group||!group->grp_name||strlen(group->grp_name)==0) continue;
238 res.append(group->grp_name);
239 }
240 }
241 }
242 if (result) {
243 newsnntp_list_free(result);
244 }
245 return res;
246}
247
248void NNTPwrapper::answeredMail(const RecMailP&) {}
249
250void NNTPwrapper::statusFolder(folderStat&target_stat,const QString&) {
251 login();
252 target_stat.message_count = 0;
253 target_stat.message_unseen = 0;
254 target_stat.message_recent = 0;
255 if (!m_nntp)
256 return;
257 int r = mailsession_status_folder(m_nntp->sto_session,0,&target_stat.message_count,
258 &target_stat.message_recent,&target_stat.message_unseen);
259}
260
261
262encodedString* NNTPwrapper::fetchRawBody(const RecMailP&mail) {
263 char*target=0;
264 size_t length=0;
265 encodedString*res = 0;
266 mailmessage * mailmsg = 0;
267 int err = mailsession_get_message(m_nntp->sto_session, mail->getNumber(), &mailmsg);
268 err = mailmessage_fetch(mailmsg,&target,&length);
269 if (mailmsg)
270 mailmessage_free(mailmsg);
271 if (target) {
272 res = new encodedString(target,length);
273 }
274 return res;
275}
276
277MAILLIB::ATYPE NNTPwrapper::getType()const {
278 return account->getType();
279}
280
281const QString&NNTPwrapper::getName()const{
282 return account->getAccountName();
283}
284
285void NNTPwrapper::deleteMail(const RecMailP&) {
286}
287
288int NNTPwrapper::deleteAllMail(const FolderP&) {
289}