-rw-r--r-- | noncore/net/mail/imapwrapper.cpp | 126 | ||||
-rw-r--r-- | noncore/net/mail/imapwrapper.h | 5 | ||||
-rw-r--r-- | noncore/net/mail/libmailwrapper/imapwrapper.cpp | 126 | ||||
-rw-r--r-- | noncore/net/mail/libmailwrapper/imapwrapper.h | 5 |
4 files changed, 106 insertions, 156 deletions
diff --git a/noncore/net/mail/imapwrapper.cpp b/noncore/net/mail/imapwrapper.cpp index 406c57c..f7e93aa 100644 --- a/noncore/net/mail/imapwrapper.cpp +++ b/noncore/net/mail/imapwrapper.cpp @@ -15,107 +15,114 @@ IMAPwrapper::IMAPwrapper( IMAPaccount *a ) IMAPwrapper::~IMAPwrapper() { logout(); } 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; /* 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(); port = account->getPort().toUInt(); if ( account->getUser().isEmpty() || account->getPassword().isEmpty() ) { LoginDialog login( account->getUser(), account->getPassword(), NULL, 0, true ); login.show(); if ( QDialog::Accepted == login.exec() ) { // ok user = strdup( login.getUser().latin1() ); pass = strdup( login.getPassword().latin1() ); } else { // cancel qDebug( "IMAP: Login canceled" ); return; } } else { user = account->getUser().latin1(); pass = account->getPassword().latin1(); } m_imap = mailimap_new( 20, &imap_progress ); + /* connect */ if (account->getSSL()) { err = mailimap_ssl_connect( m_imap, (char*)server, port ); } else { err = mailimap_socket_connect( m_imap, (char*)server, port ); } if ( err != MAILIMAP_NO_ERROR && err != MAILIMAP_NO_ERROR_AUTHENTICATED && err != MAILIMAP_NO_ERROR_NON_AUTHENTICATED ) { - Global::statusMessage(tr("error connecting imap server: %1").arg(m_imap->imap_response)); + QString failure = ""; + if (err == MAILIMAP_ERROR_CONNECTION_REFUSED) { + failure="Connection refused"; + } else { + failure="Unknown failure"; + } + 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; } 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); 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; @@ -355,340 +362,309 @@ RecMail*IMAPwrapper::parse_list_result(mailimap_msg_att* m_att) if (mFlags.testBit(FLAG_DELETED) && m) { delete m; m = 0; } if (m) { 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); 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; - if (body_desc->bd_type==MAILIMAP_BODY_1PART) { - searchBodyText(mail,body_desc->bd_data.bd_body_1part,body); - } else if (body_desc->bd_type==MAILIMAP_BODY_MPART) { - qDebug("Mulitpart mail"); - searchBodyText(mail,body_desc->bd_data.bd_body_mpart,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; } -/* this routine is just called when the mail has only ONE part. - for filling the parts of a multi-part-message there are other - routines 'cause we can not simply fetch the whole body. */ -void IMAPwrapper::searchBodyText(const RecMail&mail,mailimap_body_type_1part*mailDescription,RecBody&target_body) -{ - if (!mailDescription) { - return; - } - QString sub,body_text; - RecPart singlePart; - QValueList<int> path; - fillSinglePart(singlePart,mailDescription); - switch (mailDescription->bd_type) { - case MAILIMAP_BODY_TYPE_1PART_MSG: - path.append(1); - body_text = fetchTextPart(mail,path,true,singlePart.Encoding()); - target_body.setBodytext(body_text); - target_body.setDescription(singlePart); - break; - case MAILIMAP_BODY_TYPE_1PART_TEXT: - qDebug("Mediatype single: %s",mailDescription->bd_data.bd_type_text->bd_media_text); - path.append(1); - body_text = fetchTextPart(mail,path,true,singlePart.Encoding()); - target_body.setBodytext(body_text); - target_body.setDescription(singlePart); - break; - case MAILIMAP_BODY_TYPE_1PART_BASIC: - qDebug("Single attachment"); - target_body.setBodytext(""); - target_body.addPart(singlePart); - break; - default: - break; - } - - return; -} - QStringList IMAPwrapper::address_list_to_stringlist(clist*list) { QStringList l; QString from; bool named_from; clistcell *current = NULL; mailimap_address * current_address=NULL; if (!list) { return l; } unsigned int count = 0; for (current=clist_begin(list);current!= NULL;current=clist_next(current)) { from = ""; named_from = false; current_address=(mailimap_address*)current->data; if (current_address->ad_personal_name){ from+=convert_String((const char*)current_address->ad_personal_name); - //from+=QString(current_address->ad_personal_name); from+=" "; named_from = true; } 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; login(); if (!m_imap) { return res; } if (!internal_call) { mb = mail.getMbox().latin1(); err = mailimap_select( m_imap, (char*)mb); 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=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); } mailimap_section_part * section_part = mailimap_section_part_new(id_list); mailimap_section_spec * section_spec = mailimap_section_spec_new(MAILIMAP_SECTION_SPEC_SECTION_PART, NULL, section_part, NULL); mailimap_section * section = mailimap_section_new(section_spec); mailimap_fetch_att * fetch_att = mailimap_fetch_att_new_body_section(section); fetchType = mailimap_fetch_type_new_fetch_att(fetch_att); clist*result = 0; 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*msg_att_item; for(cur = clist_begin(msg_att->att_list) ; cur != NULL ; cur = clist_next(cur)) { msg_att_item = (mailimap_msg_att_item*)clist_content(cur); if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) { if (msg_att_item->att_data.att_static->att_type == MAILIMAP_MSG_ATT_BODY_SECTION) { char*text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part; /* detach - we take over the content */ msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = 0L; res->setContent(text,msg_att_item->att_data.att_static->att_data.att_body_section->sec_length); } } } } else { qDebug("error fetching text: %s",m_imap->imap_response); } if (result) mailimap_fetch_list_free(result); return res; } -void IMAPwrapper::searchBodyText(const RecMail&mail,mailimap_body_type_mpart*mailDescription,RecBody&target_body,int current_recursion,QValueList<int>recList) +/* current_recursion is for recursive calls. + current_count means the position inside the internal loop! */ +void IMAPwrapper::traverseBody(const RecMail&mail,mailimap_body*body,RecBody&target_body, + int current_recursion,QValueList<int>recList,int current_count) { - /* current_recursion is for avoiding ugly mails which has a to deep body-structure */ - if (!mailDescription||current_recursion==10) { + if (!body || current_recursion>=10) { return; } - clistcell*current; - mailimap_body*current_body; - unsigned int count = 0; - for (current=clist_begin(mailDescription->bd_list);current!=0;current=clist_next(current)) { - /* the point in the message */ - ++count; - current_body = (mailimap_body*)current->data; - if (current_body->bd_type==MAILIMAP_BODY_MPART) { + ++current_count; + switch (body->bd_type) { + case MAILIMAP_BODY_1PART: + { QValueList<int>countlist = recList; - countlist.append(count); - searchBodyText(mail,current_body->bd_data.bd_body_mpart,target_body,current_recursion+1,countlist); - } else if (current_body->bd_type==MAILIMAP_BODY_1PART){ + countlist.append(current_count); RecPart currentPart; - fillSinglePart(currentPart,current_body->bd_data.bd_body_1part); - QValueList<int>countlist = recList; - countlist.append(count); - /* important: Check for is NULL 'cause a body can be empty! */ - if (currentPart.Type()=="text" && target_body.Bodytext().isNull() ) { - QString body_text = fetchTextPart(mail,countlist,true,currentPart.Encoding()); - target_body.setDescription(currentPart); - target_body.setBodytext(body_text); - } else { + mailimap_body_type_1part*part1 = body->bd_data.bd_body_1part; QString id(""); + currentPart.setPositionlist(countlist); for (unsigned int j = 0; j < countlist.count();++j) { id+=(j>0?" ":""); id+=QString("%1").arg(countlist[j]); } qDebug("ID= %s",id.latin1()); currentPart.setIdentifier(id); - currentPart.setPositionlist(countlist); + fillSinglePart(currentPart,part1); + /* important: Check for is NULL 'cause a body can be empty! + And we put it only into the mail if it is the FIRST part */ + if (part1->bd_type==MAILIMAP_BODY_TYPE_1PART_TEXT && target_body.Bodytext().isNull() && countlist[0]==1) { + QString body_text = fetchTextPart(mail,countlist,true,currentPart.Encoding()); + target_body.setDescription(currentPart); + target_body.setBodytext(body_text); + } else { target_body.addPart(currentPart); } + if (part1->bd_type==MAILIMAP_BODY_TYPE_1PART_MSG) { + traverseBody(mail,part1->bd_data.bd_type_msg->bd_body,target_body,current_recursion+1,countlist); + } } + break; + case MAILIMAP_BODY_MPART: + { + clistcell*current=0; + mailimap_body*current_body=0; + unsigned int ccount = current_count-1; + mailimap_body_type_mpart*mailDescription = body->bd_data.bd_body_mpart; + for (current=clist_begin(mailDescription->bd_list);current!=0;current=clist_next(current)) { + current_body = (mailimap_body*)current->data; + traverseBody(mail,current_body,target_body,current_recursion+1,recList,ccount); + ++ccount; + } + } + break; + default: + break; } } void IMAPwrapper::fillSinglePart(RecPart&target_part,mailimap_body_type_1part*Description) { if (!Description) { return; } switch (Description->bd_type) { case MAILIMAP_BODY_TYPE_1PART_TEXT: target_part.setType("text"); fillSingleTextPart(target_part,Description->bd_data.bd_type_text); break; case MAILIMAP_BODY_TYPE_1PART_BASIC: fillSingleBasicPart(target_part,Description->bd_data.bd_type_basic); break; case MAILIMAP_BODY_TYPE_1PART_MSG: + target_part.setType("message"); fillSingleMsgPart(target_part,Description->bd_data.bd_type_msg); break; default: break; } } void IMAPwrapper::fillSingleTextPart(RecPart&target_part,mailimap_body_type_text*which) { if (!which) { return; } QString sub; sub = which->bd_media_text; target_part.setSubtype(sub.lower()); target_part.setLines(which->bd_lines); fillBodyFields(target_part,which->bd_fields); } void IMAPwrapper::fillSingleMsgPart(RecPart&target_part,mailimap_body_type_msg*which) { if (!which) { return; } -// QString sub; -// sub = which->bd_media_text; -// target_part.setSubtype(sub.lower()); + target_part.setSubtype("rfc822"); qDebug("Message part"); /* we set this type to text/plain */ - target_part.setType("text"); - target_part.setSubtype("plain"); target_part.setLines(which->bd_lines); fillBodyFields(target_part,which->bd_fields); } void IMAPwrapper::fillSingleBasicPart(RecPart&target_part,mailimap_body_type_basic*which) { if (!which) { return; } QString type,sub; switch (which->bd_media_basic->med_type) { case MAILIMAP_MEDIA_BASIC_APPLICATION: type = "application"; break; case MAILIMAP_MEDIA_BASIC_AUDIO: type = "audio"; break; case MAILIMAP_MEDIA_BASIC_IMAGE: type = "image"; break; case MAILIMAP_MEDIA_BASIC_MESSAGE: type = "message"; break; case MAILIMAP_MEDIA_BASIC_VIDEO: type = "video"; break; case MAILIMAP_MEDIA_BASIC_OTHER: default: if (which->bd_media_basic->med_basic_type) { type = which->bd_media_basic->med_basic_type; } else { type = ""; } break; } if (which->bd_media_basic->med_subtype) { sub = which->bd_media_basic->med_subtype; } else { sub = ""; } qDebug("Type = %s/%s",type.latin1(),sub.latin1()); target_part.setType(type.lower()); target_part.setSubtype(sub.lower()); fillBodyFields(target_part,which->bd_fields); } void IMAPwrapper::fillBodyFields(RecPart&target_part,mailimap_body_fields*which) { diff --git a/noncore/net/mail/imapwrapper.h b/noncore/net/mail/imapwrapper.h index 9b20288..e5846f8 100644 --- a/noncore/net/mail/imapwrapper.h +++ b/noncore/net/mail/imapwrapper.h @@ -1,67 +1,66 @@ #ifndef __IMAPWRAPPER #define __IMAPWRAPPER #include <qlist.h> #include "mailwrapper.h" #include "abstractmail.h" #include <libetpan/clist.h> struct mailimap; +struct mailimap_body; struct mailimap_body_type_1part; struct mailimap_body_type_text; struct mailimap_body_type_basic; struct mailimap_body_type_msg; struct mailimap_body_type_mpart; struct mailimap_body_fields; struct mailimap_msg_att; class encodedString; class IMAPwrapper : public AbstractMail { Q_OBJECT public: IMAPwrapper( IMAPaccount *a ); virtual ~IMAPwrapper(); virtual QList<Folder>* listFolders(); virtual void listMessages(const QString & mailbox,QList<RecMail>&target ); virtual void deleteMail(const RecMail&mail); virtual void answeredMail(const RecMail&mail); virtual int deleteAllMail(const Folder*folder); 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 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 ); protected: RecMail*parse_list_result(mailimap_msg_att*); void login(); void logout(); 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); - void searchBodyText(const RecMail&mail,mailimap_body_type_1part*mailDescription,RecBody&target_body); - void searchBodyText(const RecMail&mail,mailimap_body_type_mpart*mailDescription,RecBody&target_body,int current_recursion=0,QValueList<int>recList=QValueList<int>()); - 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 traverseBody(const RecMail&mail,mailimap_body*body,RecBody&target_body,int current_recursion,QValueList<int>recList,int current_count=0); /* 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; }; #endif diff --git a/noncore/net/mail/libmailwrapper/imapwrapper.cpp b/noncore/net/mail/libmailwrapper/imapwrapper.cpp index 406c57c..f7e93aa 100644 --- a/noncore/net/mail/libmailwrapper/imapwrapper.cpp +++ b/noncore/net/mail/libmailwrapper/imapwrapper.cpp @@ -15,107 +15,114 @@ IMAPwrapper::IMAPwrapper( IMAPaccount *a ) IMAPwrapper::~IMAPwrapper() { logout(); } 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; /* 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(); port = account->getPort().toUInt(); if ( account->getUser().isEmpty() || account->getPassword().isEmpty() ) { LoginDialog login( account->getUser(), account->getPassword(), NULL, 0, true ); login.show(); if ( QDialog::Accepted == login.exec() ) { // ok user = strdup( login.getUser().latin1() ); pass = strdup( login.getPassword().latin1() ); } else { // cancel qDebug( "IMAP: Login canceled" ); return; } } else { user = account->getUser().latin1(); pass = account->getPassword().latin1(); } m_imap = mailimap_new( 20, &imap_progress ); + /* connect */ if (account->getSSL()) { err = mailimap_ssl_connect( m_imap, (char*)server, port ); } else { err = mailimap_socket_connect( m_imap, (char*)server, port ); } if ( err != MAILIMAP_NO_ERROR && err != MAILIMAP_NO_ERROR_AUTHENTICATED && err != MAILIMAP_NO_ERROR_NON_AUTHENTICATED ) { - Global::statusMessage(tr("error connecting imap server: %1").arg(m_imap->imap_response)); + QString failure = ""; + if (err == MAILIMAP_ERROR_CONNECTION_REFUSED) { + failure="Connection refused"; + } else { + failure="Unknown failure"; + } + 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; } 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); 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; @@ -355,340 +362,309 @@ RecMail*IMAPwrapper::parse_list_result(mailimap_msg_att* m_att) if (mFlags.testBit(FLAG_DELETED) && m) { delete m; m = 0; } if (m) { 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); 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; - if (body_desc->bd_type==MAILIMAP_BODY_1PART) { - searchBodyText(mail,body_desc->bd_data.bd_body_1part,body); - } else if (body_desc->bd_type==MAILIMAP_BODY_MPART) { - qDebug("Mulitpart mail"); - searchBodyText(mail,body_desc->bd_data.bd_body_mpart,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; } -/* this routine is just called when the mail has only ONE part. - for filling the parts of a multi-part-message there are other - routines 'cause we can not simply fetch the whole body. */ -void IMAPwrapper::searchBodyText(const RecMail&mail,mailimap_body_type_1part*mailDescription,RecBody&target_body) -{ - if (!mailDescription) { - return; - } - QString sub,body_text; - RecPart singlePart; - QValueList<int> path; - fillSinglePart(singlePart,mailDescription); - switch (mailDescription->bd_type) { - case MAILIMAP_BODY_TYPE_1PART_MSG: - path.append(1); - body_text = fetchTextPart(mail,path,true,singlePart.Encoding()); - target_body.setBodytext(body_text); - target_body.setDescription(singlePart); - break; - case MAILIMAP_BODY_TYPE_1PART_TEXT: - qDebug("Mediatype single: %s",mailDescription->bd_data.bd_type_text->bd_media_text); - path.append(1); - body_text = fetchTextPart(mail,path,true,singlePart.Encoding()); - target_body.setBodytext(body_text); - target_body.setDescription(singlePart); - break; - case MAILIMAP_BODY_TYPE_1PART_BASIC: - qDebug("Single attachment"); - target_body.setBodytext(""); - target_body.addPart(singlePart); - break; - default: - break; - } - - return; -} - QStringList IMAPwrapper::address_list_to_stringlist(clist*list) { QStringList l; QString from; bool named_from; clistcell *current = NULL; mailimap_address * current_address=NULL; if (!list) { return l; } unsigned int count = 0; for (current=clist_begin(list);current!= NULL;current=clist_next(current)) { from = ""; named_from = false; current_address=(mailimap_address*)current->data; if (current_address->ad_personal_name){ from+=convert_String((const char*)current_address->ad_personal_name); - //from+=QString(current_address->ad_personal_name); from+=" "; named_from = true; } 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; login(); if (!m_imap) { return res; } if (!internal_call) { mb = mail.getMbox().latin1(); err = mailimap_select( m_imap, (char*)mb); 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=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); } mailimap_section_part * section_part = mailimap_section_part_new(id_list); mailimap_section_spec * section_spec = mailimap_section_spec_new(MAILIMAP_SECTION_SPEC_SECTION_PART, NULL, section_part, NULL); mailimap_section * section = mailimap_section_new(section_spec); mailimap_fetch_att * fetch_att = mailimap_fetch_att_new_body_section(section); fetchType = mailimap_fetch_type_new_fetch_att(fetch_att); clist*result = 0; 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*msg_att_item; for(cur = clist_begin(msg_att->att_list) ; cur != NULL ; cur = clist_next(cur)) { msg_att_item = (mailimap_msg_att_item*)clist_content(cur); if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) { if (msg_att_item->att_data.att_static->att_type == MAILIMAP_MSG_ATT_BODY_SECTION) { char*text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part; /* detach - we take over the content */ msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = 0L; res->setContent(text,msg_att_item->att_data.att_static->att_data.att_body_section->sec_length); } } } } else { qDebug("error fetching text: %s",m_imap->imap_response); } if (result) mailimap_fetch_list_free(result); return res; } -void IMAPwrapper::searchBodyText(const RecMail&mail,mailimap_body_type_mpart*mailDescription,RecBody&target_body,int current_recursion,QValueList<int>recList) +/* current_recursion is for recursive calls. + current_count means the position inside the internal loop! */ +void IMAPwrapper::traverseBody(const RecMail&mail,mailimap_body*body,RecBody&target_body, + int current_recursion,QValueList<int>recList,int current_count) { - /* current_recursion is for avoiding ugly mails which has a to deep body-structure */ - if (!mailDescription||current_recursion==10) { + if (!body || current_recursion>=10) { return; } - clistcell*current; - mailimap_body*current_body; - unsigned int count = 0; - for (current=clist_begin(mailDescription->bd_list);current!=0;current=clist_next(current)) { - /* the point in the message */ - ++count; - current_body = (mailimap_body*)current->data; - if (current_body->bd_type==MAILIMAP_BODY_MPART) { + ++current_count; + switch (body->bd_type) { + case MAILIMAP_BODY_1PART: + { QValueList<int>countlist = recList; - countlist.append(count); - searchBodyText(mail,current_body->bd_data.bd_body_mpart,target_body,current_recursion+1,countlist); - } else if (current_body->bd_type==MAILIMAP_BODY_1PART){ + countlist.append(current_count); RecPart currentPart; - fillSinglePart(currentPart,current_body->bd_data.bd_body_1part); - QValueList<int>countlist = recList; - countlist.append(count); - /* important: Check for is NULL 'cause a body can be empty! */ - if (currentPart.Type()=="text" && target_body.Bodytext().isNull() ) { - QString body_text = fetchTextPart(mail,countlist,true,currentPart.Encoding()); - target_body.setDescription(currentPart); - target_body.setBodytext(body_text); - } else { + mailimap_body_type_1part*part1 = body->bd_data.bd_body_1part; QString id(""); + currentPart.setPositionlist(countlist); for (unsigned int j = 0; j < countlist.count();++j) { id+=(j>0?" ":""); id+=QString("%1").arg(countlist[j]); } qDebug("ID= %s",id.latin1()); currentPart.setIdentifier(id); - currentPart.setPositionlist(countlist); + fillSinglePart(currentPart,part1); + /* important: Check for is NULL 'cause a body can be empty! + And we put it only into the mail if it is the FIRST part */ + if (part1->bd_type==MAILIMAP_BODY_TYPE_1PART_TEXT && target_body.Bodytext().isNull() && countlist[0]==1) { + QString body_text = fetchTextPart(mail,countlist,true,currentPart.Encoding()); + target_body.setDescription(currentPart); + target_body.setBodytext(body_text); + } else { target_body.addPart(currentPart); } + if (part1->bd_type==MAILIMAP_BODY_TYPE_1PART_MSG) { + traverseBody(mail,part1->bd_data.bd_type_msg->bd_body,target_body,current_recursion+1,countlist); + } } + break; + case MAILIMAP_BODY_MPART: + { + clistcell*current=0; + mailimap_body*current_body=0; + unsigned int ccount = current_count-1; + mailimap_body_type_mpart*mailDescription = body->bd_data.bd_body_mpart; + for (current=clist_begin(mailDescription->bd_list);current!=0;current=clist_next(current)) { + current_body = (mailimap_body*)current->data; + traverseBody(mail,current_body,target_body,current_recursion+1,recList,ccount); + ++ccount; + } + } + break; + default: + break; } } void IMAPwrapper::fillSinglePart(RecPart&target_part,mailimap_body_type_1part*Description) { if (!Description) { return; } switch (Description->bd_type) { case MAILIMAP_BODY_TYPE_1PART_TEXT: target_part.setType("text"); fillSingleTextPart(target_part,Description->bd_data.bd_type_text); break; case MAILIMAP_BODY_TYPE_1PART_BASIC: fillSingleBasicPart(target_part,Description->bd_data.bd_type_basic); break; case MAILIMAP_BODY_TYPE_1PART_MSG: + target_part.setType("message"); fillSingleMsgPart(target_part,Description->bd_data.bd_type_msg); break; default: break; } } void IMAPwrapper::fillSingleTextPart(RecPart&target_part,mailimap_body_type_text*which) { if (!which) { return; } QString sub; sub = which->bd_media_text; target_part.setSubtype(sub.lower()); target_part.setLines(which->bd_lines); fillBodyFields(target_part,which->bd_fields); } void IMAPwrapper::fillSingleMsgPart(RecPart&target_part,mailimap_body_type_msg*which) { if (!which) { return; } -// QString sub; -// sub = which->bd_media_text; -// target_part.setSubtype(sub.lower()); + target_part.setSubtype("rfc822"); qDebug("Message part"); /* we set this type to text/plain */ - target_part.setType("text"); - target_part.setSubtype("plain"); target_part.setLines(which->bd_lines); fillBodyFields(target_part,which->bd_fields); } void IMAPwrapper::fillSingleBasicPart(RecPart&target_part,mailimap_body_type_basic*which) { if (!which) { return; } QString type,sub; switch (which->bd_media_basic->med_type) { case MAILIMAP_MEDIA_BASIC_APPLICATION: type = "application"; break; case MAILIMAP_MEDIA_BASIC_AUDIO: type = "audio"; break; case MAILIMAP_MEDIA_BASIC_IMAGE: type = "image"; break; case MAILIMAP_MEDIA_BASIC_MESSAGE: type = "message"; break; case MAILIMAP_MEDIA_BASIC_VIDEO: type = "video"; break; case MAILIMAP_MEDIA_BASIC_OTHER: default: if (which->bd_media_basic->med_basic_type) { type = which->bd_media_basic->med_basic_type; } else { type = ""; } break; } if (which->bd_media_basic->med_subtype) { sub = which->bd_media_basic->med_subtype; } else { sub = ""; } qDebug("Type = %s/%s",type.latin1(),sub.latin1()); target_part.setType(type.lower()); target_part.setSubtype(sub.lower()); fillBodyFields(target_part,which->bd_fields); } void IMAPwrapper::fillBodyFields(RecPart&target_part,mailimap_body_fields*which) { diff --git a/noncore/net/mail/libmailwrapper/imapwrapper.h b/noncore/net/mail/libmailwrapper/imapwrapper.h index 9b20288..e5846f8 100644 --- a/noncore/net/mail/libmailwrapper/imapwrapper.h +++ b/noncore/net/mail/libmailwrapper/imapwrapper.h @@ -1,67 +1,66 @@ #ifndef __IMAPWRAPPER #define __IMAPWRAPPER #include <qlist.h> #include "mailwrapper.h" #include "abstractmail.h" #include <libetpan/clist.h> struct mailimap; +struct mailimap_body; struct mailimap_body_type_1part; struct mailimap_body_type_text; struct mailimap_body_type_basic; struct mailimap_body_type_msg; struct mailimap_body_type_mpart; struct mailimap_body_fields; struct mailimap_msg_att; class encodedString; class IMAPwrapper : public AbstractMail { Q_OBJECT public: IMAPwrapper( IMAPaccount *a ); virtual ~IMAPwrapper(); virtual QList<Folder>* listFolders(); virtual void listMessages(const QString & mailbox,QList<RecMail>&target ); virtual void deleteMail(const RecMail&mail); virtual void answeredMail(const RecMail&mail); virtual int deleteAllMail(const Folder*folder); 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 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 ); protected: RecMail*parse_list_result(mailimap_msg_att*); void login(); void logout(); 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); - void searchBodyText(const RecMail&mail,mailimap_body_type_1part*mailDescription,RecBody&target_body); - void searchBodyText(const RecMail&mail,mailimap_body_type_mpart*mailDescription,RecBody&target_body,int current_recursion=0,QValueList<int>recList=QValueList<int>()); - 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 traverseBody(const RecMail&mail,mailimap_body*body,RecBody&target_body,int current_recursion,QValueList<int>recList,int current_count=0); /* 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; }; #endif |