E-MailRelay
gmessagestore.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 gmessagestore.cpp
19///
20
21#include "gdef.h"
22#include "gmessagestore.h"
23#include "gstoredmessage.h"
24#include "gstr.h"
25#include "gassert.h"
26#include "gconvert.h"
27
28std::unique_ptr<GStore::StoredMessage> GStore::operator++( std::shared_ptr<MessageStore::Iterator> & iter )
29{
30 return iter.get() ? iter->next() : std::unique_ptr<StoredMessage>() ;
31}
32
33GStore::MessageStore::AddressStyle GStore::MessageStore::addressStyle( std::string_view address )
34{
35 if( address.empty() || address.at(0U) == '@' || address.at(address.size()-1U) == '@' )
36 return AddressStyle::Invalid ; // missing stuff
37
38 if( !G::Str::isPrintable(address) )
39 return AddressStyle::Invalid ; // control characters (inc. DEL)
40
41 std::size_t at_pos = address.rfind( '@' ) ;
42 std::string_view mailbox = G::Str::headView( address , at_pos , address ) ;
43 std::string_view domain = G::Str::tailView( address , at_pos ) ;
44 G_ASSERT( !mailbox.empty() ) ;
45
46 bool mailbox_ascii = G::Str::isPrintableAscii( mailbox ) ;
47 bool mailbox_utf8 = !mailbox_ascii && G::Convert::valid( mailbox ) ;
48 bool domain_ascii = domain.empty() || G::Str::isPrintableAscii( domain ) ;
49 bool domain_utf8 = !domain_ascii && G::Convert::valid( domain ) ;
50
51 if( ( !mailbox_ascii && !mailbox_utf8 ) || ( !domain_ascii && !domain_utf8 ) )
52 return AddressStyle::Invalid ; // invalid encoding
53 else if( mailbox_ascii && domain_ascii )
54 return AddressStyle::Ascii ;
55 else if( mailbox_ascii && !domain_ascii )
56 return AddressStyle::Utf8Domain ;
57 else if( !mailbox_ascii && domain_ascii )
58 return AddressStyle::Utf8Mailbox ;
59 else
60 return AddressStyle::Utf8Both ;
61}
62
static AddressStyle addressStyle(std::string_view address)
Parses an address to determine whether it has ASCII or UTF-8 parts.
static bool valid(std::string_view) noexcept
Returns true if the string is valid UTF-8.
Definition: gconvert.cpp:44
static bool isPrintable(std::string_view s) noexcept
Returns true if every character is 0x20 or above but not 0x7f.
Definition: gstr.cpp:418
static std::string_view tailView(std::string_view in, std::size_t pos, std::string_view default_={}) noexcept
Like tail() but returning a view into the input string.
Definition: gstr.cpp:1337
static std::string_view headView(std::string_view in, std::size_t pos, std::string_view default_={}) noexcept
Like head() but returning a view into the input string.
Definition: gstr.cpp:1308
static bool isPrintableAscii(std::string_view s) noexcept
Returns true if every character is between 0x20 and 0x7e inclusive.
Definition: gstr.cpp:413