www.pudn.com > fanccMSNr.src.rar > MIMEMessage.cpp


#include  
#include  
using namespace boost; 
 
#include "MIMEMessage.hpp" 
#include "Charset.hpp" 
 
 
namespace poral { 
	void MIMEMessage::parse(const string &message) { 
		string header; 
		string::size_type endOfHeader = message.find("\r\n\r\n"); 
		if(endOfHeader!=string::npos) { 
			header=message.substr(0, endOfHeader+4); 
			Charset::fromServer(body, message.substr(endOfHeader+4)); 
		} else { 
			header=message; 
		} 
 
 
		typedef tokenizer > Tokenizer; 
		Tokenizer rows(header, char_separator("\n\r")); 
		 
		bool first=true; 
		for(Tokenizer::iterator row=rows.begin(); row!=rows.end(); row++) { 
			if(first) { 
				// parse first row 
				Message::parse(*row); 
				// drop byte number of properties 
				arguments.pop_back(); 
				first=false; 
				continue; 
			} 
 
			// parse the row and store to Properties 
			Tokenizer columns(*row, char_separator(":")); 
			Tokenizer::iterator column=columns.begin(); 
			if(column==columns.end()) { 
				break; 
			} 
			Property property; 
			property.first=*column; 
			column++; 
			if(column==columns.end()) { 
				break; 
			} 
			if(property.first=="MIME-Version") { 
				Charset::trim(version, *column); 
			} else if(property.first=="Content-Type") { 
				Charset::trim(contentType, *column); 
			} else { 
				Charset::trim(property.second, *column); 
				properties.insert(property); 
			} 
		} 
	} 
	const string &MIMEMessage::toString() const { 
		string MIMEString; 
		 
		// stores MIME string 
		MIMEString.append("MIME-Version: "); 
		MIMEString.append(version); 
		MIMEString.append("\r\n"); 
 
		MIMEString.append("Content-Type: "); 
		MIMEString.append(contentType); 
		MIMEString.append("\r\n"); 
 
		string out; 
		for(Properties::const_iterator i=properties.begin(); i!=properties.end(); i++) { 
			MIMEString.append((*i).first); 
			MIMEString.append(": "); 
			MIMEString.append((*i).second); 
			MIMEString.append("\r\n"); 
		} 
		MIMEString.append("\r\n"); 
		Charset::localToUTF8(out, body); 
		MIMEString.append(out); 
		int nMIMEString=(int)MIMEString.size(); 
 
		serialized.erase(serialized.begin(), serialized.end()); 
		// stores first row 
		serialized.append(name); 
		int nArguments=size(); 
		for(int iArgument=0; iArgument