vmime/examples/example6.cpp

585 lines
14 KiB
C++
Raw Normal View History

2004-10-05 10:28:21 +00:00
//
2005-03-18 21:33:11 +00:00
// VMime library (http://www.vmime.org)
// Copyright (C) 2002-2005 Vincent Richard <vincent@vincent-richard.net>
2004-10-05 10:28:21 +00:00
//
// 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.
//
#include <iostream>
2005-05-19 18:46:43 +00:00
#include <sstream>
#include <vector>
2004-10-05 10:28:21 +00:00
#include "vmime/vmime.hpp"
2004-12-30 09:32:32 +00:00
#include "vmime/platforms/posix/posixHandler.hpp"
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
// Global session object
static vmime::utility::auto_ptr <vmime::messaging::session> g_session
= new vmime::messaging::session();
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
// Authentification handler
class interactiveAuthenticator : public vmime::messaging::authenticator
2004-10-05 10:28:21 +00:00
{
const vmime::messaging::authenticationInfos requestAuthInfos() const
{
vmime::string username, password;
2005-05-19 18:46:43 +00:00
std::cout << std::endl;
std::cout << "Please authenticate yourself:" << std::endl;
std::cout << " Username: ";
std::cout.flush();
std::getline(std::cin, username);
std::cout << " Password: ";
std::cout.flush();
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
std::getline(std::cin, password);
2004-10-05 10:28:21 +00:00
return (vmime::messaging::authenticationInfos(username, password));
}
};
2005-05-19 18:46:43 +00:00
// Exception helper
static std::ostream& operator<<(std::ostream& os, const vmime::exception& e)
{
os << "* vmime::exceptions::" << e.name() << std::endl;
os << " what = " << e.what() << std::endl;
// More information for special exceptions
if (dynamic_cast <const vmime::exceptions::command_error*>(&e))
{
const vmime::exceptions::command_error& cee =
dynamic_cast <const vmime::exceptions::command_error&>(e);
os << " command = " << cee.command() << std::endl;
os << " response = " << cee.response() << std::endl;
}
if (dynamic_cast <const vmime::exceptions::connection_greeting_error*>(&e))
{
const vmime::exceptions::connection_greeting_error& cgee =
dynamic_cast <const vmime::exceptions::connection_greeting_error&>(e);
os << " response = " << cgee.response() << std::endl;
}
if (dynamic_cast <const vmime::exceptions::authentication_error*>(&e))
{
const vmime::exceptions::authentication_error& aee =
dynamic_cast <const vmime::exceptions::authentication_error&>(e);
os << " response = " << aee.response() << std::endl;
}
if (dynamic_cast <const vmime::exceptions::filesystem_exception*>(&e))
{
const vmime::exceptions::filesystem_exception& fse =
dynamic_cast <const vmime::exceptions::filesystem_exception&>(e);
os << " path = " << vmime::platformDependant::getHandler()->
getFileSystemFactory()->pathToString(fse.path()) << std::endl;
}
if (e.other() != NULL)
os << *e.other();
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
return os;
}
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
/** Print the MIME structure of a message on the standard output.
*
* @param s structure object
* @param level current depth
*/
static void printStructure(const vmime::messaging::structure& s, const int level = 0)
2004-10-05 10:28:21 +00:00
{
2004-10-21 15:05:47 +00:00
for (int i = 1 ; i <= s.getCount() ; ++i)
2004-10-05 10:28:21 +00:00
{
const vmime::messaging::part& part = s[i];
for (int j = 0 ; j < level * 2 ; ++j)
std::cout << " ";
2004-10-21 15:05:47 +00:00
std::cout << part.getNumber() << ". "
<< part.getType().generate()
<< " [" << part.getSize() << " byte(s)]"
2004-10-05 10:28:21 +00:00
<< std::endl;
2004-10-21 15:05:47 +00:00
printStructure(part.getStructure(), level + 1);
2004-10-05 10:28:21 +00:00
}
}
2005-05-19 18:46:43 +00:00
static const vmime::string getFolderPathString(vmime::messaging::folder* f)
{
const vmime::string n = f->getName().getBuffer();
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
if (n.empty()) // root folder
{
return "/";
}
else
{
vmime::utility::auto_ptr <vmime::messaging::folder> p = f->getParent();
return getFolderPathString(p) + n + "/";
}
}
/** Print folders and sub-folders on the standard output.
*
* @param folder current folder
*/
static void printFolders(vmime::messaging::folder* folder, const int level = 0)
2004-10-05 10:28:21 +00:00
{
2005-05-19 18:46:43 +00:00
for (int j = 0 ; j < level * 2 ; ++j)
std::cout << " ";
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
std::cout << getFolderPathString(folder) << std::endl;
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
std::vector <vmime::messaging::folder*> subFolders = folder->getFolders(false);
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
for (unsigned int i = 0 ; i < subFolders.size() ; ++i)
2004-10-05 10:28:21 +00:00
{
2005-05-19 18:46:43 +00:00
printFolders(subFolders[i], level + 1);
delete subFolders[i];
}
}
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
/** Print a menu on the standard output.
*
* @param choices menu choices
*/
static const unsigned int printMenu(const std::vector <std::string>& choices)
{
std::cout << std::endl;
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
for (unsigned int i = 0 ; i < choices.size() ; ++i)
std::cout << " " << (i + 1) << ". " << choices[i] << std::endl;
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
std::cout << std::endl;
std::cout << " Your choice? [1-" << choices.size() << "] ";
std::cout.flush();
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
std::string line;
std::getline(std::cin, line);
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
std::istringstream iss(line);
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
unsigned int choice = 0;
iss >> choice;
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
std::cout << std::endl;
2005-04-27 18:51:41 +00:00
2005-05-19 18:46:43 +00:00
if (choice < 1 || choice > choices.size())
return 0;
else
return choice;
}
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
/** Send a message interactively.
*/
static void sendMessage()
{
try
{
// Request user to enter an URL
std::cout << "Enter an URL to connect to transport service." << std::endl;
std::cout << "(eg. smtp://myserver.com, sendmail://localhost)" << std::endl;
std::cout << "> ";
std::cout.flush();
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
vmime::string urlString;
std::getline(std::cin, urlString);
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
vmime::utility::url url(urlString);
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
interactiveAuthenticator auth;
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
vmime::utility::auto_ptr <vmime::messaging::transport> tr =
g_session->getTransport(url, &auth);
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
// You can also set some properties (see example7 to know the properties
// available for each service). For example, for SMTP:
// tr->setProperty("options.need-authentication", true);
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
// Information about the mail
std::cout << "Enter email of the expeditor (eg. me@somewhere.com): ";
std::cout.flush();
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
vmime::string fromString;
std::getline(std::cin, fromString);
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
vmime::mailbox from(fromString);
vmime::mailboxList to;
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
for (bool cont = true ; cont ; )
{
std::cout << "Enter email of the recipient (empty to stop): ";
std::cout.flush();
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
vmime::string toString;
std::getline(std::cin, toString);
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
cont = (toString.size() != 0);
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
if (cont)
to.appendMailbox(new vmime::mailbox(toString));
}
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
std::cout << "Enter message data (end with '.' on a single line):" << std::endl;
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
std::ostringstream data;
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
for (bool cont = true ; cont ; )
{
std::string line;
std::getline(std::cin, line);
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
if (line == ".")
cont = false;
else
data << line << "\r\n";
}
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
// Connect to server
tr->connect();
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
// Send the message
vmime::string msgData = data.str();
vmime::utility::inputStreamStringAdapter vis(msgData);
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
tr->send(from, to, vis, msgData.length());
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
// Note: you could also write this:
// vmime::message msg;
// ...
// tr->send(&msg);
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
tr->disconnect();
}
catch (vmime::exception& e)
{
std::cerr << std::endl;
std::cerr << e << std::endl;
throw;
}
catch (std::exception& e)
{
std::cerr << std::endl;
std::cerr << "std::exception: " << e.what() << std::endl;
throw;
}
}
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
/** Connect to a message store interactively.
*/
static void connectStore()
{
try
{
// Request user to enter an URL
std::cout << "Enter an URL to connect to store service." << std::endl;
std::cout << "(eg. pop3://user:pass@myserver.com, imap://myserver.com:123)" << std::endl;
std::cout << "> ";
std::cout.flush();
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
vmime::string urlString;
std::getline(std::cin, urlString);
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
vmime::utility::url url(urlString);
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
// If no authenticator is given in argument to getStore(), a default one
// is used. Its behaviour is to get the user credentials from the
// session properties "auth.username" and "auth.password".
interactiveAuthenticator auth;
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
vmime::utility::auto_ptr <vmime::messaging::store> st =
g_session->getStore(url, &auth);
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
// Connect to the mail store
st->connect();
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
// Open the default folder in this store
vmime::utility::auto_ptr <vmime::messaging::folder> f = st->getDefaultFolder();
// vmime::utility::auto_ptr <vmime::messaging::folder> f = st->getFolder(vmime::utility::path("a"));
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
f->open(vmime::messaging::folder::MODE_READ_WRITE);
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
int count = f->getMessageCount();
2004-10-05 10:28:21 +00:00
2005-05-19 18:46:43 +00:00
std::cout << std::endl;
std::cout << count << " message(s) in your inbox" << std::endl;
for (bool cont = true ; cont ; )
{
typedef std::map <int, vmime::utility::smart_ptr <vmime::messaging::message> > MessageList;
MessageList msgList;
try
{
std::vector <std::string> choices;
choices.push_back("Show message flags");
choices.push_back("Show message structure");
choices.push_back("Show message header");
choices.push_back("Show message envelope");
choices.push_back("Extract whole message");
choices.push_back("List folders");
choices.push_back("Return to main menu");
const int choice = printMenu(choices);
// Request message number
vmime::utility::smart_ptr <vmime::messaging::message> msg;
if (choice != 6 && choice != 7)
{
std::cout << "Enter message number: ";
std::cout.flush();
std::string line;
std::getline(std::cin, line);
std::istringstream iss(line);
int num = 0;
iss >> num;
if (num < 1 || num > count)
{
std::cerr << "Invalid message number." << std::endl;
continue;
}
MessageList::iterator it = msgList.find(num);
if (it != msgList.end())
{
msg = (*it).second;
}
else
{
msg = f->getMessage(num);
msgList.insert(MessageList::value_type(num, msg));
}
std::cout << std::endl;
}
switch (choice)
{
// Show message flags
case 1:
f->fetchMessage(msg, vmime::messaging::folder::FETCH_FLAGS);
if (msg->getFlags() & vmime::messaging::message::FLAG_SEEN)
std::cout << "FLAG_SEEN" << std::endl;
if (msg->getFlags() & vmime::messaging::message::FLAG_RECENT)
std::cout << "FLAG_RECENT" << std::endl;
if (msg->getFlags() & vmime::messaging::message::FLAG_REPLIED)
std::cout << "FLAG_REPLIED" << std::endl;
if (msg->getFlags() & vmime::messaging::message::FLAG_DELETED)
std::cout << "FLAG_DELETED" << std::endl;
if (msg->getFlags() & vmime::messaging::message::FLAG_MARKED)
std::cout << "FLAG_MARKED" << std::endl;
if (msg->getFlags() & vmime::messaging::message::FLAG_PASSED)
std::cout << "FLAG_PASSED" << std::endl;
break;
// Show message structure
case 2:
f->fetchMessage(msg, vmime::messaging::folder::FETCH_STRUCTURE);
printStructure(msg->getStructure());
break;
// Show message header
case 3:
f->fetchMessage(msg, vmime::messaging::folder::FETCH_FULL_HEADER);
std::cout << msg->getHeader().generate() << std::endl;
break;
// Show message envelope
case 4:
f->fetchMessage(msg, vmime::messaging::folder::FETCH_ENVELOPE);
2005-05-20 10:56:00 +00:00
#define ENV_HELPER(x) \
try { std::cout << msg->getHeader().x().generate() << std::endl; } \
2005-05-19 18:46:43 +00:00
catch (vmime::exception) { /* In case the header field does not exist. */ }
2005-05-20 10:56:00 +00:00
ENV_HELPER(From)
ENV_HELPER(To)
ENV_HELPER(Date)
ENV_HELPER(Subject)
2005-05-19 18:46:43 +00:00
#undef ENV_HELPER
break;
// Extract whole message
case 5:
{
vmime::utility::outputStreamAdapter out(std::cout);
msg->extract(out);
break;
}
// List folders
case 6:
{
vmime::utility::auto_ptr <vmime::messaging::folder>
root = st->getRootFolder();
printFolders(root);
break;
}
// Main menu
case 7:
cont = false;
break;
}
2004-10-05 10:28:21 +00:00
/*
2005-05-19 18:46:43 +00:00
// Append message
2004-10-05 10:28:21 +00:00
std::istringstream iss(
"From: me@localhost\r\n"
"To: you@localhost\r\n"
"Subject: Message Text\r\n"
"\r\n"
"This is a test message...\r\n"
"Bye bye!\r\n"
);
f->addMessage(iss, iss.str().size());
// Folder renaming
{
vmime::messaging::folder* f = st->getFolder(vmime::messaging::folder::path("c"));
f->rename(vmime::messaging::folder::path("c2"));
delete (f);
vmime::messaging::folder* g = st->getFolder(vmime::messaging::folder::path("c2"));
g->rename(vmime::messaging::folder::path("c"));
delete (g);
}
2005-05-19 18:46:43 +00:00
// Message copy: copy all messages from 'f' to 'g'
2004-10-05 10:28:21 +00:00
{
vmime::messaging::folder* g = st->getFolder(vmime::messaging::folder::path("TEMP"));
if (!g->exists())
g->create(vmime::messaging::folder::TYPE_CONTAINS_MESSAGES);
2004-10-21 15:05:47 +00:00
f->copyMessages(g->getFullPath());
2004-10-05 10:28:21 +00:00
delete (g);
}
*/
2005-05-19 18:46:43 +00:00
}
catch (vmime::exception& e)
{
std::cerr << std::endl;
std::cerr << e << std::endl;
}
catch (std::exception& e)
{
std::cerr << std::endl;
std::cerr << "std::exception: " << e.what() << std::endl;
}
}
2004-10-05 10:28:21 +00:00
}
2005-05-19 18:46:43 +00:00
catch (vmime::exception& e)
2004-10-05 10:28:21 +00:00
{
2005-05-19 18:46:43 +00:00
std::cerr << std::endl;
std::cerr << e << std::endl;
2004-10-05 10:28:21 +00:00
throw;
}
2005-05-19 18:46:43 +00:00
catch (std::exception& e)
2004-10-05 10:28:21 +00:00
{
2005-05-19 18:46:43 +00:00
std::cerr << std::endl;
std::cerr << "std::exception: " << e.what() << std::endl;
2004-10-05 10:28:21 +00:00
throw;
}
2005-05-19 18:46:43 +00:00
}
/* Show the main menu.
*
* @return true to quit the program, false to continue
*/
static const bool menu()
{
std::vector <std::string> items;
items.push_back("Connect to a message store");
items.push_back("Send a message");
items.push_back("Quit");
switch (printMenu(items))
2004-10-05 10:28:21 +00:00
{
2005-05-19 18:46:43 +00:00
// Connect to store
case 1:
connectStore();
return false;
// Send a message
case 2:
sendMessage();
return false;
// Quit
case 3:
return true;
// Other choice
default:
return false;
2004-10-05 10:28:21 +00:00
}
2005-05-19 18:46:43 +00:00
}
int main()
{
// VMime initialization
vmime::platformDependant::setHandler<vmime::platforms::posix::posixHandler>();
for (bool quit = false ; !quit ; )
2004-10-05 10:28:21 +00:00
{
2005-05-19 18:46:43 +00:00
// Loop on main menu
quit = menu();
2004-10-05 10:28:21 +00:00
}
2005-05-19 18:46:43 +00:00
return 0;
2004-10-05 10:28:21 +00:00
}
2005-05-19 18:46:43 +00:00