From cc9983b02c35f207f856da0f767e2b0102bf5126 Mon Sep 17 00:00:00 2001 From: rbbrnc Date: Thu, 12 May 2011 16:00:16 +0200 Subject: [PATCH] Add vmime library example --- src/utils/.gitignore | 2 +- src/utils/Makefile | 17 +++++++++-- src/utils/test_vmime.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 src/utils/test_vmime.cpp diff --git a/src/utils/.gitignore b/src/utils/.gitignore index 806131f..956f00c 100644 --- a/src/utils/.gitignore +++ b/src/utils/.gitignore @@ -1,5 +1,5 @@ # Generated test files -test_mime +test_vmime test_view_mail test_list test_list_x11 diff --git a/src/utils/Makefile b/src/utils/Makefile index 96596a9..3491de8 100644 --- a/src/utils/Makefile +++ b/src/utils/Makefile @@ -1,4 +1,9 @@ CC = gcc +CXX = g++ + +CXXFLAGS = -Wall -Wextra -I/usr/local/include/vmime/ +CXXFLAGS += -ffunction-sections -fdata-sections +CXXFLAGS += -O2 #-- LIB_RWIDGETS = ../applets/widgets/librwidgets.a @@ -18,12 +23,11 @@ LDFLAGS += -Wl,--as-needed LIBRARIES = TARGETS = \ - test_mime \ - test_header \ - test_view_mail \ + test_vmime \ test_list \ test_list_x11 \ test_header_osd \ + test_view_mail \ test_pop3_list \ COMMON_OBJS = \ @@ -54,6 +58,9 @@ test_list_x11: LIBRARIES += $(LIB_RWIDGETS) -L/usr/X11R6/lib -lXaw -lXt -lX11 test_header_osd: LIBRARIES += -lXft -lpng test_pop3_list: LIBRARIES += $(LIB_RPOP3) -L/usr/local/lib -lpolarssl +test_vmime: LIBRARIES += $(LIB_RPOP3) -L/usr/local/lib -lpolarssl +test_vmime: LIBRARIES += -L/usr/local/lib -lvmime + #-- %.o:%.c @echo " CC $<" @@ -85,6 +92,10 @@ test_view_mail: test_view_mail.o $(COMMON_OBJS) $(MD5_UTILS_OBJS) @echo " LD $@" $(CC) $(LDFLAGS) $^ $(LIBRARIES) -o $@ +test_vmime: test_vmime.cpp + @echo " CXX+LD $@" + $(CXX) $(CXXFLAGS) $(LDFLAGS) $^ $(LIBRARIES) -o $@ + clean: rm -f $(TARGETS) *.o *~ diff --git a/src/utils/test_vmime.cpp b/src/utils/test_vmime.cpp new file mode 100644 index 0000000..c714c34 --- /dev/null +++ b/src/utils/test_vmime.cpp @@ -0,0 +1,78 @@ +#include +#include // for ifstream + +// VMIME headers +#include "vmime/vmime.hpp" +#include "vmime/platforms/posix/posixHandler.hpp" +#include "vmime/security/digest/messageDigestFactory.hpp" + +int main(int argc, char **argv) +{ + + if (argc < 2) { + std::cout << "Usage: " << argv[0] << "" << std::endl; + return -1; + } + + // Read data from file + std::ifstream file; + + file.open(argv[1], std::ios::in | std::ios::binary); + if (!file.good()) + { + std::cout << "Cannot open input file.\n"; + return 1; + } + // VMime initialization + vmime::platform::setHandler(); + + + vmime::utility::inputStreamAdapter is(file); + vmime::string data; + vmime::utility::outputStreamStringAdapter os(data); + vmime::utility::bufferedStreamCopy(is, os); + + // Actually parse the message + vmime::ref msg = vmime::create(); + msg->parse(data); + + vmime::ref hdr = msg->getHeader(); + vmime::ref bdy = msg->getBody(); + + vmime::messageParser mp(msg); + + // Now, you can extract some of its components + vmime::charset ch(vmime::charsets::UTF_8); +// std::cout << "From : " << hdr->From()->getValue().dynamicCast()->getName().getConvertedText(ch) << std::endl; + std::cout << "From : " << hdr->From()->getValue().dynamicCast()->getEmail(); + + // Calc MD5 of address + vmime::ref var; + + var = vmime::security::digest::messageDigestFactory::getInstance()->create("md5"); + var->update(hdr->From()->getValue().dynamicCast()->getEmail()); + var->finalize(); + std::cout << " (MD5: " << var->getHexDigest() << ")" << std::endl; + + + // Prints Subjects + std::cout << "Subject: " << hdr->Subject()->getValue().dynamicCast()->getConvertedText(ch) << std::endl; + + // Enumerate attachments +// std::cout << "Attachment count" << mp.getAttachmentCount() << std::endl; + for (int i = 0 ; i < mp.getAttachmentCount() ; ++i) { + const vmime::attachment& att = *mp.getAttachmentAt(i); + // Media type (content type) is in "att.getType()" + // Name is in "att.getName()" + // Description is in "att.getDescription()" + // Data is in "att.getData()" + + std::cout << "Attachment is: " + << att.getName().getBuffer() + << std::endl; + } + + + + return 0; +} -- 2.11.4.GIT