📄 imapmessage.cpp
字号:
p.dynamicCast <IMAPpart>()->getOrCreateHeader().parse(oss.str());}void IMAPMessage::extract(ref <const part> p, utility::outputStream& os, utility::progressListener* progress, const int start, const int length, const bool headerOnly, const bool peek) const{ ref <const IMAPFolder> folder = m_folder.acquire(); IMAPMessage_literalHandler literalHandler(os, progress); // Construct section identifier std::ostringstream section; section.imbue(std::locale::classic()); if (p != NULL) { ref <const IMAPpart> currentPart = p.dynamicCast <const IMAPpart>(); std::vector <int> numbers; numbers.push_back(currentPart->getNumber()); currentPart = currentPart->getParent(); while (currentPart != NULL) { numbers.push_back(currentPart->getNumber()); currentPart = currentPart->getParent(); } numbers.erase(numbers.end() - 1); for (std::vector <int>::reverse_iterator it = numbers.rbegin() ; it != numbers.rend() ; ++it) { if (it != numbers.rbegin()) section << "."; section << (*it + 1); } } // Build the request text std::ostringstream command; command.imbue(std::locale::classic()); command << "FETCH " << m_num << " BODY"; if (peek) command << ".PEEK"; command << "["; command << section.str(); if (headerOnly) command << ".MIME"; // "MIME" not "HEADER" for parts command << "]"; if (start != 0 || length != -1) command << "<" << start << "." << length << ">"; // Send the request folder.constCast <IMAPFolder>()->m_connection->send(true, command.str(), true); // Get the response utility::auto_ptr <IMAPParser::response> resp (folder.constCast <IMAPFolder>()->m_connection->readResponse(&literalHandler)); if (resp->isBad() || resp->response_done()->response_tagged()-> resp_cond_state()->status() != IMAPParser::resp_cond_state::OK) { throw exceptions::command_error("FETCH", folder.constCast <IMAPFolder>()->m_connection->getParser()->lastLine(), "bad response"); } if (!headerOnly) { // TODO: update the flags (eg. flag "\Seen" may have been set) }}void IMAPMessage::fetch(ref <IMAPFolder> msgFolder, const int options){ ref <IMAPFolder> folder = m_folder.acquire(); if (folder != msgFolder) throw exceptions::folder_not_found(); // Send the request std::vector <int> list; list.push_back(m_num); const string command = IMAPUtils::buildFetchRequest(list, options); folder->m_connection->send(true, command, true); // Get the response utility::auto_ptr <IMAPParser::response> resp(folder->m_connection->readResponse()); if (resp->isBad() || resp->response_done()->response_tagged()-> resp_cond_state()->status() != IMAPParser::resp_cond_state::OK) { throw exceptions::command_error("FETCH", folder->m_connection->getParser()->lastLine(), "bad response"); } const std::vector <IMAPParser::continue_req_or_response_data*>& respDataList = resp->continue_req_or_response_data(); for (std::vector <IMAPParser::continue_req_or_response_data*>::const_iterator it = respDataList.begin() ; it != respDataList.end() ; ++it) { if ((*it)->response_data() == NULL) { throw exceptions::command_error("FETCH", folder->m_connection->getParser()->lastLine(), "invalid response"); } const IMAPParser::message_data* messageData = (*it)->response_data()->message_data(); // We are only interested in responses of type "FETCH" if (messageData == NULL || messageData->type() != IMAPParser::message_data::FETCH) continue; if (static_cast <int>(messageData->number()) != m_num) continue; // Process fetch response for this message processFetchResponse(options, messageData->msg_att()); }}void IMAPMessage::processFetchResponse (const int options, const IMAPParser::msg_att* msgAtt){ ref <IMAPFolder> folder = m_folder.acquire(); // Get message attributes const std::vector <IMAPParser::msg_att_item*> atts = msgAtt->items(); int flags = 0; for (std::vector <IMAPParser::msg_att_item*>::const_iterator it = atts.begin() ; it != atts.end() ; ++it) { switch ((*it)->type()) { case IMAPParser::msg_att_item::FLAGS: { flags |= IMAPUtils::messageFlagsFromFlags((*it)->flag_list()); break; } case IMAPParser::msg_att_item::UID: { std::ostringstream oss; oss.imbue(std::locale::classic()); oss << folder->m_uidValidity << ":" << (*it)->unique_id()->value(); m_uid = oss.str(); break; } case IMAPParser::msg_att_item::ENVELOPE: { if (!(options & folder::FETCH_FULL_HEADER)) { const IMAPParser::envelope* env = (*it)->envelope(); ref <vmime::header> hdr = getOrCreateHeader(); // Date hdr->Date()->setValue(env->env_date()->value()); // Subject text subject; text::decodeAndUnfold(env->env_subject()->value(), &subject); hdr->Subject()->setValue(subject); // From mailboxList from; IMAPUtils::convertAddressList(*(env->env_from()), from); if (!from.isEmpty()) hdr->From()->setValue(*(from.getMailboxAt(0))); // To mailboxList to; IMAPUtils::convertAddressList(*(env->env_to()), to); hdr->To()->setValue(to); // Sender mailboxList sender; IMAPUtils::convertAddressList(*(env->env_sender()), sender); if (!sender.isEmpty()) hdr->Sender()->setValue(*(sender.getMailboxAt(0))); // Reply-to mailboxList replyTo; IMAPUtils::convertAddressList(*(env->env_reply_to()), replyTo); if (!replyTo.isEmpty()) hdr->ReplyTo()->setValue(*(replyTo.getMailboxAt(0))); // Cc mailboxList cc; IMAPUtils::convertAddressList(*(env->env_cc()), cc); if (!cc.isEmpty()) hdr->Cc()->setValue(cc); // Bcc mailboxList bcc; IMAPUtils::convertAddressList(*(env->env_bcc()), bcc); if (!bcc.isEmpty()) hdr->Bcc()->setValue(bcc); } break; } case IMAPParser::msg_att_item::BODY_STRUCTURE: { m_structure = vmime::create <IMAPstructure>((*it)->body()); break; } case IMAPParser::msg_att_item::RFC822_HEADER: { getOrCreateHeader()->parse((*it)->nstring()->value()); break; } case IMAPParser::msg_att_item::RFC822_SIZE: { m_size = (*it)->number()->value(); break; } case IMAPParser::msg_att_item::BODY_SECTION: { if (!(options & folder::FETCH_FULL_HEADER)) { if ((*it)->section()->section_text1() && (*it)->section()->section_text1()->type() == IMAPParser::section_text::HEADER_FIELDS) { header tempHeader; tempHeader.parse((*it)->nstring()->value()); vmime::header& hdr = *getOrCreateHeader(); std::vector <ref <headerField> > fields = tempHeader.getFieldList(); for (std::vector <ref <headerField> >::const_iterator jt = fields.begin() ; jt != fields.end() ; ++jt) { hdr.appendField((*jt)->clone().dynamicCast <headerField>()); } } } break; } case IMAPParser::msg_att_item::INTERNALDATE: case IMAPParser::msg_att_item::RFC822: case IMAPParser::msg_att_item::RFC822_TEXT: case IMAPParser::msg_att_item::BODY: { break; } } } if (options & folder::FETCH_FLAGS) m_flags = flags;}ref <header> IMAPMessage::getOrCreateHeader(){ if (m_header != NULL) return (m_header); else return (m_header = vmime::create <header>());}void IMAPMessage::setFlags(const int flags, const int mode){ ref <IMAPFolder> folder = m_folder.acquire(); if (!folder) throw exceptions::folder_not_found(); else if (folder->m_mode == folder::MODE_READ_ONLY) throw exceptions::illegal_state("Folder is read-only"); // Build the request text std::ostringstream command; command.imbue(std::locale::classic()); command << "STORE " << m_num; switch (mode) { case FLAG_MODE_ADD: command << " +FLAGS"; break; case FLAG_MODE_REMOVE: command << " -FLAGS"; break; default: case FLAG_MODE_SET: command << " FLAGS"; break; } if (m_flags == FLAG_UNDEFINED) // Update local flags only if they command << ".SILENT "; // have been fetched previously else command << " "; std::vector <string> flagList; if (flags & FLAG_REPLIED) flagList.push_back("\\Answered"); if (flags & FLAG_MARKED) flagList.push_back("\\Flagged"); if (flags & FLAG_DELETED) flagList.push_back("\\Deleted"); if (flags & FLAG_SEEN) flagList.push_back("\\Seen"); if (!flagList.empty()) { command << "("; if (flagList.size() >= 2) { std::copy(flagList.begin(), flagList.end() - 1, std::ostream_iterator <string>(command, " ")); } command << *(flagList.end() - 1) << ")"; // Send the request folder->m_connection->send(true, command.str(), true); // Get the response utility::auto_ptr <IMAPParser::response> resp(folder->m_connection->readResponse()); if (resp->isBad() || resp->response_done()->response_tagged()-> resp_cond_state()->status() != IMAPParser::resp_cond_state::OK) { throw exceptions::command_error("STORE", folder->m_connection->getParser()->lastLine(), "bad response"); } // Update the local flags for this message if (m_flags != FLAG_UNDEFINED) { const std::vector <IMAPParser::continue_req_or_response_data*>& respDataList = resp->continue_req_or_response_data(); int newFlags = 0; for (std::vector <IMAPParser::continue_req_or_response_data*>::const_iterator it = respDataList.begin() ; it != respDataList.end() ; ++it) { if ((*it)->response_data() == NULL) continue; const IMAPParser::message_data* messageData = (*it)->response_data()->message_data(); // We are only interested in responses of type "FETCH" if (messageData == NULL || messageData->type() != IMAPParser::message_data::FETCH) continue; // Get message attributes const std::vector <IMAPParser::msg_att_item*> atts = messageData->msg_att()->items(); for (std::vector <IMAPParser::msg_att_item*>::const_iterator it = atts.begin() ; it != atts.end() ; ++it) { if ((*it)->type() == IMAPParser::msg_att_item::FLAGS) newFlags |= IMAPUtils::messageFlagsFromFlags((*it)->flag_list()); } } m_flags = newFlags; } // Notify message flags changed std::vector <int> nums; nums.push_back(m_num); events::messageChangedEvent event (folder, events::messageChangedEvent::TYPE_FLAGS, nums); for (std::list <IMAPFolder*>::iterator it = folder->m_store.acquire()->m_folders.begin() ; it != folder->m_store.acquire()->m_folders.end() ; ++it) { if ((*it)->getFullPath() == folder->m_path) (*it)->notifyMessageChanged(event); } }}} // imap} // net} // vmime
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -