E-MailRelay
gfilter.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2023 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
49G::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 result(Result::fail) ,
57 special(false)
58{
59 if( exit_code == 0 )
60 {
61 result = Result::ok ;
62 }
63 else if( exit_code >= 1 && exit_code < 100 )
64 {
65 result = Result::fail ;
66 }
67 else if( exit_code == 100 )
68 {
69 result = Result::abandon ;
70 }
71 else if( exit_code == 101 )
72 {
73 result = Result::ok ;
74 }
75
76 bool server_side = type == Filter::Type::server ;
77 if( server_side )
78 {
79 const bool rescan = true ;
80 if( exit_code == 102 )
81 {
82 result = Result::abandon ; special = rescan ;
83 }
84 else if( exit_code == 103 )
85 {
86 result = Result::ok ; special = rescan ;
87 }
88 else if( exit_code == 104 )
89 {
90 result = Result::fail ; special = rescan ;
91 }
92 }
93 else // client-side
94 {
95 const bool stop_scanning = true ;
96 if( exit_code == 102 )
97 {
98 result = Result::ok ; special = stop_scanning ;
99 }
100 else if( exit_code == 103 )
101 {
102 result = Result::ok ;
103 }
104 else if( exit_code == 104 )
105 {
106 result = Result::abandon ; special = stop_scanning ;
107 }
108 else if( exit_code == 105 )
109 {
110 result = Result::fail ; special = stop_scanning ;
111 }
112 }
113}
114
115bool GSmtp::Filter::Exit::ok() const
116{
117 return result == Result::ok ;
118}
119
120bool GSmtp::Filter::Exit::abandon() const
121{
122 return result == Result::abandon ;
123}
124
125#ifndef G_LIB_SMALL
126bool GSmtp::Filter::Exit::fail() const
127{
128 return result == Result::fail ;
129}
130#endif
131
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.
static G::string_view strtype(Type type) noexcept
Returns a type string for logging: "filter", "client-filter" or "routing-filter".
Definition: gfilter.cpp:49
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
A class like c++17's std::string_view.
Definition: gstringview.h:51