32 bool server_side ,
const std::string & path ) :
33 m_server_side(server_side) ,
34 m_prefix(server_side?
"filter":
"client filter") ,
35 m_exit(0,server_side) ,
37 m_task(*this,eh,
"<<filter exec error: __strerror__>>",
G::Root::nobody())
57 return m_exit.special ;
62 G_ASSERT( m_exit.ok() || m_exit.abandon() || !m_response.empty() ) ;
63 if( m_exit.ok() || m_exit.abandon() )
64 return std::string() ;
71 G_ASSERT( m_exit.ok() || m_exit.abandon() || !m_reason.empty() ) ;
72 if( m_exit.ok() || m_exit.abandon() )
73 return std::string() ;
82 args.push_back(
G::Path(message_file).str() ) ;
84 G_LOG(
"GSmtp::ExecutableFilter::start: " << m_prefix <<
": running " << commandline.displayString() ) ;
85 m_task.start( commandline ) ;
88 void GSmtp::ExecutableFilter::onTaskDone(
int exit_code ,
const std::string & output )
91 std::pair<std::string,std::string> pair = parseOutput( output ,
"rejected" ) ;
92 m_response = pair.first ;
93 m_reason = pair.second ;
94 if( m_response.find(
"filter exec error") == 0U )
96 m_reason = m_response ;
97 m_response =
"rejected" ;
100 m_exit = Exit( exit_code , m_server_side ) ;
103 G_WARNING(
"GSmtp::ExecutableFilter::onTaskDone: " << m_prefix <<
" failed: "
104 <<
"exit code " << exit_code <<
" [" << m_response <<
"]" ) ;
108 m_done_signal.emit( m_exit.result ) ;
111 std::pair<std::string,std::string> GSmtp::ExecutableFilter::parseOutput( std::string s ,
const std::string & default_ )
const
113 G_DEBUG(
"GSmtp::ExecutableFilter::parseOutput: in: \"" <<
G::Str::printable(s) <<
"\"" ) ;
115 const std::string start_1(
"<<") ;
116 const std::string end_1(
">>") ;
117 const std::string start_2(
"[[") ;
118 const std::string end_2(
"]]") ;
125 for( G::StringArray::iterator p = lines.begin() ; p != lines.end() ; )
127 std::string line = *p ;
128 size_t pos_start = line.find(start_1) ;
129 size_t pos_end = line.find(end_1) ;
130 if( pos_start != 0U )
132 pos_start = line.find(start_2) ;
133 pos_end = line.find(end_2) ;
135 if( pos_start == 0U && pos_end != std::string::npos )
141 p = lines.erase( p ) ;
145 G_DEBUG(
"GSmtp::ExecutableFilter::parseOutput: out: [" <<
G::Str::join(
"|",lines) <<
"]" ) ;
147 std::string response = ( !lines.empty() && !lines.at(0U).empty() ) ? lines.at(0U) : default_ ;
148 std::string reason = ( lines.size() > 1U && !lines.at(1U).empty() ) ? lines.at(1U) : response ;
149 return std::make_pair( response , reason ) ;
154 return m_done_signal ;
164 return m_exit.abandon() ;
static std::string printable(const std::string &in, char escape= '\\')
Returns a printable represention of the given input string.
virtual bool simple() const override
Override from from GSmtp::Filter.
std::vector< std::string > StringArray
A std::vector of std::strings.
ExecutableFilter(GNet::ExceptionHandler &, bool server_side, const std::string &)
Constructor.
virtual ~ExecutableFilter()
Destructor.
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events...
static unsigned int replaceAll(std::string &s, const std::string &from, const std::string &to)
Does a global replace on string 's', replacing all occurances of sub-string 'from' with 'to'...
virtual bool abandoned() const override
Override from from GSmtp::Filter.
virtual void start(const std::string &path) override
Override from from GSmtp::Filter.
virtual bool special() const override
Override from from GSmtp::Filter.
virtual std::string id() const override
Override from from GSmtp::Filter.
A structure representing an external program, holding a path and a set of arguments.
virtual G::Slot::Signal1< int > & doneSignal() override
Override from from GSmtp::Filter.
virtual std::string reason() const override
Override from from GSmtp::Filter.
virtual void cancel() override
Override from from GSmtp::Filter.
static std::string join(const std::string &sep, const StringArray &strings)
Concatenates an array of strings with separators.
static void splitIntoFields(const std::string &in, StringArray &out, const std::string &separators, char escape= '\0', bool remove_escapes=true)
Splits the string into fields.
A Path object represents a file system path.
virtual std::string response() const override
Override from from GSmtp::Filter.