diff options
Diffstat (limited to '')
41 files changed, 9215 insertions, 56 deletions
diff --git a/vmime/exception.hpp b/vmime/exception.hpp index 3f4c98ea..c09e8919 100644 --- a/vmime/exception.hpp +++ b/vmime/exception.hpp @@ -338,26 +338,32 @@ public: #if VMIME_HAVE_MESSAGING_FEATURES -/** Base class for exceptions thrown by the messaging module. +/** Base class for exceptions thrown by the networking module. */ -class messaging_exception : public vmime::exception +class net_exception : public vmime::exception { public: - messaging_exception(const string& what, const exception& other = NO_EXCEPTION); - ~messaging_exception() throw(); + net_exception(const string& what, const exception& other = NO_EXCEPTION); + ~net_exception() throw(); exception* clone() const; const char* name() const throw(); }; +/** Alias for 'net_exception' (compatibility with version <= 0.7.1); + * this is deprecated. + */ +typedef net_exception messaging_exception; + + /** Error while connecting to the server: this may be a DNS resolution error * or a connection error (for example, time-out while connecting). */ -class connection_error : public messaging_exception +class connection_error : public net_exception { public: @@ -372,7 +378,7 @@ public: /** Server did not initiated the connection correctly. */ -class connection_greeting_error : public messaging_exception +class connection_greeting_error : public net_exception { public: @@ -394,7 +400,7 @@ private: * or password, or wrong authentication method). */ -class authentication_error : public messaging_exception +class authentication_error : public net_exception { public: @@ -415,7 +421,7 @@ private: /** Option not supported. */ -class unsupported_option : public messaging_exception +class unsupported_option : public net_exception { public: @@ -430,7 +436,7 @@ public: /** No service available for this protocol. */ -class no_service_available : public messaging_exception +class no_service_available : public net_exception { public: @@ -446,7 +452,7 @@ public: * operation (for example, you try to close a folder which is not open). */ -class illegal_state : public messaging_exception +class illegal_state : public net_exception { public: @@ -461,7 +467,7 @@ public: /** Folder not found (does not exist). */ -class folder_not_found : public messaging_exception +class folder_not_found : public net_exception { public: @@ -476,7 +482,7 @@ public: /** Message not found (does not exist). */ -class message_not_found : public messaging_exception +class message_not_found : public net_exception { public: @@ -491,7 +497,7 @@ public: /** Operation not supported by the underlying protocol. */ -class operation_not_supported : public messaging_exception +class operation_not_supported : public net_exception { public: @@ -506,7 +512,7 @@ public: /** The operation timed out (time-out delay is elapsed). */ -class operation_timed_out : public messaging_exception +class operation_timed_out : public net_exception { public: @@ -521,7 +527,7 @@ public: /** The operation has been cancelled. */ -class operation_cancelled : public messaging_exception +class operation_cancelled : public net_exception { public: @@ -537,7 +543,7 @@ public: * the requested object. */ -class unfetched_object : public messaging_exception +class unfetched_object : public net_exception { public: @@ -552,7 +558,7 @@ public: /** The service is not currently connected. */ -class not_connected : public messaging_exception +class not_connected : public net_exception { public: @@ -567,7 +573,7 @@ public: /** The service is already connected (must disconnect before). */ -class already_connected : public messaging_exception +class already_connected : public net_exception { public: @@ -582,7 +588,7 @@ public: /** Illegal operation: cannot run this operation on the object. */ -class illegal_operation : public messaging_exception +class illegal_operation : public net_exception { public: @@ -597,7 +603,7 @@ public: /** Command error: operation failed (this is specific to the underlying protocol). */ -class command_error : public messaging_exception +class command_error : public net_exception { public: @@ -631,7 +637,7 @@ private: /** The server returned an invalid response. */ -class invalid_response : public messaging_exception +class invalid_response : public net_exception { public: @@ -665,7 +671,7 @@ private: /** Partial fetch is not supported by the underlying protocol. */ -class partial_fetch_not_supported : public messaging_exception +class partial_fetch_not_supported : public net_exception { public: @@ -680,7 +686,7 @@ public: /** The URL is malformed. */ -class malformed_url : public messaging_exception +class malformed_url : public net_exception { public: @@ -695,7 +701,7 @@ public: /** Folder name is invalid. */ -class invalid_folder_name : public messaging_exception +class invalid_folder_name : public net_exception { public: diff --git a/vmime/net/authHelper.hpp b/vmime/net/authHelper.hpp new file mode 100644 index 00000000..4ceffc92 --- /dev/null +++ b/vmime/net/authHelper.hpp @@ -0,0 +1,38 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_AUTHHELPER_HPP_INCLUDED +#define VMIME_NET_AUTHHELPER_HPP_INCLUDED + + +#include "vmime/types.hpp" + + +namespace vmime { +namespace net { + + +void hmac_md5(const string& text, const string& key, string& hexDigest); + + +} // net +} // vmime + + +#endif // VMIME_NET_AUTHHELPER_HPP_INCLUDED diff --git a/vmime/net/authenticationInfos.hpp b/vmime/net/authenticationInfos.hpp new file mode 100644 index 00000000..2bc394fd --- /dev/null +++ b/vmime/net/authenticationInfos.hpp @@ -0,0 +1,64 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_AUTHENTICATIONINFOS_HPP_INCLUDED +#define VMIME_NET_AUTHENTICATIONINFOS_HPP_INCLUDED + + +#include "vmime/types.hpp" + + +namespace vmime { +namespace net { + + +/** This class encapsulates user credentials. + */ + +class authenticationInfos : public object +{ +public: + + authenticationInfos(const string& username, const string& password); + authenticationInfos(const authenticationInfos& infos); + + /** Return the user account name. + * + * @return account name + */ + const string& getUsername() const; + + /** Return the user account password. + * + * @return account password + */ + const string& getPassword() const; + +private: + + string m_username; + string m_password; +}; + + +} // net +} // vmime + + +#endif // VMIME_NET_AUTHENTICATIONINFOS_HPP_INCLUDED diff --git a/vmime/net/authenticator.hpp b/vmime/net/authenticator.hpp new file mode 100644 index 00000000..7e58a70b --- /dev/null +++ b/vmime/net/authenticator.hpp @@ -0,0 +1,54 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_AUTHENTICATOR_HPP_INCLUDED +#define VMIME_NET_AUTHENTICATOR_HPP_INCLUDED + + +#include "vmime/types.hpp" +#include "vmime/net/authenticationInfos.hpp" + + +namespace vmime { +namespace net { + + +/** This class is used to obtain user credentials. + */ + +class authenticator : public object +{ +public: + + virtual ~authenticator(); + + /** Called when the service needs to retrieve user credentials. + * It should return the user account name and password. + * + * @return user credentials (user name and password) + */ + virtual const authenticationInfos requestAuthInfos() const = 0; +}; + + +} // net +} // vmime + + +#endif // VMIME_NET_AUTHENTICATOR_HPP_INCLUDED diff --git a/vmime/net/defaultAuthenticator.hpp b/vmime/net/defaultAuthenticator.hpp new file mode 100644 index 00000000..58dccfca --- /dev/null +++ b/vmime/net/defaultAuthenticator.hpp @@ -0,0 +1,60 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_DEFAULTAUTHENTICATOR_HPP_INCLUDED +#define VMIME_NET_DEFAULTAUTHENTICATOR_HPP_INCLUDED + + +#include "vmime/net/authenticator.hpp" +#include "vmime/propertySet.hpp" + + +namespace vmime { +namespace net { + + +class session; + + +/** Default implementation for authenticator. It simply returns + * the credentials set in the session properties (named 'username' + * and 'password'). This is the default implementation used if + * you do not write your own authenticator object. + */ + +class defaultAuthenticator : public authenticator +{ +public: + + defaultAuthenticator(weak_ref <session> session, const string& prefix); + +private: + + weak_ref <session> m_session; + const string m_prefix; + + const authenticationInfos requestAuthInfos() const; +}; + + +} // net +} // vmime + + +#endif // VMIME_NET_DEFAULTAUTHENTICATOR_HPP_INCLUDED diff --git a/vmime/net/events.hpp b/vmime/net/events.hpp new file mode 100644 index 00000000..6c3c91fc --- /dev/null +++ b/vmime/net/events.hpp @@ -0,0 +1,229 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_EVENTS_HPP_INCLUDED +#define VMIME_NET_EVENTS_HPP_INCLUDED + + +#include <vector> + +#include "vmime/utility/path.hpp" + + +namespace vmime { +namespace net { + +class folder; + +namespace events { + + +/** Event about the message count in a folder. + */ + +class messageCountEvent +{ +public: + + enum Types + { + TYPE_ADDED, /**< New messages have been added. */ + TYPE_REMOVED /**< Messages have been expunged (renumbering). */ + }; + + + messageCountEvent(ref <folder> folder, const Types type, const std::vector <int>& nums); + + /** Return the folder in which messages have been added/removed. + * + * @return folder in which message count changed + */ + ref <const folder> getFolder() const; + + /** Return the event type. + * + * @return event type (see messageCountEvent::Types) + */ + const Types getType() const; + + /** Return the numbers of the messages that have been added/removed. + * + * @return a list of message numbers + */ + const std::vector <int>& getNumbers() const; + + /** Dispatch the event to the specified listener. + * + * @param listener listener to notify + */ + void dispatch(class messageCountListener* listener) const; + +private: + + ref <folder> m_folder; + const Types m_type; + std::vector <int> m_nums; +}; + + +/** Listener for events about the message count in a folder. + */ + +class messageCountListener +{ +protected: + + virtual ~messageCountListener() { } + +public: + + virtual void messagesAdded(const messageCountEvent& event) = 0; + virtual void messagesRemoved(const messageCountEvent& event) = 0; +}; + + +/** Event occuring on a message. + */ + +class messageChangedEvent +{ +public: + + enum Types + { + TYPE_FLAGS // flags changed + }; + + + messageChangedEvent(ref <folder> folder, const Types type, const std::vector <int>& nums); + + /** Return the folder in which messages have changed. + * + * @return folder in which message count changed + */ + ref <const folder> getFolder() const; + + /** Return the event type. + * + * @return event type (see messageChangedEvent::Types) + */ + const Types getType() const; + + /** Return the numbers of the messages that have changed. + * + * @return a list of message numbers + */ + const std::vector <int>& getNumbers() const; + + /** Dispatch the event to the specified listener. + * + * @param listener listener to notify + */ + void dispatch(class messageChangedListener* listener) const; + +private: + + ref <folder> m_folder; + const Types m_type; + std::vector <int> m_nums; +}; + + +/** Listener for events occuring on a message. + */ + +class messageChangedListener +{ +protected: + + virtual ~messageChangedListener() { } + +public: + + virtual void messageChanged(const messageChangedEvent& event) = 0; +}; + + +/** Event occuring on a folder. + */ + +class folderEvent +{ +public: + + enum Types + { + TYPE_CREATED, /**< A folder was created. */ + TYPE_DELETED, /**< A folder was deleted. */ + TYPE_RENAMED /**< A folder was renamed. */ + }; + + + folderEvent(ref <folder> folder, const Types type, const utility::path& oldPath, const utility::path& newPath); + + /** Return the folder on which the event occured. + * + * @return folder on which the event occured + */ + ref <const folder> getFolder() const; + + /** Return the event type. + * + * @return event type (see folderEvent::Types) + */ + const Types getType() const; + + /** Dispatch the event to the specified listener. + * + * @param listener listener to notify + */ + void dispatch(class folderListener* listener) const; + +private: + + ref <folder> m_folder; + const Types m_type; + const utility::path m_oldPath; + const utility::path m_newPath; +}; + + +/** Listener for events occuring on a folder. + */ + +class folderListener +{ +protected: + + virtual ~folderListener() { } + +public: + + virtual void folderCreated(const folderEvent& event) = 0; + virtual void folderRenamed(const folderEvent& event) = 0; + virtual void folderDeleted(const folderEvent& event) = 0; +}; + + +} // events +} // net +} // vmime + + +#endif // VMIME_NET_EVENTS_HPP_INCLUDED diff --git a/vmime/net/folder.hpp b/vmime/net/folder.hpp new file mode 100644 index 00000000..68e0173b --- /dev/null +++ b/vmime/net/folder.hpp @@ -0,0 +1,379 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_FOLDER_HPP_INCLUDED +#define VMIME_NET_FOLDER_HPP_INCLUDED + + +#include <vector> + +#include "vmime/types.hpp" +#include "vmime/dateTime.hpp" + +#include "vmime/message.hpp" +#include "vmime/net/message.hpp" +#include "vmime/net/events.hpp" + +#include "vmime/utility/path.hpp" +#include "vmime/utility/stream.hpp" +#include "vmime/utility/progressionListener.hpp" + + +namespace vmime { +namespace net { + + +class store; + + +/** Abstract representation of a folder in a message store. + */ + +class folder : public object +{ +protected: + + folder(const folder&) : object() { } + folder() { } + +public: + + virtual ~folder() { } + + /** Type used for fully qualified path name of a folder. + */ + typedef vmime::utility::path path; + + + /** Open mode. + */ + enum Modes + { + MODE_READ_ONLY, /**< Read-only mode (no modification to folder or messages is possible). */ + MODE_READ_WRITE /**< Full access mode (read and write). */ + }; + + /** Folder types. + */ + enum Types + { + TYPE_CONTAINS_FOLDERS = (1 << 0), /**< Folder can contain folders. */ + TYPE_CONTAINS_MESSAGES = (1 << 1), /**< Folder can contain messages. */ + + TYPE_UNDEFINED = 9999 /**< Used internally (this should not be returned + by the type() function). */ + }; + + /** Folder flags. + */ + enum Flags + { + FLAG_CHILDREN = (1 << 0), /**< Folder contains subfolders. */ + FLAG_NO_OPEN = (1 << 1), /**< Folder cannot be open. */ + + FLAG_UNDEFINED = 9999 /**< Used internally (this should not be returned + by the type() function). */ + }; + + /** Return the type of this folder. + * + * @return folder type (see folder::Types) + */ + virtual const int getType() = 0; + + /** Return the flags of this folder. + * + * @return folder flags (see folder::Flags) + */ + virtual const int getFlags() = 0; + + /** Return the mode in which the folder has been open. + * + * @return folder opening mode (see folder::Modes) + */ + virtual const int getMode() const = 0; + + /** Return the name of this folder. + * + * @return folder name + */ + virtual const folder::path::component getName() const = 0; + + /** Return the fully qualified path name of this folder. + * + * @return absolute path of the folder + */ + virtual const folder::path getFullPath() const = 0; + + /** Open this folder. + * + * @param mode open mode (see folder::Modes) + * @param failIfModeIsNotAvailable if set to false and if the requested mode + * is not available, a more restricted mode will be selected automatically. + * If set to true and if the requested mode is not available, the opening + * will fail. + */ + virtual void open(const int mode, bool failIfModeIsNotAvailable = false) = 0; + + /** Close this folder. + * + * @param expunge if set to true, deleted messages are expunged + */ + virtual void close(const bool expunge) = 0; + + /** Create this folder. + * + * @param type folder type (see folder::Types) + */ + virtual void create(const int type) = 0; + + /** Test whether this folder exists. + * + * @return true if the folder exists, false otherwise + */ + virtual const bool exists() = 0; + + /** Test whether this folder is open. + * + * @return true if the folder is open, false otherwise + */ + virtual const bool isOpen() const = 0; + + /** Get a new reference to a message in this folder. + * + * @param num message sequence number + * @return a new object referencing the specified message + */ + virtual ref <message> getMessage(const int num) = 0; + + /** Get new references to messages in this folder. + * + * @param from sequence number of the first message to get + * @param to sequence number of the last message to get + * @return new objects referencing the specified messages + */ + virtual std::vector <ref <message> > getMessages(const int from = 1, const int to = -1) = 0; + + /** Get new references to messages in this folder. + * + * @param nums sequence numbers of the messages to delete + * @return new objects referencing the specified messages + */ + virtual std::vector <ref <message> > getMessages(const std::vector <int>& nums) = 0; + + /** Return the number of messages in this folder. + * + * @return number of messages in the folder + */ + virtual const int getMessageCount() = 0; + + /** Get a new reference to a sub-folder in this folder. + * + * @param name sub-folder name + * @return a new object referencing the specified folder + */ + virtual ref <folder> getFolder(const folder::path::component& name) = 0; + + /** Get the list of all sub-folders in this folder. + * + * @param recursive if set to true, all the descendant are returned. + * If set to false, only the direct children are returned. + * @return list of sub-folders + */ + virtual std::vector <ref <folder> > getFolders(const bool recursive = false) = 0; + + /** Rename (move) this folder to another location. + * + * @param newPath new path of the folder + */ + virtual void rename(const folder::path& newPath) = 0; + + /** Remove a message in this folder. + * + * @param num sequence number of the message to delete + */ + virtual void deleteMessage(const int num) = 0; + + /** Remove one or more messages from this folder. + * + * @param from sequence number of the first message to delete + * @param to sequence number of the last message to delete + */ + virtual void deleteMessages(const int from = 1, const int to = -1) = 0; + + /** Remove one or more messages from this folder. + * + * @param nums sequence numbers of the messages to delete + */ + virtual void deleteMessages(const std::vector <int>& nums) = 0; + + /** Change the flags for one or more messages in this folder. + * + * @param from sequence number of the first message to modify + * @param to sequence number of the last message to modify + * @param flags set of flags (see message::Flags) + * @param mode indicate how to treat old and new flags (see message::FlagsModes) + */ + virtual void setMessageFlags(const int from, const int to, const int flags, const int mode = message::FLAG_MODE_SET) = 0; + + /** Change the flags for one or more messages in this folder. + * + * @param nums sequence numbers of the messages to modify + * @param flags set of flags (see message::Flags) + * @param mode indicate how to treat old and new flags (see message::FlagsModes) + */ + virtual void setMessageFlags(const std::vector <int>& nums, const int flags, const int mode = message::FLAG_MODE_SET) = 0; + + /** Add a message to this folder. + * + * @param msg message to add (data: header + body) + * @param flags flags for the new message + * @param date date/time for the new message (if NULL, the current time is used) + * @param progress progression listener, or NULL if not used + */ + virtual void addMessage(ref <vmime::message> msg, const int flags = message::FLAG_UNDEFINED, vmime::datetime* date = NULL, utility::progressionListener* progress = NULL) = 0; + + /** Add a message to this folder. + * + * @param is message to add (data: header + body) + * @param size size of the message to add (in bytes) + * @param flags flags for the new message + * @param date date/time for the new message (if NULL, the current time is used) + * @param progress progression listener, or NULL if not used + */ + virtual void addMessage(utility::inputStream& is, const int size, const int flags = message::FLAG_UNDEFINED, vmime::datetime* date = NULL, utility::progressionListener* progress = NULL) = 0; + + /** Copy a message from this folder to another folder. + * + * @param dest destination folder path + * @param num sequence number of the message to copy + */ + virtual void copyMessage(const folder::path& dest, const int num) = 0; + + /** Copy messages from this folder to another folder. + * + * @param dest destination folder path + * @param from sequence number of the first message to copy + * @param to sequence number of the last message to copy + */ + virtual void copyMessages(const folder::path& dest, const int from = 1, const int to = -1) = 0; + + /** Copy messages from this folder to another folder. + * + * @param dest destination folder path + * @param nums sequence numbers of the messages to copy + */ + virtual void copyMessages(const folder::path& dest, const std::vector <int>& nums) = 0; + + /** Request folder status without opening it. + * + * @param count will receive the number of messages in the folder + * @param unseen will receive the number of unseen messages in the folder + */ + virtual void status(int& count, int& unseen) = 0; + + /** Expunge deleted messages. + */ + virtual void expunge() = 0; + + /** Return a new folder object referencing the parent folder of this folder. + * + * @return parent folder object + */ + virtual ref <folder> getParent() = 0; + + /** Return a reference to the store to which this folder belongs. + * + * @return the store object to which this folder is attached + */ + virtual weak_ref <const store> getStore() const = 0; + + /** Return a reference to the store to which this folder belongs. + * + * @return the store object to which this folder is attached + */ + virtual weak_ref <store> getStore() = 0; + + /** Fetchable objects. + */ + enum FetchOptions + { + FETCH_ENVELOPE = (1 << 0), /**< Fetch sender, recipients, date, subject. */ + FETCH_STRUCTURE = (1 << 1), /**< Fetch structure (body parts). */ + FETCH_CONTENT_INFO = (1 << 2), /**< Fetch top-level content type. */ + FETCH_FLAGS = (1 << 3), /**< Fetch message flags. */ + FETCH_SIZE = (1 << 4), /**< Fetch message size (exact or estimated). */ + FETCH_FULL_HEADER = (1 << 5), /**< Fetch full RFC-[2]822 header. */ + FETCH_UID = (1 << 6), /**< Fetch unique identifier (protocol specific). */ + FETCH_IMPORTANCE = (1 << 7), /**< Fetch header fields suitable for use with misc::importanceHelper. */ + + FETCH_CUSTOM = (1 << 16) /**< Reserved for future use. */ + }; + + /** Fetch objects for the specified messages. + * + * @param msg list of message sequence numbers + * @param options objects to fetch (combination of folder::FetchOptions flags) + * @param progress progression listener, or NULL if not used + */ + virtual void fetchMessages(std::vector <ref <message> >& msg, const int options, utility::progressionListener* progress = NULL) = 0; + + /** Fetch objects for the specified message. + * + * @param msg the message + * @param options objects to fetch (combination of folder::FetchOptions flags) + */ + virtual void fetchMessage(ref <message> msg, const int options) = 0; + + /** Return the list of fetchable objects supported by + * the underlying protocol (see folder::FetchOptions). + * + * @return list of supported fetchable objects + */ + virtual const int getFetchCapabilities() const = 0; + + // Event listeners + void addMessageChangedListener(events::messageChangedListener* l); + void removeMessageChangedListener(events::messageChangedListener* l); + + void addMessageCountListener(events::messageCountListener* l); + void removeMessageCountListener(events::messageCountListener* l); + + void addFolderListener(events::folderListener* l); + void removeFolderListener(events::folderListener* l); + +protected: + + void notifyMessageChanged(const events::messageChangedEvent& event); + void notifyMessageCount(const events::messageCountEvent& event); + void notifyFolder(const events::folderEvent& event); + +private: + + std::list <events::messageChangedListener*> m_messageChangedListeners; + std::list <events::messageCountListener*> m_messageCountListeners; + std::list <events::folderListener*> m_folderListeners; +}; + + +} // net +} // vmime + + +#endif // VMIME_NET_FOLDER_HPP_INCLUDED diff --git a/vmime/net/imap/IMAPConnection.hpp b/vmime/net/imap/IMAPConnection.hpp new file mode 100644 index 00000000..542f4869 --- /dev/null +++ b/vmime/net/imap/IMAPConnection.hpp @@ -0,0 +1,116 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_IMAP_IMAPCONNECTION_HPP_INCLUDED +#define VMIME_NET_IMAP_IMAPCONNECTION_HPP_INCLUDED + + +#include "vmime/config.hpp" + +#include "vmime/net/authenticator.hpp" +#include "vmime/net/socket.hpp" +#include "vmime/net/timeoutHandler.hpp" +#include "vmime/net/session.hpp" + +#include "vmime/net/imap/IMAPParser.hpp" + + +namespace vmime { +namespace net { +namespace imap { + + +class IMAPTag; +class IMAPStore; + + +class IMAPConnection : public object +{ +public: + + IMAPConnection(weak_ref <IMAPStore> store, ref <authenticator> auth); + ~IMAPConnection(); + + + void connect(); + const bool isConnected() const; + void disconnect(); + + + enum ProtocolStates + { + STATE_NONE, + STATE_NON_AUTHENTICATED, + STATE_AUTHENTICATED, + STATE_SELECTED, + STATE_LOGOUT + }; + + const ProtocolStates state() const; + void setState(const ProtocolStates state); + + + const char hierarchySeparator() const; + + + void send(bool tag, const string& what, bool end); + void sendRaw(const char* buffer, const int count); + + IMAPParser::response* readResponse(IMAPParser::literalHandler* lh = NULL); + + + ref <const IMAPTag> getTag() const; + ref <const IMAPParser> getParser() const; + + weak_ref <const IMAPStore> getStore() const; + weak_ref <IMAPStore> getStore(); + + ref <session> getSession(); + +private: + + weak_ref <IMAPStore> m_store; + + ref <authenticator> m_auth; + + ref <socket> m_socket; + + ref <IMAPParser> m_parser; + + ref <IMAPTag> m_tag; + + char m_hierarchySeparator; + + ProtocolStates m_state; + + ref <timeoutHandler> m_timeoutHandler; + + + void internalDisconnect(); + + void initHierarchySeparator(); +}; + + +} // imap +} // net +} // vmime + + +#endif // VMIME_NET_IMAP_IMAPCONNECTION_HPP_INCLUDED diff --git a/vmime/net/imap/IMAPFolder.hpp b/vmime/net/imap/IMAPFolder.hpp new file mode 100644 index 00000000..880c9a6e --- /dev/null +++ b/vmime/net/imap/IMAPFolder.hpp @@ -0,0 +1,158 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_IMAP_IMAPFOLDER_HPP_INCLUDED +#define VMIME_NET_IMAP_IMAPFOLDER_HPP_INCLUDED + + +#include <vector> +#include <map> + +#include "vmime/types.hpp" + +#include "vmime/net/folder.hpp" + + +namespace vmime { +namespace net { +namespace imap { + + +class IMAPStore; +class IMAPMessage; +class IMAPConnection; + + +/** IMAP folder implementation. + */ + +class IMAPFolder : public folder +{ +private: + + friend class IMAPStore; + friend class IMAPMessage; + friend class vmime::creator; // vmime::create <IMAPFolder> + + + IMAPFolder(const folder::path& path, IMAPStore* store, const int type = TYPE_UNDEFINED, const int flags = FLAG_UNDEFINED); + IMAPFolder(const IMAPFolder&) : folder() { } + + ~IMAPFolder(); + +public: + + const int getMode() const; + + const int getType(); + + const int getFlags(); + + const folder::path::component getName() const; + const folder::path getFullPath() const; + + void open(const int mode, bool failIfModeIsNotAvailable = false); + void close(const bool expunge); + void create(const int type); + + const bool exists(); + + const bool isOpen() const; + + ref <message> getMessage(const int num); + std::vector <ref <message> > getMessages(const int from = 1, const int to = -1); + std::vector <ref <message> > getMessages(const std::vector <int>& nums); + const int getMessageCount(); + + ref <folder> getFolder(const folder::path::component& name); + std::vector <ref <folder> > getFolders(const bool recursive = false); + + void rename(const folder::path& newPath); + + void deleteMessage(const int num); + void deleteMessages(const int from = 1, const int to = -1); + void deleteMessages(const std::vector <int>& nums); + + void setMessageFlags(const int from, const int to, const int flags, const int mode = message::FLAG_MODE_SET); + void setMessageFlags(const std::vector <int>& nums, const int flags, const int mode = message::FLAG_MODE_SET); + + void addMessage(ref <vmime::message> msg, const int flags = message::FLAG_UNDEFINED, vmime::datetime* date = NULL, utility::progressionListener* progress = NULL); + void addMessage(utility::inputStream& is, const int size, const int flags = message::FLAG_UNDEFINED, vmime::datetime* date = NULL, utility::progressionListener* progress = NULL); + + void copyMessage(const folder::path& dest, const int num); + void copyMessages(const folder::path& dest, const int from = 1, const int to = -1); + void copyMessages(const folder::path& dest, const std::vector <int>& nums); + + void status(int& count, int& unseen); + + void expunge(); + + ref <folder> getParent(); + + weak_ref <const store> getStore() const; + weak_ref <store> getStore(); + + + void fetchMessages(std::vector <ref <message> >& msg, const int options, utility::progressionListener* progress = NULL); + void fetchMessage(ref <message> msg, const int options); + + const int getFetchCapabilities() const; + +private: + + void registerMessage(IMAPMessage* msg); + void unregisterMessage(IMAPMessage* msg); + + void onStoreDisconnected(); + + void onClose(); + + const int testExistAndGetType(); + + void setMessageFlags(const string& set, const int flags, const int mode); + + void copyMessages(const string& set, const folder::path& dest); + + + IMAPStore* m_store; + ref <IMAPConnection> m_connection; + + folder::path m_path; + folder::path::component m_name; + + int m_mode; + bool m_open; + + int m_type; + int m_flags; + + int m_messageCount; + + int m_uidValidity; + + std::vector <IMAPMessage*> m_messages; +}; + + +} // imap +} // net +} // vmime + + +#endif // VMIME_NET_IMAP_IMAPFOLDER_HPP_INCLUDED diff --git a/vmime/net/imap/IMAPMessage.hpp b/vmime/net/imap/IMAPMessage.hpp new file mode 100644 index 00000000..bac54219 --- /dev/null +++ b/vmime/net/imap/IMAPMessage.hpp @@ -0,0 +1,111 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_IMAP_IMAPMESSAGE_HPP_INCLUDED +#define VMIME_NET_IMAP_IMAPMESSAGE_HPP_INCLUDED + + +#include "vmime/net/message.hpp" +#include "vmime/net/folder.hpp" + +#include "vmime/mailboxList.hpp" + + +namespace vmime { +namespace net { +namespace imap { + + +class IMAPFolder; + + +/** IMAP message implementation. + */ + +class IMAPMessage : public message +{ +private: + + friend class IMAPFolder; + friend class vmime::creator; // vmime::create <IMAPMessage> + + IMAPMessage(IMAPFolder* folder, const int num); + IMAPMessage(const IMAPMessage&) : message() { } + + ~IMAPMessage(); + +public: + + const int getNumber() const; + + const uid getUniqueId() const; + + const int getSize() const; + + const bool isExpunged() const; + + const structure& getStructure() const; + structure& getStructure(); + + ref <const header> getHeader() const; + + const int getFlags() const; + void setFlags(const int flags, const int mode = FLAG_MODE_SET); + + void extract(utility::outputStream& os, utility::progressionListener* progress = NULL, const int start = 0, const int length = -1, const bool peek = false) const; + void extractPart(const part& p, utility::outputStream& os, utility::progressionListener* progress = NULL, const int start = 0, const int length = -1, const bool peek = false) const; + + void fetchPartHeader(part& p); + +private: + + void fetch(IMAPFolder* folder, const int options); + + void processFetchResponse(const int options, const IMAPParser::msg_att* msgAtt); + + void extract(const part* p, utility::outputStream& os, utility::progressionListener* progress, const int start, const int length, const bool headerOnly, const bool peek) const; + + + void convertAddressList(const IMAPParser::address_list& src, mailboxList& dest); + + + ref <header> getOrCreateHeader(); + + + void onFolderClosed(); + + IMAPFolder* m_folder; + + int m_num; + int m_size; + int m_flags; + bool m_expunged; + uid m_uid; + + ref <header> m_header; + ref <structure> m_structure; +}; + + +} // imap +} // net +} // vmime + + +#endif // VMIME_NET_IMAP_IMAPMESSAGE_HPP_INCLUDED diff --git a/vmime/net/imap/IMAPParser.hpp b/vmime/net/imap/IMAPParser.hpp new file mode 100644 index 00000000..090e6ffe --- /dev/null +++ b/vmime/net/imap/IMAPParser.hpp @@ -0,0 +1,5082 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_IMAP_IMAPPARSER_HPP_INCLUDED +#define VMIME_NET_IMAP_IMAPPARSER_HPP_INCLUDED + + +#include "vmime/base.hpp" +#include "vmime/dateTime.hpp" +#include "vmime/charset.hpp" +#include "vmime/exception.hpp" + +#include "vmime/utility/smartPtr.hpp" +#include "vmime/utility/stringUtils.hpp" +#include "vmime/utility/progressionListener.hpp" + +#include "vmime/encoderB64.hpp" +#include "vmime/encoderQP.hpp" + +#include "vmime/platformDependant.hpp" + +#include "vmime/net/timeoutHandler.hpp" +#include "vmime/net/socket.hpp" + +#include "vmime/net/imap/IMAPTag.hpp" + +#include <vector> +#include <stdexcept> + + +//#define DEBUG_RESPONSE 1 + + +#if DEBUG_RESPONSE +# include <iostream> +#endif + + +namespace vmime { +namespace net { +namespace imap { + + +#if DEBUG_RESPONSE + static string DEBUG_RESPONSE_level; + static std::vector <string> DEBUG_RESPONSE_components; + +# define DEBUG_ENTER_COMPONENT(x) \ + DEBUG_RESPONSE_components.push_back(x); \ + std::cout << DEBUG_RESPONSE_level \ + << "(" << DEBUG_RESPONSE_level.length() << ") " \ + << (x) << std::endl; +# define DEBUG_FOUND(x, y) \ + std::cout << "FOUND: " << x << ": " << y << std::endl; +#else +# define DEBUG_ENTER_COMPONENT(x) +# define DEBUG_FOUND(x, y) +#endif + + +class IMAPParser : public object +{ +public: + + IMAPParser(weak_ref <IMAPTag> tag, weak_ref <socket> sok, weak_ref <timeoutHandler> _timeoutHandler) + : m_tag(tag), m_socket(sok), m_progress(NULL), + m_literalHandler(NULL), m_timeoutHandler(_timeoutHandler) + { + } + + + weak_ref <const IMAPTag> tag() const + { + return (m_tag); + } + + + const string lastLine() const + { + // Remove blanks and new lines at the end of the line. + string line(m_lastLine); + + string::const_iterator it = line.end(); + int count = 0; + + while (it != line.begin()) + { + const unsigned char c = *(it - 1); + + if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r')) + break; + + ++count; + --it; + } + + line.resize(line.length() - count); + + return (line); + } + + + + // + // literalHandler : literal content handler + // + + class component; + + class literalHandler + { + public: + + virtual ~literalHandler() { } + + + // Abstract target class + class target + { + protected: + + target(utility::progressionListener* progress) : m_progress(progress) {} + target(const target&) {} + + public: + + virtual ~target() { } + + + utility::progressionListener* progressionListener() { return (m_progress); } + + virtual void putData(const string& chunk) = 0; + + private: + + utility::progressionListener* m_progress; + }; + + + // Target: put in a string + class targetString : public target + { + public: + + targetString(utility::progressionListener* progress, vmime::string& str) + : target(progress), m_string(str) { } + + const vmime::string& string() const { return (m_string); } + vmime::string& string() { return (m_string); } + + + void putData(const vmime::string& chunk) + { + m_string += chunk; + } + + private: + + vmime::string& m_string; + }; + + + // Target: redirect to an output stream + class targetStream : public target + { + public: + + targetStream(utility::progressionListener* progress, utility::outputStream& stream) + : target(progress), m_stream(stream) { } + + const utility::outputStream& stream() const { return (m_stream); } + utility::outputStream& stream() { return (m_stream); } + + + void putData(const string& chunk) + { + m_stream.write(chunk.data(), chunk.length()); + } + + private: + + utility::outputStream& m_stream; + }; + + + // Called when the parser needs to know what to do with a literal + // . comp: the component in which we are at this moment + // . data: data specific to the component (may not be used) + // + // Returns : + // . == NULL to put the literal into the response + // . != NULL to redirect the literal to the specified target + + virtual target* targetFor(const component& comp, const int data) = 0; + }; + + + // + // Base class for a terminal or a non-terminal + // + + class component + { + public: + + component() { } + virtual ~component() { } + + virtual void go(IMAPParser& parser, string& line, string::size_type* currentPos) = 0; + + + const string makeResponseLine(const string& comp, const string& line, + const string::size_type pos) + { +#if DEBUG_RESPONSE + if (pos > line.length()) + std::cout << "WARNING: component::makeResponseLine(): pos > line.length()" << std::endl; +#endif + + string result(line.substr(0, pos)); + result += "[^]"; // indicates current parser position + result += line.substr(pos, line.length()); + if (!comp.empty()) result += " [" + comp + "]"; + + return (result); + } + }; + + + + // + // Parse one character + // + + template <char C> + class one_char : public component + { + public: + + void go(IMAPParser& /* parser */, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT(string("one_char <") + C + ">: current='" + ((*currentPos < line.length() ? line[*currentPos] : '?')) + "'"); + + const string::size_type pos = *currentPos; + + if (pos < line.length() && line[pos] == C) + *currentPos = pos + 1; + else + throw exceptions::invalid_response("", makeResponseLine("", line, pos)); + } + }; + + + // + // SPACE ::= <ASCII SP, space, 0x20> + // + + class SPACE : public component + { + public: + + void go(IMAPParser& /* parser */, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("SPACE"); + + string::size_type pos = *currentPos; + + while (pos < line.length() && (line[pos] == ' ' || line[pos] == '\t')) + ++pos; + + if (pos > *currentPos) + *currentPos = pos; + else + throw exceptions::invalid_response("", makeResponseLine("SPACE", line, pos)); + } + }; + + + // + // CR ::= <ASCII CR, carriage return, 0x0D> + // LF ::= <ASCII LF, line feed, 0x0A> + // CRLF ::= CR LF + // + + class CRLF : public component + { + public: + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("CRLF"); + + string::size_type pos = *currentPos; + + parser.check <SPACE>(line, &pos, true); + + if (pos + 1 < line.length() && + line[pos] == 0x0d && line[pos + 1] == 0x0a) + { + *currentPos = pos + 2; + } + else + { + throw exceptions::invalid_response("", makeResponseLine("CRLF", line, pos)); + } + } + }; + + + // + // SPACE ::= <ASCII SP, space, 0x20> + // CTL ::= <any ASCII control character and DEL, 0x00 - 0x1f, 0x7f> + // CHAR ::= <any 7-bit US-ASCII character except NUL, 0x01 - 0x7f> + // ATOM_CHAR ::= <any CHAR except atom_specials> + // atom_specials ::= "(" / ")" / "{" / SPACE / CTL / list_wildcards / quoted_specials + // list_wildcards ::= "%" / "*" + // quoted_specials ::= <"> / "\" + // + // tag ::= 1*<any ATOM_CHAR except "+"> (named "xtag") + // + + class xtag : public component + { + public: + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("tag"); + + string::size_type pos = *currentPos; + + bool end = false; + + string tagString; + tagString.reserve(10); + + while (!end && pos < line.length()) + { + const unsigned char c = line[pos]; + + switch (c) + { + case '+': + case '(': + case ')': + case '{': + case 0x20: // SPACE + case '%': // list_wildcards + case '*': // list_wildcards + case '"': // quoted_specials + case '\\': // quoted_specials + + end = true; + break; + + default: + + if (c <= 0x1f || c >= 0x7f) + end = true; + else + { + tagString += c; + ++pos; + } + + break; + } + } + + if (tagString == string(*(parser.tag()))) + { + *currentPos = pos; + } + else + { + // Invalid tag + throw exceptions::invalid_response("", makeResponseLine("tag", line, pos)); + } + } + }; + + + // + // digit ::= "0" / digit_nz + // digit_nz ::= "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" + // + // number ::= 1*digit + // ;; Unsigned 32-bit integer + // ;; (0 <= n < 4,294,967,296) + // + + class number : public component + { + public: + + number(const bool nonZero = false) + : m_nonZero(nonZero), m_value(0) + { + } + + void go(IMAPParser& /* parser */, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("number"); + + string::size_type pos = *currentPos; + + bool valid = true; + unsigned int val = 0; + + while (valid && pos < line.length()) + { + const char c = line[pos]; + + if (c >= '0' && c <= '9') + { + val = (val * 10) + (c - '0'); + ++pos; + } + else + { + valid = false; + } + } + + // Check for non-null length (and for non-zero number) + if (!(m_nonZero && val == 0) && pos != *currentPos) + { + m_value = val; + *currentPos = pos; + } + else + { + throw exceptions::invalid_response("", makeResponseLine("number", line, pos)); + } + } + + private: + + const bool m_nonZero; + unsigned int m_value; + + public: + + const unsigned int value() const { return (m_value); } + }; + + + // nz_number ::= digit_nz *digit + // ;; Non-zero unsigned 32-bit integer + // ;; (0 < n < 4,294,967,296) + // + + class nz_number : public number + { + public: + + nz_number() : number(true) + { + } + }; + + + // + // text ::= 1*TEXT_CHAR + // + // CHAR ::= <any 7-bit US-ASCII character except NUL, 0x01 - 0x7f> + // TEXT_CHAR ::= <any CHAR except CR and LF> + // + + class text : public component + { + public: + + text(bool allow8bits = false, const char except = 0) + : m_allow8bits(allow8bits), m_except(except) + { + } + + void go(IMAPParser& /* parser */, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("text"); + + string::size_type pos = *currentPos; + string::size_type len = 0; + + if (m_allow8bits) + { + const unsigned char except = m_except; + + for (bool end = false ; !end && pos < line.length() ; ) + { + const unsigned char c = line[pos]; + + if (c == 0x00 || c == 0x0d || c == 0x0a || c == except) + { + end = true; + } + else + { + ++pos; + ++len; + } + } + } + else + { + const unsigned char except = m_except; + + for (bool end = false ; !end && pos < line.length() ; ) + { + const unsigned char c = line[pos]; + + if (c < 0x01 || c > 0x7f || c == 0x0d || c == 0x0a || c == except) + { + end = true; + } + else + { + ++pos; + ++len; + } + } + } + + if (len != 0) + { + m_value.resize(len); + std::copy(line.begin() + *currentPos, line.begin() + pos, m_value.begin()); + + *currentPos = pos; + } + else + { + throw exceptions::invalid_response("", makeResponseLine("text", line, pos)); + } + } + + private: + + string m_value; + const bool m_allow8bits; + const char m_except; + + public: + + const string& value() const { return (m_value); } + }; + + + class text8 : public text + { + public: + + text8() : text(true) + { + } + }; + + + template <char C> + class text_except : public text + { + public: + + text_except() : text(false, C) + { + } + }; + + + template <char C> + class text8_except : public text + { + public: + + text8_except() : text(true, C) + { + } + }; + + + // + // QUOTED_CHAR ::= <any TEXT_CHAR except quoted_specials> / "\" quoted_specials + // quoted_specials ::= <"> / "\" + // TEXT_CHAR ::= <any CHAR except CR and LF> + // CHAR ::= <any 7-bit US-ASCII character except NUL, 0x01 - 0x7f> + // + + class QUOTED_CHAR : public component + { + public: + + void go(IMAPParser& /* parser */, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("quoted_char"); + + string::size_type pos = *currentPos; + + const unsigned char c = (pos < line.length() ? line[pos] : 0); + + if (c >= 0x01 && c <= 0x7f && // 0x01 - 0x7f + c != '"' && c != '\\' && // quoted_specials + c != '\r' && c != '\n') // CR and LF + { + m_value = c; + *currentPos = pos + 1; + } + else if (c == '\\' && pos + 1 < line.length() && + (line[pos + 1] == '"' || line[pos + 1] == '\\')) + { + m_value = line[pos + 1]; + *currentPos = pos + 2; + } + else + { + throw exceptions::invalid_response("", makeResponseLine("QUOTED_CHAR", line, pos)); + } + } + + private: + + char m_value; + + public: + + const char value() const { return (m_value); } + }; + + + // + // quoted ::= <"> *QUOTED_CHAR <"> + // QUOTED_CHAR ::= <any TEXT_CHAR except quoted_specials> / "\" quoted_specials + // quoted_specials ::= <"> / "\" + // TEXT_CHAR ::= <any CHAR except CR and LF> + // CHAR ::= <any 7-bit US-ASCII character except NUL, 0x01 - 0x7f> + // + + class quoted_text : public component + { + public: + + void go(IMAPParser& /* parser */, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("quoted_text"); + + string::size_type pos = *currentPos; + string::size_type len = 0; + bool valid = false; + + m_value.reserve(line.length() - pos); + + for (bool end = false, quoted = false ; !end && pos < line.length() ; ) + { + const unsigned char c = line[pos]; + + if (quoted) + { + if (c == '"' || c == '\\') + m_value += c; + else + { + m_value += '\\'; + m_value += c; + } + + quoted = false; + + ++pos; + ++len; + } + else + { + if (c == '\\') + { + quoted = true; + + ++pos; + ++len; + } + else if (c == '"') + { + valid = true; + end = true; + } + else if (c >= 0x01 && c <= 0x7f && // CHAR + c != 0x0a && c != 0x0d) // CR and LF + { + m_value += c; + + ++pos; + ++len; + } + else + { + valid = false; + end = true; + } + } + } + + if (valid) + { + *currentPos = pos; + } + else + { + throw exceptions::invalid_response("", makeResponseLine("quoted_text", line, pos)); + } + } + + private: + + string m_value; + + public: + + const string& value() const { return (m_value); } + }; + + + // + // nil ::= "NIL" + // + + class NIL : public component + { + public: + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("NIL"); + + string::size_type pos = *currentPos; + + parser.checkWithArg <special_atom>(line, &pos, "nil"); + + *currentPos = pos; + } + }; + + + // + // string ::= quoted / literal ----> named 'xstring' + // + // nil ::= "NIL" + // quoted ::= <"> *QUOTED_CHAR <"> + // QUOTED_CHAR ::= <any TEXT_CHAR except quoted_specials> / "\" quoted_specials + // quoted_specials ::= <"> / "\" + // TEXT_CHAR ::= <any CHAR except CR and LF> + // CHAR ::= <any 7-bit US-ASCII character except NUL, 0x01 - 0x7f> + // literal ::= "{" number "}" CRLF *CHAR8 + // ;; Number represents the number of CHAR8 octets + // CHAR8 ::= <any 8-bit octet except NUL, 0x01 - 0xff> + // + + class xstring : public component + { + public: + + xstring(const bool canBeNIL = false, component* comp = NULL, const int data = 0) + : m_canBeNIL(canBeNIL), m_component(comp), m_data(data) + { + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("string"); + + string::size_type pos = *currentPos; + + if (m_canBeNIL && + parser.checkWithArg <special_atom>(line, &pos, "nil", true)) + { + // NIL + } + else + { + pos = *currentPos; + + // quoted ::= <"> *QUOTED_CHAR <"> + if (parser.check <one_char <'"'> >(line, &pos, true)) + { + utility::auto_ptr <quoted_text> text(parser.get <quoted_text>(line, &pos)); + parser.check <one_char <'"'> >(line, &pos); + + if (parser.m_literalHandler != NULL) + { + literalHandler::target* target = + parser.m_literalHandler->targetFor(*m_component, m_data); + + if (target != NULL) + { + m_value = "[literal-handler]"; + + const string::size_type length = text->value().length(); + utility::progressionListener* progress = target->progressionListener(); + + if (progress) + { + progress->start(length); + } + + target->putData(text->value()); + + if (progress) + { + progress->progress(length, length); + progress->stop(length); + } + + delete (target); + } + else + { + m_value = text->value(); + } + } + else + { + m_value = text->value(); + } + + DEBUG_FOUND("string[quoted]", "<length=" << m_value.length() << ", value='" << m_value << "'>"); + } + // literal ::= "{" number "}" CRLF *CHAR8 + else + { + parser.check <one_char <'{'> >(line, &pos); + + number* num = parser.get <number>(line, &pos); + + const string::size_type length = num->value(); + delete (num); + + parser.check <one_char <'}'> >(line, &pos); + + parser.check <CRLF>(line, &pos); + + + if (parser.m_literalHandler != NULL) + { + literalHandler::target* target = + parser.m_literalHandler->targetFor(*m_component, m_data); + + if (target != NULL) + { + m_value = "[literal-handler]"; + + parser.m_progress = target->progressionListener(); + parser.readLiteral(*target, length); + parser.m_progress = NULL; + + delete (target); + } + else + { + literalHandler::targetString target(NULL, m_value); + parser.readLiteral(target, length); + } + } + else + { + literalHandler::targetString target(NULL, m_value); + parser.readLiteral(target, length); + } + + line += parser.readLine(); + + DEBUG_FOUND("string[literal]", "<length=" << length << ", value='" << m_value << "'>"); + } + } + + *currentPos = pos; + } + + private: + + bool m_canBeNIL; + string m_value; + + component* m_component; + const int m_data; + + public: + + const string& value() const { return (m_value); } + }; + + + // + // nstring ::= string / nil + // + + class nstring : public xstring + { + public: + + nstring(component* comp = NULL, const int data = 0) + : xstring(true, comp, data) + { + } + }; + + + // + // astring ::= atom / string + // + + class astring : public component + { + public: + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("astring"); + + string::size_type pos = *currentPos; + + xstring* str = NULL; + + if ((str = parser.get <xstring>(line, &pos, true))) + { + m_value = str->value(); + delete (str); + } + else + { + atom* at = parser.get <atom>(line, &pos); + m_value = at->value(); + delete (at); + } + + *currentPos = pos; + } + + private: + + string m_value; + + public: + + const string& value() const { return (m_value); } + }; + + + // + // atom ::= 1*ATOM_CHAR + // + // ATOM_CHAR ::= <any CHAR except atom_specials> + // atom_specials ::= "(" / ")" / "{" / SPACE / CTL / list_wildcards / quoted_specials + // CHAR ::= <any 7-bit US-ASCII character except NUL, 0x01 - 0x7f> + // CTL ::= <any ASCII control character and DEL, 0x00 - 0x1f, 0x7f> + // list_wildcards ::= "%" / "*" + // quoted_specials ::= <"> / "\" + // SPACE ::= <ASCII SP, space, 0x20> + // + + class atom : public component + { + public: + + void go(IMAPParser& /* parser */, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("atom"); + + string::size_type pos = *currentPos; + string::size_type len = 0; + + for (bool end = false ; !end && pos < line.length() ; ) + { + const unsigned char c = line[pos]; + + switch (c) + { + case '(': + case ')': + case '{': + case 0x20: // SPACE + case '%': // list_wildcards + case '*': // list_wildcards + case '"': // quoted_specials + case '\\': // quoted_specials + + case '[': + case ']': // for "special_atom" + + end = true; + break; + + default: + + if (c <= 0x1f || c >= 0x7f) + end = true; + else + { + ++pos; + ++len; + } + } + } + + if (len != 0) + { + m_value.resize(len); + std::copy(line.begin() + *currentPos, line.begin() + pos, m_value.begin()); + + *currentPos = pos; + } + else + { + throw exceptions::invalid_response("", makeResponseLine("atom", line, pos)); + } + } + + private: + + string m_value; + + public: + + const string& value() const { return (m_value); } + }; + + + // + // special atom (eg. "CAPABILITY", "FLAGS", "STATUS"...) + // + // " Except as noted otherwise, all alphabetic characters are case- + // insensitive. The use of upper or lower case characters to define + // token strings is for editorial clarity only. Implementations MUST + // accept these strings in a case-insensitive fashion. " + // + + class special_atom : public atom + { + public: + + special_atom(const char* str) + : m_string(str) // 'string' must be in lower-case + { + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT(string("special_atom(") + m_string + ")"); + + string::size_type pos = *currentPos; + + atom::go(parser, line, &pos); + + const char* cmp = value().c_str(); + const char* with = m_string; + + bool ok = true; + + while (ok && *cmp && *with) + { + ok = (std::tolower(*cmp, std::locale()) == *with); + + ++cmp; + ++with; + } + + if (!ok || *cmp || *with) + { + throw exceptions::invalid_response("", makeResponseLine(string("special_atom <") + m_string + ">", line, pos)); + } + else + { + *currentPos = pos; + } + } + + private: + + const char* m_string; + }; + + + // + // text_mime2 ::= "=?" <charset> "?" <encoding> "?" <encoded-text> "?=" + // ;; Syntax defined in [MIME-HDRS] + // + + class text_mime2 : public component + { + public: + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("text_mime2"); + + string::size_type pos = *currentPos; + + atom* theCharset = NULL, *theEncoding = NULL; + text* theText = NULL; + + try + { + parser.check <one_char <'='> >(line, &pos); + parser.check <one_char <'?'> >(line, &pos); + + theCharset = parser.get <atom>(line, &pos); + + parser.check <one_char <'?'> >(line, &pos); + + theEncoding = parser.get <atom>(line, &pos); + + parser.check <one_char <'?'> >(line, &pos); + + theText = parser.get <text8_except <'?'> >(line, &pos); + + parser.check <one_char <'?'> >(line, &pos); + parser.check <one_char <'='> >(line, &pos); + } + catch (std::exception& e) + { + delete (theCharset); + delete (theEncoding); + delete (theText); + + throw; + } + + m_charset = theCharset->value(); + delete (theCharset); + + // Decode text + encoder* theEncoder = NULL; + + if (theEncoding->value()[0] == 'q' || theEncoding->value()[0] == 'Q') + { + // Quoted-printable + theEncoder = new encoderQP; + theEncoder->getProperties()["rfc2047"] = true; + } + else if (theEncoding->value()[0] == 'b' || theEncoding->value()[0] == 'B') + { + // Base64 + theEncoder = new encoderB64; + } + + if (theEncoder) + { + utility::inputStreamStringAdapter in(theText->value()); + utility::outputStreamStringAdapter out(m_value); + + theEncoder->decode(in, out); + delete (theEncoder); + } + // No decoder available + else + { + m_value = theText->value(); + } + + delete (theEncoding); + delete (theText); + + *currentPos = pos; + } + + private: + + vmime::charset m_charset; + string m_value; + + public: + + const vmime::charset& charset() const { return (m_charset); } + const string& value() const { return (m_value); } + }; + + + // + // flag ::= "\Answered" / "\Flagged" / "\Deleted" / + // "\Seen" / "\Draft" / flag_keyword / flag_extension + // + // flag_extension ::= "\" atom + // ;; Future expansion. Client implementations + // ;; MUST accept flag_extension flags. Server + // ;; implementations MUST NOT generate + // ;; flag_extension flags except as defined by + // ;; future standard or standards-track + // ;; revisions of this specification. + // + // flag_keyword ::= atom + // + + class flag : public component + { + public: + + flag() + : m_flag_keyword(NULL) + { + } + + ~flag() + { + delete (m_flag_keyword); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("flag_keyword"); + + string::size_type pos = *currentPos; + + if (parser.check <one_char <'\\'> >(line, &pos, true)) + { + if (parser.check <one_char <'*'> >(line, &pos, true)) + { + m_type = STAR; + } + else + { + atom* at = parser.get <atom>(line, &pos); + const string name = utility::stringUtils::toLower(at->value()); + delete (at); + + if (name == "answered") + m_type = ANSWERED; + else if (name == "flagged") + m_type = FLAGGED; + else if (name == "deleted") + m_type = DELETED; + else if (name == "seen") + m_type = SEEN; + else if (name == "draft") + m_type = DRAFT; + else + { + m_type = UNKNOWN; + m_name = name; + } + } + } + else + { + m_flag_keyword = parser.get <atom>(line, &pos); + } + + *currentPos = pos; + } + + + enum Type + { + UNKNOWN, + ANSWERED, + FLAGGED, + DELETED, + SEEN, + DRAFT, + STAR // * = custom flags allowed + }; + + private: + + Type m_type; + string m_name; + + IMAPParser::atom* m_flag_keyword; + + public: + + const Type type() const { return (m_type); } + const string& name() const { return (m_name); } + + const IMAPParser::atom* flag_keyword() const { return (m_flag_keyword); } + }; + + + // + // flag_list ::= "(" #flag ")" + // + + class flag_list : public component + { + public: + + ~flag_list() + { + for (std::vector <flag*>::iterator it = m_flags.begin() ; + it != m_flags.end() ; ++it) + { + delete (*it); + } + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("flag_list"); + + string::size_type pos = *currentPos; + + parser.check <one_char <'('> >(line, &pos); + + while (!parser.check <one_char <')'> >(line, &pos, true)) + { + m_flags.push_back(parser.get <flag>(line, &pos)); + parser.check <SPACE>(line, &pos, true); + } + + *currentPos = pos; + } + + private: + + std::vector <flag*> m_flags; + + public: + + const std::vector <flag*>& flags() const { return (m_flags); } + }; + + + // + // mailbox ::= "INBOX" / astring + // ;; INBOX is case-insensitive. All case variants of + // ;; INBOX (e.g. "iNbOx") MUST be interpreted as INBOX + // ;; not as an astring. Refer to section 5.1 for + // ;; further semantic details of mailbox names. + // + + class mailbox : public component + { + public: + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("mailbox"); + + string::size_type pos = *currentPos; + + if (parser.checkWithArg <special_atom>(line, &pos, "inbox", true)) + { + m_type = INBOX; + m_name = "INBOX"; + } + else + { + m_type = OTHER; + + astring* astr = parser.get <astring>(line, &pos); + m_name = astr->value(); + delete (astr); + } + + *currentPos = pos; + } + + + enum Type + { + INBOX, + OTHER + }; + + private: + + Type m_type; + string m_name; + + public: + + const Type type() const { return (m_type); } + const string& name() const { return (m_name); } + }; + + + // + // mailbox_flag := "\Marked" / "\Noinferiors" / + // "\Noselect" / "\Unmarked" / flag_extension + // + + class mailbox_flag : public component + { + public: + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("mailbox_flag"); + + string::size_type pos = *currentPos; + + parser.check <one_char <'\\'> >(line, &pos); + + atom* at = parser.get <atom>(line, &pos); + const string name = utility::stringUtils::toLower(at->value()); + delete (at); + + if (name == "marked") + m_type = MARKED; + else if (name == "noinferiors") + m_type = NOINFERIORS; + else if (name == "noselect") + m_type = NOSELECT; + else if (name == "unmarked") + m_type = UNMARKED; + else + { + m_type = UNKNOWN; + m_name = name; + } + + *currentPos = pos; + } + + + enum Type + { + UNKNOWN, + MARKED, + NOINFERIORS, + NOSELECT, + UNMARKED + }; + + private: + + Type m_type; + string m_name; + + public: + + const Type type() const { return (m_type); } + const string& name() const { return (m_name); } + }; + + + // + // mailbox_flag_list ::= "(" #(mailbox_flag) ")" + // + + class mailbox_flag_list : public component + { + public: + + ~mailbox_flag_list() + { + for (std::vector <mailbox_flag*>::iterator it = m_flags.begin() ; + it != m_flags.end() ; ++it) + { + delete (*it); + } + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("mailbox_flag_list"); + + string::size_type pos = *currentPos; + + parser.check <one_char <'('> >(line, &pos); + + while (!parser.check <one_char <')'> >(line, &pos, true)) + { + m_flags.push_back(parser.get <mailbox_flag>(line, &pos)); + parser.check <SPACE>(line, &pos, true); + } + + *currentPos = pos; + } + + private: + + std::vector <mailbox_flag*> m_flags; + + public: + + const std::vector <mailbox_flag*>& flags() const { return (m_flags); } + }; + + + // + // mailbox_list ::= mailbox_flag_list SPACE + // (<"> QUOTED_CHAR <"> / nil) SPACE mailbox + // + + class mailbox_list : public component + { + public: + + mailbox_list() + : m_mailbox_flag_list(NULL), + m_mailbox(NULL), m_quoted_char('\0') + { + } + + ~mailbox_list() + { + delete (m_mailbox_flag_list); + delete (m_mailbox); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("mailbox_list"); + + string::size_type pos = *currentPos; + + m_mailbox_flag_list = parser.get <IMAPParser::mailbox_flag_list>(line, &pos); + + parser.check <SPACE>(line, &pos); + + if (!parser.check <NIL>(line, &pos, true)) + { + parser.check <one_char <'"'> >(line, &pos); + + QUOTED_CHAR* qc = parser.get <QUOTED_CHAR>(line, &pos); + m_quoted_char = qc->value(); + delete (qc); + + parser.check <one_char <'"'> >(line, &pos); + } + + parser.check <SPACE>(line, &pos); + + m_mailbox = parser.get <IMAPParser::mailbox>(line, &pos); + + *currentPos = pos; + } + + private: + + IMAPParser::mailbox_flag_list* m_mailbox_flag_list; + IMAPParser::mailbox* m_mailbox; + char m_quoted_char; + + public: + + const IMAPParser::mailbox_flag_list* mailbox_flag_list() const { return (m_mailbox_flag_list); } + const IMAPParser::mailbox* mailbox() const { return (m_mailbox); } + const char quoted_char() const { return (m_quoted_char); } + }; + + + // + // resp_text_code ::= "ALERT" / "PARSE" / + // "PERMANENTFLAGS" SPACE "(" #(flag / "\*") ")" / + // "READ-ONLY" / "READ-WRITE" / "TRYCREATE" / + // "UIDVALIDITY" SPACE nz_number / + // "UNSEEN" SPACE nz_number / + // atom [SPACE 1*<any TEXT_CHAR except "]">] + + class resp_text_code : public component + { + public: + + resp_text_code() + : m_nz_number(NULL), m_atom(NULL), m_flag_list(NULL), m_text(NULL) + { + } + + ~resp_text_code() + { + delete (m_nz_number); + delete (m_atom); + delete (m_flag_list); + delete (m_text); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("resp_text_code"); + + string::size_type pos = *currentPos; + + // "ALERT" + if (parser.checkWithArg <special_atom>(line, &pos, "alert", true)) + { + m_type = ALERT; + } + // "PARSE" + else if (parser.checkWithArg <special_atom>(line, &pos, "parse", true)) + { + m_type = PARSE; + } + // "PERMANENTFLAGS" SPACE flag_list + else if (parser.checkWithArg <special_atom>(line, &pos, "permanentflags", true)) + { + m_type = PERMANENTFLAGS; + + parser.check <SPACE>(line, &pos); + + m_flag_list = parser.get <IMAPParser::flag_list>(line, &pos); + } + // "READ-ONLY" + else if (parser.checkWithArg <special_atom>(line, &pos, "read-only", true)) + { + m_type = READ_ONLY; + } + // "READ-WRITE" + else if (parser.checkWithArg <special_atom>(line, &pos, "read-write", true)) + { + m_type = READ_WRITE; + } + // "TRYCREATE" + else if (parser.checkWithArg <special_atom>(line, &pos, "trycreate", true)) + { + m_type = TRYCREATE; + } + // "UIDVALIDITY" SPACE nz_number + else if (parser.checkWithArg <special_atom>(line, &pos, "uidvalidity", true)) + { + m_type = UIDVALIDITY; + + parser.check <SPACE>(line, &pos); + m_nz_number = parser.get <IMAPParser::nz_number>(line, &pos); + } + // "UNSEEN" SPACE nz_number + else if (parser.checkWithArg <special_atom>(line, &pos, "unseen", true)) + { + m_type = UNSEEN; + + parser.check <SPACE>(line, &pos); + m_nz_number = parser.get <IMAPParser::nz_number>(line, &pos); + } + // atom [SPACE 1*<any TEXT_CHAR except "]">] + else + { + m_type = OTHER; + + m_atom = parser.get <IMAPParser::atom>(line, &pos); + + if (parser.check <SPACE>(line, &pos, true)) + m_text = parser.get <text_except <']'> >(line, &pos); + } + + *currentPos = pos; + } + + + enum Type + { + ALERT, + PARSE, + PERMANENTFLAGS, + READ_ONLY, + READ_WRITE, + TRYCREATE, + UIDVALIDITY, + UNSEEN, + OTHER + }; + + private: + + Type m_type; + + IMAPParser::nz_number* m_nz_number; + IMAPParser::atom* m_atom; + IMAPParser::flag_list* m_flag_list; + IMAPParser::text* m_text; + + public: + + const Type type() const { return (m_type); } + + const IMAPParser::nz_number* nz_number() const { return (m_nz_number); } + const IMAPParser::atom* atom() const { return (m_atom); } + const IMAPParser::flag_list* flag_list() const { return (m_flag_list); } + const IMAPParser::text* text() const { return (m_text); } + }; + + + // + // resp_text ::= ["[" resp_text_code "]" SPACE] (text_mime2 / text) + // ;; text SHOULD NOT begin with "[" or "=" + + class resp_text : public component + { + public: + + resp_text() + : m_resp_text_code(NULL) + { + } + + ~resp_text() + { + delete (m_resp_text_code); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("resp_text"); + + string::size_type pos = *currentPos; + + if (parser.check <one_char <'['> >(line, &pos, true)) + { + m_resp_text_code = parser.get <IMAPParser::resp_text_code>(line, &pos); + + parser.check <one_char <']'> >(line, &pos); + parser.check <SPACE>(line, &pos); + } + + text_mime2* text1 = parser.get <text_mime2>(line, &pos, true); + + if (text1 != NULL) + { + m_text = text1->value(); + delete (text1); + } + else + { + IMAPParser::text* text2 = + parser.get <IMAPParser::text>(line, &pos); + + m_text = text2->value(); + delete (text2); + } + + *currentPos = pos; + } + + private: + + IMAPParser::resp_text_code* m_resp_text_code; + string m_text; + + public: + + const IMAPParser::resp_text_code* resp_text_code() const { return (m_resp_text_code); } + const string& text() const { return (m_text); } + }; + + + // + // continue_req ::= "+" SPACE (resp_text / base64) + // + + class continue_req : public component + { + public: + + continue_req() + : m_resp_text(NULL) + { + } + + ~continue_req() + { + delete (m_resp_text); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("continue_req"); + + string::size_type pos = *currentPos; + + parser.check <one_char <'+'> >(line, &pos); + parser.check <SPACE>(line, &pos); + + m_resp_text = parser.get <IMAPParser::resp_text>(line, &pos); + + parser.check <CRLF>(line, &pos); + + *currentPos = pos; + } + + private: + + IMAPParser::resp_text* m_resp_text; + + public: + + const IMAPParser::resp_text* resp_text() const { return (m_resp_text); } + }; + + + // + // auth_type ::= atom + // ;; Defined by [IMAP-AUTH] + // + + class auth_type : public component + { + public: + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("auth_type"); + + atom* at = parser.get <atom>(line, currentPos); + m_name = utility::stringUtils::toLower(at->value()); + delete (at); + + if (m_name == "kerberos_v4") + m_type = KERBEROS_V4; + else if (m_name == "gssapi") + m_type = GSSAPI; + else if (m_name == "skey") + m_type = SKEY; + else + m_type = UNKNOWN; + } + + + enum Type + { + UNKNOWN, + + // RFC 1731 - IMAP4 Authentication Mechanisms + KERBEROS_V4, + GSSAPI, + SKEY + }; + + private: + + Type m_type; + string m_name; + + public: + + const Type type() const { return (m_type); } + const string name() const { return (m_name); } + }; + + + // + // status_att ::= "MESSAGES" / "RECENT" / "UIDNEXT" / + // "UIDVALIDITY" / "UNSEEN" + // + + class status_att : public component + { + public: + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("status_att"); + + string::size_type pos = *currentPos; + + if (parser.checkWithArg <special_atom>(line, &pos, "messages", true)) + { + m_type = MESSAGES; + } + else if (parser.checkWithArg <special_atom>(line, &pos, "recent", true)) + { + m_type = RECENT; + } + else if (parser.checkWithArg <special_atom>(line, &pos, "uidnext", true)) + { + m_type = UIDNEXT; + } + else if (parser.checkWithArg <special_atom>(line, &pos, "uidvalidity", true)) + { + m_type = UIDVALIDITY; + } + else + { + parser.checkWithArg <special_atom>(line, &pos, "unseen"); + m_type = UNSEEN; + } + + *currentPos = pos; + } + + + enum Type + { + MESSAGES, + RECENT, + UIDNEXT, + UIDVALIDITY, + UNSEEN + }; + + private: + + Type m_type; + + public: + + const Type type() const { return (m_type); } + }; + + + // + // capability ::= "AUTH=" auth_type / atom + // ;; New capabilities MUST begin with "X" or be + // ;; registered with IANA as standard or standards-track + // + + class capability : public component + { + public: + + capability() + : m_auth_type(NULL), m_atom(NULL) + { + } + + ~capability() + { + delete (m_auth_type); + delete (m_atom); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("capability"); + + string::size_type pos = *currentPos; + + class atom* at = parser.get <IMAPParser::atom>(line, &pos); + + string value = at->value(); + const char* str = value.c_str(); + + if ((str[0] == 'a' || str[0] == 'A') && + (str[1] == 'u' || str[1] == 'U') && + (str[2] == 't' || str[2] == 'T') && + (str[3] == 'h' || str[3] == 'H') && + (str[4] == '=')) + { + string::size_type pos = 5; + m_auth_type = parser.get <IMAPParser::auth_type>(value, &pos); + delete (at); + } + else + { + m_atom = at; + } + + *currentPos = pos; + } + + private: + + IMAPParser::auth_type* m_auth_type; + IMAPParser::atom* m_atom; + + public: + + const IMAPParser::auth_type* auth_type() const { return (m_auth_type); } + const IMAPParser::atom* atom() const { return (m_atom); } + }; + + + // + // capability_data ::= "CAPABILITY" SPACE [1#capability SPACE] "IMAP4rev1" + // [SPACE 1#capability] + // ;; IMAP4rev1 servers which offer RFC 1730 + // ;; compatibility MUST list "IMAP4" as the first + // ;; capability. + // + + class capability_data : public component + { + public: + + ~capability_data() + { + for (std::vector <capability*>::iterator it = m_capabilities.begin() ; + it != m_capabilities.end() ; ++it) + { + delete (*it); + } + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("capability_data"); + + string::size_type pos = *currentPos; + + parser.checkWithArg <special_atom>(line, &pos, "capability"); + parser.check <SPACE>(line, &pos); + + bool IMAP4rev1 = false; + + for (bool end = false ; !end && !IMAP4rev1 ; ) + { + if (parser.checkWithArg <special_atom>(line, &pos, "imap4rev1", true)) + { + IMAP4rev1 = true; + } + else + { + capability* cap = parser.get <capability>(line, &pos); + end = (cap == NULL); + + if (cap) + { + m_capabilities.push_back(cap); + } + } + + parser.check <SPACE>(line, &pos); + } + + + if (parser.check <SPACE>(line, &pos, true)) + { + for (capability* cap = NULL ; + (cap = parser.get <capability>(line, &pos)) != NULL ; ) + { + m_capabilities.push_back(cap); + + parser.check <SPACE>(line, &pos); + } + } + + *currentPos = pos; + } + + private: + + std::vector <capability*> m_capabilities; + + public: + + const std::vector <capability*>& capabilities() const { return (m_capabilities); } + }; + + + // + // date_day_fixed ::= (SPACE digit) / 2digit + // ;; Fixed-format version of date_day + // + // date_month ::= "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" / + // "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec" + // + // date_year ::= 4digit + // + // time ::= 2digit ":" 2digit ":" 2digit + // ;; Hours minutes seconds + // + // zone ::= ("+" / "-") 4digit + // ;; Signed four-digit value of hhmm representing + // ;; hours and minutes west of Greenwich (that is, + // ;; (the amount that the given time differs from + // ;; Universal Time). Subtracting the timezone + // ;; from the given time will give the UT form. + // ;; The Universal Time zone is "+0000". + // + // date_time ::= <"> date_day_fixed "-" date_month "-" date_year + // SPACE time SPACE zone <"> + // + + class date_time : public component + { + public: + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("date_time"); + + string::size_type pos = *currentPos; + + // <"> date_day_fixed "-" date_month "-" date_year + parser.check <one_char <'"'> >(line, &pos); + parser.check <SPACE>(line, &pos, true); + + utility::auto_ptr <number> nd(parser.get <number>(line, &pos)); + + parser.check <one_char <'-'> >(line, &pos); + + utility::auto_ptr <atom> amo(parser.get <atom>(line, &pos)); + + parser.check <one_char <'-'> >(line, &pos); + + utility::auto_ptr <number> ny(parser.get <number>(line, &pos)); + + parser.check <SPACE>(line, &pos, true); + + // 2digit ":" 2digit ":" 2digit + utility::auto_ptr <number> nh(parser.get <number>(line, &pos)); + + parser.check <one_char <':'> >(line, &pos); + + utility::auto_ptr <number> nmi(parser.get <number>(line, &pos)); + + parser.check <one_char <':'> >(line, &pos); + + utility::auto_ptr <number> ns(parser.get <number>(line, &pos)); + + parser.check <SPACE>(line, &pos, true); + + // ("+" / "-") 4digit + int sign = 1; + + if (!(parser.check <one_char <'+'> >(line, &pos, true))) + parser.check <one_char <'-'> >(line, &pos); + + utility::auto_ptr <number> nz(parser.get <number>(line, &pos)); + + parser.check <one_char <'"'> >(line, &pos); + + + m_datetime.setHour(std::min(std::max(nh->value(), 0u), 23u)); + m_datetime.setMinute(std::min(std::max(nmi->value(), 0u), 59u)); + m_datetime.setSecond(std::min(std::max(ns->value(), 0u), 59u)); + + const int zone = static_cast <int>(nz->value()); + const int zh = zone / 100; // hour offset + const int zm = zone % 100; // minute offset + + m_datetime.setZone(((zh * 60) + zm) * sign); + + m_datetime.setDay(std::min(std::max(nd->value(), 1u), 31u)); + m_datetime.setYear(ny->value()); + + const string month(utility::stringUtils::toLower(amo->value())); + int mon = vmime::datetime::JANUARY; + + if (month.length() >= 3) + { + switch (month[0]) + { + case 'j': + { + switch (month[1]) + { + case 'a': mon = vmime::datetime::JANUARY; break; + case 'u': + { + switch (month[2]) + { + case 'n': mon = vmime::datetime::JUNE; break; + default: mon = vmime::datetime::JULY; break; + } + + break; + } + + } + + break; + } + case 'f': mon = vmime::datetime::FEBRUARY; break; + case 'm': + { + switch (month[2]) + { + case 'r': mon = vmime::datetime::MARCH; break; + default: mon = vmime::datetime::MAY; break; + } + + break; + } + case 'a': + { + switch (month[1]) + { + case 'p': mon = vmime::datetime::APRIL; break; + default: mon = vmime::datetime::AUGUST; break; + } + + break; + } + case 's': mon = vmime::datetime::SEPTEMBER; break; + case 'o': mon = vmime::datetime::OCTOBER; break; + case 'n': mon = vmime::datetime::NOVEMBER; break; + case 'd': mon = vmime::datetime::DECEMBER; break; + } + } + + m_datetime.setMonth(mon); + + *currentPos = pos; + } + + private: + + vmime::datetime m_datetime; + }; + + + // + // header_fld_name ::= astring + // + + typedef astring header_fld_name; + + + // + // header_list ::= "(" 1#header_fld_name ")" + // + + class header_list : public component + { + public: + + ~header_list() + { + for (std::vector <header_fld_name*>::iterator it = m_fld_names.begin() ; + it != m_fld_names.end() ; ++it) + { + delete (*it); + } + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("header_list"); + + string::size_type pos = *currentPos; + + parser.check <one_char <'('> >(line, &pos); + + while (!parser.check <one_char <')'> >(line, &pos, true)) + { + m_fld_names.push_back(parser.get <header_fld_name>(line, &pos)); + parser.check <SPACE>(line, &pos, true); + } + + *currentPos = pos; + } + + private: + + std::vector <header_fld_name*> m_fld_names; + + public: + + const std::vector <header_fld_name*>& fld_names() const { return (m_fld_names); } + }; + + + // + // body_extension ::= nstring / number / "(" 1#body_extension ")" + // ;; Future expansion. Client implementations + // ;; MUST accept body_extension fields. Server + // ;; implementations MUST NOT generate + // ;; body_extension fields except as defined by + // ;; future standard or standards-track + // ;; revisions of this specification. + // + + class body_extension : public component + { + public: + + body_extension() + : m_nstring(NULL), m_number(NULL) + { + } + + ~body_extension() + { + delete (m_nstring); + delete (m_number); + + for (std::vector <body_extension*>::iterator it = m_body_extensions.begin() ; + it != m_body_extensions.end() ; ++it) + { + delete (*it); + } + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + string::size_type pos = *currentPos; + + if (parser.check <one_char <'('> >(line, &pos, true)) + { + m_body_extensions.push_back + (parser.get <body_extension>(line, &pos)); + + while (!parser.check <one_char <')'> >(line, &pos, true)) + m_body_extensions.push_back(parser.get <body_extension>(line, &pos, true)); + } + else + { + if (!(m_nstring = parser.get <IMAPParser::nstring>(line, &pos, true))) + m_number = parser.get <IMAPParser::number>(line, &pos); + } + + *currentPos = pos; + } + + private: + + IMAPParser::nstring* m_nstring; + IMAPParser::number* m_number; + + std::vector <body_extension*> m_body_extensions; + + public: + + IMAPParser::nstring* nstring() const { return (m_nstring); } + IMAPParser::number* number() const { return (m_number); } + + const std::vector <body_extension*>& body_extensions() const { return (m_body_extensions); } + }; + + + // + // section_text ::= "HEADER" / "HEADER.FIELDS" [".NOT"] + // SPACE header_list / "TEXT" / "MIME" + // + + class section_text : public component + { + public: + + section_text() + : m_header_list(NULL) + { + } + + ~section_text() + { + delete (m_header_list); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("section_text"); + + string::size_type pos = *currentPos; + + // "HEADER.FIELDS" [".NOT"] SPACE header_list + const bool b1 = parser.checkWithArg <special_atom>(line, &pos, "header.fields.not", true); + const bool b2 = (b1 ? false : parser.checkWithArg <special_atom>(line, &pos, "header.fields", true)); + + if (b1 || b2) + { + m_type = b1 ? HEADER_FIELDS_NOT : HEADER_FIELDS; + + parser.check <SPACE>(line, &pos); + m_header_list = parser.get <IMAPParser::header_list>(line, &pos); + } + // "HEADER" + else if (parser.checkWithArg <special_atom>(line, &pos, "header", true)) + { + m_type = HEADER; + } + // "MIME" + else if (parser.checkWithArg <special_atom>(line, &pos, "mime", true)) + { + m_type = MIME; + } + // "TEXT" + else + { + m_type = TEXT; + + parser.checkWithArg <special_atom>(line, &pos, "text"); + } + + *currentPos = pos; + } + + + enum Type + { + HEADER, + HEADER_FIELDS, + HEADER_FIELDS_NOT, + MIME, + TEXT + }; + + private: + + Type m_type; + IMAPParser::header_list* m_header_list; + + public: + + const Type type() const { return (m_type); } + const IMAPParser::header_list* header_list() const { return (m_header_list); } + }; + + + // + // section ::= "[" [section_text / (nz_number *["." nz_number] + // ["." (section_text / "MIME")])] "]" + // + + class section : public component + { + public: + + section() + : m_section_text1(NULL), m_section_text2(NULL) + { + } + + ~section() + { + delete (m_section_text1); + delete (m_section_text2); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("section"); + + string::size_type pos = *currentPos; + + parser.check <one_char <'['> >(line, &pos); + + if (!parser.check <one_char <']'> >(line, &pos, true)) + { + if (!(m_section_text1 = parser.get <section_text>(line, &pos, true))) + { + nz_number* num = parser.get <nz_number>(line, &pos); + m_nz_numbers.push_back(num->value()); + delete (num); + + while (parser.check <one_char <'.'> >(line, &pos, true)) + { + if ((num = parser.get <nz_number>(line, &pos, true))) + { + m_nz_numbers.push_back(num->value()); + delete (num); + } + else + { + m_section_text2 = parser.get <section_text>(line, &pos); + break; + } + } + } + + parser.check <one_char <']'> >(line, &pos); + } + + *currentPos = pos; + } + + private: + + section_text* m_section_text1; + section_text* m_section_text2; + std::vector <unsigned int> m_nz_numbers; + + public: + + const section_text* section_text1() const { return (m_section_text1); } + const section_text* section_text2() const { return (m_section_text2); } + const std::vector <unsigned int>& nz_numbers() const { return (m_nz_numbers); } + }; + + + // + // addr_adl ::= nstring + // ;; Holds route from [RFC-822] route-addr if + // ;; non-NIL + // + // addr_host ::= nstring + // ;; NIL indicates [RFC-822] group syntax. + // ;; Otherwise, holds [RFC-822] domain name + // + // addr_mailbox ::= nstring + // ;; NIL indicates end of [RFC-822] group; if + // ;; non-NIL and addr_host is NIL, holds + // ;; [RFC-822] group name. + // ;; Otherwise, holds [RFC-822] local-part + // + // addr_name ::= nstring + // ;; Holds phrase from [RFC-822] mailbox if + // ;; non-NIL + // + // address ::= "(" addr_name SPACE addr_adl SPACE addr_mailbox + // SPACE addr_host ")" + // + + class address : public component + { + public: + + address() + : m_addr_name(NULL), m_addr_adl(NULL), + m_addr_mailbox(NULL), m_addr_host(NULL) + { + } + + ~address() + { + delete (m_addr_name); + delete (m_addr_adl); + delete (m_addr_mailbox); + delete (m_addr_host); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("address"); + + string::size_type pos = *currentPos; + + parser.check <one_char <'('> >(line, &pos); + m_addr_name = parser.get <nstring>(line, &pos); + parser.check <SPACE>(line, &pos); + m_addr_adl = parser.get <nstring>(line, &pos); + parser.check <SPACE>(line, &pos); + m_addr_mailbox = parser.get <nstring>(line, &pos); + parser.check <SPACE>(line, &pos); + m_addr_host = parser.get <nstring>(line, &pos); + parser.check <one_char <')'> >(line, &pos); + + *currentPos = pos; + } + + private: + + nstring* m_addr_name; + nstring* m_addr_adl; + nstring* m_addr_mailbox; + nstring* m_addr_host; + + public: + + nstring* addr_name() const { return (m_addr_name); } + nstring* addr_adl() const { return (m_addr_adl); } + nstring* addr_mailbox() const { return (m_addr_mailbox); } + nstring* addr_host() const { return (m_addr_host); } + }; + + + // + // address_list ::= "(" 1*address ")" / nil + // + + class address_list : public component + { + public: + + ~address_list() + { + for (std::vector <address*>::iterator it = m_addresses.begin() ; + it != m_addresses.end() ; ++it) + { + delete (*it); + } + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("address_list"); + + string::size_type pos = *currentPos; + + if (!parser.check <NIL>(line, &pos, true)) + { + parser.check <one_char <'('> >(line, &pos); + + while (!parser.check <one_char <')'> >(line, &pos, true)) + { + m_addresses.push_back(parser.get <address>(line, &pos)); + parser.check <SPACE>(line, &pos, true); + } + } + + *currentPos = pos; + } + + private: + + std::vector <address*> m_addresses; + + public: + + const std::vector <address*>& addresses() const { return (m_addresses); } + }; + + + // + // env_bcc ::= "(" 1*address ")" / nil + // + + typedef address_list env_bcc; + + + // + // env_cc ::= "(" 1*address ")" / nil + // + + typedef address_list env_cc; + + + // + // env_date ::= nstring + // + + typedef nstring env_date; + + + // + // env_from ::= "(" 1*address ")" / nil + // + + typedef address_list env_from; + + + // + // env_in_reply_to ::= nstring + // + + typedef nstring env_in_reply_to; + + + // + // env_message_id ::= nstring + // + + typedef nstring env_message_id; + + + // + // env_reply_to ::= "(" 1*address ")" / nil + // + + typedef address_list env_reply_to; + + + // + // env_sender ::= "(" 1*address ")" / nil + // + + typedef address_list env_sender; + + + // + // env_subject ::= nstring + // + + typedef nstring env_subject; + + + // + // env_to ::= "(" 1*address ")" / nil + // + + typedef address_list env_to; + + + // + // envelope ::= "(" env_date SPACE env_subject SPACE env_from + // SPACE env_sender SPACE env_reply_to SPACE env_to + // SPACE env_cc SPACE env_bcc SPACE env_in_reply_to + // SPACE env_message_id ")" + // + + class envelope : public component + { + public: + + envelope() + : m_env_date(NULL), m_env_subject(NULL), + m_env_from(NULL), m_env_sender(NULL), m_env_reply_to(NULL), + m_env_to(NULL), m_env_cc(NULL), m_env_bcc(NULL), + m_env_in_reply_to(NULL), m_env_message_id(NULL) + { + } + + ~envelope() + { + delete (m_env_date); + delete (m_env_subject); + delete (m_env_from); + delete (m_env_sender); + delete (m_env_reply_to); + delete (m_env_to); + delete (m_env_cc); + delete (m_env_bcc); + delete (m_env_in_reply_to); + delete (m_env_message_id); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("envelope"); + + string::size_type pos = *currentPos; + + parser.check <one_char <'('> >(line, &pos); + + m_env_date = parser.get <IMAPParser::env_date>(line, &pos); + parser.check <SPACE>(line, &pos); + + m_env_subject = parser.get <IMAPParser::env_subject>(line, &pos); + parser.check <SPACE>(line, &pos); + + m_env_from = parser.get <IMAPParser::env_from>(line, &pos); + parser.check <SPACE>(line, &pos); + + m_env_sender = parser.get <IMAPParser::env_sender>(line, &pos); + parser.check <SPACE>(line, &pos); + + m_env_reply_to = parser.get <IMAPParser::env_reply_to>(line, &pos); + parser.check <SPACE>(line, &pos); + + m_env_to = parser.get <IMAPParser::env_to>(line, &pos); + parser.check <SPACE>(line, &pos); + + m_env_cc = parser.get <IMAPParser::env_cc>(line, &pos); + parser.check <SPACE>(line, &pos); + + m_env_bcc = parser.get <IMAPParser::env_bcc>(line, &pos); + parser.check <SPACE>(line, &pos); + + m_env_in_reply_to = parser.get <IMAPParser::env_in_reply_to>(line, &pos); + parser.check <SPACE>(line, &pos); + + m_env_message_id = parser.get <IMAPParser::env_message_id>(line, &pos); + + parser.check <one_char <')'> >(line, &pos); + + *currentPos = pos; + } + + private: + + IMAPParser::env_date* m_env_date; + IMAPParser::env_subject* m_env_subject; + IMAPParser::env_from* m_env_from; + IMAPParser::env_sender* m_env_sender; + IMAPParser::env_reply_to* m_env_reply_to; + IMAPParser::env_to* m_env_to; + IMAPParser::env_cc* m_env_cc; + IMAPParser::env_bcc* m_env_bcc; + IMAPParser::env_in_reply_to* m_env_in_reply_to; + IMAPParser::env_message_id* m_env_message_id; + + public: + + const IMAPParser::env_date* env_date() const { return (m_env_date); } + const IMAPParser::env_subject* env_subject() const { return (m_env_subject); } + const IMAPParser::env_from* env_from() const { return (m_env_from); } + const IMAPParser::env_sender* env_sender() const { return (m_env_sender); } + const IMAPParser::env_reply_to* env_reply_to() const { return (m_env_reply_to); } + const IMAPParser::env_to* env_to() const { return (m_env_to); } + const IMAPParser::env_cc* env_cc() const { return (m_env_cc); } + const IMAPParser::env_bcc* env_bcc() const { return (m_env_bcc); } + const IMAPParser::env_in_reply_to* env_in_reply_to() const { return (m_env_in_reply_to); } + const IMAPParser::env_message_id* env_message_id() const { return (m_env_message_id); } + }; + + + // + // body_fld_desc ::= nstring + // + + typedef nstring body_fld_desc; + + + // + // body_fld_id ::= nstring + // + + typedef nstring body_fld_id; + + + // + // body_fld_md5 ::= nstring + // + + typedef nstring body_fld_md5; + + + // + // body_fld_octets ::= number + // + + typedef number body_fld_octets; + + + // + // body_fld_lines ::= number + // + + typedef number body_fld_lines; + + + // + // body_fld_enc ::= (<"> ("7BIT" / "8BIT" / "BINARY" / "BASE64"/ + // "QUOTED-PRINTABLE") <">) / string + // + + typedef xstring body_fld_enc; + + + // + // body_fld_param_item ::= string SPACE string + // + + class body_fld_param_item : public component + { + public: + + body_fld_param_item() + : m_string1(NULL), m_string2(NULL) + { + } + + ~body_fld_param_item() + { + delete (m_string1); + delete (m_string2); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("body_fld_param_item"); + + string::size_type pos = *currentPos; + + m_string1 = parser.get <xstring>(line, &pos); + parser.check <SPACE>(line, &pos); + m_string2 = parser.get <xstring>(line, &pos); + + DEBUG_FOUND("body_fld_param_item", "<" << m_string1->value() << ", " << m_string2->value() << ">"); + + *currentPos = pos; + } + + private: + + xstring* m_string1; + xstring* m_string2; + + public: + + const xstring* string1() const { return (m_string1); } + const xstring* string2() const { return (m_string2); } + }; + + + // + // body_fld_param ::= "(" 1#(body_fld_param_item) ")" / nil + // + + class body_fld_param : public component + { + public: + + ~body_fld_param() + { + for (std::vector <body_fld_param_item*>::iterator it = m_items.begin() ; + it != m_items.end() ; ++it) + { + delete (*it); + } + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("body_fld_param"); + + string::size_type pos = *currentPos; + + if (!parser.check <NIL>(line, &pos, true)) + { + parser.check <one_char <'('> >(line, &pos); + + m_items.push_back(parser.get <body_fld_param_item>(line, &pos)); + + while (!parser.check <one_char <')'> >(line, &pos, true)) + { + parser.check <SPACE>(line, &pos); + m_items.push_back(parser.get <body_fld_param_item>(line, &pos)); + } + } + + *currentPos = pos; + } + + private: + + std::vector <body_fld_param_item*> m_items; + + public: + + const std::vector <body_fld_param_item*>& items() const { return (m_items); } + }; + + + // + // body_fld_dsp ::= "(" string SPACE body_fld_param ")" / nil + // + + class body_fld_dsp : public component + { + public: + + body_fld_dsp() + : m_string(NULL), m_body_fld_param(NULL) + { + } + + ~body_fld_dsp() + { + delete (m_string); + delete (m_body_fld_param); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("body_fld_dsp"); + + string::size_type pos = *currentPos; + + if (!parser.check <NIL>(line, &pos, true)) + { + parser.check <one_char <'('> >(line, &pos); + m_string = parser.get <xstring>(line, &pos); + parser.check <SPACE>(line, &pos); + m_body_fld_param = parser.get <class body_fld_param>(line, &pos); + parser.check <one_char <')'> >(line, &pos); + } + + *currentPos = pos; + } + + private: + + class xstring* m_string; + class body_fld_param* m_body_fld_param; + + public: + + const class xstring* str() const { return (m_string); } + const class body_fld_param* body_fld_param() const { return (m_body_fld_param); } + }; + + + // + // body_fld_lang ::= nstring / "(" 1#string ")" + // + + class body_fld_lang : public component + { + public: + + ~body_fld_lang() + { + for (std::vector <xstring*>::iterator it = m_strings.begin() ; + it != m_strings.end() ; ++it) + { + delete (*it); + } + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("body_fld_lang"); + + string::size_type pos = *currentPos; + + if (parser.check <one_char <'('> >(line, &pos, true)) + { + m_strings.push_back(parser.get <class xstring>(line, &pos)); + + while (!parser.check <one_char <')'> >(line, &pos, true)) + m_strings.push_back(parser.get <class xstring>(line, &pos)); + } + else + { + m_strings.push_back(parser.get <class nstring>(line, &pos)); + } + + *currentPos = pos; + } + + private: + + std::vector <xstring*> m_strings; + + public: + + const std::vector <xstring*>& strings() const { return (m_strings); } + }; + + + // + // body_fields ::= body_fld_param SPACE body_fld_id SPACE + // body_fld_desc SPACE body_fld_enc SPACE + // body_fld_octets + // + + class body_fields : public component + { + public: + + body_fields() + : m_body_fld_param(NULL), m_body_fld_id(NULL), + m_body_fld_desc(NULL), m_body_fld_enc(NULL), m_body_fld_octets(NULL) + { + } + + ~body_fields() + { + delete (m_body_fld_param); + delete (m_body_fld_id); + delete (m_body_fld_desc); + delete (m_body_fld_enc); + delete (m_body_fld_octets); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("body_fields"); + + string::size_type pos = *currentPos; + + m_body_fld_param = parser.get <IMAPParser::body_fld_param>(line, &pos); + parser.check <SPACE>(line, &pos); + m_body_fld_id = parser.get <IMAPParser::body_fld_id>(line, &pos); + parser.check <SPACE>(line, &pos); + m_body_fld_desc = parser.get <IMAPParser::body_fld_desc>(line, &pos); + parser.check <SPACE>(line, &pos); + m_body_fld_enc = parser.get <IMAPParser::body_fld_enc>(line, &pos); + parser.check <SPACE>(line, &pos); + m_body_fld_octets = parser.get <IMAPParser::body_fld_octets>(line, &pos); + + *currentPos = pos; + } + + private: + + IMAPParser::body_fld_param* m_body_fld_param; + IMAPParser::body_fld_id* m_body_fld_id; + IMAPParser::body_fld_desc* m_body_fld_desc; + IMAPParser::body_fld_enc* m_body_fld_enc; + IMAPParser::body_fld_octets* m_body_fld_octets; + + public: + + const IMAPParser::body_fld_param* body_fld_param() const { return (m_body_fld_param); } + const IMAPParser::body_fld_id* body_fld_id() const { return (m_body_fld_id); } + const IMAPParser::body_fld_desc* body_fld_desc() const { return (m_body_fld_desc); } + const IMAPParser::body_fld_enc* body_fld_enc() const { return (m_body_fld_enc); } + const IMAPParser::body_fld_octets* body_fld_octets() const { return (m_body_fld_octets); } + }; + + + // + // media_subtype ::= string + // ;; Defined in [MIME-IMT] + // + + typedef xstring media_subtype; + + + // + // media_text ::= <"> "TEXT" <"> SPACE media_subtype + // ;; Defined in [MIME-IMT] + // + + class media_text : public component + { + public: + + media_text() + : m_media_subtype(NULL) + { + } + + ~media_text() + { + delete (m_media_subtype); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("media_text"); + + string::size_type pos = *currentPos; + + parser.check <one_char <'"'> >(line, &pos); + parser.checkWithArg <special_atom>(line, &pos, "text"); + parser.check <one_char <'"'> >(line, &pos); + parser.check <SPACE>(line, &pos); + + m_media_subtype = parser.get <IMAPParser::media_subtype>(line, &pos); + + *currentPos = pos; + } + + private: + + IMAPParser::media_subtype* m_media_subtype; + + public: + + const IMAPParser::media_subtype* media_subtype() const { return (m_media_subtype); } + }; + + + // + // media_message ::= <"> "MESSAGE" <"> SPACE <"> "RFC822" <"> + // ;; Defined in [MIME-IMT] + // + + class media_message : public component + { + public: + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("media_message"); + + string::size_type pos = *currentPos; + + parser.check <one_char <'"'> >(line, &pos); + parser.checkWithArg <special_atom>(line, &pos, "message"); + parser.check <one_char <'"'> >(line, &pos); + parser.check <SPACE>(line, &pos); + + //parser.check <one_char <'"'> >(line, &pos); + //parser.checkWithArg <special_atom>(line, &pos, "rfc822"); + //parser.check <one_char <'"'> >(line, &pos); + + m_media_subtype = parser.get <IMAPParser::media_subtype>(line, &pos); + + *currentPos = pos; + } + + private: + + IMAPParser::media_subtype* m_media_subtype; + + public: + + const IMAPParser::media_subtype* media_subtype() const { return (m_media_subtype); } + }; + + + // + // media_basic ::= (<"> ("APPLICATION" / "AUDIO" / "IMAGE" / + // "MESSAGE" / "VIDEO") <">) / string) + // SPACE media_subtype + // ;; Defined in [MIME-IMT] + + class media_basic : public component + { + public: + + media_basic() + : m_media_type(NULL), m_media_subtype(NULL) + { + } + + ~media_basic() + { + delete (m_media_type); + delete (m_media_subtype); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("media_basic"); + + string::size_type pos = *currentPos; + + m_media_type = parser.get <xstring>(line, &pos); + + parser.check <SPACE>(line, &pos); + + m_media_subtype = parser.get <IMAPParser::media_subtype>(line, &pos); + + *currentPos = pos; + } + + private: + + IMAPParser::xstring* m_media_type; + IMAPParser::media_subtype* m_media_subtype; + + public: + + const IMAPParser::xstring* media_type() const { return (m_media_type); } + const IMAPParser::media_subtype* media_subtype() const { return (m_media_subtype); } + }; + + + // + // body_ext_1part ::= body_fld_md5 [SPACE body_fld_dsp + // [SPACE body_fld_lang + // [SPACE 1#body_extension]]] + // ;; MUST NOT be returned on non-extensible + // ;; "BODY" fetch + // + + class body_ext_1part : public component + { + public: + + body_ext_1part() + : m_body_fld_md5(NULL), m_body_fld_dsp(NULL), m_body_fld_lang(NULL) + { + } + + ~body_ext_1part() + { + delete (m_body_fld_md5); + delete (m_body_fld_dsp); + delete (m_body_fld_lang); + + for (std::vector <body_extension*>::iterator it = m_body_extensions.begin() ; + it != m_body_extensions.end() ; ++it) + { + delete (*it); + } + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("body_ext_1part"); + + string::size_type pos = *currentPos; + + m_body_fld_md5 = parser.get <IMAPParser::body_fld_md5>(line, &pos); + + // [SPACE body_fld_dsp + if (parser.check <SPACE>(line, &pos, true)) + { + m_body_fld_dsp = parser.get <IMAPParser::body_fld_dsp>(line, &pos); + + // [SPACE body_fld_lang + if (parser.check <SPACE>(line, &pos, true)) + { + m_body_fld_lang = parser.get <IMAPParser::body_fld_lang>(line, &pos); + + // [SPACE 1#body_extension] + if (parser.check <SPACE>(line, &pos, true)) + { + m_body_extensions.push_back + (parser.get <body_extension>(line, &pos)); + + body_extension* ext = NULL; + + while ((ext = parser.get <body_extension>(line, &pos, true)) != NULL) + m_body_extensions.push_back(ext); + } + } + } + + *currentPos = pos; + } + + private: + + IMAPParser::body_fld_md5* m_body_fld_md5; + IMAPParser::body_fld_dsp* m_body_fld_dsp; + IMAPParser::body_fld_lang* m_body_fld_lang; + + std::vector <body_extension*> m_body_extensions; + + public: + + const IMAPParser::body_fld_md5* body_fld_md5() const { return (m_body_fld_md5); } + const IMAPParser::body_fld_dsp* body_fld_dsp() const { return (m_body_fld_dsp); } + const IMAPParser::body_fld_lang* body_fld_lang() const { return (m_body_fld_lang); } + + const std::vector <body_extension*> body_extensions() const { return (m_body_extensions); } + }; + + + // + // body_ext_mpart ::= body_fld_param + // [SPACE body_fld_dsp SPACE body_fld_lang + // [SPACE 1#body_extension]] + // ;; MUST NOT be returned on non-extensible + // ;; "BODY" fetch + + class body_ext_mpart : public component + { + public: + + body_ext_mpart() + : m_body_fld_param(NULL), m_body_fld_dsp(NULL), m_body_fld_lang(NULL) + { + } + + ~body_ext_mpart() + { + delete (m_body_fld_param); + delete (m_body_fld_dsp); + delete (m_body_fld_lang); + + for (std::vector <body_extension*>::iterator it = m_body_extensions.begin() ; + it != m_body_extensions.end() ; ++it) + { + delete (*it); + } + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("body_ext_mpart"); + + string::size_type pos = *currentPos; + + m_body_fld_param = parser.get <IMAPParser::body_fld_param>(line, &pos); + + // [SPACE body_fld_dsp SPACE body_fld_lang [SPACE 1#body_extension]] + if (parser.check <SPACE>(line, &pos, true)) + { + m_body_fld_dsp = parser.get <IMAPParser::body_fld_dsp>(line, &pos); + parser.check <SPACE>(line, &pos); + m_body_fld_lang = parser.get <IMAPParser::body_fld_lang>(line, &pos); + + // [SPACE 1#body_extension] + if (parser.check <SPACE>(line, &pos, true)) + { + m_body_extensions.push_back + (parser.get <body_extension>(line, &pos)); + + body_extension* ext = NULL; + + while ((ext = parser.get <body_extension>(line, &pos, true)) != NULL) + m_body_extensions.push_back(ext); + } + } + + *currentPos = pos; + } + + private: + + IMAPParser::body_fld_param* m_body_fld_param; + IMAPParser::body_fld_dsp* m_body_fld_dsp; + IMAPParser::body_fld_lang* m_body_fld_lang; + + std::vector <body_extension*> m_body_extensions; + + public: + + const IMAPParser::body_fld_param* body_fld_param() const { return (m_body_fld_param); } + const IMAPParser::body_fld_dsp* body_fld_dsp() const { return (m_body_fld_dsp); } + const IMAPParser::body_fld_lang* body_fld_lang() const { return (m_body_fld_lang); } + + const std::vector <body_extension*> body_extensions() const { return (m_body_extensions); } + }; + + + // + // body_type_basic ::= media_basic SPACE body_fields + // ;; MESSAGE subtype MUST NOT be "RFC822" + // + + class body_type_basic : public component + { + public: + + body_type_basic() + : m_media_basic(NULL), m_body_fields(NULL) + { + } + + ~body_type_basic() + { + delete (m_media_basic); + delete (m_body_fields); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("body_type_basic"); + + string::size_type pos = *currentPos; + + m_media_basic = parser.get <IMAPParser::media_basic>(line, &pos); + parser.check <SPACE>(line, &pos); + m_body_fields = parser.get <IMAPParser::body_fields>(line, &pos); + + *currentPos = pos; + } + + private: + + IMAPParser::media_basic* m_media_basic; + IMAPParser::body_fields* m_body_fields; + + public: + + const IMAPParser::media_basic* media_basic() const { return (m_media_basic); } + const IMAPParser::body_fields* body_fields() const { return (m_body_fields); } + }; + + + // + // body_type_msg ::= media_message SPACE body_fields SPACE envelope + // SPACE body SPACE body_fld_lines + // + + class xbody; + typedef xbody body; + + class body_type_msg : public component + { + public: + + body_type_msg() + : m_media_message(NULL), m_body_fields(NULL), + m_envelope(NULL), m_body(NULL), m_body_fld_lines(NULL) + { + } + + ~body_type_msg() + { + delete (m_media_message); + delete (m_body_fields); + delete (m_envelope); + delete (m_body); + delete (m_body_fld_lines); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("body_type_msg"); + + string::size_type pos = *currentPos; + + m_media_message = parser.get <IMAPParser::media_message>(line, &pos); + parser.check <SPACE>(line, &pos); + m_body_fields = parser.get <IMAPParser::body_fields>(line, &pos); + parser.check <SPACE>(line, &pos); + m_envelope = parser.get <IMAPParser::envelope>(line, &pos); + parser.check <SPACE>(line, &pos); + m_body = parser.get <IMAPParser::xbody>(line, &pos); + parser.check <SPACE>(line, &pos); + m_body_fld_lines = parser.get <IMAPParser::body_fld_lines>(line, &pos); + + *currentPos = pos; + } + + private: + + IMAPParser::media_message* m_media_message; + IMAPParser::body_fields* m_body_fields; + IMAPParser::envelope* m_envelope; + IMAPParser::xbody* m_body; + IMAPParser::body_fld_lines* m_body_fld_lines; + + public: + + const IMAPParser::media_message* media_message() const { return (m_media_message); } + const IMAPParser::body_fields* body_fields() const { return (m_body_fields); } + const IMAPParser::envelope* envelope() const { return (m_envelope); } + const IMAPParser::xbody* body() const { return (m_body); } + const IMAPParser::body_fld_lines* body_fld_lines() const { return (m_body_fld_lines); } + }; + + + // + // body_type_text ::= media_text SPACE body_fields SPACE body_fld_lines + // + + class body_type_text : public component + { + public: + + body_type_text() + : m_media_text(NULL), + m_body_fields(NULL), m_body_fld_lines(NULL) + { + } + + ~body_type_text() + { + delete (m_media_text); + delete (m_body_fields); + delete (m_body_fld_lines); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("body_type_text"); + + string::size_type pos = *currentPos; + + m_media_text = parser.get <IMAPParser::media_text>(line, &pos); + parser.check <SPACE>(line, &pos); + m_body_fields = parser.get <IMAPParser::body_fields>(line, &pos); + parser.check <SPACE>(line, &pos); + m_body_fld_lines = parser.get <IMAPParser::body_fld_lines>(line, &pos); + + *currentPos = pos; + } + + private: + + IMAPParser::media_text* m_media_text; + IMAPParser::body_fields* m_body_fields; + IMAPParser::body_fld_lines* m_body_fld_lines; + + public: + + const IMAPParser::media_text* media_text() const { return (m_media_text); } + const IMAPParser::body_fields* body_fields() const { return (m_body_fields); } + const IMAPParser::body_fld_lines* body_fld_lines() const { return (m_body_fld_lines); } + }; + + + // + // body_type_1part ::= (body_type_basic / body_type_msg / body_type_text) + // [SPACE body_ext_1part] + // + + class body_type_1part : public component + { + public: + + body_type_1part() + : m_body_type_basic(NULL), m_body_type_msg(NULL), + m_body_type_text(NULL), m_body_ext_1part(NULL) + { + } + + ~body_type_1part() + { + delete (m_body_type_basic); + delete (m_body_type_msg); + delete (m_body_type_text); + + delete (m_body_ext_1part); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("body_type_1part"); + + string::size_type pos = *currentPos; + + if (!(m_body_type_text = parser.get <IMAPParser::body_type_text>(line, &pos, true))) + if (!(m_body_type_msg = parser.get <IMAPParser::body_type_msg>(line, &pos, true))) + m_body_type_basic = parser.get <IMAPParser::body_type_basic>(line, &pos); + + if (parser.check <SPACE>(line, &pos, true)) + m_body_ext_1part = parser.get <IMAPParser::body_ext_1part>(line, &pos); + + *currentPos = pos; + } + + private: + + IMAPParser::body_type_basic* m_body_type_basic; + IMAPParser::body_type_msg* m_body_type_msg; + IMAPParser::body_type_text* m_body_type_text; + + IMAPParser::body_ext_1part* m_body_ext_1part; + + public: + + const IMAPParser::body_type_basic* body_type_basic() const { return (m_body_type_basic); } + const IMAPParser::body_type_msg* body_type_msg() const { return (m_body_type_msg); } + const IMAPParser::body_type_text* body_type_text() const { return (m_body_type_text); } + + const IMAPParser::body_ext_1part* body_ext_1part() const { return (m_body_ext_1part); } + }; + + + // + // body_type_mpart ::= 1*body SPACE media_subtype + // [SPACE body_ext_mpart] + // + + class body_type_mpart : public component + { + public: + + body_type_mpart() + : m_media_subtype(NULL), m_body_ext_mpart(NULL) + { + } + + ~body_type_mpart() + { + delete (m_media_subtype); + delete (m_body_ext_mpart); + + for (std::vector <xbody*>::iterator it = m_list.begin() ; + it != m_list.end() ; ++it) + { + delete (*it); + } + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("body_type_mpart"); + + string::size_type pos = *currentPos; + + m_list.push_back(parser.get <xbody>(line, &pos)); + + for (xbody* b ; (b = parser.get <xbody>(line, &pos, true)) ; ) + m_list.push_back(b); + + parser.check <SPACE>(line, &pos); + + m_media_subtype = parser.get <IMAPParser::media_subtype>(line, &pos); + + if (parser.check <SPACE>(line, &pos, true)) + m_body_ext_mpart = parser.get <IMAPParser::body_ext_mpart>(line, &pos); + + *currentPos = pos; + } + + private: + + IMAPParser::media_subtype* m_media_subtype; + IMAPParser::body_ext_mpart* m_body_ext_mpart; + + std::vector <xbody*> m_list; + + public: + + const std::vector <IMAPParser::xbody*>& list() const { return (m_list); } + + const IMAPParser::media_subtype* media_subtype() const { return (m_media_subtype); } + const IMAPParser::body_ext_mpart* body_ext_mpart() const { return (m_body_ext_mpart); } + }; + + + // + // xbody ::= "(" body_type_1part / body_type_mpart ")" + // + + class xbody : public component + { + public: + + xbody() + : m_body_type_1part(NULL), m_body_type_mpart(NULL) + { + } + + ~xbody() + { + delete (m_body_type_1part); + delete (m_body_type_mpart); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("body"); + + string::size_type pos = *currentPos; + + parser.check <one_char <'('> >(line, &pos); + + if (!(m_body_type_1part = parser.get <IMAPParser::body_type_1part>(line, &pos, true))) + m_body_type_mpart = parser.get <IMAPParser::body_type_mpart>(line, &pos); + + parser.check <one_char <')'> >(line, &pos); + + *currentPos = pos; + } + + private: + + IMAPParser::body_type_1part* m_body_type_1part; + IMAPParser::body_type_mpart* m_body_type_mpart; + + public: + + const IMAPParser::body_type_1part* body_type_1part() const { return (m_body_type_1part); } + const IMAPParser::body_type_mpart* body_type_mpart() const { return (m_body_type_mpart); } + }; + + + // + // uniqueid ::= nz_number + // ;; Strictly ascending + // + // msg_att_item ::= "ENVELOPE" SPACE envelope / + // "FLAGS" SPACE "(" #(flag / "\Recent") ")" / + // "INTERNALDATE" SPACE date_time / + // "RFC822" [".HEADER" / ".TEXT"] SPACE nstring / + // "RFC822.SIZE" SPACE number / + // "BODY" ["STRUCTURE"] SPACE body / + // "BODY" section ["<" number ">"] SPACE nstring / + // "UID" SPACE uniqueid + // + + class msg_att_item : public component + { + public: + + msg_att_item() + : m_date_time(NULL), m_number(NULL), m_envelope(NULL), + m_uniqueid(NULL), m_nstring(NULL), m_body(NULL), m_flag_list(NULL) + { + } + + ~msg_att_item() + { + delete (m_date_time); + delete (m_number); + delete (m_envelope); + delete (m_uniqueid); + delete (m_nstring); + delete (m_body); + delete (m_flag_list); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("msg_att_item"); + + string::size_type pos = *currentPos; + + // "ENVELOPE" SPACE envelope + if (parser.checkWithArg <special_atom>(line, &pos, "envelope", true)) + { + m_type = ENVELOPE; + + parser.check <SPACE>(line, &pos); + m_envelope = parser.get <IMAPParser::envelope>(line, &pos); + } + // "FLAGS" SPACE "(" #(flag / "\Recent") ")" + else if (parser.checkWithArg <special_atom>(line, &pos, "flags", true)) + { + m_type = FLAGS; + + parser.check <SPACE>(line, &pos); + + m_flag_list = parser.get <IMAPParser::flag_list>(line, &pos); + } + // "INTERNALDATE" SPACE date_time + else if (parser.checkWithArg <special_atom>(line, &pos, "internaldate", true)) + { + m_type = INTERNALDATE; + + parser.check <SPACE>(line, &pos); + m_date_time = parser.get <IMAPParser::date_time>(line, &pos); + } + // "RFC822" ".HEADER" SPACE nstring + else if (parser.checkWithArg <special_atom>(line, &pos, "rfc822.header", true)) + { + m_type = RFC822_HEADER; + + parser.check <SPACE>(line, &pos); + + m_nstring = parser.get <IMAPParser::nstring>(line, &pos); + } + // "RFC822" ".TEXT" SPACE nstring + else if (parser.checkWithArg <special_atom>(line, &pos, "rfc822.text", true)) + { + m_type = RFC822_TEXT; + + parser.check <SPACE>(line, &pos); + + m_nstring = parser.getWithArgs <IMAPParser::nstring> + (line, &pos, this, RFC822_TEXT); + } + // "RFC822.SIZE" SPACE number + else if (parser.checkWithArg <special_atom>(line, &pos, "rfc822.size", true)) + { + m_type = RFC822_SIZE; + + parser.check <SPACE>(line, &pos); + m_number = parser.get <IMAPParser::number>(line, &pos); + } + // "RFC822" SPACE nstring + else if (parser.checkWithArg <special_atom>(line, &pos, "rfc822", true)) + { + m_type = RFC822; + + parser.check <SPACE>(line, &pos); + + m_nstring = parser.get <IMAPParser::nstring>(line, &pos); + } + // "BODY" "STRUCTURE" SPACE body + else if (parser.checkWithArg <special_atom>(line, &pos, "bodystructure", true)) + { + m_type = BODY_STRUCTURE; + + parser.check <SPACE>(line, &pos); + + m_body = parser.get <IMAPParser::body>(line, &pos); + } + // "BODY" section ["<" number ">"] SPACE nstring + // "BODY" SPACE body + else if (parser.checkWithArg <special_atom>(line, &pos, "body", true)) + { + m_section = parser.get <IMAPParser::section>(line, &pos, true); + + // "BODY" section ["<" number ">"] SPACE nstring + if (m_section != NULL) + { + m_type = BODY_SECTION; + + if (parser.check <one_char <'<'> >(line, &pos, true)) + { + m_number = parser.get <IMAPParser::number>(line, &pos); + parser.check <one_char <'>'> >(line, &pos); + } + + parser.check <SPACE>(line, &pos); + + m_nstring = parser.getWithArgs <IMAPParser::nstring> + (line, &pos, this, BODY_SECTION); + } + // "BODY" SPACE body + else + { + m_type = BODY; + + parser.check <SPACE>(line, &pos); + + m_body = parser.get <IMAPParser::body>(line, &pos); + } + } + // "UID" SPACE uniqueid + else + { + m_type = UID; + + parser.checkWithArg <special_atom>(line, &pos, "uid"); + parser.check <SPACE>(line, &pos); + + m_uniqueid = parser.get <nz_number>(line, &pos); + } + + *currentPos = pos; + } + + + enum Type + { + ENVELOPE, + FLAGS, + INTERNALDATE, + RFC822, + RFC822_SIZE, + RFC822_HEADER, + RFC822_TEXT, + BODY, + BODY_SECTION, + BODY_STRUCTURE, + UID + }; + + private: + + Type m_type; + + IMAPParser::date_time* m_date_time; + IMAPParser::number* m_number; + IMAPParser::envelope* m_envelope; + IMAPParser::nz_number* m_uniqueid; + IMAPParser::nstring* m_nstring; + IMAPParser::xbody* m_body; + IMAPParser::flag_list* m_flag_list; + IMAPParser::section* m_section; + + public: + + const Type type() const { return (m_type); } + + const IMAPParser::date_time* date_time() const { return (m_date_time); } + const IMAPParser::number* number() const { return (m_number); } + const IMAPParser::envelope* envelope() const { return (m_envelope); } + const IMAPParser::nz_number* unique_id() const { return (m_uniqueid); } + const IMAPParser::nstring* nstring() const { return (m_nstring); } + const IMAPParser::xbody* body() const { return (m_body); } + const IMAPParser::flag_list* flag_list() const { return (m_flag_list); } + const IMAPParser::section* section() const { return (m_section); } + }; + + + // + // msg_att ::= "(" 1#(msg_att_item) ")" + // + + class msg_att : public component + { + public: + + ~msg_att() + { + for (std::vector <msg_att_item*>::iterator it = m_items.begin() ; + it != m_items.end() ; ++it) + { + delete (*it); + } + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("msg_att"); + + string::size_type pos = *currentPos; + + parser.check <one_char <'('> >(line, &pos); + + m_items.push_back(parser.get <msg_att_item>(line, &pos)); + + while (!parser.check <one_char <')'> >(line, &pos, true)) + { + parser.check <SPACE>(line, &pos); + m_items.push_back(parser.get <msg_att_item>(line, &pos)); + } + + *currentPos = pos; + } + + private: + + std::vector <msg_att_item*> m_items; + + public: + + const std::vector <msg_att_item*>& items() const { return (m_items); } + }; + + + // + // message_data ::= nz_number SPACE ("EXPUNGE" / + // ("FETCH" SPACE msg_att)) + // + + class message_data : public component + { + public: + + message_data() + : m_number(0), m_msg_att(NULL) + { + } + + ~message_data() + { + delete (m_msg_att); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("message_data"); + + string::size_type pos = *currentPos; + + nz_number* num = parser.get <nz_number>(line, &pos); + m_number = num->value(); + delete (num); + + parser.check <SPACE>(line, &pos); + + if (parser.checkWithArg <special_atom>(line, &pos, "expunge", true)) + { + m_type = EXPUNGE; + } + else + { + parser.checkWithArg <special_atom>(line, &pos, "fetch"); + + parser.check <SPACE>(line, &pos); + + m_type = FETCH; + m_msg_att = parser.get <IMAPParser::msg_att>(line, &pos); + } + + *currentPos = pos; + } + + + enum Type + { + EXPUNGE, + FETCH + }; + + private: + + Type m_type; + unsigned int m_number; + IMAPParser::msg_att* m_msg_att; + + public: + + const Type type() const { return (m_type); } + const unsigned int number() const { return (m_number); } + const IMAPParser::msg_att* msg_att() const { return (m_msg_att); } + }; + + + // + // resp_cond_state ::= ("OK" / "NO" / "BAD") SPACE resp_text + // ;; Status condition + // + + class resp_cond_state : public component + { + public: + + resp_cond_state() + : m_resp_text(NULL), m_status(BAD) + { + } + + ~resp_cond_state() + { + delete (m_resp_text); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("resp_cond_state"); + + string::size_type pos = *currentPos; + + if (parser.checkWithArg <special_atom>(line, &pos, "ok", true)) + { + m_status = OK; + } + else if (parser.checkWithArg <special_atom>(line, &pos, "no", true)) + { + m_status = NO; + } + else + { + parser.checkWithArg <special_atom>(line, &pos, "bad"); + m_status = BAD; + } + + parser.check <SPACE>(line, &pos); + + m_resp_text = parser.get <IMAPParser::resp_text>(line, &pos); + + *currentPos = pos; + } + + + enum Status + { + OK, + NO, + BAD + }; + + private: + + IMAPParser::resp_text* m_resp_text; + Status m_status; + + public: + + const IMAPParser::resp_text* resp_text() const { return (m_resp_text); } + const Status status() const { return (m_status); } + }; + + + // + // resp_cond_bye ::= "BYE" SPACE resp_text + // + + class resp_cond_bye : public component + { + public: + + resp_cond_bye() + : m_resp_text(NULL) + { + } + + ~resp_cond_bye() + { + delete (m_resp_text); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("resp_cond_bye"); + + string::size_type pos = *currentPos; + + parser.checkWithArg <special_atom>(line, &pos, "bye"); + + parser.check <SPACE>(line, &pos); + + m_resp_text = parser.get <IMAPParser::resp_text>(line, &pos); + + *currentPos = pos; + } + + private: + + IMAPParser::resp_text* m_resp_text; + + public: + + const IMAPParser::resp_text* resp_text() const { return (m_resp_text); } + }; + + + // + // resp_cond_auth ::= ("OK" / "PREAUTH") SPACE resp_text + // ;; Authentication condition + // + + class resp_cond_auth : public component + { + public: + + resp_cond_auth() + : m_resp_text(NULL) + { + } + + ~resp_cond_auth() + { + delete (m_resp_text); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("resp_cond_auth"); + + string::size_type pos = *currentPos; + + if (parser.checkWithArg <special_atom>(line, &pos, "ok", true)) + { + m_cond = OK; + } + else + { + parser.checkWithArg <special_atom>(line, &pos, "preauth"); + + m_cond = PREAUTH; + } + + parser.check <SPACE>(line, &pos); + + m_resp_text = parser.get <IMAPParser::resp_text>(line, &pos); + + *currentPos = pos; + } + + + enum Condition + { + OK, + PREAUTH + }; + + private: + + Condition m_cond; + IMAPParser::resp_text* m_resp_text; + + public: + + const Condition condition() const { return (m_cond); } + const IMAPParser::resp_text* resp_text() const { return (m_resp_text); } + }; + + + // + // status_info ::= status_att SPACE number + // + + class status_info : public component + { + public: + + status_info() + : m_status_att(NULL), m_number(NULL) + { + } + + ~status_info() + { + delete (m_status_att); + delete (m_number); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("status_info"); + + string::size_type pos = *currentPos; + + m_status_att = parser.get <IMAPParser::status_att>(line, &pos); + parser.check <SPACE>(line, &pos); + m_number = parser.get <IMAPParser::number>(line, &pos); + + *currentPos = pos; + } + + private: + + IMAPParser::status_att* m_status_att; + IMAPParser::number* m_number; + + public: + + const IMAPParser::status_att* status_att() const { return (m_status_att); } + const IMAPParser::number* number() const { return (m_number); } + }; + + + // + // mailbox_data ::= "FLAGS" SPACE mailbox_flag_list / + // "LIST" SPACE mailbox_list / + // "LSUB" SPACE mailbox_list / + // "MAILBOX" SPACE text / + // "SEARCH" [SPACE 1#nz_number] / + // "STATUS" SPACE mailbox SPACE + // "(" #<status_att number ")" / + // number SPACE "EXISTS" / + // number SPACE "RECENT" + // + + class mailbox_data : public component + { + public: + + mailbox_data() + : m_number(NULL), m_mailbox_flag_list(NULL), m_mailbox_list(NULL), + m_mailbox(NULL), m_text(NULL) + { + } + + ~mailbox_data() + { + delete (m_number); + delete (m_mailbox_flag_list); + delete (m_mailbox_list); + delete (m_mailbox); + delete (m_text); + + for (std::vector <nz_number*>::iterator it = m_search_nz_number_list.begin() ; + it != m_search_nz_number_list.end() ; ++it) + { + delete (*it); + } + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("mailbox_data"); + + string::size_type pos = *currentPos; + + m_number = parser.get <IMAPParser::number>(line, &pos, true); + + if (m_number) + { + parser.check <SPACE>(line, &pos); + + if (parser.checkWithArg <special_atom>(line, &pos, "exists", true)) + { + m_type = EXISTS; + } + else + { + parser.checkWithArg <special_atom>(line, &pos, "recent"); + + m_type = RECENT; + } + } + else + { + // "FLAGS" SPACE mailbox_flag_list + if (parser.checkWithArg <special_atom>(line, &pos, "flags", true)) + { + parser.check <SPACE>(line, &pos); + + m_mailbox_flag_list = parser.get <IMAPParser::mailbox_flag_list>(line, &pos); + + m_type = FLAGS; + } + // "LIST" SPACE mailbox_list + else if (parser.checkWithArg <special_atom>(line, &pos, "list", true)) + { + parser.check <SPACE>(line, &pos); + + m_mailbox_list = parser.get <IMAPParser::mailbox_list>(line, &pos); + + m_type = LIST; + } + // "LSUB" SPACE mailbox_list + else if (parser.checkWithArg <special_atom>(line, &pos, "lsub", true)) + { + parser.check <SPACE>(line, &pos); + + m_mailbox_list = parser.get <IMAPParser::mailbox_list>(line, &pos); + + m_type = LSUB; + } + // "MAILBOX" SPACE text + else if (parser.checkWithArg <special_atom>(line, &pos, "mailbox", true)) + { + parser.check <SPACE>(line, &pos); + + m_text = parser.get <IMAPParser::text>(line, &pos); + + m_type = MAILBOX; + } + // "SEARCH" [SPACE 1#nz_number] + else if (parser.checkWithArg <special_atom>(line, &pos, "search", true)) + { + if (parser.check <SPACE>(line, &pos, true)) + { + m_search_nz_number_list.push_back + (parser.get <nz_number>(line, &pos)); + + while (parser.check <SPACE>(line, &pos, true)) + { + m_search_nz_number_list.push_back + (parser.get <nz_number>(line, &pos)); + } + } + + m_type = SEARCH; + } + // "STATUS" SPACE mailbox SPACE + // "(" #<status_att number)] ")" + // + // "(" [status_att SPACE number *(SPACE status_att SPACE number)] ")" + else + { + parser.checkWithArg <special_atom>(line, &pos, "status"); + parser.check <SPACE>(line, &pos); + + m_mailbox = parser.get <IMAPParser::mailbox>(line, &pos); + + parser.check <SPACE>(line, &pos); + parser.check <one_char <'('> >(line, &pos); + + m_status_info_list.push_back(parser.get <status_info>(line, &pos)); + + while (!parser.check <one_char <')'> >(line, &pos, true)) + { + parser.check <SPACE>(line, &pos); + m_status_info_list.push_back(parser.get <status_info>(line, &pos)); + } + + m_type = STATUS; + } + } + + *currentPos = pos; + } + + + enum Type + { + FLAGS, + LIST, + LSUB, + MAILBOX, + SEARCH, + STATUS, + EXISTS, + RECENT + }; + + private: + + Type m_type; + + IMAPParser::number* m_number; + IMAPParser::mailbox_flag_list* m_mailbox_flag_list; + IMAPParser::mailbox_list* m_mailbox_list; + IMAPParser::mailbox* m_mailbox; + IMAPParser::text* m_text; + std::vector <nz_number*> m_search_nz_number_list; + std::vector <status_info*> m_status_info_list; + + public: + + const Type type() const { return (m_type); } + + const IMAPParser::number* number() const { return (m_number); } + const IMAPParser::mailbox_flag_list* mailbox_flag_list() const { return (m_mailbox_flag_list); } + const IMAPParser::mailbox_list* mailbox_list() const { return (m_mailbox_list); } + const IMAPParser::mailbox* mailbox() const { return (m_mailbox); } + const IMAPParser::text* text() const { return (m_text); } + const std::vector <nz_number*>& search_nz_number_list() const { return (m_search_nz_number_list); } + const std::vector <status_info*>& status_info_list() const { return (m_status_info_list); } + }; + + + // + // response_data ::= "*" SPACE (resp_cond_state / resp_cond_bye / + // mailbox_data / message_data / capability_data) CRLF + // + + class response_data : public component + { + public: + + response_data() + : m_resp_cond_state(NULL), m_resp_cond_bye(NULL), + m_mailbox_data(NULL), m_message_data(NULL), m_capability_data(NULL) + { + } + + ~response_data() + { + delete (m_resp_cond_state); + delete (m_resp_cond_bye); + delete (m_mailbox_data); + delete (m_message_data); + delete (m_capability_data); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("response_data"); + + string::size_type pos = *currentPos; + + parser.check <one_char <'*'> >(line, &pos); + parser.check <SPACE>(line, &pos); + + if (!(m_resp_cond_state = parser.get <IMAPParser::resp_cond_state>(line, &pos, true))) + if (!(m_resp_cond_bye = parser.get <IMAPParser::resp_cond_bye>(line, &pos, true))) + if (!(m_mailbox_data = parser.get <IMAPParser::mailbox_data>(line, &pos, true))) + if (!(m_message_data = parser.get <IMAPParser::message_data>(line, &pos, true))) + m_capability_data = parser.get <IMAPParser::capability_data>(line, &pos); + + parser.check <CRLF>(line, &pos); + + *currentPos = pos; + } + + private: + + IMAPParser::resp_cond_state* m_resp_cond_state; + IMAPParser::resp_cond_bye* m_resp_cond_bye; + IMAPParser::mailbox_data* m_mailbox_data; + IMAPParser::message_data* m_message_data; + IMAPParser::capability_data* m_capability_data; + + public: + + const IMAPParser::resp_cond_state* resp_cond_state() const { return (m_resp_cond_state); } + const IMAPParser::resp_cond_bye* resp_cond_bye() const { return (m_resp_cond_bye); } + const IMAPParser::mailbox_data* mailbox_data() const { return (m_mailbox_data); } + const IMAPParser::message_data* message_data() const { return (m_message_data); } + const IMAPParser::capability_data* capability_data() const { return (m_capability_data); } + }; + + + class continue_req_or_response_data : public component + { + public: + + continue_req_or_response_data() + : m_continue_req(NULL), m_response_data(NULL) + { + } + + ~continue_req_or_response_data() + { + delete (m_continue_req); + delete (m_response_data); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("continue_req_or_response_data"); + + string::size_type pos = *currentPos; + + if (!(m_continue_req = parser.get <IMAPParser::continue_req>(line, &pos, true))) + m_response_data = parser.get <IMAPParser::response_data>(line, &pos); + + *currentPos = pos; + } + + private: + + IMAPParser::continue_req* m_continue_req; + IMAPParser::response_data* m_response_data; + + public: + + const IMAPParser::continue_req* continue_req() const { return (m_continue_req); } + const IMAPParser::response_data* response_data() const { return (m_response_data); } + }; + + + // + // response_fatal ::= "*" SPACE resp_cond_bye CRLF + // ;; Server closes connection immediately + // + + class response_fatal : public component + { + public: + + response_fatal() + : m_resp_cond_bye(NULL) + { + } + + ~response_fatal() + { + delete (m_resp_cond_bye); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("response_fatal"); + + string::size_type pos = *currentPos; + + parser.check <one_char <'*'> >(line, &pos); + parser.check <SPACE>(line, &pos); + + m_resp_cond_bye = parser.get <IMAPParser::resp_cond_bye>(line, &pos); + + parser.check <CRLF>(line, &pos); + + *currentPos = pos; + } + + private: + + IMAPParser::resp_cond_bye* m_resp_cond_bye; + + public: + + const IMAPParser::resp_cond_bye* resp_cond_bye() const { return (m_resp_cond_bye); } + }; + + + // + // response_tagged ::= tag SPACE resp_cond_state CRLF + // + + class response_tagged : public component + { + public: + + response_tagged() + : m_resp_cond_state(NULL) + { + } + + ~response_tagged() + { + delete (m_resp_cond_state); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("response_tagged"); + + string::size_type pos = *currentPos; + + parser.check <IMAPParser::xtag>(line, &pos); + parser.check <SPACE>(line, &pos); + m_resp_cond_state = parser.get <IMAPParser::resp_cond_state>(line, &pos); + parser.check <CRLF>(line, &pos); + + *currentPos = pos; + } + + private: + + IMAPParser::resp_cond_state* m_resp_cond_state; + + public: + + const IMAPParser::resp_cond_state* resp_cond_state() const { return (m_resp_cond_state); } + }; + + + // + // response_done ::= response_tagged / response_fatal + // + + class response_done : public component + { + public: + + response_done() + : m_response_tagged(NULL), m_response_fatal(NULL) + { + } + + ~response_done() + { + delete (m_response_tagged); + delete (m_response_fatal); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("response_done"); + + string::size_type pos = *currentPos; + + if (!(m_response_tagged = parser.get <IMAPParser::response_tagged>(line, &pos, true))) + m_response_fatal = parser.get <IMAPParser::response_fatal>(line, &pos); + + *currentPos = pos; + } + + private: + + IMAPParser::response_tagged* m_response_tagged; + IMAPParser::response_fatal* m_response_fatal; + + public: + + const IMAPParser::response_tagged* response_tagged() const { return (m_response_tagged); } + const IMAPParser::response_fatal* response_fatal() const { return (m_response_fatal); } + }; + + + // + // response ::= *(continue_req / response_data) response_done + // + + class response : public component + { + public: + + response() + : m_response_done(NULL) + { + } + + ~response() + { + for (std::vector <IMAPParser::continue_req_or_response_data*>::iterator + it = m_continue_req_or_response_data.begin() ; + it != m_continue_req_or_response_data.end() ; ++it) + { + delete (*it); + } + + delete (m_response_done); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("response"); + + string::size_type pos = *currentPos; + string curLine = line; + bool partial = false; // partial response + + IMAPParser::continue_req_or_response_data* resp = NULL; + + while ((resp = parser.get <IMAPParser::continue_req_or_response_data>(curLine, &pos, true)) != NULL) + { + m_continue_req_or_response_data.push_back(resp); + + // Partial response (continue_req) + if (resp->continue_req()) + { + partial = true; + break; + } + + // We have read a CRLF, read another line + curLine = parser.readLine(); + pos = 0; + } + + if (!partial) + m_response_done = parser.get <IMAPParser::response_done>(curLine, &pos); + + *currentPos = pos; + } + + + const bool isBad() const + { + if (!response_done()) // incomplete (partial) response + return (true); + + if (response_done()->response_fatal()) + return (true); + + if (response_done()->response_tagged()->resp_cond_state()-> + status() == IMAPParser::resp_cond_state::BAD) + { + return (true); + } + + return (false); + } + + private: + + std::vector <IMAPParser::continue_req_or_response_data*> m_continue_req_or_response_data; + IMAPParser::response_done* m_response_done; + + public: + + const std::vector <IMAPParser::continue_req_or_response_data*>& continue_req_or_response_data() const { return (m_continue_req_or_response_data); } + const IMAPParser::response_done* response_done() const { return (m_response_done); } + }; + + + // + // greeting ::= "*" SPACE (resp_cond_auth / resp_cond_bye) CRLF + // + + class greeting : public component + { + public: + + greeting() + : m_resp_cond_auth(NULL), m_resp_cond_bye(NULL) + { + } + + ~greeting() + { + delete (m_resp_cond_auth); + delete (m_resp_cond_bye); + } + + void go(IMAPParser& parser, string& line, string::size_type* currentPos) + { + DEBUG_ENTER_COMPONENT("greeting"); + + string::size_type pos = *currentPos; + + parser.check <one_char <'*'> >(line, &pos); + parser.check <SPACE>(line, &pos); + + if (!(m_resp_cond_auth = parser.get <IMAPParser::resp_cond_auth>(line, &pos, true))) + m_resp_cond_bye = parser.get <IMAPParser::resp_cond_bye>(line, &pos); + + parser.check <CRLF>(line, &pos); + + *currentPos = pos; + } + + private: + + IMAPParser::resp_cond_auth* m_resp_cond_auth; + IMAPParser::resp_cond_bye* m_resp_cond_bye; + + public: + + const IMAPParser::resp_cond_auth* resp_cond_auth() const { return (m_resp_cond_auth); } + const IMAPParser::resp_cond_bye* resp_cond_bye() const { return (m_resp_cond_bye); } + }; + + + + // + // The main functions used to parse a response + // + + response* readResponse(literalHandler* lh = NULL) + { + string::size_type pos = 0; + string line = readLine(); + + m_literalHandler = lh; + response* resp = get <response>(line, &pos); + m_literalHandler = NULL; + + return (resp); + } + + + greeting* readGreeting() + { + string::size_type pos = 0; + string line = readLine(); + + return get <greeting>(line, &pos); + } + + + // + // Get a token and advance + // + + template <class TYPE> + TYPE* get(string& line, string::size_type* currentPos, + const bool noThrow = false) + { + component* resp = new TYPE; + return internalGet <TYPE>(resp, line, currentPos, noThrow); + } + + + template <class TYPE, class ARG1_TYPE, class ARG2_TYPE> + TYPE* getWithArgs(string& line, string::size_type* currentPos, + ARG1_TYPE arg1, ARG2_TYPE arg2, const bool noThrow = false) + { + component* resp = new TYPE(arg1, arg2); + return internalGet <TYPE>(resp, line, currentPos, noThrow); + } + + +private: + + template <class TYPE> + TYPE* internalGet(component* resp, string& line, string::size_type* currentPos, + const bool noThrow = false) + { +#if DEBUG_RESPONSE + DEBUG_RESPONSE_level += " "; +#endif + + try + { + resp->go(*this, line, currentPos); + +#if DEBUG_RESPONSE + std::cout << DEBUG_RESPONSE_level << "SUCCESS! (" << DEBUG_RESPONSE_components.back() << ")" << std::endl; + + DEBUG_RESPONSE_level.erase(DEBUG_RESPONSE_level.begin() + DEBUG_RESPONSE_level.length() - 1); + DEBUG_RESPONSE_components.pop_back(); +#endif + } + catch (...) + { +#if DEBUG_RESPONSE + std::cout << DEBUG_RESPONSE_level << "FAILED! (" << DEBUG_RESPONSE_components.back() << ")" << std::endl; + + DEBUG_RESPONSE_level.erase(DEBUG_RESPONSE_level.begin() + DEBUG_RESPONSE_level.length() - 1); + DEBUG_RESPONSE_components.pop_back(); +#endif + + delete (resp); + if (!noThrow) throw; + return (NULL); + } + + return static_cast <TYPE*>(resp); + } + + +public: + + // + // Check a token and advance + // + + template <class TYPE> + const bool check(string& line, string::size_type* currentPos, + const bool noThrow = false) + { + try + { + TYPE term; + term.go(*this, line, currentPos); + +#if DEBUG_RESPONSE + std::cout << DEBUG_RESPONSE_level << "SUCCESS! (" << DEBUG_RESPONSE_components.back() << ")" << std::endl; + DEBUG_RESPONSE_components.pop_back(); +#endif + } + catch (...) + { +#if DEBUG_RESPONSE + std::cout << DEBUG_RESPONSE_level << "FAILED! (" << DEBUG_RESPONSE_components.back() << ")" << std::endl; + DEBUG_RESPONSE_components.pop_back(); +#endif + + if (!noThrow) throw; + return false; + } + + return true; + } + + template <class TYPE, class ARG_TYPE> + const bool checkWithArg(string& line, string::size_type* currentPos, + const ARG_TYPE arg, const bool noThrow = false) + { + try + { + TYPE term(arg); + term.go(*this, line, currentPos); + +#if DEBUG_RESPONSE + std::cout << DEBUG_RESPONSE_level << "SUCCESS! (" << DEBUG_RESPONSE_components.back() << ")" << std::endl; + DEBUG_RESPONSE_components.pop_back(); +#endif + } + catch (...) + { +#if DEBUG_RESPONSE + std::cout << DEBUG_RESPONSE_level << "FAILED! (" << DEBUG_RESPONSE_components.back() << ")" << std::endl; + DEBUG_RESPONSE_components.pop_back(); +#endif + + if (!noThrow) throw; + return false; + } + + return true; + } + + +private: + + weak_ref <IMAPTag> m_tag; + weak_ref <socket> m_socket; + + utility::progressionListener* m_progress; + + literalHandler* m_literalHandler; + + weak_ref <timeoutHandler> m_timeoutHandler; + + + string m_buffer; + int m_pos; + + string m_lastLine; + +public: + + // + // Read one line + // + + const string readLine() + { + string::size_type pos; + + while ((pos = m_buffer.find('\n')) == string::npos) + { + read(); + } + + string line; + line.resize(pos + 1); + std::copy(m_buffer.begin(), m_buffer.begin() + pos + 1, line.begin()); + + m_buffer.erase(m_buffer.begin(), m_buffer.begin() + pos + 1); + + m_lastLine = line; + +#if DEBUG_RESPONSE + std::cout << std::endl << "Read line:" << std::endl << line << std::endl; +#endif + + return (line); + } + + + // + // Read available data from socket stream + // + + void read() + { + string receiveBuffer; + + while (receiveBuffer.empty()) + { + // Check whether the time-out delay is elapsed + if (m_timeoutHandler && m_timeoutHandler->isTimeOut()) + { + if (!m_timeoutHandler->handleTimeOut()) + throw exceptions::operation_timed_out(); + } + + // We have received data: reset the time-out counter + m_socket->receive(receiveBuffer); + + if (receiveBuffer.empty()) // buffer is empty + { + platformDependant::getHandler()->wait(); + continue; + } + + // We have received data ... + if (m_timeoutHandler) + m_timeoutHandler->resetTimeOut(); + } + + m_buffer += receiveBuffer; + } + + + void readLiteral(literalHandler::target& buffer, string::size_type count) + { + string::size_type len = 0; + string receiveBuffer; + + if (m_progress) + m_progress->start(count); + + if (m_timeoutHandler) + m_timeoutHandler->resetTimeOut(); + + if (!m_buffer.empty()) + { + if (m_buffer.length() > count) + { + buffer.putData(string(m_buffer.begin(), m_buffer.begin() + count)); + m_buffer.erase(m_buffer.begin(), m_buffer.begin() + count); + len = count; + } + else + { + len += m_buffer.length(); + buffer.putData(m_buffer); + m_buffer.clear(); + } + } + + while (len < count) + { + // Check whether the time-out delay is elapsed + if (m_timeoutHandler && m_timeoutHandler->isTimeOut()) + { + if (!m_timeoutHandler->handleTimeOut()) + throw exceptions::operation_timed_out(); + } + + // Receive data from the socket + m_socket->receive(receiveBuffer); + + if (receiveBuffer.empty()) // buffer is empty + { + platformDependant::getHandler()->wait(); + continue; + } + + // We have received data: reset the time-out counter + if (m_timeoutHandler) + m_timeoutHandler->resetTimeOut(); + + if (len + receiveBuffer.length() > count) + { + const string::size_type remaining = count - len; + + // Get the needed amount of data + buffer.putData(string(receiveBuffer.begin(), receiveBuffer.begin() + remaining)); + + // Put the remaining data into the internal response buffer + receiveBuffer.erase(receiveBuffer.begin(), receiveBuffer.begin() + remaining); + m_buffer += receiveBuffer; + + len = count; + } + else + { + buffer.putData(receiveBuffer); + len += receiveBuffer.length(); + } + + // Notify progression + if (m_progress) + m_progress->progress(len, count); + } + + if (m_progress) + m_progress->stop(count); + } +}; + + +} // imap +} // net +} // vmime + + +#endif // VMIME_NET_IMAP_IMAPPARSER_HPP_INCLUDED diff --git a/vmime/net/imap/IMAPStore.hpp b/vmime/net/imap/IMAPStore.hpp new file mode 100644 index 00000000..887894f4 --- /dev/null +++ b/vmime/net/imap/IMAPStore.hpp @@ -0,0 +1,137 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_IMAP_IMAPSTORE_HPP_INCLUDED +#define VMIME_NET_IMAP_IMAPSTORE_HPP_INCLUDED + + +#include "vmime/config.hpp" + +#include "vmime/net/store.hpp" +#include "vmime/net/socket.hpp" +#include "vmime/net/folder.hpp" + +#include <ostream> + + +namespace vmime { +namespace net { +namespace imap { + + +class IMAPParser; +class IMAPTag; +class IMAPConnection; +class IMAPFolder; + + +/** IMAP store service. + */ + +class IMAPStore : public store +{ + friend class IMAPFolder; + friend class IMAPMessage; + friend class IMAPConnection; + +public: + + IMAPStore(ref <session> sess, ref <authenticator> auth); + ~IMAPStore(); + + const string getProtocolName() const; + + ref <folder> getDefaultFolder(); + ref <folder> getRootFolder(); + ref <folder> getFolder(const folder::path& path); + + const bool isValidFolderName(const folder::path::component& name) const; + + static const serviceInfos& getInfosInstance(); + const serviceInfos& getInfos() const; + + void connect(); + const bool isConnected() const; + void disconnect(); + + void noop(); + + const int getCapabilities() const; + +private: + + // Connection + ref <IMAPConnection> m_connection; + + // Used to request the authentication informations only the + // first time, and reuse these informations the next time. + ref <class authenticator> m_oneTimeAuth; + + + + ref <class authenticator> oneTimeAuthenticator(); + + + ref <IMAPConnection> connection(); + + + void registerFolder(IMAPFolder* folder); + void unregisterFolder(IMAPFolder* folder); + + std::list <IMAPFolder*> m_folders; + + + + // Service infos + class _infos : public serviceInfos + { + public: + + struct props + { + // IMAP-specific options + // (none) + + // Common properties + serviceInfos::property PROPERTY_AUTH_USERNAME; + serviceInfos::property PROPERTY_AUTH_PASSWORD; + + serviceInfos::property PROPERTY_SERVER_ADDRESS; + serviceInfos::property PROPERTY_SERVER_PORT; + serviceInfos::property PROPERTY_SERVER_SOCKETFACTORY; + + serviceInfos::property PROPERTY_TIMEOUT_FACTORY; + }; + + const props& getProperties() const; + + const string getPropertyPrefix() const; + const std::vector <serviceInfos::property> getAvailableProperties() const; + }; + + static _infos sm_infos; +}; + + +} // imap +} // net +} // vmime + + +#endif // VMIME_NET_IMAP_IMAPSTORE_HPP_INCLUDED diff --git a/vmime/net/imap/IMAPTag.hpp b/vmime/net/imap/IMAPTag.hpp new file mode 100644 index 00000000..6b20db75 --- /dev/null +++ b/vmime/net/imap/IMAPTag.hpp @@ -0,0 +1,66 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_IMAP_IMAPTAG_HPP_INCLUDED +#define VMIME_NET_IMAP_IMAPTAG_HPP_INCLUDED + + +#include "vmime/types.hpp" + + +namespace vmime { +namespace net { +namespace imap { + + +class IMAPTag : public object +{ +private: + + IMAPTag(const int number); + IMAPTag(const IMAPTag& tag); + +public: + + IMAPTag(); + + IMAPTag& operator++(); // ++IMAPTag + const IMAPTag operator++(int); // IMAPTag++ + + const int number() const; + + operator string() const; + +private: + + void generate(); + + static const int sm_maxNumber; + + int m_number; + string m_tag; +}; + + +} // imap +} // net +} // vmime + + +#endif // VMIME_NET_IMAP_IMAPTAG_HPP_INCLUDED diff --git a/vmime/net/imap/IMAPUtils.hpp b/vmime/net/imap/IMAPUtils.hpp new file mode 100644 index 00000000..1fb99e74 --- /dev/null +++ b/vmime/net/imap/IMAPUtils.hpp @@ -0,0 +1,68 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_IMAP_IMAPUTILS_HPP_INCLUDED +#define VMIME_NET_IMAP_IMAPUTILS_HPP_INCLUDED + + +#include "vmime/types.hpp" +#include "vmime/dateTime.hpp" + +#include "vmime/net/folder.hpp" +#include "vmime/net/imap/IMAPParser.hpp" + +#include <vector> + + +namespace vmime { +namespace net { +namespace imap { + + +class IMAPUtils +{ +public: + + static const string pathToString(const char hierarchySeparator, const folder::path& path); + static const folder::path stringToPath(const char hierarchySeparator, const string& str); + + static const string toModifiedUTF7(const char hierarchySeparator, const folder::path::component& text); + static const folder::path::component fromModifiedUTF7(const string& text); + + static const string quoteString(const string& text); + + static const int folderTypeFromFlags(const IMAPParser::mailbox_flag_list* list); + static const int folderFlagsFromFlags(const IMAPParser::mailbox_flag_list* list); + + static const int messageFlagsFromFlags(const IMAPParser::flag_list* list); + + static const string messageFlagList(const int flags); + + static const string listToSet(const std::vector <int>& list, const int max = -1, const bool alreadySorted = false); + + static const string dateTime(const vmime::datetime& date); +}; + + +} // imap +} // net +} // vmime + + +#endif // VMIME_NET_IMAP_IMAPUTILS_HPP_INCLUDED diff --git a/vmime/net/maildir/maildirFolder.hpp b/vmime/net/maildir/maildirFolder.hpp new file mode 100644 index 00000000..2ff4fca4 --- /dev/null +++ b/vmime/net/maildir/maildirFolder.hpp @@ -0,0 +1,178 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_MAILDIR_MAILDIRFOLDER_HPP_INCLUDED +#define VMIME_NET_MAILDIR_MAILDIRFOLDER_HPP_INCLUDED + + +#include <vector> +#include <map> + +#include "vmime/types.hpp" + +#include "vmime/net/folder.hpp" + +#include "vmime/utility/file.hpp" + + +namespace vmime { +namespace net { +namespace maildir { + + +class maildirStore; +class maildirMessage; + + +/** maildir folder implementation. + */ + +class maildirFolder : public folder +{ +private: + + friend class maildirStore; + friend class maildirMessage; + friend class vmime::creator; // vmime::create <maildirFolder> + + + maildirFolder(const folder::path& path, weak_ref <maildirStore> store); + maildirFolder(const maildirFolder&) : folder() { } + + ~maildirFolder(); + +public: + + const int getMode() const; + + const int getType(); + + const int getFlags(); + + const folder::path::component getName() const; + const folder::path getFullPath() const; + + void open(const int mode, bool failIfModeIsNotAvailable = false); + void close(const bool expunge); + void create(const int type); + + const bool exists(); + + const bool isOpen() const; + + ref <message> getMessage(const int num); + std::vector <ref <message> > getMessages(const int from = 1, const int to = -1); + std::vector <ref <message> > getMessages(const std::vector <int>& nums); + const int getMessageCount(); + + ref <folder> getFolder(const folder::path::component& name); + std::vector <ref <folder> > getFolders(const bool recursive = false); + + void rename(const folder::path& newPath); + + void deleteMessage(const int num); + void deleteMessages(const int from = 1, const int to = -1); + void deleteMessages(const std::vector <int>& nums); + + void setMessageFlags(const int from, const int to, const int flags, const int mode = message::FLAG_MODE_SET); + void setMessageFlags(const std::vector <int>& nums, const int flags, const int mode = message::FLAG_MODE_SET); + + void addMessage(ref <vmime::message> msg, const int flags = message::FLAG_UNDEFINED, vmime::datetime* date = NULL, utility::progressionListener* progress = NULL); + void addMessage(utility::inputStream& is, const int size, const int flags = message::FLAG_UNDEFINED, vmime::datetime* date = NULL, utility::progressionListener* progress = NULL); + + void copyMessage(const folder::path& dest, const int num); + void copyMessages(const folder::path& dest, const int from = 1, const int to = -1); + void copyMessages(const folder::path& dest, const std::vector <int>& nums); + + void status(int& count, int& unseen); + + void expunge(); + + ref <folder> getParent(); + + weak_ref <const store> getStore() const; + weak_ref <store> getStore(); + + + void fetchMessages(std::vector <ref <message> >& msg, const int options, utility::progressionListener* progress = NULL); + void fetchMessage(ref <message> msg, const int options); + + const int getFetchCapabilities() const; + +private: + + void scanFolder(); + + void listFolders(std::vector <ref <folder> >& list, const bool recursive); + + void registerMessage(maildirMessage* msg); + void unregisterMessage(maildirMessage* msg); + + const utility::file::path getMessageFSPath(const int number) const; + + void onStoreDisconnected(); + + void onClose(); + + void deleteMessagesImpl(const std::vector <int>& nums); + void setMessageFlagsImpl(const std::vector <int>& nums, const int flags, const int mode); + + void copyMessagesImpl(const folder::path& dest, const std::vector <int>& nums); + void copyMessageImpl(const utility::file::path& tmpDirPath, const utility::file::path& curDirPath, const utility::file::path::component& filename, utility::inputStream& is, const utility::stream::size_type size, utility::progressionListener* progress); + + void notifyMessagesCopied(const folder::path& dest); + + + weak_ref <maildirStore> m_store; + + folder::path m_path; + folder::path::component m_name; + + int m_mode; + bool m_open; + + int m_unreadMessageCount; + int m_messageCount; + + // Store information about scanned messages + struct messageInfos + { + enum Type + { + TYPE_CUR, + TYPE_DELETED + }; + + utility::file::path::component path; // filename + Type type; // current location + }; + + std::vector <messageInfos> m_messageInfos; + + // Instanciated message objects + std::vector <maildirMessage*> m_messages; +}; + + +} // maildir +} // net +} // vmime + + +#endif // VMIME_NET_MAILDIR_MAILDIRFOLDER_HPP_INCLUDED diff --git a/vmime/net/maildir/maildirMessage.hpp b/vmime/net/maildir/maildirMessage.hpp new file mode 100644 index 00000000..82aa9976 --- /dev/null +++ b/vmime/net/maildir/maildirMessage.hpp @@ -0,0 +1,103 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_MAILDIR_MAILDIRMESSAGE_HPP_INCLUDED +#define VMIME_NET_MAILDIR_MAILDIRMESSAGE_HPP_INCLUDED + + +#include "vmime/net/message.hpp" +#include "vmime/net/folder.hpp" + + +namespace vmime { +namespace net { +namespace maildir { + + +class maildirFolder; + + +/** maildir message implementation. + */ + +class maildirMessage : public message +{ + friend class maildirFolder; + friend class vmime::creator; // vmime::create <maildirMessage> + +private: + + maildirMessage(weak_ref <maildirFolder> folder, const int num); + maildirMessage(const maildirMessage&) : message() { } + + ~maildirMessage(); + +public: + + const int getNumber() const; + + const uid getUniqueId() const; + + const int getSize() const; + + const bool isExpunged() const; + + const structure& getStructure() const; + structure& getStructure(); + + ref <const header> getHeader() const; + + const int getFlags() const; + void setFlags(const int flags, const int mode = FLAG_MODE_SET); + + void extract(utility::outputStream& os, utility::progressionListener* progress = NULL, const int start = 0, const int length = -1, const bool peek = false) const; + void extractPart(const part& p, utility::outputStream& os, utility::progressionListener* progress = NULL, const int start = 0, const int length = -1, const bool peek = false) const; + + void fetchPartHeader(part& p); + +private: + + void fetch(weak_ref <maildirFolder> folder, const int options); + + void onFolderClosed(); + + ref <header> getOrCreateHeader(); + + void extractImpl(utility::outputStream& os, utility::progressionListener* progress, const int start, const int length, const int partialStart, const int partialLength, const bool peek) const; + + + weak_ref <maildirFolder> m_folder; + + int m_num; + int m_size; + int m_flags; + bool m_expunged; + uid m_uid; + + ref <header> m_header; + ref <structure> m_structure; +}; + + +} // maildir +} // net +} // vmime + + +#endif // VMIME_NET_MAILDIR_MAILDIRMESSAGE_HPP_INCLUDED diff --git a/vmime/net/maildir/maildirStore.hpp b/vmime/net/maildir/maildirStore.hpp new file mode 100644 index 00000000..32451808 --- /dev/null +++ b/vmime/net/maildir/maildirStore.hpp @@ -0,0 +1,114 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_MAILDIR_MAILDIRSTORE_HPP_INCLUDED +#define VMIME_NET_MAILDIR_MAILDIRSTORE_HPP_INCLUDED + + +#include "vmime/config.hpp" + +#include "vmime/net/store.hpp" +#include "vmime/net/socket.hpp" +#include "vmime/net/folder.hpp" + +#include "vmime/utility/file.hpp" + +#include <ostream> + + +namespace vmime { +namespace net { +namespace maildir { + + +class maildirFolder; + + +/** maildir store service. + */ + +class maildirStore : public store +{ + friend class maildirFolder; + +public: + + maildirStore(ref <session> sess, ref <authenticator> auth); + ~maildirStore(); + + const string getProtocolName() const; + + ref <folder> getDefaultFolder(); + ref <folder> getRootFolder(); + ref <folder> getFolder(const folder::path& path); + + const bool isValidFolderName(const folder::path::component& name) const; + + static const serviceInfos& getInfosInstance(); + const serviceInfos& getInfos() const; + + void connect(); + const bool isConnected() const; + void disconnect(); + + void noop(); + + const utility::path& getFileSystemPath() const; + + const int getCapabilities() const; + +private: + + void registerFolder(maildirFolder* folder); + void unregisterFolder(maildirFolder* folder); + + + std::list <maildirFolder*> m_folders; + + bool m_connected; + + utility::path m_fsPath; + + + // Service infos + class _infos : public serviceInfos + { + public: + + struct props + { + serviceInfos::property PROPERTY_SERVER_ROOTPATH; + }; + + const props& getProperties() const; + + const string getPropertyPrefix() const; + const std::vector <serviceInfos::property> getAvailableProperties() const; + }; + + static _infos sm_infos; +}; + + +} // maildir +} // net +} // vmime + + +#endif // VMIME_NET_MAILDIR_MAILDIRSTORE_HPP_INCLUDED diff --git a/vmime/net/maildir/maildirUtils.hpp b/vmime/net/maildir/maildirUtils.hpp new file mode 100644 index 00000000..06ecf69b --- /dev/null +++ b/vmime/net/maildir/maildirUtils.hpp @@ -0,0 +1,160 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_MAILDIR_MAILDIRUTILS_HPP_INCLUDED +#define VMIME_NET_MAILDIR_MAILDIRUTILS_HPP_INCLUDED + + +#include "vmime/utility/file.hpp" +#include "vmime/utility/path.hpp" + + +namespace vmime { +namespace net { +namespace maildir { + + +class maildirStore; + + +/** Miscellaneous helpers functions for maildir messaging system. + */ + +class maildirUtils +{ +public: + + /** Comparator for message filenames, based only on the + * unique identifier part of the filename. + */ + class messageIdComparator + { + public: + + messageIdComparator(const utility::file::path::component& comp); + + const bool operator()(const utility::file::path::component& other) const; + + private: + + const utility::file::path::component m_comp; + }; + + /** Mode for return value of getFolderFSPath(). */ + enum FolderFSPathMode + { + FOLDER_PATH_ROOT, /**< Root folder. Eg: ~/Mail/MyFolder */ + FOLDER_PATH_NEW, /**< Folder containing unread messages. Eg: ~/Mail/MyFolder/new */ + FOLDER_PATH_CUR, /**< Folder containing messages that have been seen. Eg: ~/Mail/MyFolder/cur */ + FOLDER_PATH_TMP, /**< Temporary folder used for reliable delivery. Eg: ~/Mail/MyFolder/tmp */ + FOLDER_PATH_CONTAINER /**< Container for sub-folders. Eg: ~/Mail/.MyFolder.directory */ + }; + + /** Return the path on the filesystem for the folder in specified store. + * + * @param store parent store + * @param folderPath path of the folder + * @param mode type of path to return (see FolderFSPathMode) + * @return filesystem path for the specified folder + */ + static const utility::file::path getFolderFSPath(weak_ref <maildirStore> store, + const utility::path& folderPath, const FolderFSPathMode mode); + + /** Test whether the specified file-system directory corresponds to + * a maildir sub-folder. The name of the directory should not start + * with '.' to be listed as a sub-folder. + * + * @param file reference to a file-system directory + * @return true if the specified directory is a maildir sub-folder, + * false otherwise + */ + static const bool isSubfolderDirectory(const utility::file& file); + + /** Test whether the specified file-system object is a message. + * + * @param file reference to a file-system object + * @return true if the specified object is a message file, + * false otherwise + */ + static const bool isMessageFile(const utility::file& file); + + /** Extract the unique identifier part of the message filename. + * Eg: for the filename "1071577232.28549.m03s:2,RS", it will + * return "1071577232.28549.m03s". + * + * @param filename filename part + * @return part of the filename that corresponds to the unique + * identifier of the message + */ + static const utility::file::path::component extractId(const utility::file::path::component& filename); + + /** Extract message flags from the specified message filename. + * Eg: for the filename "1071577232.28549.m03s:2,RS", it will + * return (message::FLAG_SEEN | message::FLAG_REPLIED). + * + * @param comp filename part + * @return message flags extracted from the specified filename + */ + static const int extractFlags(const utility::file::path::component& comp); + + /** Return a string representing the specified message flags. + * Eg: for (message::FLAG_SEEN | message::FLAG_REPLIED), it will + * return "RS". + * + * @param flags set of flags + * @return message flags in a string representation + */ + static const utility::file::path::component buildFlags(const int flags); + + /** Build a filename with the specified id and flags. + * + * @param id id part of the filename + * @param flags flags part of the filename + * @return message filename + */ + static const utility::file::path::component buildFilename(const utility::file::path::component& id, const utility::file::path::component& flags); + + /** Build a filename with the specified id and flags. + * + * @param id id part of the filename + * @param flags set of flags + * @return message filename + */ + static const utility::file::path::component buildFilename(const utility::file::path::component& id, const int flags); + + /** Generate a new unique message identifier. + * + * @return unique message id + */ + static const utility::file::path::component generateId(); + +private: + + static const vmime::word TMP_DIR; + static const vmime::word CUR_DIR; + static const vmime::word NEW_DIR; +}; + + +} // maildir +} // net +} // vmime + + +#endif // VMIME_NET_MAILDIR_MAILDIRUTILS_HPP_INCLUDED diff --git a/vmime/net/message.hpp b/vmime/net/message.hpp new file mode 100644 index 00000000..640f945c --- /dev/null +++ b/vmime/net/message.hpp @@ -0,0 +1,292 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_MESSAGE_HPP_INCLUDED +#define VMIME_NET_MESSAGE_HPP_INCLUDED + + +#include "vmime/header.hpp" + +#include "vmime/utility/progressionListener.hpp" +#include "vmime/utility/stream.hpp" + + +namespace vmime { +namespace net { + + +class structure; + + +/** A MIME part in a message. + */ + +class part : public object +{ +protected: + + part() { } + part(const part&) : object() { } + + virtual ~part() { } + +public: + + /** Return the structure of this part. + * + * @return structure of the part + */ + virtual const structure& getStructure() const = 0; + + /** Return the structure of this part. + * + * @return structure of the part + */ + virtual structure& getStructure() = 0; + + /** Return the header section for this part (you must fetch header + * before using this function: see message::fetchPartHeader). + * + * @return header section + */ + virtual const header& getHeader() const = 0; + + /** Return the media-type of the content in this part. + * + * @return content media type + */ + virtual const mediaType& getType() const = 0; + + /** Return the size of this part. + * + * @return size of the part (in bytes) + */ + virtual const int getSize() const = 0; + + /** Return the part sequence number (index) + * + * @return part number + */ + virtual const int getNumber() const = 0; // begin at 1 + + /** Return the sub-part at the specified position. + * This provide easy access to parts: + * Eg: "message->extractPart(message->getStructure()[3][1][2])". + * + * @param x index of the sub-part + * @return sub-part at position 'x' + */ + const part& operator[](const int x) const; + + /** Return the sub-part at the specified position. + * This provide easy access to parts: + * Eg: "message->extractPart(message->getStructure()[3][1][2])". + * + * @param x index of the sub-part + * @return sub-part at position 'x' + */ + part& operator[](const int x); + + /** Return the number of sub-parts in this part. + * + * @return number of sub-parts + */ + const int getCount() const; +}; + + +/** Structure of a MIME part/message. + */ + +class structure : public object +{ +protected: + + structure() { } + structure(const structure&) : object() { } + +public: + + virtual ~structure() { } + + /** Return the part at the specified position (first + * part is at position 1). + * + * @param x position + * @return part at position 'x' + */ + virtual const part& operator[](const int x) const = 0; + + /** Return the part at the specified position (first + * part is at position 1). + * + * @param x position + * @return part at position 'x' + */ + virtual part& operator[](const int x) = 0; + + /** Return the number of parts in this part. + * + * @return number of parts + */ + virtual const int getCount() const = 0; +}; + + +/** Abstract representation of a message in a store/transport service. + */ + +class message : public object +{ +protected: + + message() { } + message(const message&) : object() { } + +public: + + virtual ~message() { } + + /** The type for an unique message identifier. + */ + typedef string uid; + + /** Return the MIME structure of the message (must fetch before). + * + * @return MIME structure of the message + */ + virtual const structure& getStructure() const = 0; + + /** Return the MIME structure of the message (must fetch before). + * + * @return MIME structure of the message + */ + virtual structure& getStructure() = 0; + + /** Return a reference to the header fields of the message (must fetch before). + * + * @return header section of the message + */ + virtual ref <const header> getHeader() const = 0; + + /** Return the sequence number of this message. This number is + * used to reference the message in the folder. + * + * @return sequence number of the message + */ + virtual const int getNumber() const = 0; + + /** Return the unique identified of this message (must fetch before). + * + * @return UID of the message + */ + virtual const uid getUniqueId() const = 0; + + /** Return the size of the message (must fetch before). + * + * @return size of the message (in bytes) + */ + virtual const int getSize() const = 0; + + /** Check whether this message has been expunged + * (ie: definitively deleted). + * + * @return true if the message is expunged, false otherwise + */ + virtual const bool isExpunged() const = 0; + + /** Possible flags for a message. + */ + enum Flags + { + FLAG_SEEN = (1 << 0), /**< Message has been seen. */ + FLAG_RECENT = (1 << 1), /**< Message has been recently received. */ + FLAG_DELETED = (1 << 2), /**< Message is marked for deletion. */ + FLAG_REPLIED = (1 << 3), /**< User replied to this message. */ + FLAG_MARKED = (1 << 4), /**< Used-defined flag. */ + FLAG_PASSED = (1 << 5), /**< Message has been resent/forwarded/bounced. */ + + FLAG_UNDEFINED = 9999 /**< Used internally (this should not be returned + by the flags() function). */ + }; + + /** Methods for setting the flags. + */ + enum FlagsModes + { + FLAG_MODE_SET, /**< Set (replace) the flags. */ + FLAG_MODE_ADD, /**< Add the flags. */ + FLAG_MODE_REMOVE /**< Remove the flags. */ + }; + + /** Return the flags of this message. + * + * @return flags of the message + */ + virtual const int getFlags() const = 0; + + /** Set the flags of this message. + * + * @param flags set of flags (see Flags) + * @param mode indicate how to treat old and new flags (see FlagsModes) + */ + virtual void setFlags(const int flags, const int mode = FLAG_MODE_SET) = 0; + + /** Extract the whole message data (header + contents). + * + * \warning Partial fetch might not be supported by the underlying protocol. + * + * @param os output stream in which to write message data + * @param progress progression listener, or NULL if not used + * @param start index of the first byte to retrieve (used for partial fetch) + * @param length number of bytes to retrieve (used for partial fetch) + * @param peek if true, try not to mark the message as read. This may not + * be supported by the protocol (IMAP supports this), but it will NOT throw + * an exception if not supported. + */ + virtual void extract(utility::outputStream& os, utility::progressionListener* progress = NULL, const int start = 0, const int length = -1, const bool peek = false) const = 0; + + /** Extract the specified MIME part of the message (header + contents). + * + * \warning Partial fetch might not be supported by the underlying protocol. + * + * @param p part to extract + * @param os output stream in which to write part data + * @param progress progression listener, or NULL if not used + * @param start index of the first byte to retrieve (used for partial fetch) + * @param length number of bytes to retrieve (used for partial fetch) + * @param peek if true, try not to mark the message as read. This may not + * be supported by the protocol (IMAP supports this), but it will NOT throw + * an exception if not supported. + */ + virtual void extractPart(const part& p, utility::outputStream& os, utility::progressionListener* progress = NULL, const int start = 0, const int length = -1, const bool peek = false) const = 0; + + /** Fetch the MIME header for the specified part. + * + * @param p the part for which to fetch the header + */ + virtual void fetchPartHeader(part& p) = 0; +}; + + +} // net +} // vmime + + +#endif // VMIME_NET_MESSAGE_HPP_INCLUDED diff --git a/vmime/net/pop3/POP3Folder.hpp b/vmime/net/pop3/POP3Folder.hpp new file mode 100644 index 00000000..71f4d79f --- /dev/null +++ b/vmime/net/pop3/POP3Folder.hpp @@ -0,0 +1,148 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_POP3_POP3FOLDER_HPP_INCLUDED +#define VMIME_NET_POP3_POP3FOLDER_HPP_INCLUDED + + +#include <vector> +#include <map> + +#include "vmime/config.hpp" +#include "vmime/types.hpp" + +#include "vmime/net/folder.hpp" + + +namespace vmime { +namespace net { +namespace pop3 { + + +class POP3Store; +class POP3Message; + + +/** POP3 folder implementation. + */ + +class POP3Folder : public folder +{ +private: + + friend class POP3Store; + friend class POP3Message; + friend class vmime::creator; // vmime::create <POP3Folder> + + POP3Folder(const folder::path& path, POP3Store* store); + POP3Folder(const POP3Folder&) : folder() { } + + ~POP3Folder(); + +public: + + const int getMode() const; + + const int getType(); + + const int getFlags(); + + const folder::path::component getName() const; + const folder::path getFullPath() const; + + void open(const int mode, bool failIfModeIsNotAvailable = false); + void close(const bool expunge); + void create(const int type); + + const bool exists(); + + const bool isOpen() const; + + ref <message> getMessage(const int num); + std::vector <ref <message> > getMessages(const int from = 1, const int to = -1); + std::vector <ref <message> > getMessages(const std::vector <int>& nums); + const int getMessageCount(); + + ref <folder> getFolder(const folder::path::component& name); + std::vector <ref <folder> > getFolders(const bool recursive = false); + + void rename(const folder::path& newPath); + + void deleteMessage(const int num); + void deleteMessages(const int from = 1, const int to = -1); + void deleteMessages(const std::vector <int>& nums); + + void setMessageFlags(const int from, const int to, const int flags, const int mode = message::FLAG_MODE_SET); + void setMessageFlags(const std::vector <int>& nums, const int flags, const int mode = message::FLAG_MODE_SET); + + void addMessage(ref <vmime::message> msg, const int flags = message::FLAG_UNDEFINED, vmime::datetime* date = NULL, utility::progressionListener* progress = NULL); + void addMessage(utility::inputStream& is, const int size, const int flags = message::FLAG_UNDEFINED, vmime::datetime* date = NULL, utility::progressionListener* progress = NULL); + + void copyMessage(const folder::path& dest, const int num); + void copyMessages(const folder::path& dest, const int from = 1, const int to = -1); + void copyMessages(const folder::path& dest, const std::vector <int>& nums); + + void status(int& count, int& unseen); + + void expunge(); + + ref <folder> getParent(); + + weak_ref <const store> getStore() const; + weak_ref <store> getStore(); + + + void fetchMessages(std::vector <ref <message> >& msg, const int options, utility::progressionListener* progress = NULL); + void fetchMessage(ref <message> msg, const int options); + + const int getFetchCapabilities() const; + +private: + + void registerMessage(POP3Message* msg); + void unregisterMessage(POP3Message* msg); + + void onStoreDisconnected(); + + void onClose(); + + void parseMultiListOrUidlResponse(const string& response, std::map <int, string>& result); + + + POP3Store* m_store; + + folder::path m_path; + folder::path::component m_name; + + int m_mode; + bool m_open; + + int m_messageCount; + + typedef std::map <POP3Message*, int> MessageMap; + MessageMap m_messages; +}; + + +} // pop3 +} // net +} // vmime + + +#endif // VMIME_NET_POP3_POP3FOLDER_HPP_INCLUDED diff --git a/vmime/net/pop3/POP3Message.hpp b/vmime/net/pop3/POP3Message.hpp new file mode 100644 index 00000000..f4c48bb6 --- /dev/null +++ b/vmime/net/pop3/POP3Message.hpp @@ -0,0 +1,98 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_POP3_POP3MESSAGE_HPP_INCLUDED +#define VMIME_NET_POP3_POP3MESSAGE_HPP_INCLUDED + + +#include "vmime/config.hpp" + +#include "vmime/net/message.hpp" +#include "vmime/net/folder.hpp" + + +namespace vmime { +namespace net { +namespace pop3 { + + +class POP3Folder; + + +/** POP3 message implementation. + */ + +class POP3Message : public message +{ +private: + + friend class POP3Folder; + friend class vmime::creator; // vmime::create <POP3Message> + + POP3Message(POP3Folder* folder, const int num); + POP3Message(const POP3Message&) : message() { } + + ~POP3Message(); + +public: + + const int getNumber() const; + + const uid getUniqueId() const; + + const int getSize() const; + + const bool isExpunged() const; + + const structure& getStructure() const; + structure& getStructure(); + + ref <const header> getHeader() const; + + const int getFlags() const; + void setFlags(const int flags, const int mode = FLAG_MODE_SET); + + void extract(utility::outputStream& os, utility::progressionListener* progress = NULL, const int start = 0, const int length = -1, const bool peek = false) const; + void extractPart(const part& p, utility::outputStream& os, utility::progressionListener* progress = NULL, const int start = 0, const int length = -1, const bool peek = false) const; + + void fetchPartHeader(part& p); + +private: + + void fetch(POP3Folder* folder, const int options); + + void onFolderClosed(); + + POP3Folder* m_folder; + int m_num; + uid m_uid; + int m_size; + + bool m_deleted; + + ref <header> m_header; +}; + + +} // pop3 +} // net +} // vmime + + +#endif // VMIME_NET_POP3_POP3MESSAGE_HPP_INCLUDED diff --git a/vmime/net/pop3/POP3Store.hpp b/vmime/net/pop3/POP3Store.hpp new file mode 100644 index 00000000..e50bbeef --- /dev/null +++ b/vmime/net/pop3/POP3Store.hpp @@ -0,0 +1,138 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_POP3_POP3STORE_HPP_INCLUDED +#define VMIME_NET_POP3_POP3STORE_HPP_INCLUDED + + +#include "vmime/config.hpp" + +#include "vmime/net/store.hpp" +#include "vmime/net/socket.hpp" +#include "vmime/net/timeoutHandler.hpp" + +#include "vmime/utility/stream.hpp" + + +namespace vmime { +namespace net { +namespace pop3 { + + +class POP3Folder; + + +/** POP3 store service. + */ + +class POP3Store : public store +{ + friend class POP3Folder; + friend class POP3Message; + +public: + + POP3Store(ref <session> sess, ref <authenticator> auth); + ~POP3Store(); + + const string getProtocolName() const; + + ref <folder> getDefaultFolder(); + ref <folder> getRootFolder(); + ref <folder> getFolder(const folder::path& path); + + const bool isValidFolderName(const folder::path::component& name) const; + + static const serviceInfos& getInfosInstance(); + const serviceInfos& getInfos() const; + + void connect(); + const bool isConnected() const; + void disconnect(); + + void noop(); + + const int getCapabilities() const; + +private: + + static const bool isSuccessResponse(const string& buffer); + static const bool stripFirstLine(const string& buffer, string& result, string* firstLine = NULL); + static void stripResponseCode(const string& buffer, string& result); + + void sendRequest(const string& buffer, const bool end = true); + void readResponse(string& buffer, const bool multiLine, utility::progressionListener* progress = NULL); + void readResponse(utility::outputStream& os, utility::progressionListener* progress = NULL, const int predictedSize = 0); + + static const bool checkTerminator(string& buffer, const bool multiLine); + static const bool checkOneTerminator(string& buffer, const string& term); + + void internalDisconnect(); + + + void registerFolder(POP3Folder* folder); + void unregisterFolder(POP3Folder* folder); + + std::list <POP3Folder*> m_folders; + + + ref <socket> m_socket; + bool m_authentified; + + ref <timeoutHandler> m_timeoutHandler; + + + // Service infos + class _infos : public serviceInfos + { + public: + + struct props + { + // POP3-specific options + serviceInfos::property PROPERTY_OPTIONS_APOP; + serviceInfos::property PROPERTY_OPTIONS_APOP_FALLBACK; + + // Common properties + serviceInfos::property PROPERTY_AUTH_USERNAME; + serviceInfos::property PROPERTY_AUTH_PASSWORD; + + serviceInfos::property PROPERTY_SERVER_ADDRESS; + serviceInfos::property PROPERTY_SERVER_PORT; + serviceInfos::property PROPERTY_SERVER_SOCKETFACTORY; + + serviceInfos::property PROPERTY_TIMEOUT_FACTORY; + }; + + const props& getProperties() const; + + const string getPropertyPrefix() const; + const std::vector <serviceInfos::property> getAvailableProperties() const; + }; + + static _infos sm_infos; +}; + + +} // pop3 +} // net +} // vmime + + +#endif // VMIME_NET_POP3_POP3STORE_HPP_INCLUDED diff --git a/vmime/net/sendmail/sendmailTransport.hpp b/vmime/net/sendmail/sendmailTransport.hpp new file mode 100644 index 00000000..292f5ebe --- /dev/null +++ b/vmime/net/sendmail/sendmailTransport.hpp @@ -0,0 +1,103 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_SENDMAIL_SENDMAILTRANSPORT_HPP_INCLUDED +#define VMIME_NET_SENDMAIL_SENDMAILTRANSPORT_HPP_INCLUDED + + +#include "vmime/config.hpp" + +#include "vmime/net/transport.hpp" +#include "vmime/net/socket.hpp" +#include "vmime/net/timeoutHandler.hpp" + + +#if VMIME_BUILTIN_PLATFORM_POSIX + + +namespace vmime { +namespace net { +namespace sendmail { + + +/** Sendmail local transport service. + */ + +class sendmailTransport : public transport +{ +public: + + sendmailTransport(ref <session> sess, ref <authenticator> auth); + ~sendmailTransport(); + + const string getProtocolName() const; + + static const serviceInfos& getInfosInstance(); + const serviceInfos& getInfos() const; + + void connect(); + const bool isConnected() const; + void disconnect(); + + void noop(); + + void send(const mailbox& expeditor, const mailboxList& recipients, utility::inputStream& is, const utility::stream::size_type size, utility::progressionListener* progress = NULL); + +private: + + void internalDisconnect(); + + void internalSend(const std::vector <string> args, utility::inputStream& is, + const utility::stream::size_type size, utility::progressionListener* progress); + + + string m_sendmailPath; + + bool m_connected; + + + // Service infos + class _infos : public serviceInfos + { + public: + + struct props + { + serviceInfos::property PROPERTY_BINPATH; + }; + + const props& getProperties() const; + + const string getPropertyPrefix() const; + const std::vector <serviceInfos::property> getAvailableProperties() const; + }; + + static _infos sm_infos; +}; + + +} // sendmail +} // net +} // vmime + + +#endif // VMIME_BUILTIN_PLATFORM_POSIX + + +#endif // VMIME_NET_SENDMAIL_SENDMAILTRANSPORT_HPP_INCLUDED diff --git a/vmime/net/service.hpp b/vmime/net/service.hpp new file mode 100644 index 00000000..cfb5c736 --- /dev/null +++ b/vmime/net/service.hpp @@ -0,0 +1,161 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_SERVICE_HPP_INCLUDED +#define VMIME_NET_SERVICE_HPP_INCLUDED + + +#include "vmime/types.hpp" + +#include "vmime/net/session.hpp" +#include "vmime/net/authenticator.hpp" + +#include "vmime/net/serviceFactory.hpp" +#include "vmime/net/serviceInfos.hpp" + +#include "vmime/utility/progressionListener.hpp" + + +namespace vmime { +namespace net { + + +/** Base class for messaging services. + */ + +class service : public object +{ +protected: + + service(ref <session> sess, const serviceInfos& infos, ref <authenticator> auth); + +public: + + virtual ~service(); + + // Possible service types + enum Type + { + TYPE_STORE = 0, /**< The service is a message store. */ + TYPE_TRANSPORT /**< The service sends messages. */ + }; + + /** Return the type of service. + * + * @return type of service + */ + virtual const Type getType() const = 0; + + /** Return the protocol name of this service. + * + * @return protocol name + */ + virtual const string getProtocolName() const = 0; + + /** Return the session object associated with this service instance. + * + * @return session object + */ + ref <const session> getSession() const; + + /** Return the session object associated with this service instance. + * + * @return session object + */ + ref <session> getSession(); + + /** Return information about this service. + * + * @return information about the service + */ + virtual const serviceInfos& getInfos() const = 0; + + /** Connect to service. + */ + virtual void connect() = 0; + + /** Disconnect from service. + */ + virtual void disconnect() = 0; + + /** Test whether this service is connected. + * + * @return true if the service is connected, false otherwise + */ + virtual const bool isConnected() const = 0; + + /** Do nothing but ensure the server do not disconnect (for + * example, this can reset the auto-logout timer on the + * server, if one exists). + */ + virtual void noop() = 0; + + /** Return the authenticator object used with this service instance. + * + * @return authenticator object + */ + ref <const authenticator> getAuthenticator() const; + + /** Return the authenticator object used with this service instance. + * + * @return authenticator object + */ + ref <authenticator> getAuthenticator(); + + /** Set a property for this service (service prefix is added automatically). + * + * WARNING: this sets the property on the session object, so all service + * instances created with the session object will inherit the property. + * + * @param name property name + * @param value property value + */ + template <typename TYPE> + void setProperty(const string& name, const TYPE& value) + { + m_session->getProperties()[getInfos().getPropertyPrefix() + name] = value; + } + +#ifndef VMIME_BUILDING_DOC + // Basic service registerer + template <class S> + class initializer + { + public: + + initializer(const string& protocol) + { + serviceFactory::getInstance()-> + template registerServiceByProtocol <S>(protocol); + } + }; +#endif // VMIME_BUILDING_DOC + +private: + + ref <session> m_session; + ref <authenticator> m_auth; +}; + + +} // net +} // vmime + + +#endif // VMIME_NET_SERVICE_HPP_INCLUDED diff --git a/vmime/net/serviceFactory.hpp b/vmime/net/serviceFactory.hpp new file mode 100644 index 00000000..2eb04e0e --- /dev/null +++ b/vmime/net/serviceFactory.hpp @@ -0,0 +1,188 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_SERVICEFACTORY_HPP_INCLUDED +#define VMIME_NET_SERVICEFACTORY_HPP_INCLUDED + + +#include <map> + +#include "vmime/types.hpp" +#include "vmime/base.hpp" + +#include "vmime/utility/stringUtils.hpp" +#include "vmime/utility/url.hpp" + +#include "vmime/net/serviceInfos.hpp" +#include "vmime/net/authenticator.hpp" +#include "vmime/net/timeoutHandler.hpp" + +#include "vmime/utility/progressionListener.hpp" + + +namespace vmime { +namespace net { + + +class service; +class session; + + +/** A factory to create 'service' objects for a specified protocol. + */ + +class serviceFactory +{ +private: + + serviceFactory(); + ~serviceFactory(); + +public: + + static serviceFactory* getInstance(); + + /** Information about a registered service. */ + class registeredService : public object + { + friend class serviceFactory; + + protected: + + virtual ~registeredService() { } + + public: + + virtual ref <service> create(ref <session> sess, ref <authenticator> auth) const = 0; + + virtual const string& getName() const = 0; + virtual const serviceInfos& getInfos() const = 0; + }; + +private: + + template <class S> + class registeredServiceImpl : public registeredService + { + friend class serviceFactory; + friend class vmime::creator; + + protected: + + registeredServiceImpl(const string& name) + : m_name(name), m_servInfos(S::getInfosInstance()) + { + } + + public: + + ref <service> create(ref <session> sess, ref <authenticator> auth) const + { + return vmime::create <S>(sess, auth); + } + + const serviceInfos& getInfos() const + { + return (m_servInfos); + } + + const string& getName() const + { + return (m_name); + } + + private: + + const string m_name; + const serviceInfos& m_servInfos; + }; + + std::vector <ref <registeredService> > m_services; + +public: + + /** Register a new service by its protocol name. + * + * @param protocol protocol name + */ + template <class S> + void registerServiceByProtocol(const string& protocol) + { + const string name = utility::stringUtils::toLower(protocol); + m_services.push_back(vmime::create <registeredServiceImpl <S> >(name)); + } + + /** Create a new service instance from a protocol name. + * + * @param sess session + * @param protocol protocol name (eg. "pop3") + * @param auth authenticator used to provide credentials (can be NULL if not used) + * @return a new service instance for the specified protocol + * @throw exceptions::no_service_available if no service is registered + * for this protocol + */ + ref <service> create(ref <session> sess, const string& protocol, ref <authenticator> auth = NULL); + + /** Create a new service instance from a URL. + * + * @param sess session + * @param u full URL with at least protocol and server (you can also specify + * port, username and password) + * @param auth authenticator used to provide credentials (can be NULL if not used) + * @return a new service instance for the specified protocol + * @throw exceptions::no_service_available if no service is registered + * for this protocol + */ + ref <service> create(ref <session> sess, const utility::url& u, ref <authenticator> auth = NULL); + + /** Return information about a registered protocol. + * + * @param protocol protocol name + * @return information about this protocol + * @throw exceptions::no_service_available if no service is registered + * for this protocol + */ + ref <const registeredService> getServiceByProtocol(const string& protocol) const; + + /** Return the number of registered services. + * + * @return number of registered services + */ + const int getServiceCount() const; + + /** Return the registered service at the specified position. + * + * @param pos position of the registered service to return + * @return registered service at the specified position + */ + ref <const registeredService> getServiceAt(const int pos) const; + + /** Return a list of all registered services. + * + * @return list of registered services + */ + const std::vector <ref <const registeredService> > getServiceList() const; +}; + + +} // net +} // vmime + + +#endif // VMIME_NET_SERVICEFACTORY_HPP_INCLUDED diff --git a/vmime/net/serviceInfos.hpp b/vmime/net/serviceInfos.hpp new file mode 100644 index 00000000..8a343667 --- /dev/null +++ b/vmime/net/serviceInfos.hpp @@ -0,0 +1,228 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_SERVICEINFOS_HPP_INCLUDED +#define VMIME_NET_SERVICEINFOS_HPP_INCLUDED + + +#include <vector> + +#include "vmime/types.hpp" + +#include "vmime/net/session.hpp" + + +namespace vmime { +namespace net { + + +/** Stores information about a messaging service. + */ + +class serviceInfos +{ + friend class serviceFactory; + +protected: + + serviceInfos(); + serviceInfos(const serviceInfos&); + +private: + + serviceInfos& operator=(const serviceInfos&); + +public: + + virtual ~serviceInfos(); + + + /** A service property. + */ + class property + { + public: + + /** The common property 'server.address' which is + * the host name or the IP address of the server. */ + static const property SERVER_ADDRESS; + + /** The common property 'server.port' which is + * the port used to connect to the server. */ + static const property SERVER_PORT; + + /** The common property 'server.rootpath' which is + * the full path of the folder on the server (for + * maildir, this is the local filesystem directory). */ + static const property SERVER_ROOTPATH; + + /** The common property 'server.socket-factory' used + * to indicate which factory to use to instanciate + * new socket objects. */ + static const property SERVER_SOCKETFACTORY; + + /** The common property 'auth.username' which is the + * username used to authenticate with the server. */ + static const property AUTH_USERNAME; + + /** The common property 'auth.password' which is the + * password used to authenticate with the server. */ + static const property AUTH_PASSWORD; + + /** The common property 'timeout.factory' used to + * specify which factory to use to instanciate + * time-out handler objects. If none is specified, + * no time-out handler is used. */ + static const property TIMEOUT_FACTORY; + + + /** Value types. + */ + enum Types + { + TYPE_INTEGER, /*< Integer number. */ + TYPE_STRING, /*< Character string. */ + TYPE_BOOL, /*< Boolean (true or false). */ + + TYPE_DEFAULT = TYPE_STRING + }; + + /** Property flags. + */ + enum Flags + { + FLAG_NONE = 0, /*< No flags. */ + FLAG_REQUIRED = (1 << 0), /*< The property must be valued. */ + FLAG_HIDDEN = (1 << 1), /*< The property should not be shown + to the user but can be modified. */ + + FLAG_DEFAULT = FLAG_NONE /*< Default flags. */ + }; + + + /** Construct a new property. + * + * @param name property name + * @param type value type + * @param defaultValue default value + * @param flags property attributes + */ + property(const string& name, const Types type, const string& defaultValue = "", const int flags = FLAG_DEFAULT); + + /** Construct a new property from an existing property. + * + * @param p source property + * @param addFlags flags to add + * @param removeFlags flags to remove + */ + property(const property& p, const int addFlags = FLAG_NONE, const int removeFlags = FLAG_NONE); + + /** Construct a new property from an existing property. + * + * @param p source property + * @param newDefaultValue new default value + * @param addFlags flags to add + * @param removeFlags flags to remove + */ + property(const property& p, const string& newDefaultValue, const int addFlags = FLAG_NONE, const int removeFlags = FLAG_NONE); + + property& operator=(const property& p); + + /** Return the name of the property. + * + * @return property name + */ + const string& getName() const; + + /** Return the default value of the property or + * an empty string if there is no default value. + * + * @return default value for the property + */ + const string& getDefaultValue() const; + + /** Return the value type of the property. + * + * @return property value type + */ + const Types getType() const; + + /** Return the attributes of the property (see + * serviceInfos::property::Types constants). + * + * @return property attributes + */ + const int getFlags() const; + + private: + + string m_name; + string m_defaultValue; + Types m_type; + int m_flags; + }; + + + /** Return the property prefix used by this service. + * Use this to set/get properties in the session object. + * + * @return property prefix + */ + virtual const string getPropertyPrefix() const = 0; + + /** Return a list of available properties for this service. + * + * @return list of properties + */ + virtual const std::vector <property> getAvailableProperties() const = 0; + + /** Helper function to retrieve the value of a property. + * + * @param s session object + * @param p property to retrieve + * @throw exceptions::no_such_property if the property does not exist + * and has the flag property::FLAG_REQUIRED + * @return value of the property + */ + template <typename TYPE> + const TYPE getPropertyValue(ref <session> s, const property& p) const + { + if (p.getFlags() & property::FLAG_REQUIRED) + return s->getProperties()[getPropertyPrefix() + p.getName()].template getValue <TYPE>(); + + return s->getProperties().template getProperty <TYPE>(getPropertyPrefix() + p.getName(), + propertySet::valueFromString <TYPE>(p.getDefaultValue())); + } + + /** Helper function to test if the specified property is set in + * the session object. + * + * @param s session object + * @param p property to test + * @return true if the property is set, false otherwise + */ + const bool hasProperty(ref <session> s, const property& p) const; +}; + + +} // net +} // vmime + + +#endif // VMIME_NET_SERVICEINFOS_HPP_INCLUDED diff --git a/vmime/net/session.hpp b/vmime/net/session.hpp new file mode 100644 index 00000000..c01ae5f2 --- /dev/null +++ b/vmime/net/session.hpp @@ -0,0 +1,135 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_SESSION_HPP_INCLUDED +#define VMIME_NET_SESSION_HPP_INCLUDED + + +#include "vmime/net/authenticator.hpp" + +#include "vmime/utility/url.hpp" + +#include "vmime/propertySet.hpp" + + +namespace vmime { +namespace net { + + +class store; +class transport; + + +/** An object that contains all the information needed + * for connection to a service. + */ + +class session : public object +{ +public: + + session(); + session(const session& sess); + session(const propertySet& props); + + virtual ~session(); + + /** Return a transport service instance for the protocol specified + * in the session properties. + * + * The property "transport.protocol" specify the protocol to use. + * + * @param auth authenticator object to use for the new transport service. If + * NULL, a default one is used. The default authenticator simply return user + * credentials by reading the session properties "auth.username" and "auth.password". + * @return a new transport service + */ + ref <transport> getTransport(ref <authenticator> auth = NULL); + + /** Return a transport service instance for the specified protocol. + * + * @param protocol transport protocol to use (eg. "smtp") + * @param auth authenticator object to use for the new transport service. If + * NULL, a default one is used. The default authenticator simply return user + * credentials by reading the session properties "auth.username" and "auth.password". + * @return a new transport service + */ + ref <transport> getTransport(const string& protocol, ref <authenticator> auth = NULL); + + /** Return a transport service instance for the specified URL. + * + * @param url full URL with at least the protocol to use (eg: "smtp://myserver.com/") + * @param auth authenticator object to use for the new transport service. If + * NULL, a default one is used. The default authenticator simply return user + * credentials by reading the session properties "auth.username" and "auth.password". + * @return a new transport service + */ + ref <transport> getTransport(const utility::url& url, ref <authenticator> auth = NULL); + + /** Return a transport service instance for the protocol specified + * in the session properties. + * + * The property "store.protocol" specify the protocol to use. + * + * @param auth authenticator object to use for the new store service. If + * NULL, a default one is used. The default authenticator simply return user + * credentials by reading the session properties "auth.username" and "auth.password". + * @return a new store service + */ + ref <store> getStore(ref <authenticator> auth = NULL); + + /** Return a store service instance for the specified protocol. + * + * @param protocol store protocol to use (eg. "imap") + * @param auth authenticator object to use for the new store service. If + * NULL, a default one is used. The default authenticator simply return user + * credentials by reading the session properties "auth.username" and "auth.password". + * @return a new store service + */ + ref <store> getStore(const string& protocol, ref <authenticator> auth = NULL); + + /** Return a store service instance for the specified URL. + * + * @param url full URL with at least the protocol to use (eg: "imap://username:[email protected]/") + * @param auth authenticator object to use for the new store service. If + * NULL, a default one is used. The default authenticator simply return user + * credentials by reading the session properties "auth.username" and "auth.password". + * @return a new store service + */ + ref <store> getStore(const utility::url& url, ref <authenticator> auth = NULL); + + /** Properties for the session and for the services. + */ + const propertySet& getProperties() const; + + /** Properties for the session and for the services. + */ + propertySet& getProperties(); + +private: + + propertySet m_props; +}; + + +} // net +} // vmime + + +#endif // VMIME_NET_SESSION_HPP_INCLUDED diff --git a/vmime/net/simpleAuthenticator.hpp b/vmime/net/simpleAuthenticator.hpp new file mode 100644 index 00000000..e60b5174 --- /dev/null +++ b/vmime/net/simpleAuthenticator.hpp @@ -0,0 +1,62 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_SIMPLEAUTHENTICATOR_HPP_INCLUDED +#define VMIME_NET_SIMPLEAUTHENTICATOR_HPP_INCLUDED + + +#include "vmime/net/authenticator.hpp" + + +namespace vmime { +namespace net { + + +/** Basic implementation for an authenticator. + */ + +class simpleAuthenticator : public authenticator +{ +public: + + simpleAuthenticator(); + simpleAuthenticator(const string& username, const string& password); + +public: + + const string& getUsername() const; + void setUsername(const string& username); + + const string& getPassword() const; + void setPassword(const string& password); + +private: + + string m_username; + string m_password; + + const authenticationInfos getAuthInfos() const; +}; + + +} // net +} // vmime + + +#endif // VMIME_NET_SIMPLEAUTHENTICATOR_HPP_INCLUDED diff --git a/vmime/net/smtp/SMTPTransport.hpp b/vmime/net/smtp/SMTPTransport.hpp new file mode 100644 index 00000000..1743ee7c --- /dev/null +++ b/vmime/net/smtp/SMTPTransport.hpp @@ -0,0 +1,113 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_SMTP_SMTPTRANSPORT_HPP_INCLUDED +#define VMIME_NET_SMTP_SMTPTRANSPORT_HPP_INCLUDED + + +#include "vmime/config.hpp" + +#include "vmime/net/transport.hpp" +#include "vmime/net/socket.hpp" +#include "vmime/net/timeoutHandler.hpp" + + +namespace vmime { +namespace net { +namespace smtp { + + +/** SMTP transport service. + */ + +class SMTPTransport : public transport +{ +public: + + SMTPTransport(ref <session> sess, ref <authenticator> auth); + ~SMTPTransport(); + + const string getProtocolName() const; + + static const serviceInfos& getInfosInstance(); + const serviceInfos& getInfos() const; + + void connect(); + const bool isConnected() const; + void disconnect(); + + void noop(); + + void send(const mailbox& expeditor, const mailboxList& recipients, utility::inputStream& is, const utility::stream::size_type size, utility::progressionListener* progress = NULL); + +private: + + static const int responseCode(const string& response); + static const string responseText(const string& response); + + void sendRequest(const string& buffer, const bool end = true); + + void readResponse(string& buffer); + + void internalDisconnect(); + + ref <socket> m_socket; + bool m_authentified; + bool m_extendedSMTP; + + ref <timeoutHandler> m_timeoutHandler; + + + // Service infos + class _infos : public serviceInfos + { + public: + + struct props + { + // SMTP-specific options + serviceInfos::property PROPERTY_OPTIONS_NEEDAUTH; + + // Common properties + serviceInfos::property PROPERTY_AUTH_USERNAME; + serviceInfos::property PROPERTY_AUTH_PASSWORD; + + serviceInfos::property PROPERTY_SERVER_ADDRESS; + serviceInfos::property PROPERTY_SERVER_PORT; + serviceInfos::property PROPERTY_SERVER_SOCKETFACTORY; + + serviceInfos::property PROPERTY_TIMEOUT_FACTORY; + }; + + const props& getProperties() const; + + const string getPropertyPrefix() const; + const std::vector <serviceInfos::property> getAvailableProperties() const; + }; + + static _infos sm_infos; +}; + + +} // smtp +} // net +} // vmime + + +#endif // VMIME_NET_SMTP_SMTPTRANSPORT_HPP_INCLUDED diff --git a/vmime/net/socket.hpp b/vmime/net/socket.hpp new file mode 100644 index 00000000..a79eac1d --- /dev/null +++ b/vmime/net/socket.hpp @@ -0,0 +1,112 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_SOCKET_HPP_INCLUDED +#define VMIME_NET_SOCKET_HPP_INCLUDED + + +#include "vmime/base.hpp" + + +namespace vmime { +namespace net { + + +/** Interface for connecting to servers. + */ + +class socket : public object +{ +public: + + virtual ~socket() { } + + /** Connect to the specified address and port. + * + * @param address server address (this can be a full qualified domain name + * or an IP address, doesn't matter) + * @param port server port + */ + virtual void connect(const string& address, const port_t port) = 0; + + /** Disconnect from the server. + */ + virtual void disconnect() = 0; + + /** Test whether this socket is connected. + * + * @return true if the socket is connected, false otherwise + */ + virtual const bool isConnected() const = 0; + + /** Receive (text) data from the socket. + * + * @param buffer buffer in which to write received data + */ + virtual void receive(string& buffer) = 0; + + /** Receive (raw) data from the socket. + * + * @param buffer buffer in which to write received data + * @param count maximum number of bytes to receive (size of buffer) + * @return number of bytes received/written into output buffer + */ + virtual const int receiveRaw(char* buffer, const int count) = 0; + + /** Send (text) data to the socket. + * + * @param buffer data to send + */ + virtual void send(const string& buffer) = 0; + + /** Send (raw) data to the socket. + * + * @param buffer data to send + * @param count number of bytes to send (size of buffer) + */ + virtual void sendRaw(const char* buffer, const int count) = 0; + +protected: + + socket() { } + +private: + + socket(const socket&) : object() { } +}; + + +/** A class to create 'socket' objects. + */ + +class socketFactory +{ +public: + + virtual ~socketFactory() { } + + virtual ref <socket> create() = 0; +}; + + +} // net +} // vmime + + +#endif // VMIME_NET_SOCKET_HPP_INCLUDED diff --git a/vmime/net/store.hpp b/vmime/net/store.hpp new file mode 100644 index 00000000..5855c76d --- /dev/null +++ b/vmime/net/store.hpp @@ -0,0 +1,102 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_STORE_HPP_INCLUDED +#define VMIME_NET_STORE_HPP_INCLUDED + + +#include "vmime/net/service.hpp" +#include "vmime/net/folder.hpp" + + +namespace vmime { +namespace net { + + +/** A store service. + * Encapsulate protocols that provide access to user's mail drop. + */ + +class store : public service +{ +protected: + + store(ref <session> sess, const serviceInfos& infos, ref <authenticator> auth) + : service(sess, infos, auth) { } + +public: + + /** Return the default folder. This is protocol dependant + * and usually is the INBOX folder. + * + * @return default folder + */ + virtual ref <folder> getDefaultFolder() = 0; + + /** Return the root folder. This is protocol dependant + * and usually is the user's mail drop root folder. + * + * @return root folder + */ + virtual ref <folder> getRootFolder() = 0; + + /** Return the folder specified by the path. + * + * @param path absolute folder path + * @return folder at the specified path + */ + virtual ref <folder> getFolder(const folder::path& path) = 0; + + /** Test whether the specified folder name is a syntactically + * a valid name. + * + * @return true if the specified folder name is valid, false otherwise + */ + virtual const bool isValidFolderName(const folder::path::component& name) const = 0; + + /** Store capabilities. */ + enum Capabilities + { + CAPABILITY_CREATE_FOLDER = (1 << 0), /**< Can create folders. */ + CAPABILITY_RENAME_FOLDER = (1 << 1), /**< Can rename folders. */ + CAPABILITY_ADD_MESSAGE = (1 << 2), /**< Can append message to folders. */ + CAPABILITY_COPY_MESSAGE = (1 << 3), /**< Can copy messages from a folder to another one. */ + CAPABILITY_DELETE_MESSAGE = (1 << 4), /**< Can delete messages. */ + CAPABILITY_PARTIAL_FETCH = (1 << 5), /**< Is partial fetch supported? */ + CAPABILITY_MESSAGE_FLAGS = (1 << 6), /**< Can set flags on messages. */ + CAPABILITY_EXTRACT_PART = (1 << 7) /**< Can extract a specific part of the message. */ + }; + + /** Return the features supported by this service. This is + * a combination of store::CAPABILITY_xxx flags. + * + * @return features supported by this service + */ + virtual const int getCapabilities() const = 0; + + + const Type getType() const { return (TYPE_STORE); } +}; + + +} // net +} // vmime + + +#endif // VMIME_NET_STORE_HPP_INCLUDED diff --git a/vmime/net/timeoutHandler.hpp b/vmime/net/timeoutHandler.hpp new file mode 100644 index 00000000..5270978c --- /dev/null +++ b/vmime/net/timeoutHandler.hpp @@ -0,0 +1,77 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_TIMEOUTHANDLER_HPP_INCLUDED +#define VMIME_NET_TIMEOUTHANDLER_HPP_INCLUDED + + +#include "vmime/types.hpp" + + +namespace vmime { +namespace net { + + +/** A class to manage time-out in messaging services. + */ + +class timeoutHandler : public object +{ +public: + + virtual ~timeoutHandler() { } + + /** Called to test if the time limit has been reached. + * + * @return true if the time-out delay is elapsed + */ + virtual const bool isTimeOut() = 0; + + /** Called to reset the time-out counter. + */ + virtual void resetTimeOut() = 0; + + /** Called when the time limit has been reached (when + * isTimeOut() returned true). + * + * @return true to continue (and reset the time-out) + * or false to cancel the current operation + */ + virtual const bool handleTimeOut() = 0; +}; + + +/** A class to create 'timeoutHandler' objects. + */ + +class timeoutHandlerFactory +{ +public: + + virtual ~timeoutHandlerFactory() { } + + virtual ref <timeoutHandler> create() = 0; +}; + + +} // net +} // vmime + + +#endif // VMIME_NET_TIMEOUTHANDLER_HPP_INCLUDED diff --git a/vmime/net/transport.hpp b/vmime/net/transport.hpp new file mode 100644 index 00000000..f4be446d --- /dev/null +++ b/vmime/net/transport.hpp @@ -0,0 +1,75 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 Vincent Richard <[email protected]> +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#ifndef VMIME_NET_TRANSPORT_HPP_INCLUDED +#define VMIME_NET_TRANSPORT_HPP_INCLUDED + + +#include "vmime/net/service.hpp" +#include "vmime/utility/stream.hpp" + + +namespace vmime { + +class message; +class mailbox; +class mailboxList; + +namespace net { + + +/** A transport service. + * Encapsulate protocols that can send messages. + */ + +class transport : public service +{ +protected: + + transport(ref <session> sess, const serviceInfos& infos, ref <authenticator> auth); + +public: + + /** Send a message over this transport service. + * + * @param msg message to send + * @param progress progression listener, or NULL if not used + */ + virtual void send(ref <vmime::message> msg, utility::progressionListener* progress = NULL); + + /** Send a message over this transport service. + * + * @param expeditor expeditor mailbox + * @param recipients list of recipient mailboxes + * @param is input stream provding message data (header + body) + * @param size size of the message data + * @param progress progression listener, or NULL if not used + */ + virtual void send(const mailbox& expeditor, const mailboxList& recipients, utility::inputStream& is, const utility::stream::size_type size, utility::progressionListener* progress = NULL) = 0; + + + const Type getType() const; +}; + + +} // net +} // vmime + + +#endif // VMIME_NET_TRANSPORT_HPP_INCLUDED diff --git a/vmime/platformDependant.hpp b/vmime/platformDependant.hpp index b6de8e76..0492f8e6 100644 --- a/vmime/platformDependant.hpp +++ b/vmime/platformDependant.hpp @@ -27,8 +27,8 @@ #include "vmime/charset.hpp" #if VMIME_HAVE_MESSAGING_FEATURES - #include "vmime/messaging/socket.hpp" - #include "vmime/messaging/timeoutHandler.hpp" + #include "vmime/net/socket.hpp" + #include "vmime/net/timeoutHandler.hpp" #endif #if VMIME_HAVE_FILESYSTEM_FEATURES @@ -110,7 +110,7 @@ public: * session object * @return socket factory */ - virtual messaging::socketFactory* getSocketFactory(const string& name = "default") const = 0; + virtual net::socketFactory* getSocketFactory(const string& name = "default") const = 0; /** Return a pointer to a timeout-handler factory for the specified name. * The returned object will not be deleted by VMime, so it can be a @@ -124,7 +124,7 @@ public: * @param name time-out type name * @return time-out factory */ - virtual messaging::timeoutHandlerFactory* getTimeoutHandlerFactory(const string& name = "default") const = 0; + virtual net::timeoutHandlerFactory* getTimeoutHandlerFactory(const string& name = "default") const = 0; #endif #if VMIME_HAVE_FILESYSTEM_FEATURES /** Return a pointer to a factory that creates file-system objects. diff --git a/vmime/platforms/posix/posixHandler.hpp b/vmime/platforms/posix/posixHandler.hpp index 62500d9a..da8344fb 100644 --- a/vmime/platforms/posix/posixHandler.hpp +++ b/vmime/platforms/posix/posixHandler.hpp @@ -57,9 +57,9 @@ public: const unsigned int getProcessId() const; #if VMIME_HAVE_MESSAGING_FEATURES - vmime::messaging::socketFactory* getSocketFactory(const vmime::string& name) const; + vmime::net::socketFactory* getSocketFactory(const vmime::string& name) const; - vmime::messaging::timeoutHandlerFactory* getTimeoutHandlerFactory(const vmime::string& name) const; + vmime::net::timeoutHandlerFactory* getTimeoutHandlerFactory(const vmime::string& name) const; #endif #if VMIME_HAVE_FILESYSTEM_FEATURES diff --git a/vmime/platforms/posix/posixSocket.hpp b/vmime/platforms/posix/posixSocket.hpp index 909ada6a..93315cf9 100644 --- a/vmime/platforms/posix/posixSocket.hpp +++ b/vmime/platforms/posix/posixSocket.hpp @@ -21,7 +21,7 @@ #define VMIME_PLATFORMS_POSIX_SOCKET_HPP_INCLUDED -#include "vmime/messaging/socket.hpp" +#include "vmime/net/socket.hpp" #if VMIME_HAVE_MESSAGING_FEATURES @@ -32,7 +32,7 @@ namespace platforms { namespace posix { -class posixSocket : public vmime::messaging::socket +class posixSocket : public vmime::net::socket { public: @@ -57,11 +57,11 @@ private: -class posixSocketFactory : public vmime::messaging::socketFactory +class posixSocketFactory : public vmime::net::socketFactory { public: - ref <vmime::messaging::socket> create(); + ref <vmime::net::socket> create(); }; diff --git a/vmime/platforms/windows/windowsHandler.hpp b/vmime/platforms/windows/windowsHandler.hpp index 0f2c5b88..62ebe093 100644 --- a/vmime/platforms/windows/windowsHandler.hpp +++ b/vmime/platforms/windows/windowsHandler.hpp @@ -56,9 +56,9 @@ public: const unsigned int getProcessId() const; #if VMIME_HAVE_MESSAGING_FEATURES - vmime::messaging::socketFactory* getSocketFactory(const vmime::string& name) const; + vmime::net::socketFactory* getSocketFactory(const vmime::string& name) const; - vmime::messaging::timeoutHandlerFactory* getTimeoutHandlerFactory(const vmime::string& name) const; + vmime::net::timeoutHandlerFactory* getTimeoutHandlerFactory(const vmime::string& name) const; #endif #if VMIME_HAVE_FILESYSTEM_FEATURES diff --git a/vmime/platforms/windows/windowsSocket.hpp b/vmime/platforms/windows/windowsSocket.hpp index bbf76643..e5c3c44a 100644 --- a/vmime/platforms/windows/windowsSocket.hpp +++ b/vmime/platforms/windows/windowsSocket.hpp @@ -22,7 +22,7 @@ #include <winsock2.h> -#include "vmime/messaging/socket.hpp" +#include "vmime/net/socket.hpp" #if VMIME_HAVE_MESSAGING_FEATURES @@ -33,7 +33,7 @@ namespace platforms { namespace windows { -class windowsSocket : public vmime::messaging::socket +class windowsSocket : public vmime::net::socket { public: windowsSocket(); @@ -59,11 +59,11 @@ private: -class windowsSocketFactory : public vmime::messaging::socketFactory +class windowsSocketFactory : public vmime::net::socketFactory { public: - ref <vmime::messaging::socket> create(); + ref <vmime::net::socket> create(); }; diff --git a/vmime/types.hpp b/vmime/types.hpp index 1028fed3..38485f9a 100644 --- a/vmime/types.hpp +++ b/vmime/types.hpp @@ -47,6 +47,10 @@ namespace vmime using vmime::utility::null_ref; extern const null_ref null; + + // For compatibility with versions <= 0.7.1 (deprecated) + namespace net { } + namespace messaging = net; } diff --git a/vmime/utility/stream.hpp b/vmime/utility/stream.hpp index 5dd9d4dc..4b1cba2d 100644 --- a/vmime/utility/stream.hpp +++ b/vmime/utility/stream.hpp @@ -33,9 +33,9 @@ #if VMIME_HAVE_MESSAGING_FEATURES namespace vmime { - namespace messaging { + namespace net { class socket; // forward reference - } // messaging + } // net } // vmime #endif @@ -336,7 +336,7 @@ class outputStreamSocketAdapter : public outputStream { public: - outputStreamSocketAdapter(messaging::socket& sok); + outputStreamSocketAdapter(net::socket& sok); void write(const value_type* const data, const size_type count); @@ -344,7 +344,7 @@ private: outputStreamSocketAdapter(const outputStreamSocketAdapter&); - messaging::socket& m_socket; + net::socket& m_socket; }; @@ -355,7 +355,7 @@ class inputStreamSocketAdapter : public inputStream { public: - inputStreamSocketAdapter(messaging::socket& sok); + inputStreamSocketAdapter(net::socket& sok); const bool eof() const; void reset(); @@ -366,7 +366,7 @@ private: inputStreamSocketAdapter(const inputStreamSocketAdapter&); - messaging::socket& m_socket; + net::socket& m_socket; }; diff --git a/vmime/vmime.hpp b/vmime/vmime.hpp index b5cac466..3a6b6474 100644 --- a/vmime/vmime.hpp +++ b/vmime/vmime.hpp @@ -88,19 +88,19 @@ // Messaging features #if VMIME_HAVE_MESSAGING_FEATURES - #include "vmime/messaging/socket.hpp" + #include "vmime/net/socket.hpp" - #include "vmime/messaging/service.hpp" - #include "vmime/messaging/store.hpp" - #include "vmime/messaging/transport.hpp" + #include "vmime/net/service.hpp" + #include "vmime/net/store.hpp" + #include "vmime/net/transport.hpp" - #include "vmime/messaging/session.hpp" - #include "vmime/messaging/authenticator.hpp" - #include "vmime/messaging/defaultAuthenticator.hpp" - #include "vmime/messaging/simpleAuthenticator.hpp" + #include "vmime/net/session.hpp" + #include "vmime/net/authenticator.hpp" + #include "vmime/net/defaultAuthenticator.hpp" + #include "vmime/net/simpleAuthenticator.hpp" - #include "vmime/messaging/folder.hpp" - #include "vmime/messaging/message.hpp" + #include "vmime/net/folder.hpp" + #include "vmime/net/message.hpp" #endif // VMIME_HAVE_MESSAGING_FEATURES |