📄 imapmessage.cpp
字号:
//// VMime library (http://www.vmime.org)// Copyright (C) 2002-2008 Vincent Richard <vincent@vincent-richard.net>//// This program is free software; you can redistribute it and/or// modify it under the terms of the GNU General Public License as// published by the Free Software Foundation; either version 2 of// the License, or (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// General Public License for more details.//// You should have received a copy of the GNU General Public License along// with this program; if not, write to the Free Software Foundation, Inc.,// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.//// Linking this library statically or dynamically with other modules is making// a combined work based on this library. Thus, the terms and conditions of// the GNU General Public License cover the whole combination.//#include "vmime/net/imap/IMAPParser.hpp"#include "vmime/net/imap/IMAPMessage.hpp"#include "vmime/net/imap/IMAPFolder.hpp"#include "vmime/net/imap/IMAPStore.hpp"#include "vmime/net/imap/IMAPConnection.hpp"#include "vmime/net/imap/IMAPUtils.hpp"#include <sstream>#include <iterator>#include <typeinfo>namespace vmime {namespace net {namespace imap {//// IMAPpart//class IMAPstructure;class IMAPpart : public part{private: friend class vmime::creator; IMAPpart(ref <IMAPpart> parent, const int number, const IMAPParser::body_type_mpart* mpart); IMAPpart(ref <IMAPpart> parent, const int number, const IMAPParser::body_type_1part* part);public: ref <const structure> getStructure() const; ref <structure> getStructure(); ref <const IMAPpart> getParent() const { return m_parent.acquire(); } const mediaType& getType() const { return (m_mediaType); } int getSize() const { return (m_size); } int getNumber() const { return (m_number); } ref <const header> getHeader() const { if (m_header == NULL) throw exceptions::unfetched_object(); else return m_header; } static ref <IMAPpart> create (ref <IMAPpart> parent, const int number, const IMAPParser::body* body) { if (body->body_type_mpart()) { ref <IMAPpart> part = vmime::create <IMAPpart>(parent, number, body->body_type_mpart()); part->m_structure = vmime::create <IMAPstructure>(part, body->body_type_mpart()->list()); return part; } else { return vmime::create <IMAPpart>(parent, number, body->body_type_1part()); } } header& getOrCreateHeader() { if (m_header != NULL) return (*m_header); else return (*(m_header = vmime::create <header>())); }private: ref <IMAPstructure> m_structure; weak_ref <IMAPpart> m_parent; ref <header> m_header; int m_number; int m_size; mediaType m_mediaType;};//// IMAPstructure//class IMAPstructure : public structure{public: IMAPstructure() { } IMAPstructure(const IMAPParser::body* body) { m_parts.push_back(IMAPpart::create(NULL, 0, body)); } IMAPstructure(ref <IMAPpart> parent, const std::vector <IMAPParser::body*>& list) { int number = 0; for (std::vector <IMAPParser::body*>::const_iterator it = list.begin() ; it != list.end() ; ++it, ++number) { m_parts.push_back(IMAPpart::create(parent, number, *it)); } } ref <const part> getPartAt(const int x) const { return m_parts[x]; } ref <part> getPartAt(const int x) { return m_parts[x]; } int getPartCount() const { return m_parts.size(); } static ref <IMAPstructure> emptyStructure() { return (m_emptyStructure); }private: static ref <IMAPstructure> m_emptyStructure; std::vector <ref <IMAPpart> > m_parts;};ref <IMAPstructure> IMAPstructure::m_emptyStructure = vmime::create <IMAPstructure>();IMAPpart::IMAPpart(ref <IMAPpart> parent, const int number, const IMAPParser::body_type_mpart* mpart) : m_parent(parent), m_header(NULL), m_number(number), m_size(0){ m_mediaType = vmime::mediaType ("multipart", mpart->media_subtype()->value());}IMAPpart::IMAPpart(ref <IMAPpart> parent, const int number, const IMAPParser::body_type_1part* part) : m_parent(parent), m_header(NULL), m_number(number), m_size(0){ if (part->body_type_text()) { m_mediaType = vmime::mediaType ("text", part->body_type_text()-> media_text()->media_subtype()->value()); m_size = part->body_type_text()->body_fields()->body_fld_octets()->value(); } else if (part->body_type_msg()) { m_mediaType = vmime::mediaType ("message", part->body_type_msg()-> media_message()->media_subtype()->value()); } else { m_mediaType = vmime::mediaType (part->body_type_basic()->media_basic()->media_type()->value(), part->body_type_basic()->media_basic()->media_subtype()->value()); m_size = part->body_type_basic()->body_fields()->body_fld_octets()->value(); } m_structure = NULL;}ref <const structure> IMAPpart::getStructure() const{ if (m_structure != NULL) return (m_structure); else return (IMAPstructure::emptyStructure());}ref <structure> IMAPpart::getStructure(){ if (m_structure != NULL) return (m_structure); else return (IMAPstructure::emptyStructure());}#ifndef VMIME_BUILDING_DOC//// IMAPMessage_literalHandler//class IMAPMessage_literalHandler : public IMAPParser::literalHandler{public: IMAPMessage_literalHandler(utility::outputStream& os, utility::progressListener* progress) : m_os(os), m_progress(progress) { } target* targetFor(const IMAPParser::component& comp, const int /* data */) { if (typeid(comp) == typeid(IMAPParser::msg_att_item)) { const int type = static_cast <const IMAPParser::msg_att_item&>(comp).type(); if (type == IMAPParser::msg_att_item::BODY_SECTION || type == IMAPParser::msg_att_item::RFC822_TEXT) { return new targetStream(m_progress, m_os); } } return (NULL); }private: utility::outputStream& m_os; utility::progressListener* m_progress;};#endif // VMIME_BUILDING_DOC//// IMAPMessage//IMAPMessage::IMAPMessage(ref <IMAPFolder> folder, const int num) : m_folder(folder), m_num(num), m_size(-1), m_flags(FLAG_UNDEFINED), m_expunged(false), m_structure(NULL){ folder->registerMessage(this);}IMAPMessage::~IMAPMessage(){ ref <IMAPFolder> folder = m_folder.acquire(); if (folder) folder->unregisterMessage(this);}void IMAPMessage::onFolderClosed(){ m_folder = NULL;}int IMAPMessage::getNumber() const{ return (m_num);}const message::uid IMAPMessage::getUniqueId() const{ return (m_uid);}int IMAPMessage::getSize() const{ if (m_size == -1) throw exceptions::unfetched_object(); return (m_size);}bool IMAPMessage::isExpunged() const{ return (m_expunged);}int IMAPMessage::getFlags() const{ if (m_flags == FLAG_UNDEFINED) throw exceptions::unfetched_object(); return (m_flags);}ref <const structure> IMAPMessage::getStructure() const{ if (m_structure == NULL) throw exceptions::unfetched_object(); return m_structure;}ref <structure> IMAPMessage::getStructure(){ if (m_structure == NULL) throw exceptions::unfetched_object(); return m_structure;}ref <const header> IMAPMessage::getHeader() const{ if (m_header == NULL) throw exceptions::unfetched_object(); return (m_header);}void IMAPMessage::extract(utility::outputStream& os, utility::progressListener* progress, const int start, const int length, const bool peek) const{ ref <const IMAPFolder> folder = m_folder.acquire(); if (!folder) throw exceptions::folder_not_found(); extract(NULL, os, progress, start, length, false, peek);}void IMAPMessage::extractPart (ref <const part> p, utility::outputStream& os, utility::progressListener* progress, const int start, const int length, const bool peek) const{ ref <const IMAPFolder> folder = m_folder.acquire(); if (!folder) throw exceptions::folder_not_found(); extract(p, os, progress, start, length, false, peek);}void IMAPMessage::fetchPartHeader(ref <part> p){ ref <IMAPFolder> folder = m_folder.acquire(); if (!folder) throw exceptions::folder_not_found(); std::ostringstream oss; utility::outputStreamAdapter ossAdapter(oss); extract(p, ossAdapter, NULL, 0, -1, true, true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -