summaryrefslogtreecommitdiff
path: root/noncore/net/mail/libmailwrapper/nntpwrapper.cpp
blob: 798879dfa9f46ca276b2df53d0e2c7d382b762d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "nntpwrapper.h"
#include "logindialog.h"
#include "mailtypes.h"

#include <qfile.h>

#include <stdlib.h>

#include <libetpan/libetpan.h>


#define HARD_MSG_SIZE_LIMIT 5242880

NNTPwrapper::NNTPwrapper( NNTPaccount *a )
: Genericwrapper() {
    account = a;
    m_nntp = NULL;
    msgTempName = a->getFileName()+"_msg_cache";
    last_msg_id = 0;
}

NNTPwrapper::~NNTPwrapper() {
    logout();
    QFile msg_cache(msgTempName);
    if (msg_cache.exists()) {
        msg_cache.remove();
    }
}

void NNTPwrapper::nntp_progress( size_t current, size_t maximum ) {
    qDebug( "NNTP: %i of %i", current, maximum );
}


RecBody NNTPwrapper::fetchBody( const RecMail &mail ) {
    int err = NEWSNNTP_NO_ERROR;
    char *message = 0;
    size_t length = 0;

    login();
    if ( !m_nntp ) {
        return RecBody();
    }

    RecBody body;
    mailmessage * mailmsg;
    if (mail.Msgsize()>HARD_MSG_SIZE_LIMIT) {
        qDebug("Message to large: %i",mail.Msgsize());
        return body;
    }

    QFile msg_cache(msgTempName);

    cleanMimeCache();

    if (mail.getNumber()!=last_msg_id) {
        if (msg_cache.exists()) {
            msg_cache.remove();
        }
        msg_cache.open(IO_ReadWrite|IO_Truncate);
        last_msg_id = mail.getNumber();
        err = mailsession_get_message(m_nntp->sto_session, mail.getNumber(), &mailmsg);
        err = mailmessage_fetch(mailmsg,&message,&length);
        msg_cache.writeBlock(message,length);
    } else {
        QString msg="";
        msg_cache.open(IO_ReadOnly);
        message = new char[4096];
        memset(message,0,4096);
        while (msg_cache.readBlock(message,4095)>0) {
            msg+=message;
            memset(message,0,4096);
        }
        delete message;
        message = (char*)malloc(msg.length()+1*sizeof(char));
        memset(message,0,msg.length()+1);
        memcpy(message,msg.latin1(),msg.length());
        /* transform to libetpan stuff */
        mailmsg = mailmessage_new();
        mailmessage_init(mailmsg, NULL, data_message_driver, 0, strlen(message));
        generic_message_t * msg_data;
        msg_data = (generic_message_t *)mailmsg->msg_data;
        msg_data->msg_fetched = 1;
        msg_data->msg_message = message;
        msg_data->msg_length = strlen(message);
    }
    body = parseMail(mailmsg);

    /* clean up */
    if (mailmsg)
        mailmessage_free(mailmsg);
    if (message)
        free(message);

    return body;
}


void NNTPwrapper::listMessages(const QString & which, QList<RecMail> &target )
{
    login();
    if (!m_nntp)
        return;
    uint32_t res_messages,res_recent,res_unseen;
    mailsession_status_folder(m_nntp->sto_session,(char*)which.latin1(),&res_messages,&res_recent,&res_unseen);
    parseList(target,m_nntp->sto_session,which);
}

void NNTPwrapper::login()
{
    if (account->getOffline())
        return;
    /* we'll hold the line */
    if ( m_nntp != NULL )
        return;

    const char *server, *user, *pass;
    QString User,Pass;
    uint16_t port;
    int err = NEWSNNTP_NO_ERROR;

    server = account->getServer().latin1();
    port = account->getPort().toUInt();

    user = pass = 0;

    if ( ( account->getUser().isEmpty() || account->getPassword().isEmpty() ) && account->getLogin() ) {
        LoginDialog login( account->getUser(), account->getPassword(), NULL, 0, true );
        login.show();
        if ( QDialog::Accepted == login.exec() ) {
            // ok
            User = login.getUser().latin1();
            Pass = login.getPassword().latin1();
        } else {
            // cancel
            qDebug( "NNTP: Login canceled" );
            return;
        }
    } else {
        User = account->getUser().latin1();
        Pass = account->getPassword().latin1();
    }

    if (User.isEmpty()) {
        user=0;
        pass = 0;
    } else {
        user=User.latin1();
        pass=Pass.latin1();
    }
    //  bool ssl = account->getSSL();

    m_nntp=mailstorage_new(NULL);

    int conntypeset = account->ConnectionType();
    int conntype = 0;
    if ( conntypeset == 3 ) {
        conntype = CONNECTION_TYPE_COMMAND;
    } else if ( conntypeset == 2 ) {
        conntype = CONNECTION_TYPE_TLS;
    } else if ( conntypeset == 1 ) {
        conntype = CONNECTION_TYPE_STARTTLS;
    } else if ( conntypeset == 0 ) {
        conntype = CONNECTION_TYPE_TRY_STARTTLS;
    }

   nntp_mailstorage_init(m_nntp,(char*)server, port, NULL, CONNECTION_TYPE_PLAIN, NNTP_AUTH_TYPE_PLAIN,
                          (char*)user,(char*)pass,0,0,0);

    err = mailstorage_connect( m_nntp );

   if (err != NEWSNNTP_NO_ERROR) {
        qDebug( QString( "FEHLERNUMMER %1" ).arg(  err ) );
     //   Global::statusMessage(tr("Error initializing folder"));
        mailstorage_free(m_nntp);
        m_nntp = 0;

    }

}

void NNTPwrapper::logout()
{
    int err = NEWSNNTP_NO_ERROR;
    if ( m_nntp == NULL )
        return;
    mailstorage_free(m_nntp);
   m_nntp = 0;
}

QList<Folder>* NNTPwrapper::listFolders() {

    QList<Folder> * folders = new QList<Folder>();
    folders->setAutoDelete( false );
    QStringList groups;
    if (account) {
        groups = account->getGroups();
    }
    for ( QStringList::Iterator it = groups.begin(); it != groups.end(); ++it ) {
        folders->append(new Folder((*it),"."));
    }
    return folders;
}

  clist *  NNTPwrapper::listAllNewsgroups() {
 login();
    clist *result = 0;
    clistcell *current;
    newsnntp_group_description *list;
  if ( m_nntp )   {
  mailsession * session = m_nntp->sto_session;
 newsnntp * news =  ( (  nntp_session_state_data * )session->sess_data )->nntp_session;
   int err = newsnntp_list_newsgroups(news, NULL, &result);

   if ( err == NEWSNNTP_NO_ERROR ) {
        return result;
   }
 }
}

void NNTPwrapper::answeredMail(const RecMail&) {}

void NNTPwrapper::statusFolder(folderStat&target_stat,const QString&) {
    login();
    target_stat.message_count = 0;
    target_stat.message_unseen = 0;
    target_stat.message_recent = 0;
    if (!m_nntp)
        return;
    int r = mailsession_status_folder(m_nntp->sto_session,0,&target_stat.message_count,
                                      &target_stat.message_recent,&target_stat.message_unseen);
}


encodedString* NNTPwrapper::fetchRawBody(const RecMail&mail) {
    char*target=0;
    size_t length=0;
    encodedString*res = 0;
    mailmessage * mailmsg = 0;
    int err = mailsession_get_message(m_nntp->sto_session, mail.getNumber(), &mailmsg);
    err = mailmessage_fetch(mailmsg,&target,&length);
    if (mailmsg)
        mailmessage_free(mailmsg);
    if (target) {
        res = new encodedString(target,length);
    }
    return res;
}

const QString&NNTPwrapper::getType()const {
    return account->getType();
}

const QString&NNTPwrapper::getName()const{
    return account->getAccountName();
}

void NNTPwrapper::deleteMail(const RecMail&mail) {
}

int NNTPwrapper::deleteAllMail(const Folder*) {
}