|
| 1 | +// |
| 2 | +// Copyright Marshall Clow 2009-2010 |
| 3 | +// Distributed under the Boost Software License, Version 1.0. |
| 4 | +// (See accompanying file LICENSE_1_0.txt or copy at |
| 5 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +// |
| 7 | +// |
| 8 | + |
| 9 | +#include<boost/mime.hpp> |
| 10 | + |
| 11 | +#include<fstream> |
| 12 | +#include<iostream> |
| 13 | +#include<iterator> |
| 14 | +#include<sstream> |
| 15 | + |
| 16 | +structmy_traits { |
| 17 | +typedefstd::string string_type; |
| 18 | +//typedef std::pair < std::string, string_type > header_type; |
| 19 | +typedef std::string body_type; |
| 20 | +}; |
| 21 | + |
| 22 | +//typedef boost::mime::mime_part<>mime_part; |
| 23 | +typedef boost::mime::basic_mime<my_traits>mime_part; |
| 24 | + |
| 25 | +template<typename Container> |
| 26 | +voidDumpContainer ( std::ostream & out,const std::string &prefix,const Container &c ) { |
| 27 | +out << prefix <<''; |
| 28 | +if ( c.size () <10 ) { |
| 29 | +for (typename Container::const_iterator iter = c.begin(); iter != c.end(); ++iter ) |
| 30 | +out << (int) *iter <<''; |
| 31 | +} |
| 32 | +else { |
| 33 | +for (int i =0; i <5; i++ ) |
| 34 | +out <<int(c.begin()[i]) <<''; |
| 35 | +out <<"..."; |
| 36 | +for (int i =0; i <5; i++ ) |
| 37 | +out <<int(c.rbegin()[i]) <<''; |
| 38 | +} |
| 39 | +out << std::endl; |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | +voidDumpStructure ( std::ostream & out,constchar *title,const mime_part &mp, std::string prefix ) { |
| 44 | +std::string content_type = mp.get_content_type (); |
| 45 | +if (NULL != title ) |
| 46 | +out << prefix <<"Data from:" << title << std::endl; |
| 47 | +out << prefix <<"Content-Type:" << content_type << std::endl; |
| 48 | +out << prefix <<"There are" <<std::distance ( mp.header_begin (), mp.header_end ()) <<" headers" << std::endl; |
| 49 | +size_t subpart_count =std::distance ( mp.subpart_begin (), mp.subpart_end ()); |
| 50 | +switch ( mp.get_part_kind ()) { |
| 51 | +case mime_part::simple_part: |
| 52 | +if ( subpart_count !=0 ) |
| 53 | +out <<str (boost::format ("%s ### %d subparts on a simple (%s) type!" ) % prefix % subpart_count % content_type ) << std::endl; |
| 54 | +out << prefix <<"The body is" << mp.body_size () <<" bytes long" << std::endl; |
| 55 | +DumpContainer ( out, prefix, *mp.body ()); |
| 56 | +break; |
| 57 | + |
| 58 | +case mime_part::multi_part: |
| 59 | +break; |
| 60 | + |
| 61 | +case mime_part::message_part: |
| 62 | +if (boost::iequals ( content_type,"message/delivery-status" )) |
| 63 | +out << prefix <<"The body is" << mp.body_size () <<" bytes long" << std::endl; |
| 64 | +elseif (1 != subpart_count ) |
| 65 | +out <<str (boost::format ("%s ### %d subparts on a message (%s) type!" ) % subpart_count % prefix % content_type ) << std::endl; |
| 66 | +break; |
| 67 | +} |
| 68 | + |
| 69 | +if ( subpart_count !=0 ) { |
| 70 | +out << prefix <<"There are" <<std::distance ( mp.subpart_begin (), mp.subpart_end ()) <<" sub parts" << std::endl; |
| 71 | +for ( mime_part::constPartIter iter = mp.subpart_begin (); iter != mp.subpart_end (); ++iter ) |
| 72 | +DumpStructure ( out,NULL, **iter, prefix +"" ); |
| 73 | +} |
| 74 | +} |
| 75 | + |
| 76 | +intmain (int argc,char * argv[] ) { |
| 77 | + |
| 78 | +if ( argc ==1 ) |
| 79 | +std::cerr <<"Usage: basic_parsing <files-to-parse>" << std::endl; |
| 80 | + |
| 81 | +for (int i =1; i < argc; ++i ) { |
| 82 | +boost::shared_ptr<mime_part> rmp; |
| 83 | +try { |
| 84 | +std::ifstreamin ( argv[i] ); |
| 85 | +if ( !in ) { |
| 86 | +std::cerr <<"Can't open file" << argv[i] << std::endl; |
| 87 | +continue; |
| 88 | +} |
| 89 | + |
| 90 | +in >> std::noskipws; |
| 91 | +std::cout <<"**********************************" << std::endl; |
| 92 | +rmp =mime_part::parse_mime ( in ); |
| 93 | +} |
| 94 | +catch (const boost::mime::mime_parsing_error &err ) { |
| 95 | +std::cout <<"Caught an error parsing '" << argv[i] <<"'" << std::endl; |
| 96 | +std::cout <<"" << err.what () << std::endl; |
| 97 | +continue; |
| 98 | +} |
| 99 | +catch (const boost::exception &berr ) { |
| 100 | +std::cout <<"Caught an boost error parsing '" << argv[i] <<"'" << std::endl; |
| 101 | +//std::cout << " " << berr.what () << std::endl; |
| 102 | +continue; |
| 103 | +} |
| 104 | + |
| 105 | +try { |
| 106 | +DumpStructure ( std::cout, argv[i], *rmp,std::string ()); |
| 107 | +//std::ofstream out ( (std::string ( argv[i] ) + "-Results").c_str (), std::ios::binary ); |
| 108 | +//out << rmp; |
| 109 | +} |
| 110 | +catch (const std::runtime_error &err ) { |
| 111 | +std::cout <<"Caught an error writing '" << argv[i] <<"'" << std::endl; |
| 112 | +std::cout <<"" << err.what () << std::endl; |
| 113 | +continue; |
| 114 | +} |
| 115 | +} |
| 116 | + |
| 117 | +return0; |
| 118 | +} |