30 unsigned int connect_timeout ,
unsigned int response_timeout ) :
31 GNet::
Client(resolver_info,connect_timeout,response_timeout,0U,
"\n") ,
36 m_header_out_index(0U) ,
40 << connect_timeout <<
" " << response_timeout ) ;
49 return m_in.get() != NULL || m_out.get() != NULL ;
62 G_WARNING(
"GSmtp::SpamClient::onDeleteImp: error: " << reason ) ;
68 eventSignal().emit(
"spam" , m_out_size >= headerBodyLength() ? headerResult() : reason ) ;
70 Base::onDeleteImp( reason , b ) ;
75 G_DEBUG(
"GSmtp::SpamClient::request: \"" << path <<
"\"" ) ;
77 throw ProtocolError() ;
80 m_in <<=
new std::ifstream( path.c_str() , std::ios_base::binary | std::ios_base::in ) ;
84 std::string username =
"spam" ;
85 m_header_out.push_back( std::string() +
"PROCESS SPAMC/1.4" ) ;
86 m_header_out.push_back( std::string() +
"User: " + username ) ;
87 m_header_out.push_back( std::string() +
"Content-length: " +
G::File::sizeString(m_path) ) ;
88 m_header_out.push_back( std::string() ) ;
89 m_header_out_index = 0U ;
91 m_timer.startTimer( 0U ) ;
97 G_DEBUG(
"GSmtp::SpamClient::onConnect" ) ;
102 void GSmtp::SpamClient::onTimeout()
113 void GSmtp::SpamClient::sendContent()
116 while( nextContentLine(line) )
118 if( !send( line +
"\r\n" ) )
123 bool GSmtp::SpamClient::nextContentLine( std::string & line )
126 if( m_in.get() != NULL )
128 if( m_header_out_index < m_header_out.size() )
130 line = m_header_out[m_header_out_index++] ;
136 std::istream & stream = *(m_in.get()) ;
145 m_in_size += ( line.length() + 2U ) ;
149 G_LOG(
"GSmtp::SpamClient::addBody: spam>>: [" << m_in_lines
150 <<
" lines of body text, " << m_in_size <<
" bytes]" ) ;
160 void GSmtp::SpamClient::turnRound()
163 socket().shutdown() ;
167 m_out <<=
new std::ofstream( m_path.c_str() , std::ios_base::binary | std::ios_base::out | std::ios_base::trunc ) ;
178 if( m_in.get() != NULL )
181 if( ! haveCompleteHeader() )
189 bool GSmtp::SpamClient::haveCompleteHeader()
const
191 return m_header_in.size() != 0U && m_header_in.back().empty() ;
194 void GSmtp::SpamClient::addHeader(
const std::string & line )
201 void GSmtp::SpamClient::addBody(
const std::string & line )
203 if( m_out.get() != NULL && m_out_size < headerBodyLength() )
205 std::ostream & out = *m_out.get() ;
206 out << line <<
"\n" ;
207 m_out_size += line.length() + 1U ;
210 if( m_out_size >= headerBodyLength() )
211 G_LOG(
"GSmtp::SpamClient::addBody: spam<<: [" << m_out_lines
212 <<
" lines of body text, " << m_out_size <<
" bytes]" ) ;
216 std::string GSmtp::SpamClient::headerResult()
const
218 return G::Str::lower( part(headerLine(
"Spam:"),1U,
"true") ) ==
"true" ?
"marked as spam" : std::string() ;
221 std::string GSmtp::SpamClient::part(
const std::string & line ,
unsigned int i ,
const std::string & default_ )
const
225 return part.size() > i ? part[i] : default_ ;
228 unsigned long GSmtp::SpamClient::headerBodyLength()
const
231 (
const_cast<SpamClient*
>(
this))->m_n =
G::Str::toULong( part(headerLine(
"Content-length:"),1U,
"0") ) ;
235 std::string GSmtp::SpamClient::headerLine(
const std::string & key ,
const std::string & default_ )
const
237 for( StringArray::const_iterator p = m_header_in.begin() ; p != m_header_in.end() ; ++p )
239 if( (*p).find(key) == 0U )
bool busy() const
Returns true after request() and before the subsequent event signal.
static std::string printable(const std::string &in, char escape= '\\')
Returns a printable represention of the given input string.
virtual bool onReceive(const std::string &)
Final override from GNet::Client.
std::vector< std::string > StringArray
A std::vector of std::strings.
A class that holds a host/service name pair and optionally the results of a name-to-address lookup...
static void splitIntoTokens(const std::string &in, Strings &out, const std::string &ws)
Splits the string into 'ws'-delimited tokens.
virtual void onDeleteImp(const std::string &, bool)
Final override from GNet::Client.
static std::string lower(const std::string &s)
Returns a copy of 's' in which all uppercase characters have been replaced by lowercase characters...
virtual void onSendComplete()
Final override from GNet::BufferedClient.
void request(const std::string &)
Issues a request.
virtual ~SpamClient()
Destructor.
virtual void onConnect()
Final override from GNet::SimpleClient.
virtual void onDelete(const std::string &, bool)
Final override from GNet::HeapClient.
A client class that interacts with a remote process using a protocol somewhat similar to the spamassa...
virtual void onSecure(const std::string &)
Final override from GNet::SocketProtocol.
static std::string trimmed(const std::string &s, const std::string &ws)
Returns a trim()med version of s.
static std::string readLineFrom(std::istream &stream, const std::string &eol=std::string())
Reads a line from the stream using the given line terminator.
A class which acts as an SMTP client, extracting messages from a message store and forwarding them to...
std::string displayString(bool simple=false) const
Returns a string representation for logging and debug.
static std::string sizeString(const Path &file)
Returns the file's size in string format.
SpamClient(const GNet::ResolverInfo &host_and_service, unsigned int connect_timeout, unsigned int response_timeout)
Constructor.
static unsigned long toULong(const std::string &s, bool limited=false)
Converts string 's' to an unsigned long.