00001 #include "nntpwrapper.h"
00002 #include "logindialog.h"
00003 #include "mailtypes.h"
00004
00005 #include <qfile.h>
00006
00007 #include <stdlib.h>
00008
00009 #include <libetpan/libetpan.h>
00010
00011 #include <opie2/odebug.h>
00012
00013 #define HARD_MSG_SIZE_LIMIT 5242880
00014
00015 using namespace Opie::Core;
00016 NNTPwrapper::NNTPwrapper( NNTPaccount *a )
00017 : Genericwrapper() {
00018 account = a;
00019 m_nntp = NULL;
00020 msgTempName = a->getFileName()+"_msg_cache";
00021 last_msg_id = 0;
00022 }
00023
00024 NNTPwrapper::~NNTPwrapper() {
00025 logout();
00026 QFile msg_cache(msgTempName);
00027 if (msg_cache.exists()) {
00028 msg_cache.remove();
00029 }
00030 }
00031
00032 void NNTPwrapper::nntp_progress( size_t current, size_t maximum ) {
00033 odebug << "NNTP: " << current << " of " << maximum << "" << oendl;
00034 }
00035
00036
00037 RecBodyP NNTPwrapper::fetchBody( const RecMailP &mail ) {
00038 int err = NEWSNNTP_NO_ERROR;
00039 char *message = 0;
00040 size_t length = 0;
00041
00042 RecBodyP body = new RecBody();
00043 login();
00044 if ( !m_nntp ) {
00045 return body;
00046 }
00047
00048 mailmessage * mailmsg;
00049 if (mail->Msgsize()>HARD_MSG_SIZE_LIMIT) {
00050 odebug << "Message to large: " << mail->Msgsize() << "" << oendl;
00051 return body;
00052 }
00053
00054 QFile msg_cache(msgTempName);
00055
00056 cleanMimeCache();
00057
00058 if (mail->getNumber()!=last_msg_id) {
00059 if (msg_cache.exists()) {
00060 msg_cache.remove();
00061 }
00062 msg_cache.open(IO_ReadWrite|IO_Truncate);
00063 last_msg_id = mail->getNumber();
00064 err = mailsession_get_message(m_nntp->sto_session, mail->getNumber(), &mailmsg);
00065 err = mailmessage_fetch(mailmsg,&message,&length);
00066 msg_cache.writeBlock(message,length);
00067 } else {
00068 QString msg="";
00069 msg_cache.open(IO_ReadOnly);
00070 message = new char[4096];
00071 memset(message,0,4096);
00072 while (msg_cache.readBlock(message,4095)>0) {
00073 msg+=message;
00074 memset(message,0,4096);
00075 }
00076 delete message;
00077 message = (char*)malloc(msg.length()+1*sizeof(char));
00078 memset(message,0,msg.length()+1);
00079 memcpy(message,msg.latin1(),msg.length());
00080
00081 mailmsg = mailmessage_new();
00082 mailmessage_init(mailmsg, NULL, data_message_driver, 0, strlen(message));
00083 generic_message_t * msg_data;
00084 msg_data = (generic_message_t *)mailmsg->msg_data;
00085 msg_data->msg_fetched = 1;
00086 msg_data->msg_message = message;
00087 msg_data->msg_length = strlen(message);
00088 }
00089 body = parseMail(mailmsg);
00090
00091
00092 if (mailmsg)
00093 mailmessage_free(mailmsg);
00094 if (message)
00095 free(message);
00096
00097 return body;
00098 }
00099
00100
00101 void NNTPwrapper::listMessages(const QString & which, QValueList<Opie::Core::OSmartPointer<RecMail> > &target )
00102 {
00103 login();
00104 if (!m_nntp)
00105 return;
00106 uint32_t res_messages,res_recent,res_unseen;
00107 mailsession_status_folder(m_nntp->sto_session,(char*)which.latin1(),&res_messages,&res_recent,&res_unseen);
00108 parseList(target,m_nntp->sto_session,which,true);
00109 }
00110
00111 void NNTPwrapper::login()
00112 {
00113 if (account->getOffline())
00114 return;
00115
00116 if ( m_nntp != NULL )
00117 return;
00118
00119 const char *server, *user, *pass;
00120 QString User,Pass;
00121 uint16_t port;
00122 int err = NEWSNNTP_NO_ERROR;
00123
00124 server = account->getServer().latin1();
00125 port = account->getPort().toUInt();
00126
00127 user = pass = 0;
00128
00129 if ( ( account->getUser().isEmpty() || account->getPassword().isEmpty() ) && account->getLogin() ) {
00130 LoginDialog login( account->getUser(), account->getPassword(), NULL, 0, true );
00131 login.show();
00132 if ( QDialog::Accepted == login.exec() ) {
00133
00134 User = login.getUser().latin1();
00135 Pass = login.getPassword().latin1();
00136 } else {
00137
00138 odebug << "NNTP: Login canceled" << oendl;
00139 return;
00140 }
00141 } else {
00142 User = account->getUser().latin1();
00143 Pass = account->getPassword().latin1();
00144 }
00145
00146 if (User.isEmpty()) {
00147 user=0;
00148 pass = 0;
00149 } else {
00150 user=User.latin1();
00151 pass=Pass.latin1();
00152 }
00153
00154
00155 m_nntp=mailstorage_new(NULL);
00156
00157 int conntypeset = account->ConnectionType();
00158 int conntype = 0;
00159 if ( conntypeset == 3 ) {
00160 conntype = CONNECTION_TYPE_COMMAND;
00161 } else if ( conntypeset == 2 ) {
00162 conntype = CONNECTION_TYPE_TLS;
00163 } else if ( conntypeset == 1 ) {
00164 conntype = CONNECTION_TYPE_STARTTLS;
00165 } else if ( conntypeset == 0 ) {
00166 conntype = CONNECTION_TYPE_TRY_STARTTLS;
00167 }
00168
00169 nntp_mailstorage_init(m_nntp,(char*)server, port, NULL, CONNECTION_TYPE_PLAIN, NNTP_AUTH_TYPE_PLAIN,
00170 (char*)user,(char*)pass,0,0,0);
00171
00172 err = mailstorage_connect( m_nntp );
00173
00174 if (err != NEWSNNTP_NO_ERROR) {
00175 odebug << QString( "FEHLERNUMMER %1" ).arg( err ) << oendl;
00176
00177 mailstorage_free(m_nntp);
00178 m_nntp = 0;
00179
00180 } else {
00181 mailsession * session = m_nntp->sto_session;
00182 newsnntp * news = ( ( nntp_session_state_data * )session->sess_data )->nntp_session;
00183 news->nntp_progr_fun = &nntp_progress;
00184 }
00185
00186 }
00187
00188 void NNTPwrapper::logout()
00189 {
00190 int err = NEWSNNTP_NO_ERROR;
00191 if ( m_nntp == NULL )
00192 return;
00193 mailstorage_free(m_nntp);
00194 m_nntp = 0;
00195 }
00196
00197 QValueList<Opie::Core::OSmartPointer<Folder> >* NNTPwrapper::listFolders() {
00198
00199 QValueList<Opie::Core::OSmartPointer<Folder> >* folders = new QValueList<Opie::Core::OSmartPointer<Folder> >();
00200 QStringList groups;
00201 if (account) {
00202 groups = account->getGroups();
00203 }
00204 for ( QStringList::Iterator it = groups.begin(); it != groups.end(); ++it ) {
00205 folders->append(new Folder((*it),"."));
00206 }
00207 return folders;
00208 }
00209
00210
00211
00212
00213
00214
00215
00216 QStringList NNTPwrapper::listAllNewsgroups(const QString&mask) {
00217 login();
00218 QStringList res;
00219 clist *result = 0;
00220 clistcell *current = 0;
00221 newsnntp_group_description *group;
00222
00223 if ( m_nntp ) {
00224 mailsession * session = m_nntp->sto_session;
00225 newsnntp * news = ( ( nntp_session_state_data * )session->sess_data )->nntp_session;
00226 int err = NEWSNNTP_NO_ERROR;
00227 if (mask.isEmpty()) {
00228 err = newsnntp_list(news, &result);
00229 } else {
00230
00231 QString nmask = mask+".*";
00232 err = newsnntp_list_active(news, nmask.latin1(), &result);
00233 }
00234 if ( err == NEWSNNTP_NO_ERROR && result) {
00235 for ( current=clist_begin(result);current!=NULL;current=clist_next(current) ) {
00236 group = ( newsnntp_group_description* ) current->data;
00237 if (!group||!group->grp_name||strlen(group->grp_name)==0) continue;
00238 res.append(group->grp_name);
00239 }
00240 }
00241 }
00242 if (result) {
00243 newsnntp_list_free(result);
00244 }
00245 return res;
00246 }
00247
00248 void NNTPwrapper::answeredMail(const RecMailP&) {}
00249
00250 void NNTPwrapper::statusFolder(folderStat&target_stat,const QString&) {
00251 login();
00252 target_stat.message_count = 0;
00253 target_stat.message_unseen = 0;
00254 target_stat.message_recent = 0;
00255 if (!m_nntp)
00256 return;
00257 int r = mailsession_status_folder(m_nntp->sto_session,0,&target_stat.message_count,
00258 &target_stat.message_recent,&target_stat.message_unseen);
00259 }
00260
00261
00262 encodedString* NNTPwrapper::fetchRawBody(const RecMailP&mail) {
00263 char*target=0;
00264 size_t length=0;
00265 encodedString*res = 0;
00266 mailmessage * mailmsg = 0;
00267 int err = mailsession_get_message(m_nntp->sto_session, mail->getNumber(), &mailmsg);
00268 err = mailmessage_fetch(mailmsg,&target,&length);
00269 if (mailmsg)
00270 mailmessage_free(mailmsg);
00271 if (target) {
00272 res = new encodedString(target,length);
00273 }
00274 return res;
00275 }
00276
00277 MAILLIB::ATYPE NNTPwrapper::getType()const {
00278 return account->getType();
00279 }
00280
00281 const QString&NNTPwrapper::getName()const{
00282 return account->getAccountName();
00283 }
00284
00285 void NNTPwrapper::deleteMail(const RecMailP&) {
00286 }
00287
00288 int NNTPwrapper::deleteAllMail(const FolderP&) {
00289 }