E-MailRelay
gfilter.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file gfilter.cpp
19///
20
21#include "gdef.h"
22#include "gfilter.h"
23#include "gstringview.h"
24#include "gstr.h"
25
26std::string GSmtp::Filter::str( Filter::Type type ) const
27{
28 std::ostringstream ss ;
29
30 auto r = result() ;
31 if( r == Result::fail )
32 ss << "failed " ;
33 else if( r == Result::abandon )
34 ss << "ok(abandon) " ;
35 else
36 ss << "ok " ;
37
38 if( special() )
39 ss << ( type == Filter::Type::server ? "+rescan " : "+break " ) ;
40
41 ss << "response=[" << response() << "]" ;
42
43 if( reason() != response() )
44 ss << " reason=[" << reason() << "]" ;
45
46 return ss.str() ;
47}
48
49std::string_view GSmtp::Filter::strtype( Filter::Type type ) noexcept
50{
51 return type == Type::server ? "filter"_sv :
52 ( type == Type::client ? "client-filter"_sv : "routing-filter"_sv ) ;
53}
54
55GSmtp::Filter::Exit::Exit( int exit_code , Filter::Type type )
56{
57 if( exit_code == 0 )
58 {
59 result = Result::ok ;
60 }
61 else if( exit_code >= 1 && exit_code < 100 )
62 {
63 result = Result::fail ;
64 }
65 else if( exit_code == 100 )
66 {
67 result = Result::abandon ;
68 }
69 else if( exit_code == 101 )
70 {
71 result = Result::ok ;
72 }
73
74 bool server_side = type == Filter::Type::server ;
75 if( server_side )
76 {
77 const bool rescan = true ;
78 if( exit_code == 102 )
79 {
80 result = Result::abandon ; special = rescan ;
81 }
82 else if( exit_code == 103 )
83 {
84 result = Result::ok ; special = rescan ;
85 }
86 else if( exit_code == 104 )
87 {
88 result = Result::fail ; special = rescan ;
89 }
90 }
91 else // client-side
92 {
93 const bool stop_scanning = true ;
94 if( exit_code == 102 )
95 {
96 result = Result::ok ; special = stop_scanning ;
97 }
98 else if( exit_code == 103 )
99 {
100 result = Result::ok ;
101 }
102 else if( exit_code == 104 )
103 {
104 result = Result::abandon ; special = stop_scanning ;
105 }
106 else if( exit_code == 105 )
107 {
108 result = Result::fail ; special = stop_scanning ;
109 }
110 }
111}
112
113bool GSmtp::Filter::Exit::ok() const
114{
115 return result == Result::ok ;
116}
117
118bool GSmtp::Filter::Exit::abandon() const
119{
120 return result == Result::abandon ;
121}
122
123#ifndef G_LIB_SMALL
124bool GSmtp::Filter::Exit::fail() const
125{
126 return result == Result::fail ;
127}
128#endif
129
virtual Result result() const =0
Returns the filter result, after the doneSignal() has been emitted.
virtual bool special() const =0
Returns true if the filter indicated special handling is required.
virtual std::string response() const =0
Returns a non-empty SMTP response string iff the filter failed, or an empty response if successful or...
virtual std::string reason() const =0
Returns a non-empty reason string iff the filter failed, or an empty reason if successful or abandone...
std::string str(Type type) const
Returns a diagnostic string for logging, including the filter result.
Definition: gfilter.cpp:26
static std::string_view strtype(Type type) noexcept
Returns a type string for logging: "filter", "client-filter" or "routing-filter".
Definition: gfilter.cpp:49