30 std::string GSmtp::SpamClient::m_username ;
33 unsigned int connect_timeout ,
unsigned int response_timeout ) :
34 GNet::
Client(location,connect_timeout,response_timeout,0U,
GNet::LineBufferConfig::newline()) ,
40 G_LOG(
"GSmtp::SpamClient::ctor: spam connection to [" << location <<
"]" ) ;
41 G_DEBUG(
"GSmtp::SpamClient::ctor: spam read/only=" << read_only ) ;
42 G_DEBUG(
"GSmtp::SpamClient::ctor: spam connection timeout " << connect_timeout ) ;
43 G_DEBUG(
"GSmtp::SpamClient::ctor: spam response timeout " << response_timeout ) ;
48 G_DEBUG(
"GSmtp::SpamClient::dtor: busy=" << m_busy ) ;
53 m_username = username ;
63 G_DEBUG(
"GSmtp::SpamClient::request: path=" << path ) ;
65 throw Error(
"protocol error" ) ;
68 m_timer.startTimer( 0U ) ;
71 void GSmtp::SpamClient::onTimeout()
73 G_DEBUG(
"GSmtp::SpamClient::onTimeout: connected=" << connected() ) ;
92 void GSmtp::SpamClient::start()
94 m_request.send( m_path , m_username ) ;
99 m_request.sendMore() ;
104 m_response.add( m_path , std::string(line_data,line_size) ) ;
105 if( m_response.complete() )
106 eventSignal().emit(
"spam" , m_response.result() ) ;
112 GSmtp::SpamClient::Request::Request(
Client & client ) :
118 void GSmtp::SpamClient::Request::send(
const std::string & path ,
const std::string & username )
120 G_LOG(
"GSmtp::SpamClient::Request::send: spam request for [" << path <<
"]" ) ;
121 m_stream.open( path.c_str() , std::ios_base::binary ) ;
122 if( !m_stream.good() )
123 throw SpamClient::Error(
"cannot read content file" , path ) ;
126 G_DEBUG(
"GSmtp::SpamClient::Request::send: spam request file size: " << file_size ) ;
128 std::ostringstream ss ;
129 std::string eol =
"\r\n" ;
130 ss <<
"PROCESS SPAMC/1.4" << eol ;
131 if( !username.empty() )
132 ss <<
"User: " << username << eol ;
133 ss <<
"Content-length: " << file_size << eol ;
136 bool sent = m_client->send( ss.str() ) ;
141 G_DEBUG(
"GSmtp::SpamClient::Request::send: spam sent" ) ;
144 bool GSmtp::SpamClient::Request::sendMore()
146 m_stream.read( &m_buffer[0] , m_buffer.size() ) ;
147 std::streamsize n = m_stream.gcount() ;
150 G_LOG(
"GSmtp::SpamClient::Request::sendMore: spam request done" ) ;
155 G_DEBUG(
"GSmtp::SpamClient::Request::sendMore: spam request sending " << n <<
" bytes" ) ;
156 return m_client->send( std::string(&m_buffer[0],static_cast<size_t>(n)) ) ;
162 GSmtp::SpamClient::Response::Response(
bool read_only ) :
163 m_read_only(read_only) ,
165 m_content_length(0U) ,
170 GSmtp::SpamClient::Response::~Response()
172 if( m_stream.is_open() )
179 void GSmtp::SpamClient::Response::add(
const std::string & path ,
const std::string & line )
181 if( m_state == 0 && !ok(line) )
185 else if( m_state == 0 )
187 G_DEBUG(
"GSmtp::SpamClient::Request::sendMore: spam response" ) ;
188 m_path_final = path ;
189 m_path_tmp = path +
".spamd" ;
190 if( !m_read_only && !m_stream.is_open() )
192 m_stream.open( m_path_tmp.c_str() , std::ios_base::binary ) ;
193 if( !m_stream.good() )
194 throw SpamClient::Error(
"cannot write temporary content file" , m_path_tmp ) ;
196 m_content_length = m_size = 0U ;
202 if( line.find(
"Spam:") == 0U )
206 else if( ( line.empty() || line ==
"\r" ) && m_content_length == 0U )
207 throw SpamClient::Error(
"invalid response headers" ) ;
208 else if( line.empty() || line ==
"\r" )
211 else if( m_state == 2 )
213 m_size += ( line.size() + 1U ) ;
215 if( m_stream.is_open() )
216 m_stream << line <<
"\n" ;
218 if( m_size >= m_content_length )
220 if( m_size != m_content_length )
221 G_WARNING(
"GSmtp::SpamClient::Response::add: incorrect content length in spam response" ) ;
222 G_LOG(
"GSmtp::SpamClient::add: spam response size: " << m_content_length ) ;
224 if( m_stream.is_open() )
227 if( m_stream.fail() )
228 throw SpamClient::Error(
"cannot write temporary content file" , m_path_tmp ) ;
239 bool GSmtp::SpamClient::Response::complete()
const
241 return m_state == 3 ;
244 bool GSmtp::SpamClient::Response::ok(
const std::string & line )
const
247 if( line.empty() )
return false ;
248 if( line.find(
"SPAMD/") != 0U )
return false ;
250 if( parts.size() < 2U )
return false ;
251 return parts.at(1U) ==
"0" ;
254 std::string GSmtp::SpamClient::Response::result()
const
257 return std::string() ;
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.
SpamClient(const GNet::Location &host_and_service, bool read_only, unsigned int connect_timeout, unsigned int response_timeout)
Constructor.
virtual bool onReceive(const char *, size_t, size_t) override
Override from GNet::Client.
static bool imatch(const std::string &, const std::string &)
Returns true if the two strings are the same, ignoring Latin-1 case.
std::vector< std::string > StringArray
A std::vector of std::strings.
static void splitIntoTokens(const std::string &in, StringArray &out, const std::string &ws)
Splits the string into 'ws'-delimited tokens.
static void username(const std::string &)
Sets the username used in the network protocol.
static bool rename(const Path &from, const Path &to, const NoThrow &)
Renames the file. Returns false on error.
virtual void onSecure(const std::string &) override
Override from GNet::SocketProtocolSink.
virtual void onDelete(const std::string &) override
Override from GNet::HeapClient.
A class that holds a host/service name pair and the preferred address family (if any), and also the results of a name-to-address lookup, ie.
static unsigned int toUInt(const std::string &s)
Converts string 's' to an unsigned int.
virtual ~SpamClient()
Destructor.
A client class that interacts with a remote process using a protocol somewhat similar to the spamassa...
static std::string trimmed(const std::string &s, const std::string &ws)
Returns a trim()med version of s.
static bool remove(const Path &path, const NoThrow &)
Deletes the file or directory. Returns false on error.
A class which acts as an SMTP client, extracting messages from a message store and forwarding them to...
An overload discriminator class for File methods.
virtual void onSendComplete() override
Override from GNet::SimpleClient.
virtual void onConnect() override
Override from GNet::SimpleClient.
static std::string sizeString(const Path &file)
Returns the file's size in string format.
static std::string ws()
Returns a string of standard whitespace characters.
void request(const std::string &file_path)
Starts sending a request that comprises a few http-like header lines followed by the contents of the ...