-rw-r--r-- | noncore/net/mail/libmailwrapper/imapwrapper.cpp | 63 | ||||
-rw-r--r-- | noncore/net/mail/libmailwrapper/imapwrapper.h | 4 |
2 files changed, 38 insertions, 29 deletions
diff --git a/noncore/net/mail/libmailwrapper/imapwrapper.cpp b/noncore/net/mail/libmailwrapper/imapwrapper.cpp index 23c678b..98634a3 100644 --- a/noncore/net/mail/libmailwrapper/imapwrapper.cpp +++ b/noncore/net/mail/libmailwrapper/imapwrapper.cpp @@ -1,44 +1,62 @@ #include <stdlib.h> #include <libetpan/libetpan.h> #include <qpe/global.h> #include "imapwrapper.h" #include "mailtypes.h" #include "logindialog.h" IMAPwrapper::IMAPwrapper( IMAPaccount *a ) : AbstractMail() { account = a; m_imap = 0; + m_Lastmbox = ""; } IMAPwrapper::~IMAPwrapper() { logout(); } +/* to avoid to often select statements in loops etc. + we trust that we are logged in and connection is established!*/ +int IMAPwrapper::selectMbox(const QString&mbox) +{ + if (mbox == m_Lastmbox) { + return MAILIMAP_NO_ERROR; + } + int err = mailimap_select( m_imap, (char*)mbox.latin1()); + if ( err != MAILIMAP_NO_ERROR ) { + qDebug("error selecting mailbox: %s",m_imap->imap_response); + m_Lastmbox = ""; + return err; + } + m_Lastmbox = mbox; + return err; +} + void IMAPwrapper::imap_progress( size_t current, size_t maximum ) { qDebug( "IMAP: %i of %i", current, maximum ); } void IMAPwrapper::login() { const char *server, *user, *pass; uint16_t port; int err = MAILIMAP_NO_ERROR; if (account->getOffline()) return; /* we are connected this moment */ /* TODO: setup a timer holding the line or if connection closed - delete the value */ if (m_imap) { err = mailimap_noop(m_imap); if (err!=MAILIMAP_NO_ERROR) { logout(); } else { mailstream_flush(m_imap->imap_stream); return; } } server = account->getServer().latin1(); @@ -81,69 +99,66 @@ void IMAPwrapper::login() Global::statusMessage(tr("error connecting imap server: %1").arg(failure)); mailimap_free( m_imap ); m_imap = 0; return; } /* login */ err = mailimap_login_simple( m_imap, (char*)user, (char*)pass ); if ( err != MAILIMAP_NO_ERROR ) { Global::statusMessage(tr("error logging in imap server: %1").arg(m_imap->imap_response)); err = mailimap_close( m_imap ); mailimap_free( m_imap ); m_imap = 0; } } void IMAPwrapper::logout() { int err = MAILIMAP_NO_ERROR; if (!m_imap) return; err = mailimap_logout( m_imap ); err = mailimap_close( m_imap ); mailimap_free( m_imap ); m_imap = 0; + m_Lastmbox = ""; } void IMAPwrapper::listMessages(const QString&mailbox,QList<RecMail> &target ) { - const char *mb = 0; int err = MAILIMAP_NO_ERROR; clist *result = 0; clistcell *current; -// mailimap_fetch_att *fetchAtt,*fetchAttFlags,*fetchAttDate,*fetchAttSize; mailimap_fetch_type *fetchType = 0; mailimap_set *set = 0; - mb = mailbox.latin1(); login(); if (!m_imap) { return; } /* select mailbox READONLY for operations */ - err = mailimap_examine( m_imap, (char*)mb); + err = selectMbox(mailbox); if ( err != MAILIMAP_NO_ERROR ) { - Global::statusMessage(tr("error selecting mailbox: %1").arg(m_imap->imap_response)); return; } int last = m_imap->imap_selection_info->sel_exists; if (last == 0) { Global::statusMessage(tr("Mailbox has no mails")); return; } else { Global::statusMessage(tr("Mailbox has %1 mails").arg(last)); } /* the range has to start at 1!!! not with 0!!!! */ set = mailimap_set_new_interval( 1, last ); fetchType = mailimap_fetch_type_new_fetch_att_list_empty(); mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_envelope()); mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_flags()); mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_internaldate()); mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_rfc822_size()); err = mailimap_fetch( m_imap, set, fetchType, &result ); mailimap_set_free( set ); mailimap_fetch_type_free( fetchType ); @@ -368,52 +383,50 @@ RecMail*IMAPwrapper::parse_list_result(mailimap_msg_att* m_att) m->setFlags(mFlags); m->setMsgsize(size); } return m; } RecBody IMAPwrapper::fetchBody(const RecMail&mail) { RecBody body; const char *mb; int err = MAILIMAP_NO_ERROR; clist *result = 0; clistcell *current; mailimap_fetch_att *fetchAtt = 0; mailimap_fetch_type *fetchType = 0; mailimap_set *set = 0; mailimap_body*body_desc = 0; mb = mail.getMbox().latin1(); login(); if (!m_imap) { return body; } - - err = mailimap_select( m_imap, (char*)mb); + err = selectMbox(mail.getMbox()); if ( err != MAILIMAP_NO_ERROR ) { - qDebug("error selecting mailbox: %s",m_imap->imap_response); return body; } /* the range has to start at 1!!! not with 0!!!! */ set = mailimap_set_new_interval( mail.getNumber(),mail.getNumber() ); fetchAtt = mailimap_fetch_att_new_bodystructure(); fetchType = mailimap_fetch_type_new_fetch_att(fetchAtt); err = mailimap_fetch( m_imap, set, fetchType, &result ); mailimap_set_free( set ); mailimap_fetch_type_free( fetchType ); if (err == MAILIMAP_NO_ERROR && (current=clist_begin(result)) ) { mailimap_msg_att * msg_att; msg_att = (mailimap_msg_att*)current->data; mailimap_msg_att_item*item = (mailimap_msg_att_item*)msg_att->att_list->first->data; QValueList<int> path; body_desc = item->att_data.att_static->att_data.att_body; traverseBody(mail,body_desc,body,0,path); } else { qDebug("error fetching body: %s",m_imap->imap_response); } if (result) mailimap_fetch_list_free(result); return body; } @@ -441,67 +454,64 @@ QStringList IMAPwrapper::address_list_to_stringlist(clist*list) if (named_from && (current_address->ad_mailbox_name || current_address->ad_host_name)) { from+="<"; } if (current_address->ad_mailbox_name) { from+=QString(current_address->ad_mailbox_name); from+="@"; } if (current_address->ad_host_name) { from+=QString(current_address->ad_host_name); } if (named_from && (current_address->ad_mailbox_name || current_address->ad_host_name)) { from+=">"; } l.append(QString(from)); if (++count > 99) { break; } } return l; } encodedString*IMAPwrapper::fetchRawPart(const RecMail&mail,const QValueList<int>&path,bool internal_call) { encodedString*res=new encodedString; - const char*mb; int err; mailimap_fetch_type *fetchType; mailimap_set *set; clistcell*current,*cur; mailimap_section_part * section_part = 0; mailimap_section_spec * section_spec = 0; mailimap_section * section = 0; mailimap_fetch_att * fetch_att = 0; login(); if (!m_imap) { return res; } if (!internal_call) { - mb = mail.getMbox().latin1(); - err = mailimap_select( m_imap, (char*)mb); + err = selectMbox(mail.getMbox()); if ( err != MAILIMAP_NO_ERROR ) { - qDebug("error selecting mailbox: %s",m_imap->imap_response); return res; } } set = mailimap_set_new_single(mail.getNumber()); clist*id_list = 0; /* if path == empty then its a request for the whole rfc822 mail and generates a "fetch <id> (body[])" statement on imap server */ if (path.count()>0 ) { id_list = clist_new(); for (unsigned j=0; j < path.count();++j) { uint32_t * p_id = (uint32_t *)malloc(sizeof(*p_id)); *p_id = path[j]; clist_append(id_list,p_id); } section_part = mailimap_section_part_new(id_list); section_spec = mailimap_section_spec_new(MAILIMAP_SECTION_SPEC_SECTION_PART, NULL, section_part, NULL); } section = mailimap_section_new(section_spec); fetch_att = mailimap_fetch_att_new_body_section(section); fetchType = mailimap_fetch_type_new_fetch_att(fetch_att); @@ -752,89 +762,85 @@ void IMAPwrapper::fillBodyFields(RecPart&target_part,mailimap_body_fields*which) if (enc->enc_value) { char*t=enc->enc_value; encoding=QString(enc->enc_value); enc->enc_value=0L; free(t); } } if (which->bd_description) { target_part.setDescription(QString(which->bd_description)); } target_part.setEncoding(encoding); target_part.setSize(which->bd_size); } void IMAPwrapper::deleteMail(const RecMail&mail) { mailimap_flag_list*flist; mailimap_set *set; mailimap_store_att_flags * store_flags; int err; login(); if (!m_imap) { return; } - const char *mb = mail.getMbox().latin1(); - err = mailimap_select( m_imap, (char*)mb); + err = selectMbox(mail.getMbox()); if ( err != MAILIMAP_NO_ERROR ) { - qDebug("error selecting mailbox for delete: %s",m_imap->imap_response); return; } flist = mailimap_flag_list_new_empty(); mailimap_flag_list_add(flist,mailimap_flag_new_deleted()); store_flags = mailimap_store_att_flags_new_set_flags(flist); set = mailimap_set_new_single(mail.getNumber()); err = mailimap_store(m_imap,set,store_flags); mailimap_set_free( set ); mailimap_store_att_flags_free(store_flags); if (err != MAILIMAP_NO_ERROR) { qDebug("error deleting mail: %s",m_imap->imap_response); return; } qDebug("deleting mail: %s",m_imap->imap_response); /* should we realy do that at this moment? */ err = mailimap_expunge(m_imap); if (err != MAILIMAP_NO_ERROR) { qDebug("error deleting mail: %s",m_imap->imap_response); } qDebug("Delete successfull %s",m_imap->imap_response); } void IMAPwrapper::answeredMail(const RecMail&mail) { mailimap_flag_list*flist; mailimap_set *set; mailimap_store_att_flags * store_flags; int err; login(); if (!m_imap) { return; } - const char *mb = mail.getMbox().latin1(); - err = mailimap_select( m_imap, (char*)mb); + err = selectMbox(mail.getMbox()); if ( err != MAILIMAP_NO_ERROR ) { - qDebug("error selecting mailbox for mark: %s",m_imap->imap_response); return; } flist = mailimap_flag_list_new_empty(); mailimap_flag_list_add(flist,mailimap_flag_new_answered()); store_flags = mailimap_store_att_flags_new_add_flags(flist); set = mailimap_set_new_single(mail.getNumber()); err = mailimap_store(m_imap,set,store_flags); mailimap_set_free( set ); mailimap_store_att_flags_free(store_flags); if (err != MAILIMAP_NO_ERROR) { qDebug("error marking mail: %s",m_imap->imap_response); return; } } QString IMAPwrapper::fetchTextPart(const RecMail&mail,const QValueList<int>&path,bool internal_call,const QString&enc) { QString body(""); encodedString*res = fetchRawPart(mail,path,internal_call); encodedString*r = decode_String(res,enc); delete res; if (r) { if (r->Length()>0) { @@ -851,53 +857,53 @@ QString IMAPwrapper::fetchTextPart(const RecMail&mail,const RecPart&part) } encodedString* IMAPwrapper::fetchDecodedPart(const RecMail&mail,const RecPart&part) { encodedString*res = fetchRawPart(mail,part.Positionlist(),false); encodedString*r = decode_String(res,part.Encoding()); delete res; return r; } encodedString* IMAPwrapper::fetchRawPart(const RecMail&mail,const RecPart&part) { return fetchRawPart(mail,part.Positionlist(),false); } int IMAPwrapper::deleteAllMail(const Folder*folder) { login(); if (!m_imap) { return 0; } mailimap_flag_list*flist; mailimap_set *set; mailimap_store_att_flags * store_flags; - int err = mailimap_select( m_imap, folder->getName().latin1()); + int err = selectMbox(folder->getName()); if ( err != MAILIMAP_NO_ERROR ) { - Global::statusMessage(tr("error selecting mailbox: %1").arg(m_imap->imap_response)); return 0; } + int last = m_imap->imap_selection_info->sel_exists; if (last == 0) { Global::statusMessage(tr("Mailbox has no mails!")); return 0; } flist = mailimap_flag_list_new_empty(); mailimap_flag_list_add(flist,mailimap_flag_new_deleted()); store_flags = mailimap_store_att_flags_new_set_flags(flist); set = mailimap_set_new_interval( 1, last ); err = mailimap_store(m_imap,set,store_flags); mailimap_set_free( set ); mailimap_store_att_flags_free(store_flags); if (err != MAILIMAP_NO_ERROR) { Global::statusMessage(tr("error deleting mail: %s").arg(m_imap->imap_response)); return 0; } qDebug("deleting mail: %s",m_imap->imap_response); /* should we realy do that at this moment? */ err = mailimap_expunge(m_imap); if (err != MAILIMAP_NO_ERROR) { Global::statusMessage(tr("error deleting mail: %s").arg(m_imap->imap_response)); return 0; } qDebug("Delete successfull %s",m_imap->imap_response); @@ -1001,51 +1007,52 @@ void IMAPwrapper::storeMessage(const char*msg,size_t length, const QString&folde Global::statusMessage("Error storing mail!"); } } const QString&IMAPwrapper::getType()const { return account->getType(); } const QString&IMAPwrapper::getName()const { qDebug("Get name: %s",account->getAccountName().latin1()); return account->getAccountName(); } encodedString* IMAPwrapper::fetchRawBody(const RecMail&mail) { // dummy QValueList<int> path; return fetchRawPart(mail,path,false); } void IMAPwrapper::mvcpAllMails(Folder*fromFolder,const QString&targetFolder,AbstractMail*targetWrapper,bool moveit) { - qDebug("mvcp mail imap"); if (targetWrapper != this) { AbstractMail::mvcpAllMails(fromFolder,targetFolder,targetWrapper,moveit); qDebug("Using generic"); return; } - qDebug("Using internal"); mailimap_set *set = 0; - - int err = mailimap_select( m_imap, fromFolder->getName().latin1()); + login(); + if (!m_imap) { + return; + } + int err = selectMbox(fromFolder->getName()); if ( err != MAILIMAP_NO_ERROR ) { - Global::statusMessage(tr("error selecting mailbox: %1").arg(m_imap->imap_response)); return; } - int last = m_imap->imap_selection_info->sel_exists; set = mailimap_set_new_interval( 1, last ); err = mailimap_copy(m_imap,set,targetFolder.latin1()); + mailimap_set_free( set ); if ( err != MAILIMAP_NO_ERROR ) { - Global::statusMessage(tr("error copy mails: %1").arg(m_imap->imap_response)); + QString error_msg = tr("error copy mails: %1").arg(m_imap->imap_response); + Global::statusMessage(error_msg); + qDebug(error_msg); return; } - mailimap_set_free( set ); if (moveit) { deleteAllMail(fromFolder); } } diff --git a/noncore/net/mail/libmailwrapper/imapwrapper.h b/noncore/net/mail/libmailwrapper/imapwrapper.h index 07c6210..99986c2 100644 --- a/noncore/net/mail/libmailwrapper/imapwrapper.h +++ b/noncore/net/mail/libmailwrapper/imapwrapper.h @@ -33,42 +33,44 @@ public: virtual void storeMessage(const char*msg,size_t length, const QString&folder); virtual void mvcpAllMails(Folder*fromFolder,const QString&targetFolder,AbstractMail*targetWrapper,bool moveit); virtual RecBody fetchBody(const RecMail&mail); virtual QString fetchTextPart(const RecMail&mail,const RecPart&part); virtual encodedString* fetchDecodedPart(const RecMail&mail,const RecPart&part); virtual encodedString* fetchRawPart(const RecMail&mail,const RecPart&part); virtual encodedString* fetchRawBody(const RecMail&mail); virtual int createMbox(const QString&,const Folder*parentfolder=0,const QString& delemiter="/",bool getsubfolder=false); virtual int deleteMbox(const Folder*folder); static void imap_progress( size_t current, size_t maximum ); virtual void logout(); virtual const QString&getType()const; virtual const QString&getName()const; protected: RecMail*parse_list_result(mailimap_msg_att*); void login(); virtual QString fetchTextPart(const RecMail&mail,const QValueList<int>&path,bool internal_call=false,const QString&enc=""); virtual encodedString*fetchRawPart(const RecMail&mail,const QValueList<int>&path,bool internal_call); - + int selectMbox(const QString&mbox); + void fillSinglePart(RecPart&target_part,mailimap_body_type_1part*Description); void fillSingleTextPart(RecPart&target_part,mailimap_body_type_text*which); void fillSingleBasicPart(RecPart&target_part,mailimap_body_type_basic*which); void fillSingleMsgPart(RecPart&target_part,mailimap_body_type_msg*which); void fillMultiPart(RecPart&target_part,mailimap_body_type_mpart*which); void traverseBody(const RecMail&mail,mailimap_body*body,RecBody&target_body,int current_recursion,QValueList<int>recList,int current_count=1); /* just helpers */ static void fillBodyFields(RecPart&target_part,mailimap_body_fields*which); static QStringList address_list_to_stringlist(clist*list); IMAPaccount *account; mailimap *m_imap; + QString m_Lastmbox; }; #endif |