2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-11-16 02:37:09 +01:00
|
|
|
|
|
|
|
#include <ircd/spirit.h>
|
|
|
|
|
2017-12-12 21:13:06 +01:00
|
|
|
namespace ircd::m
|
|
|
|
{
|
2018-03-17 18:44:40 +01:00
|
|
|
using namespace ircd::spirit;
|
2017-12-12 21:13:06 +01:00
|
|
|
|
|
|
|
[[noreturn]] void failure(const qi::expectation_failure<const char *> &, const string_view &);
|
|
|
|
}
|
2017-11-16 02:37:09 +01:00
|
|
|
|
|
|
|
template<class it>
|
2017-12-12 21:13:06 +01:00
|
|
|
struct ircd::m::id::input
|
2018-03-17 18:44:40 +01:00
|
|
|
:qi::grammar<it, unused_type>
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-03-17 18:44:40 +01:00
|
|
|
template<class R = unused_type, class... S> using rule = qi::rule<it, R, S...>;
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2017-12-12 21:13:06 +01:00
|
|
|
// Sigils
|
2018-02-20 05:29:38 +01:00
|
|
|
const rule<m::id::sigil> event_id_sigil { lit(char(m::id::EVENT)) ,"event_id sigil" };
|
|
|
|
const rule<m::id::sigil> user_id_sigil { lit(char(m::id::USER)) ,"user_id sigil" };
|
|
|
|
const rule<m::id::sigil> room_id_sigil { lit(char(m::id::ROOM)) ,"room_id sigil" };
|
|
|
|
const rule<m::id::sigil> room_alias_sigil { lit(char(m::id::ROOM_ALIAS)) ,"room_alias sigil" };
|
|
|
|
const rule<m::id::sigil> group_id_sigil { lit(char(m::id::GROUP)) ,"group_id sigil" };
|
2018-03-05 17:21:12 +01:00
|
|
|
const rule<m::id::sigil> node_sigil { lit(char(m::id::NODE)) ,"node sigil" };
|
2018-02-20 05:29:38 +01:00
|
|
|
const rule<m::id::sigil> device_sigil { lit(char(m::id::DEVICE)) ,"device sigil" };
|
|
|
|
const rule<m::id::sigil> sigil
|
2017-12-12 21:13:06 +01:00
|
|
|
{
|
|
|
|
event_id_sigil |
|
|
|
|
user_id_sigil |
|
|
|
|
room_id_sigil |
|
|
|
|
room_alias_sigil |
|
|
|
|
group_id_sigil |
|
2018-03-16 22:25:36 +01:00
|
|
|
node_sigil |
|
2018-02-15 22:01:07 +01:00
|
|
|
device_sigil
|
2017-12-12 21:13:06 +01:00
|
|
|
,"sigil"
|
|
|
|
};
|
|
|
|
|
|
|
|
// character of a localpart; must not contain ':' because that's the terminator
|
|
|
|
const rule<> localpart_char
|
|
|
|
{
|
|
|
|
char_ - ':'
|
|
|
|
,"localpart character"
|
|
|
|
};
|
|
|
|
|
|
|
|
// a localpart is zero or more localpart characters
|
|
|
|
const rule<> localpart
|
|
|
|
{
|
2018-03-16 22:25:36 +01:00
|
|
|
*localpart_char
|
2017-12-12 21:13:06 +01:00
|
|
|
,"localpart"
|
|
|
|
};
|
|
|
|
|
|
|
|
// character of a non-historical user_id localpart
|
|
|
|
const rule<> user_id_char
|
|
|
|
{
|
2018-02-20 22:23:42 +01:00
|
|
|
char_(rfc1459::character::charset(rfc1459::character::NICK) + "./=")
|
2017-12-12 21:13:06 +01:00
|
|
|
,"user_id character"
|
|
|
|
};
|
|
|
|
|
|
|
|
// a user_id localpart is 1 or more user_id localpart characters
|
|
|
|
const rule<> user_id_localpart
|
|
|
|
{
|
|
|
|
+user_id_char
|
|
|
|
,"user_id localpart"
|
|
|
|
};
|
|
|
|
|
|
|
|
// a prefix is a sigil and a localpart; user_id prefix
|
|
|
|
const rule<> user_id_prefix
|
|
|
|
{
|
|
|
|
user_id_sigil >> user_id_localpart
|
|
|
|
,"user_id prefix"
|
|
|
|
};
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2017-12-12 21:13:06 +01:00
|
|
|
// a prefix is a sigil and a localpart; proper invert of user_id prefix
|
|
|
|
const rule<> non_user_id_prefix
|
|
|
|
{
|
|
|
|
((!user_id_sigil) > sigil) >> localpart
|
|
|
|
,"non user_id prefix"
|
|
|
|
};
|
|
|
|
|
|
|
|
// a prefix is a sigil and a localpart
|
|
|
|
const rule<> prefix
|
|
|
|
{
|
|
|
|
user_id_prefix | non_user_id_prefix
|
|
|
|
,"prefix"
|
|
|
|
};
|
|
|
|
|
2018-03-13 23:39:24 +01:00
|
|
|
//TODO: ---- share grammar with rfc3986.cc
|
|
|
|
|
2018-03-14 00:22:47 +01:00
|
|
|
const rule<uint16_t> port
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
|
|
|
ushort_
|
2018-03-13 21:22:13 +01:00
|
|
|
,"port number"
|
|
|
|
};
|
|
|
|
|
|
|
|
const rule<> ip4_octet
|
|
|
|
{
|
|
|
|
repeat(1,3)[char_("0-9")]
|
|
|
|
,"IPv4 octet"
|
|
|
|
};
|
|
|
|
|
|
|
|
const rule<> ip4_literal
|
|
|
|
{
|
|
|
|
repeat(3)[ip4_octet >> '.'] >> ip4_octet
|
|
|
|
,"IPv4 literal"
|
|
|
|
};
|
|
|
|
|
|
|
|
const rule<> ip6_char
|
|
|
|
{
|
|
|
|
char_("0-9a-fA-F")
|
|
|
|
,"IPv6 character"
|
|
|
|
};
|
|
|
|
|
|
|
|
const rule<> ip6_h16
|
|
|
|
{
|
|
|
|
repeat(1,4)[ip6_char]
|
|
|
|
,"IPv6 hexdigit"
|
|
|
|
};
|
|
|
|
|
|
|
|
const rule<> ip6_piece
|
|
|
|
{
|
|
|
|
ip6_h16 >> ':'
|
|
|
|
,"IPv6 address piece"
|
|
|
|
};
|
|
|
|
|
|
|
|
// This is reversed from the BNF in the RFC otherwise it requires
|
|
|
|
// backtracking during the repeat[]; grammars are adjusted accordingly.
|
|
|
|
const rule<> ip6_ipiece
|
|
|
|
{
|
|
|
|
':' >> ip6_h16
|
|
|
|
,"IPv6 address piece"
|
|
|
|
};
|
|
|
|
|
|
|
|
const rule<> ip6_ls32
|
|
|
|
{
|
|
|
|
(ip6_h16 >> ':' >> ip6_h16) | ip4_literal
|
|
|
|
};
|
|
|
|
|
|
|
|
/// https://tools.ietf.org/html/rfc3986 Appendix A
|
|
|
|
const rule<> ip6_addr[10]
|
|
|
|
{
|
|
|
|
{ repeat(6)[ip6_piece] >> ip6_ls32 },
|
|
|
|
{ lit("::") >> repeat(5)[ip6_piece] >> ip6_ls32 },
|
|
|
|
{ ip6_h16 >> lit("::") >> repeat(4)[ip6_piece] >> ip6_ls32 },
|
|
|
|
{ ip6_h16 >> repeat(0,1)[ip6_ipiece] >> lit("::") >> repeat(3)[ip6_piece] >> ip6_ls32 },
|
|
|
|
{ ip6_h16 >> repeat(0,2)[ip6_ipiece] >> lit("::") >> repeat(2)[ip6_piece] >> ip6_ls32 },
|
|
|
|
{ ip6_h16 >> repeat(0,3)[ip6_ipiece] >> lit("::") >> ip6_piece >> ip6_ls32 },
|
|
|
|
{ ip6_h16 >> repeat(0,4)[ip6_ipiece] >> lit("::") >> ip6_ls32 },
|
|
|
|
{ ip6_h16 >> repeat(0,5)[ip6_ipiece] >> lit("::") >> -ip6_h16 },
|
|
|
|
{ lit("::") >> -ip6_h16 },
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const rule<> ip6_address
|
|
|
|
{
|
2018-03-13 21:22:13 +01:00
|
|
|
ip6_addr[0] | ip6_addr[1] | ip6_addr[2] | ip6_addr[3] | ip6_addr[4] | ip6_addr[5] |
|
|
|
|
ip6_addr[6] | ip6_addr[7] | ip6_addr[8] | ip6_addr[9]
|
|
|
|
,"IPv6 address"
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const rule<> ip6_literal
|
|
|
|
{
|
|
|
|
'[' >> ip6_address >> ']'
|
|
|
|
,"ip6 literal"
|
|
|
|
};
|
|
|
|
|
2018-03-13 21:22:13 +01:00
|
|
|
const rule<> hostlabel
|
|
|
|
{
|
|
|
|
char_("A-Za-z0-9") >> *(char_("A-Za-z0-9\x2D")) // x2D is '-'
|
|
|
|
};
|
|
|
|
|
|
|
|
const rule<> hostname
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-03-13 21:22:13 +01:00
|
|
|
hostlabel % '.',
|
|
|
|
"hostname"
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
2018-03-13 21:22:13 +01:00
|
|
|
const rule<> dns_name
|
|
|
|
{
|
|
|
|
ip6_literal | ip4_literal | hostname
|
|
|
|
,"DNS name"
|
|
|
|
};
|
2017-12-12 21:13:06 +01:00
|
|
|
|
2018-03-13 23:39:24 +01:00
|
|
|
//TODO: /---- share grammar with rfc3986.cc
|
|
|
|
|
2017-12-12 21:13:06 +01:00
|
|
|
/// (Appendix 4.1) Server Name
|
|
|
|
/// A homeserver is uniquely identified by its server name. This value
|
|
|
|
/// is used in a number of identifiers, as described below. The server
|
|
|
|
/// name represents the address at which the homeserver in question can
|
|
|
|
/// be reached by other homeservers. The complete grammar is:
|
|
|
|
/// `server_name = dns_name [ ":" port]`
|
|
|
|
/// `dns_name = host`
|
|
|
|
/// `port = *DIGIT`
|
|
|
|
/// where host is as defined by RFC3986, section 3.2.2. Examples of valid
|
|
|
|
/// server names are:
|
|
|
|
/// `matrix.org`
|
|
|
|
/// `matrix.org:8888`
|
|
|
|
/// `1.2.3.4` (IPv4 literal)
|
|
|
|
/// `1.2.3.4:1234` (IPv4 literal with explicit port)
|
|
|
|
/// `[1234:5678::abcd]` (IPv6 literal)
|
|
|
|
/// `[1234:5678::abcd]:5678` (IPv6 literal with explicit port)
|
2017-11-16 02:37:09 +01:00
|
|
|
const rule<> server_name
|
|
|
|
{
|
2018-03-13 21:22:13 +01:00
|
|
|
dns_name >> -(':' > port)
|
2017-11-16 02:37:09 +01:00
|
|
|
,"server name"
|
|
|
|
};
|
|
|
|
|
|
|
|
const rule<> mxid
|
|
|
|
{
|
2017-12-12 21:13:06 +01:00
|
|
|
prefix >> ':' >> server_name
|
2017-11-16 02:37:09 +01:00
|
|
|
,"mxid"
|
|
|
|
};
|
|
|
|
|
2017-12-12 21:13:06 +01:00
|
|
|
input()
|
|
|
|
:input::base_type{rule<>{}}
|
2017-11-16 02:37:09 +01:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2017-12-12 21:13:06 +01:00
|
|
|
template<class it>
|
|
|
|
struct ircd::m::id::output
|
2018-03-17 18:44:40 +01:00
|
|
|
:karma::grammar<it, unused_type>
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-03-17 18:44:40 +01:00
|
|
|
template<class T = unused_type> using rule = karma::rule<it, T>;
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2017-12-12 21:13:06 +01:00
|
|
|
output()
|
|
|
|
:output::base_type{rule<>{}}
|
|
|
|
{}
|
|
|
|
};
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2017-12-12 21:13:06 +01:00
|
|
|
struct ircd::m::id::parser
|
|
|
|
:input<const char *>
|
|
|
|
{
|
2017-11-16 02:37:09 +01:00
|
|
|
string_view operator()(const id::sigil &, const string_view &id) const;
|
|
|
|
string_view operator()(const string_view &id) const;
|
|
|
|
}
|
|
|
|
const ircd::m::id::parser;
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::id::parser::operator()(const id::sigil &sigil,
|
|
|
|
const string_view &id)
|
2017-12-12 21:13:06 +01:00
|
|
|
const try
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-03-16 22:25:36 +01:00
|
|
|
const rule<> sigil_type
|
|
|
|
{
|
|
|
|
&lit(char(sigil))
|
|
|
|
,"sigil type"
|
|
|
|
};
|
|
|
|
|
2017-12-12 21:13:06 +01:00
|
|
|
const rule<string_view> view_mxid
|
|
|
|
{
|
2018-03-16 22:25:36 +01:00
|
|
|
raw[eps > (sigil_type > mxid)]
|
2017-12-12 21:13:06 +01:00
|
|
|
};
|
|
|
|
|
2017-11-16 02:37:09 +01:00
|
|
|
string_view out;
|
2018-03-14 07:33:37 +01:00
|
|
|
const char *start{id.begin()};
|
2018-04-25 04:18:50 +02:00
|
|
|
const char *const stop
|
|
|
|
{
|
|
|
|
std::min(id.end(), start + MAX_SIZE)
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool res{qi::parse(start, stop, view_mxid, out)};
|
2018-03-16 22:25:36 +01:00
|
|
|
assert(res == true);
|
2017-11-16 02:37:09 +01:00
|
|
|
return out;
|
|
|
|
}
|
2017-12-12 21:13:06 +01:00
|
|
|
catch(const qi::expectation_failure<const char *> &e)
|
|
|
|
{
|
2018-02-11 02:00:20 +01:00
|
|
|
failure(e, reflect(sigil));
|
2017-12-12 21:13:06 +01:00
|
|
|
}
|
2017-11-16 02:37:09 +01:00
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::id::parser::operator()(const string_view &id)
|
2017-12-12 21:13:06 +01:00
|
|
|
const try
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2017-12-12 21:13:06 +01:00
|
|
|
static const rule<string_view> view_mxid
|
|
|
|
{
|
2018-03-16 22:25:36 +01:00
|
|
|
raw[eps > mxid]
|
2017-12-12 21:13:06 +01:00
|
|
|
};
|
|
|
|
|
2017-11-16 02:37:09 +01:00
|
|
|
string_view out;
|
2018-03-14 07:33:37 +01:00
|
|
|
const char *start{id.begin()};
|
2018-04-25 04:18:50 +02:00
|
|
|
const char *const stop
|
|
|
|
{
|
|
|
|
std::min(id.end(), start + MAX_SIZE)
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool res(qi::parse(start, stop, view_mxid, out));
|
2018-03-16 22:25:36 +01:00
|
|
|
assert(res == true);
|
2017-11-16 02:37:09 +01:00
|
|
|
return out;
|
|
|
|
}
|
2017-12-12 21:13:06 +01:00
|
|
|
catch(const qi::expectation_failure<const char *> &e)
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2017-12-12 21:13:06 +01:00
|
|
|
failure(e, "mxid");
|
|
|
|
}
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2017-12-12 21:13:06 +01:00
|
|
|
struct ircd::m::id::validator
|
|
|
|
:input<const char *>
|
|
|
|
{
|
2017-11-16 02:37:09 +01:00
|
|
|
void operator()(const id::sigil &sigil, const string_view &id) const;
|
|
|
|
void operator()(const string_view &id) const;
|
|
|
|
}
|
2017-12-12 21:13:06 +01:00
|
|
|
const ircd::m::id::validator;
|
2017-11-16 02:37:09 +01:00
|
|
|
|
|
|
|
void
|
2017-12-12 21:13:06 +01:00
|
|
|
ircd::m::id::validator::operator()(const string_view &id)
|
2017-11-16 02:37:09 +01:00
|
|
|
const try
|
|
|
|
{
|
2018-03-14 07:33:37 +01:00
|
|
|
const char *start{id.begin()};
|
2018-04-25 04:18:50 +02:00
|
|
|
const char *const stop
|
|
|
|
{
|
|
|
|
std::min(id.end(), start + MAX_SIZE)
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool ret(qi::parse(start, stop, eps > mxid));
|
2018-03-16 22:25:36 +01:00
|
|
|
assert(ret == true);
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
catch(const qi::expectation_failure<const char *> &e)
|
|
|
|
{
|
2017-12-12 21:13:06 +01:00
|
|
|
failure(e, "mxid");
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-12-12 21:13:06 +01:00
|
|
|
ircd::m::id::validator::operator()(const id::sigil &sigil,
|
|
|
|
const string_view &id)
|
2017-11-16 02:37:09 +01:00
|
|
|
const try
|
|
|
|
{
|
2018-03-16 22:25:36 +01:00
|
|
|
const rule<> sigil_type
|
|
|
|
{
|
|
|
|
&lit(char(sigil))
|
|
|
|
,"sigil type"
|
|
|
|
};
|
|
|
|
|
|
|
|
const rule<> valid_mxid
|
2017-12-12 21:13:06 +01:00
|
|
|
{
|
2018-03-16 22:25:36 +01:00
|
|
|
eps > (sigil_type > mxid)
|
2017-12-12 21:13:06 +01:00
|
|
|
};
|
|
|
|
|
2018-03-14 07:33:37 +01:00
|
|
|
const char *start{id.begin()};
|
2018-04-25 04:18:50 +02:00
|
|
|
const char *const stop
|
|
|
|
{
|
|
|
|
std::min(id.end(), start + MAX_SIZE)
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool ret(qi::parse(start, stop, valid_mxid));
|
2018-03-16 22:25:36 +01:00
|
|
|
assert(ret == true);
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
catch(const qi::expectation_failure<const char *> &e)
|
|
|
|
{
|
2017-12-12 21:13:06 +01:00
|
|
|
failure(e, reflect(sigil));
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: abstract this pattern with ircd::json::printer in ircd/spirit.h
|
|
|
|
struct ircd::m::id::printer
|
|
|
|
:output<const char *>
|
|
|
|
{
|
|
|
|
template<class generator,
|
|
|
|
class attribute>
|
|
|
|
bool operator()(char *&out, char *const &stop, generator&& g, attribute&& a) const
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2017-12-12 21:13:06 +01:00
|
|
|
const auto throws{[&out, &stop]
|
|
|
|
{
|
|
|
|
throw INVALID_MXID
|
|
|
|
{
|
|
|
|
"Failed to print attribute '%s' generator '%s' (%zd bytes in buffer)",
|
|
|
|
demangle<decltype(a)>(),
|
|
|
|
demangle<decltype(g)>(),
|
|
|
|
size_t(stop - out)
|
|
|
|
};
|
|
|
|
}};
|
|
|
|
|
|
|
|
const auto gg
|
|
|
|
{
|
|
|
|
maxwidth(size_t(stop - out))[std::forward<generator>(g)] | eps[throws]
|
|
|
|
};
|
|
|
|
|
|
|
|
return karma::generate(out, gg, std::forward<attribute>(a));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class generator>
|
|
|
|
bool operator()(char *&out, char *const &stop, generator&& g) const
|
|
|
|
{
|
|
|
|
const auto throws{[&out, &stop]
|
|
|
|
{
|
|
|
|
throw INVALID_MXID
|
|
|
|
{
|
|
|
|
"Failed to print generator '%s' (%zd bytes in buffer)",
|
|
|
|
demangle<decltype(g)>(),
|
|
|
|
size_t(stop - out)
|
|
|
|
};
|
|
|
|
}};
|
|
|
|
|
|
|
|
const auto gg
|
|
|
|
{
|
|
|
|
maxwidth(size_t(stop - out))[std::forward<generator>(g)] | eps[throws]
|
|
|
|
};
|
|
|
|
|
|
|
|
return karma::generate(out, gg);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class... args>
|
|
|
|
bool operator()(mutable_buffer &out, args&&... a) const
|
|
|
|
{
|
|
|
|
return operator()(buffer::begin(out), buffer::end(out), std::forward<args>(a)...);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const ircd::m::id::printer;
|
|
|
|
|
2017-11-16 02:37:09 +01:00
|
|
|
//
|
2017-12-12 21:13:06 +01:00
|
|
|
// id
|
2017-11-16 02:37:09 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::id::id(const string_view &id)
|
|
|
|
:string_view{id}
|
|
|
|
{
|
|
|
|
validate(m::sigil(id), id);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::id::id(const id::sigil &sigil,
|
|
|
|
const string_view &id)
|
|
|
|
:string_view{id}
|
|
|
|
{
|
|
|
|
validate(sigil, id);
|
|
|
|
}
|
|
|
|
|
2018-01-19 05:44:55 +01:00
|
|
|
ircd::m::id::id(const enum sigil &sigil,
|
2017-11-16 02:37:09 +01:00
|
|
|
const mutable_buffer &buf,
|
2018-01-19 05:44:55 +01:00
|
|
|
const string_view &local,
|
|
|
|
const string_view &host)
|
|
|
|
:string_view{[&sigil, &buf, &local, &host]
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
|
|
|
const string_view src
|
|
|
|
{
|
2018-03-03 12:29:32 +01:00
|
|
|
startswith(local, sigil)? fmt::sprintf
|
|
|
|
{
|
|
|
|
buf, "%s:%s", local, host
|
|
|
|
}
|
|
|
|
: fmt::sprintf
|
2018-02-22 06:55:45 +01:00
|
|
|
{
|
|
|
|
buf, "%c%s:%s", char(sigil), local, host
|
|
|
|
}
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return parser(sigil, src);
|
|
|
|
}()}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-01-19 05:44:55 +01:00
|
|
|
ircd::m::id::id(const id::sigil &sigil,
|
2017-11-16 02:37:09 +01:00
|
|
|
const mutable_buffer &buf,
|
2018-01-19 05:44:55 +01:00
|
|
|
const string_view &id)
|
|
|
|
:string_view{[&sigil, &buf, &id]
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-01-19 05:44:55 +01:00
|
|
|
const auto len
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-01-19 05:44:55 +01:00
|
|
|
buffer::data(buf) != id.data()?
|
|
|
|
strlcpy(buffer::data(buf), id, buffer::size(buf)):
|
|
|
|
id.size()
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
2018-01-19 05:44:55 +01:00
|
|
|
const string_view src
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-01-19 05:44:55 +01:00
|
|
|
buffer::data(buf), len
|
|
|
|
};
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2018-01-19 05:44:55 +01:00
|
|
|
return parser(sigil, src);
|
2017-11-16 02:37:09 +01:00
|
|
|
}()}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::id::id(const enum sigil &sigil,
|
|
|
|
const mutable_buffer &buf,
|
|
|
|
const generate_t &,
|
|
|
|
const string_view &host)
|
|
|
|
:string_view{[&]
|
|
|
|
{
|
2017-12-12 21:13:06 +01:00
|
|
|
//TODO: output grammar
|
|
|
|
|
2018-02-18 01:10:01 +01:00
|
|
|
char namebuf[64];
|
|
|
|
string_view name; switch(sigil)
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
|
|
|
case sigil::USER:
|
2018-02-22 06:55:45 +01:00
|
|
|
name = fmt::sprintf
|
|
|
|
{
|
|
|
|
namebuf, "guest%lu", rand::integer()
|
|
|
|
};
|
2017-11-16 02:37:09 +01:00
|
|
|
break;
|
|
|
|
|
2017-12-12 21:13:06 +01:00
|
|
|
case sigil::ROOM_ALIAS:
|
2018-02-22 06:55:45 +01:00
|
|
|
name = fmt::sprintf
|
|
|
|
{
|
|
|
|
namebuf, "%lu", rand::integer()
|
|
|
|
};
|
2017-11-16 02:37:09 +01:00
|
|
|
break;
|
|
|
|
|
2018-02-22 06:55:45 +01:00
|
|
|
case sigil::ROOM:
|
|
|
|
{
|
|
|
|
mutable_buffer buf{namebuf, 16};
|
|
|
|
consume(buf, buffer::copy(buf, "AAAA"_sv)); //TODO: cluster euid
|
|
|
|
rand::string(rand::dict::alnum, buf);
|
|
|
|
name = {namebuf, 16};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-02-15 22:01:07 +01:00
|
|
|
case sigil::DEVICE:
|
2018-02-22 06:55:45 +01:00
|
|
|
{
|
|
|
|
static const auto &dict{rand::dict::alpha};
|
|
|
|
name = rand::string(dict, mutable_buffer{namebuf, 16});
|
2018-02-15 22:01:07 +01:00
|
|
|
break;
|
2018-02-22 06:55:45 +01:00
|
|
|
}
|
2018-02-15 22:01:07 +01:00
|
|
|
|
2017-11-16 02:37:09 +01:00
|
|
|
default:
|
2018-02-22 06:55:45 +01:00
|
|
|
name = fmt::sprintf
|
|
|
|
{
|
|
|
|
namebuf, "%c%lu", rand::character(), rand::integer()
|
|
|
|
};
|
2017-11-16 02:37:09 +01:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
2018-02-18 01:10:01 +01:00
|
|
|
return fmt::sprintf
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-02-18 01:10:01 +01:00
|
|
|
buf, "%c%s:%s", char(sigil), name, host
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
}()}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:13:06 +01:00
|
|
|
uint16_t
|
2018-03-14 00:22:47 +01:00
|
|
|
ircd::m::id::port()
|
|
|
|
const
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-03-14 00:22:47 +01:00
|
|
|
static const parser::rule<uint16_t> rule
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-03-14 00:22:47 +01:00
|
|
|
omit[parser.prefix >> ':' >> parser.dns_name >> ':'] >> parser.port
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
2018-04-04 22:03:13 +02:00
|
|
|
uint16_t ret{0};
|
2018-03-14 07:33:37 +01:00
|
|
|
auto *start{begin()};
|
2018-03-14 00:22:47 +01:00
|
|
|
const auto res
|
|
|
|
{
|
2018-03-14 07:33:37 +01:00
|
|
|
qi::parse(start, end(), rule, ret)
|
2018-03-14 00:22:47 +01:00
|
|
|
};
|
|
|
|
|
2018-04-04 22:03:13 +02:00
|
|
|
assert(res || ret == 0);
|
2018-03-14 00:22:47 +01:00
|
|
|
return ret;
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
2017-12-12 21:13:06 +01:00
|
|
|
ircd::m::id::hostname()
|
|
|
|
const
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-03-14 00:22:47 +01:00
|
|
|
static const parser::rule<string_view> dns_name
|
|
|
|
{
|
|
|
|
parser.dns_name
|
|
|
|
};
|
|
|
|
|
|
|
|
static const parser::rule<string_view> rule
|
|
|
|
{
|
|
|
|
omit[parser.prefix >> ':'] >> raw[dns_name]
|
|
|
|
};
|
|
|
|
|
|
|
|
string_view ret;
|
2018-03-14 07:33:37 +01:00
|
|
|
auto *start{begin()};
|
2018-03-14 00:22:47 +01:00
|
|
|
const auto res
|
|
|
|
{
|
2018-03-14 07:33:37 +01:00
|
|
|
qi::parse(start, end(), rule, ret)
|
2018-03-14 00:22:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
assert(res == true);
|
|
|
|
assert(!ret.empty());
|
|
|
|
return ret;
|
2017-12-12 21:13:06 +01:00
|
|
|
}
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2017-12-12 21:13:06 +01:00
|
|
|
ircd::string_view
|
2018-03-14 00:22:47 +01:00
|
|
|
ircd::m::id::localname()
|
2017-12-12 21:13:06 +01:00
|
|
|
const
|
|
|
|
{
|
2018-03-14 00:22:47 +01:00
|
|
|
auto ret{local()};
|
|
|
|
assert(!ret.empty());
|
|
|
|
ret.pop_front();
|
|
|
|
return ret;
|
2017-12-12 21:13:06 +01:00
|
|
|
}
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2017-12-12 21:13:06 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::m::id::host()
|
|
|
|
const
|
|
|
|
{
|
2018-03-14 00:22:47 +01:00
|
|
|
static const parser::rule<string_view> server_name
|
|
|
|
{
|
|
|
|
parser.server_name
|
|
|
|
};
|
|
|
|
|
|
|
|
static const parser::rule<string_view> rule
|
|
|
|
{
|
|
|
|
omit[parser.prefix >> ':'] >> raw[server_name]
|
|
|
|
};
|
|
|
|
|
|
|
|
string_view ret;
|
2018-03-14 07:33:37 +01:00
|
|
|
auto *start{begin()};
|
2018-03-14 00:22:47 +01:00
|
|
|
const auto res
|
|
|
|
{
|
2018-03-14 07:33:37 +01:00
|
|
|
qi::parse(start, end(), rule, ret)
|
2018-03-14 00:22:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
assert(res == true);
|
|
|
|
assert(!ret.empty());
|
|
|
|
return ret;
|
2017-12-12 21:13:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::id::local()
|
|
|
|
const
|
|
|
|
{
|
2018-03-14 00:22:47 +01:00
|
|
|
static const parser::rule<string_view> prefix
|
|
|
|
{
|
|
|
|
parser.prefix
|
|
|
|
};
|
|
|
|
|
|
|
|
static const parser::rule<string_view> rule
|
|
|
|
{
|
|
|
|
eps > raw[prefix]
|
|
|
|
};
|
|
|
|
|
|
|
|
string_view ret;
|
2018-03-14 07:33:37 +01:00
|
|
|
auto *start{begin()};
|
|
|
|
qi::parse(start, end(), rule, ret);
|
2018-03-14 00:22:47 +01:00
|
|
|
assert(!ret.empty());
|
|
|
|
return ret;
|
2017-12-12 21:13:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::my(const id &id)
|
|
|
|
{
|
|
|
|
return my_host(id.host());
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::validate(const id::sigil &sigil,
|
|
|
|
const string_view &id)
|
|
|
|
{
|
2017-12-12 21:13:06 +01:00
|
|
|
id::validator(sigil, id);
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::valid(const id::sigil &sigil,
|
|
|
|
const string_view &id)
|
|
|
|
noexcept try
|
|
|
|
{
|
2018-03-13 23:40:22 +01:00
|
|
|
validate(sigil, id);
|
2017-11-16 02:37:09 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-03 11:34:42 +01:00
|
|
|
bool
|
|
|
|
ircd::m::valid_local_only(const id::sigil &sigil,
|
|
|
|
const string_view &id)
|
|
|
|
noexcept try
|
|
|
|
{
|
2018-04-28 06:26:33 +02:00
|
|
|
static const auto &test
|
2018-03-03 11:34:42 +01:00
|
|
|
{
|
2018-03-03 13:05:43 +01:00
|
|
|
m::id::parser.prefix
|
2018-03-03 11:34:42 +01:00
|
|
|
};
|
|
|
|
|
2018-04-28 06:26:33 +02:00
|
|
|
const char *start{data(id)};
|
2018-04-25 04:18:50 +02:00
|
|
|
const char *const &stop
|
|
|
|
{
|
2018-04-28 06:26:33 +02:00
|
|
|
start + std::min(size(id), id::MAX_SIZE)
|
2018-04-25 04:18:50 +02:00
|
|
|
};
|
|
|
|
|
2018-03-03 13:05:43 +01:00
|
|
|
return id.at(0) == sigil && qi::parse(start, stop, test) && start == stop;
|
2018-03-03 11:34:42 +01:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-16 02:37:09 +01:00
|
|
|
bool
|
|
|
|
ircd::m::valid_local(const id::sigil &sigil,
|
|
|
|
const string_view &id)
|
2018-03-03 13:05:43 +01:00
|
|
|
noexcept try
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-04-28 06:26:33 +02:00
|
|
|
static const auto &test
|
2017-12-12 21:13:06 +01:00
|
|
|
{
|
2018-03-03 13:05:43 +01:00
|
|
|
m::id::parser.prefix
|
2017-12-12 21:13:06 +01:00
|
|
|
};
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2018-04-28 06:26:33 +02:00
|
|
|
const char *start{data(id)};
|
2018-04-25 04:18:50 +02:00
|
|
|
const char *const &stop
|
|
|
|
{
|
2018-04-28 06:26:33 +02:00
|
|
|
start + std::min(size(id), id::MAX_SIZE)
|
2018-04-25 04:18:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return id.at(0) == sigil && qi::parse(start, stop, test);
|
2018-03-03 13:05:43 +01:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return false;
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-02-17 01:27:33 +01:00
|
|
|
ircd::m::has_sigil(const string_view &s)
|
2018-03-03 13:05:43 +01:00
|
|
|
noexcept try
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-02-17 01:27:33 +01:00
|
|
|
return is_sigil(s.at(0));
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
2018-03-03 13:05:43 +01:00
|
|
|
catch(...)
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-02-17 01:27:33 +01:00
|
|
|
ircd::m::is_sigil(const char &c)
|
2018-03-03 13:05:43 +01:00
|
|
|
noexcept
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2017-12-12 21:13:06 +01:00
|
|
|
const char *start{&c};
|
|
|
|
const char *const stop{start + 1};
|
|
|
|
return qi::parse(start, stop, id::parser.sigil);
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
enum ircd::m::id::sigil
|
|
|
|
ircd::m::sigil(const string_view &s)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return sigil(s.at(0));
|
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
2017-12-12 21:13:06 +01:00
|
|
|
throw BAD_SIGIL("no sigil provided");
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
enum ircd::m::id::sigil
|
|
|
|
ircd::m::sigil(const char &c)
|
|
|
|
{
|
2017-12-12 21:13:06 +01:00
|
|
|
id::sigil ret;
|
|
|
|
const char *start{&c};
|
2018-02-20 05:29:38 +01:00
|
|
|
const char *const stop{&c + 1};
|
2017-12-12 21:13:06 +01:00
|
|
|
if(!qi::parse(start, stop, id::parser.sigil, ret))
|
|
|
|
throw BAD_SIGIL("'%c' is not a valid sigil", c);
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2018-02-20 05:29:38 +01:00
|
|
|
assert(start == stop);
|
2017-12-12 21:13:06 +01:00
|
|
|
return ret;
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
|
2018-02-17 01:27:33 +01:00
|
|
|
ircd::string_view
|
2018-02-20 05:29:38 +01:00
|
|
|
ircd::m::reflect(const id::sigil &c)
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
|
|
|
switch(c)
|
|
|
|
{
|
2018-02-17 01:27:33 +01:00
|
|
|
case id::EVENT: return "EVENT"_sv;
|
|
|
|
case id::USER: return "USER"_sv;
|
|
|
|
case id::ROOM: return "ROOM"_sv;
|
|
|
|
case id::ROOM_ALIAS: return "ROOM_ALIAS"_sv;
|
|
|
|
case id::GROUP: return "GROUP"_sv;
|
2018-03-05 17:21:12 +01:00
|
|
|
case id::NODE: return "NODE"_sv;
|
2018-02-17 01:27:33 +01:00
|
|
|
case id::DEVICE: return "DEVICE"_sv;
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
|
2018-02-17 01:27:33 +01:00
|
|
|
return "?????"_sv;
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
2017-12-12 21:13:06 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::failure(const qi::expectation_failure<const char *> &e,
|
|
|
|
const string_view &goal)
|
|
|
|
{
|
2018-03-13 23:40:22 +01:00
|
|
|
const auto rule
|
2017-12-12 21:13:06 +01:00
|
|
|
{
|
|
|
|
ircd::string(e.what_)
|
|
|
|
};
|
|
|
|
|
|
|
|
throw INVALID_MXID
|
|
|
|
{
|
|
|
|
"Not a valid %s because of an invalid %s.", goal, between(rule, '<', '>')
|
|
|
|
};
|
|
|
|
}
|