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
|
|
|
|
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
|
|
|
|
2022-06-24 03:12:21 +02:00
|
|
|
struct [[gnu::visibility("hidden")]]
|
|
|
|
ircd::m::id::parser
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-10-01 02:16:06 +02:00
|
|
|
using id = m::id;
|
2022-06-24 03:12:21 +02:00
|
|
|
|
|
|
|
template<class R = unused_type,
|
|
|
|
class... S>
|
|
|
|
struct [[gnu::visibility("hidden")]] rule
|
|
|
|
:qi::rule<const char *, R, S...>
|
|
|
|
{
|
|
|
|
using qi::rule<const char *, R, S...>::rule;
|
|
|
|
};
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2017-12-12 21:13:06 +01:00
|
|
|
// Sigils
|
2018-10-01 02:16:06 +02:00
|
|
|
const rule<id::sigil> event_id_sigil { lit(char(id::EVENT)) ,"event_id sigil" };
|
|
|
|
const rule<id::sigil> user_id_sigil { lit(char(id::USER)) ,"user_id sigil" };
|
|
|
|
const rule<id::sigil> room_id_sigil { lit(char(id::ROOM)) ,"room_id sigil" };
|
|
|
|
const rule<id::sigil> room_alias_sigil { lit(char(id::ROOM_ALIAS)) ,"room_alias sigil" };
|
|
|
|
const rule<id::sigil> group_id_sigil { lit(char(id::GROUP)) ,"group_id sigil" };
|
|
|
|
const rule<id::sigil> device_sigil { lit(char(id::DEVICE)) ,"device sigil" };
|
2019-08-12 07:51:26 +02:00
|
|
|
const rule<id::sigil> node_sigil { lit(char(id::NODE)) ,"node sigil" };
|
2018-10-01 02:16:06 +02:00
|
|
|
const rule<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 |
|
2019-08-12 07:51:26 +02:00
|
|
|
device_sigil |
|
|
|
|
node_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
|
2020-10-21 07:33:18 +02:00
|
|
|
const rule<> non_historical_user_id_char
|
2017-12-12 21:13:06 +01:00
|
|
|
{
|
2019-06-08 14:20:23 +02:00
|
|
|
char_('\x21', '\x39') | char_('\x3B', '\x7E')
|
2017-12-12 21:13:06 +01:00
|
|
|
,"user_id character"
|
|
|
|
};
|
|
|
|
|
2020-10-21 07:33:18 +02:00
|
|
|
// character of a non-historical'ish user_id localpart
|
|
|
|
const rule<> user_id_char
|
|
|
|
{
|
|
|
|
char_('\x21', '\x39') | char_('\x3B', '\x7E') | char_('\x80', '\xFF')
|
|
|
|
,"user_id character"
|
|
|
|
};
|
|
|
|
|
2017-12-12 21:13:06 +01:00
|
|
|
// 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"
|
|
|
|
};
|
|
|
|
|
2019-06-26 12:35:10 +02:00
|
|
|
// character of a v3 event_id
|
|
|
|
const rule<> event_id_v3_char
|
|
|
|
{
|
|
|
|
char_("A-Za-z0-9+/") // base64 alphabet
|
|
|
|
,"event_id version 3 character"
|
|
|
|
};
|
|
|
|
|
|
|
|
// fully qualified v3 event_id
|
|
|
|
const rule<> event_id_v3
|
|
|
|
{
|
|
|
|
event_id_sigil >> repeat(43)[event_id_v3_char]
|
|
|
|
,"event_id version 3"
|
|
|
|
};
|
|
|
|
|
|
|
|
// character of a v4 event_id
|
|
|
|
const rule<> event_id_v4_char
|
|
|
|
{
|
|
|
|
char_("A-Za-z0-9_-") // base64 url-safe alphabet
|
|
|
|
,"event_id version 4 character"
|
|
|
|
};
|
|
|
|
|
|
|
|
// fully qualified v4 event_id
|
|
|
|
const rule<> event_id_v4
|
|
|
|
{
|
|
|
|
event_id_sigil >> repeat(43)[event_id_v4_char]
|
|
|
|
,"event_id version 4"
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
{
|
2019-02-26 22:07:52 +01:00
|
|
|
rfc3986::parser::remote
|
2017-11-16 02:37:09 +01:00
|
|
|
,"server name"
|
|
|
|
};
|
|
|
|
|
|
|
|
const rule<> mxid
|
|
|
|
{
|
2019-06-26 12:35:10 +02:00
|
|
|
(prefix >> ':' >> server_name)
|
|
|
|
| event_id_v4
|
|
|
|
| event_id_v3
|
2017-11-16 02:37:09 +01:00
|
|
|
,"mxid"
|
|
|
|
};
|
|
|
|
|
|
|
|
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)]
|
2022-06-24 03:12:21 +02:00
|
|
|
,"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)
|
|
|
|
};
|
|
|
|
|
2022-06-24 00:23:39 +02:00
|
|
|
const bool res
|
|
|
|
{
|
|
|
|
ircd::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]
|
2022-06-24 03:12:21 +02:00
|
|
|
,"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)
|
|
|
|
};
|
|
|
|
|
2022-06-24 00:23:39 +02:00
|
|
|
const bool res
|
|
|
|
{
|
|
|
|
ircd::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
|
|
|
|
2019-07-09 10:59:00 +02:00
|
|
|
//
|
|
|
|
// valid
|
|
|
|
//
|
|
|
|
|
|
|
|
decltype(ircd::m::id::valid)
|
|
|
|
ircd::m::id::valid
|
|
|
|
{};
|
2017-11-16 02:37:09 +01:00
|
|
|
|
|
|
|
void
|
2019-07-09 10:59:00 +02:00
|
|
|
ircd::m::id::valid::operator()(const string_view &id)
|
2017-11-16 02:37:09 +01:00
|
|
|
const try
|
|
|
|
{
|
2022-06-29 00:33:21 +02:00
|
|
|
static const parser::rule<> rule
|
|
|
|
{
|
|
|
|
eps > parser.mxid > eoi
|
|
|
|
,"valid mxid"
|
|
|
|
};
|
|
|
|
|
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)
|
|
|
|
};
|
|
|
|
|
2022-06-24 00:23:39 +02:00
|
|
|
const bool ret
|
|
|
|
{
|
2022-06-29 00:33:21 +02:00
|
|
|
ircd::parse(start, stop, rule)
|
2022-06-24 00:23:39 +02:00
|
|
|
};
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-09 10:59:00 +02:00
|
|
|
bool
|
|
|
|
ircd::m::id::valid::operator()(std::nothrow_t,
|
|
|
|
const string_view &id)
|
|
|
|
const noexcept
|
|
|
|
{
|
2022-06-29 00:33:21 +02:00
|
|
|
static const parser::rule<> rule
|
|
|
|
{
|
|
|
|
parser.mxid >> eoi
|
|
|
|
,"valid mxid"
|
|
|
|
};
|
|
|
|
|
2019-07-09 10:59:00 +02:00
|
|
|
const char *start{id.begin()};
|
|
|
|
const char *const stop
|
|
|
|
{
|
|
|
|
std::min(id.end(), start + MAX_SIZE)
|
|
|
|
};
|
|
|
|
|
2022-06-29 00:33:21 +02:00
|
|
|
return ircd::parse(std::nothrow, start, stop, rule);
|
2019-07-09 10:59:00 +02:00
|
|
|
}
|
|
|
|
|
2017-11-16 02:37:09 +01:00
|
|
|
void
|
2019-07-09 10:59:00 +02:00
|
|
|
ircd::m::id::valid::operator()(const id::sigil &sigil,
|
|
|
|
const string_view &id)
|
2017-11-16 02:37:09 +01:00
|
|
|
const try
|
|
|
|
{
|
2019-07-09 10:59:00 +02:00
|
|
|
const parser::rule<> sigil_type
|
2018-03-16 22:25:36 +01:00
|
|
|
{
|
|
|
|
&lit(char(sigil))
|
|
|
|
,"sigil type"
|
|
|
|
};
|
|
|
|
|
2019-07-09 10:59:00 +02:00
|
|
|
const parser::rule<> valid_mxid
|
2017-12-12 21:13:06 +01:00
|
|
|
{
|
2022-06-29 00:33:21 +02:00
|
|
|
eps > sigil_type > parser.mxid > eoi
|
2022-06-24 03:12:21 +02:00
|
|
|
,"valid 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)
|
|
|
|
};
|
|
|
|
|
2022-06-24 00:23:39 +02:00
|
|
|
const bool ret
|
|
|
|
{
|
|
|
|
ircd::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));
|
|
|
|
}
|
|
|
|
|
2019-07-09 10:59:00 +02:00
|
|
|
bool
|
|
|
|
ircd::m::id::valid::operator()(std::nothrow_t,
|
|
|
|
const id::sigil &sigil,
|
|
|
|
const string_view &id)
|
2022-06-29 00:42:19 +02:00
|
|
|
const noexcept
|
2019-07-09 10:59:00 +02:00
|
|
|
{
|
|
|
|
const parser::rule<> sigil_type
|
|
|
|
{
|
|
|
|
&lit(char(sigil))
|
|
|
|
,"sigil type"
|
|
|
|
};
|
|
|
|
|
|
|
|
const parser::rule<> valid_mxid
|
|
|
|
{
|
2022-06-29 00:42:19 +02:00
|
|
|
sigil_type >> parser.mxid >> eoi
|
2022-06-24 03:12:21 +02:00
|
|
|
,"valid mxid"
|
2019-07-09 10:59:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *start{id.begin()};
|
|
|
|
const char *const stop
|
|
|
|
{
|
|
|
|
std::min(id.end(), start + MAX_SIZE)
|
|
|
|
};
|
|
|
|
|
2022-06-29 00:42:19 +02:00
|
|
|
return ircd::parse(std::nothrow, start, stop, valid_mxid);
|
2019-07-09 10:59:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// printer
|
|
|
|
//
|
|
|
|
|
2017-12-12 21:13:06 +01:00
|
|
|
//TODO: abstract this pattern with ircd::json::printer in ircd/spirit.h
|
2022-06-24 03:12:21 +02:00
|
|
|
struct [[gnu::visibility("hidden")]]
|
|
|
|
ircd::m::id::printer
|
2017-12-12 21:13:06 +01:00
|
|
|
{
|
2022-06-24 03:12:21 +02:00
|
|
|
template<class R = unused_type,
|
|
|
|
class... S>
|
|
|
|
struct [[gnu::visibility("hidden")]] rule
|
|
|
|
:karma::rule<char *, R, S...>
|
|
|
|
{
|
|
|
|
using karma::rule<char *, R, S...>::rule;
|
|
|
|
};
|
|
|
|
|
2017-12-12 21:13:06 +01:00
|
|
|
template<class generator,
|
|
|
|
class attribute>
|
2018-10-01 02:16:06 +02:00
|
|
|
bool operator()(char *&out, char *const &stop, generator&&, attribute&&) const;
|
|
|
|
|
|
|
|
template<class generator>
|
|
|
|
bool operator()(char *&out, char *const &stop, generator&&) const;
|
|
|
|
|
|
|
|
template<class... args>
|
|
|
|
bool operator()(mutable_buffer &out, args&&... a) const;
|
|
|
|
}
|
|
|
|
const ircd::m::id::printer;
|
|
|
|
|
|
|
|
template<class gen,
|
|
|
|
class attr>
|
|
|
|
bool
|
|
|
|
ircd::m::id::printer::operator()(char *&out,
|
|
|
|
char *const &stop,
|
|
|
|
gen&& g,
|
|
|
|
attr&& a)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto throws{[&out, &stop]
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2018-10-01 02:16:06 +02:00
|
|
|
throw INVALID_MXID
|
2017-12-12 21:13:06 +01:00
|
|
|
{
|
2018-10-01 02:16:06 +02:00
|
|
|
"Failed to print attribute '%s' generator '%s' (%zd bytes in buffer)",
|
|
|
|
demangle<decltype(a)>(),
|
|
|
|
demangle<decltype(g)>(),
|
|
|
|
size_t(stop - out)
|
2017-12-12 21:13:06 +01:00
|
|
|
};
|
2018-10-01 02:16:06 +02:00
|
|
|
}};
|
2017-12-12 21:13:06 +01:00
|
|
|
|
2020-05-26 12:30:38 +02:00
|
|
|
mutable_buffer buf
|
|
|
|
{
|
|
|
|
out, stop
|
|
|
|
};
|
|
|
|
|
2018-10-01 02:16:06 +02:00
|
|
|
const auto gg
|
|
|
|
{
|
2020-05-26 12:30:38 +02:00
|
|
|
std::forward<decltype(g)>(g) | eps[throws]
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto ret
|
|
|
|
{
|
|
|
|
generate(buf, gg, std::forward<decltype(a)>(a))
|
2018-10-01 02:16:06 +02:00
|
|
|
};
|
2017-12-12 21:13:06 +01:00
|
|
|
|
2020-05-26 12:30:38 +02:00
|
|
|
out = buffer::data(buf);
|
|
|
|
return ret;
|
2018-10-01 02:16:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class gen>
|
|
|
|
bool
|
|
|
|
ircd::m::id::printer::operator()(char *&out,
|
|
|
|
char *const &stop,
|
|
|
|
gen&& g)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto throws{[&out, &stop]
|
2017-12-12 21:13:06 +01:00
|
|
|
{
|
2018-10-01 02:16:06 +02:00
|
|
|
throw INVALID_MXID
|
2017-12-12 21:13:06 +01:00
|
|
|
{
|
2018-10-01 02:16:06 +02:00
|
|
|
"Failed to print generator '%s' (%zd bytes in buffer)",
|
|
|
|
demangle<decltype(g)>(),
|
|
|
|
size_t(stop - out)
|
2017-12-12 21:13:06 +01:00
|
|
|
};
|
2018-10-01 02:16:06 +02:00
|
|
|
}};
|
2017-12-12 21:13:06 +01:00
|
|
|
|
2020-05-26 12:30:38 +02:00
|
|
|
mutable_buffer buf
|
|
|
|
{
|
|
|
|
out, stop
|
|
|
|
};
|
|
|
|
|
2018-10-01 02:16:06 +02:00
|
|
|
const auto gg
|
2017-12-12 21:13:06 +01:00
|
|
|
{
|
2020-05-26 12:30:38 +02:00
|
|
|
std::forward<decltype(g)>(g) | eps[throws]
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto ret
|
|
|
|
{
|
|
|
|
generate(buf, gg)
|
2018-10-01 02:16:06 +02:00
|
|
|
};
|
|
|
|
|
2020-05-26 12:30:38 +02:00
|
|
|
out = buffer::data(buf);
|
|
|
|
return ret;
|
2018-10-01 02:16:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class... args>
|
|
|
|
bool
|
|
|
|
ircd::m::id::printer::operator()(mutable_buffer &out,
|
|
|
|
args&&... a)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return operator()(buffer::begin(out), buffer::end(out), std::forward<args>(a)...);
|
2017-12-12 21:13:06 +01:00
|
|
|
}
|
|
|
|
|
2017-11-16 02:37:09 +01:00
|
|
|
//
|
2018-10-01 02:16:06 +02:00
|
|
|
// id::id
|
2017-11-16 02:37:09 +01:00
|
|
|
//
|
|
|
|
|
2020-12-03 10:18:17 +01:00
|
|
|
ircd::m::id::id(const string_view &str)
|
|
|
|
:id
|
|
|
|
{
|
|
|
|
m::sigil(str), str
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::id::id(const id::sigil &sigil,
|
|
|
|
const string_view &id)
|
|
|
|
:string_view
|
|
|
|
{
|
|
|
|
parser(sigil, id)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-06-29 01:27:33 +02:00
|
|
|
namespace ircd::m
|
|
|
|
{
|
|
|
|
static thread_local char
|
|
|
|
tmp_buf alignas(64) [2][id::MAX_SIZE];
|
|
|
|
}
|
|
|
|
|
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)
|
2022-06-29 01:27:33 +02:00
|
|
|
:string_view
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2022-06-29 01:27:33 +02:00
|
|
|
parser(sigil, string_view
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2019-02-05 10:30:55 +01:00
|
|
|
startswith(local, sigil)?
|
|
|
|
fmt::sprintf
|
|
|
|
{
|
2019-07-10 11:00:50 +02:00
|
|
|
buf, "%s%s%s",
|
2019-08-06 07:12:51 +02:00
|
|
|
sigil == sigil::ROOM_ALIAS?
|
2022-06-29 01:27:33 +02:00
|
|
|
tolower(tmp_buf[0], local):
|
2019-08-06 07:12:51 +02:00
|
|
|
local,
|
|
|
|
host?
|
|
|
|
":"_sv:
|
|
|
|
string_view{},
|
2022-06-29 01:27:33 +02:00
|
|
|
tolower(tmp_buf[1], host),
|
|
|
|
}:
|
2019-02-05 10:30:55 +01:00
|
|
|
fmt::sprintf
|
|
|
|
{
|
2019-07-10 11:00:50 +02:00
|
|
|
buf, "%c%s%s%s",
|
|
|
|
char(sigil),
|
2019-08-06 07:12:51 +02:00
|
|
|
sigil == sigil::ROOM_ALIAS?
|
2022-06-29 01:27:33 +02:00
|
|
|
tolower(tmp_buf[0], local):
|
2019-08-06 07:12:51 +02:00
|
|
|
local,
|
|
|
|
host?
|
|
|
|
":"_sv:
|
|
|
|
string_view{},
|
2022-06-29 01:27:33 +02:00
|
|
|
tolower(tmp_buf[1], host),
|
2019-02-05 10:30:55 +01:00
|
|
|
}
|
2022-06-29 01:27:33 +02:00
|
|
|
})
|
|
|
|
}
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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)
|
2022-06-29 01:27:33 +02:00
|
|
|
:string_view
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2022-06-29 01:27:33 +02:00
|
|
|
parser(sigil, string_view
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2019-08-06 07:12:51 +02:00
|
|
|
sigil == sigil::ROOM_ALIAS?
|
|
|
|
tolower(buf, id): //XXX no null here
|
|
|
|
buffer::data(buf) != id.data()?
|
|
|
|
strlcpy(buf, id):
|
|
|
|
id
|
2022-06-29 01:27:33 +02:00
|
|
|
})
|
|
|
|
}
|
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
|
|
|
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
|
|
|
|
{
|
2022-06-29 01:27:33 +02:00
|
|
|
tmp_buf[0], "guest%lu",
|
|
|
|
rand::integer()
|
2018-02-22 06:55:45 +01:00
|
|
|
};
|
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
|
|
|
|
{
|
2022-06-29 01:27:33 +02:00
|
|
|
tmp_buf[0], "%lu",
|
|
|
|
rand::integer()
|
2018-02-22 06:55:45 +01:00
|
|
|
};
|
2017-11-16 02:37:09 +01:00
|
|
|
break;
|
|
|
|
|
2018-02-22 06:55:45 +01:00
|
|
|
case sigil::ROOM:
|
|
|
|
{
|
2019-07-26 01:18:41 +02:00
|
|
|
static const auto &dict{rand::dict::alnum};
|
2022-06-29 01:27:33 +02:00
|
|
|
const mutable_buffer dst{tmp_buf[0], 18};
|
2020-10-07 00:46:36 +02:00
|
|
|
name = rand::string(dst, dict);
|
2018-02-22 06:55:45 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-02-15 22:01:07 +01:00
|
|
|
case sigil::DEVICE:
|
2018-02-22 06:55:45 +01:00
|
|
|
{
|
2019-07-26 01:18:41 +02:00
|
|
|
static const auto &dict{rand::dict::alnum};
|
2022-06-29 01:27:33 +02:00
|
|
|
const mutable_buffer dst{tmp_buf[0], 10};
|
2020-10-07 00:46:36 +02:00
|
|
|
name = rand::string(dst, dict);
|
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
|
|
|
|
{
|
2022-06-29 01:27:33 +02:00
|
|
|
tmp_buf[0], "%c%lu",
|
|
|
|
rand::character(),
|
|
|
|
rand::integer()
|
2018-02-22 06:55:45 +01:00
|
|
|
};
|
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
|
|
|
{
|
2022-06-29 01:27:33 +02:00
|
|
|
buf, "%c%s:%s",
|
|
|
|
char(sigil),
|
|
|
|
name,
|
|
|
|
host
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
}()}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-31 00:35:05 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::m::id::swap(const mutable_buffer &buf)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return swap(*this, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::id::swap(const id &id,
|
|
|
|
const mutable_buffer &buf_)
|
|
|
|
{
|
|
|
|
using buffer::consume;
|
|
|
|
using buffer::copy;
|
|
|
|
using buffer::data;
|
|
|
|
|
|
|
|
mutable_buffer buf(buf_);
|
|
|
|
consume(buf, copy(buf, id.host()));
|
|
|
|
consume(buf, copy(buf, id.local()));
|
|
|
|
return { data(buf_), data(buf) };
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::id
|
|
|
|
ircd::m::id::unswap(const string_view &str,
|
|
|
|
const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
size_t i(0);
|
|
|
|
for(; i < str.size(); ++i)
|
|
|
|
if(is_sigil(str[i]))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(unlikely(!i || i >= str.size()))
|
|
|
|
throw m::INVALID_MXID
|
|
|
|
{
|
|
|
|
"Failed to reconstruct any MXID out of '%s'", str
|
|
|
|
};
|
|
|
|
|
|
|
|
return id
|
|
|
|
{
|
|
|
|
sigil(str[i]), buf, str.substr(i), str.substr(0, i)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-26 22:24:46 +01:00
|
|
|
bool
|
|
|
|
ircd::m::id::literal()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
static const parser::rule<string_view> rule
|
|
|
|
{
|
|
|
|
rfc3986::parser::ip4_literal |
|
|
|
|
rfc3986::parser::ip6_literal
|
2022-06-24 03:12:21 +02:00
|
|
|
,"literal"
|
2019-02-26 22:24:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto &hostname
|
|
|
|
{
|
|
|
|
this->hostname()
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *start(hostname.begin()), *const stop(hostname.end());
|
2022-06-24 00:23:39 +02:00
|
|
|
return ircd::parse(start, stop, rule);
|
2019-02-26 22:24:46 +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
|
|
|
{
|
2019-02-26 22:07:52 +01:00
|
|
|
static const auto &host{rfc3986::parser::host};
|
|
|
|
static const auto &port{rfc3986::parser::port};
|
2018-03-14 00:22:47 +01:00
|
|
|
static const parser::rule<uint16_t> rule
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2019-02-26 22:07:52 +01:00
|
|
|
omit[parser.prefix >> ':' >> host >> ':'] >> port
|
2017-11-16 02:37:09 +01:00
|
|
|
};
|
|
|
|
|
2018-04-04 22:03:13 +02:00
|
|
|
uint16_t ret{0};
|
2022-07-05 02:28:19 +02:00
|
|
|
const char *start(begin()), *const stop(end());
|
2018-03-14 00:22:47 +01:00
|
|
|
const auto res
|
|
|
|
{
|
2022-07-05 02:28:19 +02:00
|
|
|
ircd::parse(start, stop, 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
|
|
|
{
|
2019-02-26 22:07:52 +01:00
|
|
|
static const parser::rule<string_view> host
|
2018-03-14 00:22:47 +01:00
|
|
|
{
|
2019-02-26 22:07:52 +01:00
|
|
|
rfc3986::parser::host
|
2022-06-24 03:12:21 +02:00
|
|
|
,"rfc3986 host"
|
2018-03-14 00:22:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static const parser::rule<string_view> rule
|
|
|
|
{
|
2019-02-26 22:07:52 +01:00
|
|
|
omit[parser.prefix >> ':'] >> raw[host]
|
2022-06-24 03:12:21 +02:00
|
|
|
,"host"
|
2018-03-14 00:22:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
string_view ret;
|
2022-07-05 02:28:19 +02:00
|
|
|
const char *start(begin()), *const stop(end());
|
2018-03-14 00:22:47 +01:00
|
|
|
const auto res
|
|
|
|
{
|
2022-07-05 02:28:19 +02:00
|
|
|
ircd::parse(start, stop, rule, ret)
|
2018-03-14 00:22:47 +01:00
|
|
|
};
|
|
|
|
|
2019-06-30 10:37:12 +02:00
|
|
|
assert(res || event::v4::is(*this) || event::v3::is(*this));
|
|
|
|
assert(!res || !ret.empty());
|
2018-03-14 00:22:47 +01:00
|
|
|
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
|
2022-06-24 03:12:21 +02:00
|
|
|
,"server name"
|
2018-03-14 00:22:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static const parser::rule<string_view> rule
|
|
|
|
{
|
|
|
|
omit[parser.prefix >> ':'] >> raw[server_name]
|
2022-06-24 03:12:21 +02:00
|
|
|
,"host"
|
2018-03-14 00:22:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
string_view ret;
|
2022-07-05 02:28:19 +02:00
|
|
|
const char *start(begin()), *const stop(end());
|
2018-03-14 00:22:47 +01:00
|
|
|
const auto res
|
|
|
|
{
|
2022-07-05 02:28:19 +02:00
|
|
|
ircd::parse(start, stop, rule, ret)
|
2018-03-14 00:22:47 +01:00
|
|
|
};
|
|
|
|
|
2019-06-30 10:37:12 +02:00
|
|
|
assert(res || event::v4::is(*this) || event::v3::is(*this));
|
|
|
|
assert(!res || !ret.empty());
|
2018-03-14 00:22:47 +01:00
|
|
|
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
|
2022-06-24 03:12:21 +02:00
|
|
|
,"prefix"
|
2018-03-14 00:22:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static const parser::rule<string_view> rule
|
|
|
|
{
|
|
|
|
eps > raw[prefix]
|
2022-06-24 03:12:21 +02:00
|
|
|
,"local"
|
2018-03-14 00:22:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
string_view ret;
|
2022-07-05 02:28:19 +02:00
|
|
|
const char *start(begin()), *const stop(end());
|
|
|
|
ircd::parse(start, stop, rule, ret);
|
2018-03-14 00:22:47 +01:00
|
|
|
assert(!ret.empty());
|
|
|
|
return ret;
|
2017-12-12 21:13:06 +01:00
|
|
|
}
|
|
|
|
|
2019-06-26 12:35:34 +02:00
|
|
|
//
|
|
|
|
// id::event
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::id::event::version()
|
|
|
|
const
|
|
|
|
{
|
2019-06-30 10:31:07 +02:00
|
|
|
static const parser::rule<> is_v4
|
2019-06-26 12:35:34 +02:00
|
|
|
{
|
2019-06-30 10:31:07 +02:00
|
|
|
parser.event_id_v4 >> eoi
|
2022-06-24 03:12:21 +02:00
|
|
|
,"is v4"
|
2019-06-26 12:35:34 +02:00
|
|
|
};
|
|
|
|
|
2019-06-30 10:31:07 +02:00
|
|
|
static const parser::rule<> is_v3
|
2019-06-26 12:35:34 +02:00
|
|
|
{
|
2019-06-30 10:31:07 +02:00
|
|
|
parser.event_id_v3 >> eoi
|
2022-06-24 03:12:21 +02:00
|
|
|
,"is v3"
|
2019-06-26 12:35:34 +02:00
|
|
|
};
|
|
|
|
|
2022-07-05 02:28:19 +02:00
|
|
|
const char
|
|
|
|
*start[2] {begin(), begin()},
|
|
|
|
*const stop{end()};
|
2019-06-26 12:35:34 +02:00
|
|
|
return
|
2022-07-05 02:28:19 +02:00
|
|
|
ircd::parse(start[0], stop, is_v4)? "4":
|
|
|
|
ircd::parse(start[1], stop, is_v3)? "3":
|
|
|
|
"1";
|
2019-06-26 12:35:34 +02:00
|
|
|
}
|
|
|
|
|
2019-06-26 13:15:56 +02:00
|
|
|
//
|
|
|
|
// id::event::v3
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::id::event::v3::v3(const string_view &id)
|
|
|
|
:id::event{id}
|
|
|
|
{
|
2020-04-03 20:30:37 +02:00
|
|
|
if(unlikely(!is(id)))
|
2019-06-26 13:15:56 +02:00
|
|
|
throw m::INVALID_MXID
|
|
|
|
{
|
2019-08-10 17:03:41 +02:00
|
|
|
"'%s' is not a version 3 event mxid; maybe version %s?",
|
|
|
|
id,
|
|
|
|
version(),
|
2019-06-26 13:15:56 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::id::event::v3::v3(const mutable_buffer &out,
|
2019-06-28 04:46:37 +02:00
|
|
|
const m::event &event)
|
2019-06-26 13:15:56 +02:00
|
|
|
:v3{[&out, event]
|
|
|
|
{
|
|
|
|
if(unlikely(buffer::size(out) < 44))
|
|
|
|
throw std::out_of_range
|
|
|
|
{
|
|
|
|
"Output buffer insufficient for v3 event_id"
|
|
|
|
};
|
|
|
|
|
2019-06-28 04:46:37 +02:00
|
|
|
thread_local char content_buffer[m::event::MAX_SIZE];
|
|
|
|
const m::event essential
|
|
|
|
{
|
|
|
|
m::essential(event, content_buffer)
|
|
|
|
};
|
|
|
|
|
|
|
|
thread_local char preimage_buffer[m::event::MAX_SIZE];
|
|
|
|
const json::object &preimage
|
|
|
|
{
|
|
|
|
json::stringify(preimage_buffer, essential)
|
|
|
|
};
|
|
|
|
|
2019-06-26 13:15:56 +02:00
|
|
|
const sha256::buf hash
|
|
|
|
{
|
2019-06-28 04:46:37 +02:00
|
|
|
sha256{preimage}
|
2019-06-26 13:15:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
out[0] = '$';
|
2019-06-28 04:31:50 +02:00
|
|
|
const string_view hashb64
|
|
|
|
{
|
2020-08-10 12:51:09 +02:00
|
|
|
b64::encode_unpadded(out + 1, hash)
|
2019-06-28 04:31:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return string_view
|
|
|
|
{
|
|
|
|
ircd::data(out), 1 + ircd::size(hashb64)
|
|
|
|
};
|
2019-06-26 13:15:56 +02:00
|
|
|
}()}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-28 07:56:50 +02:00
|
|
|
bool
|
|
|
|
ircd::m::id::event::v3::is(const string_view &id)
|
|
|
|
noexcept
|
|
|
|
{
|
2019-06-30 10:31:07 +02:00
|
|
|
static const parser::rule<> valid
|
2019-06-28 07:56:50 +02:00
|
|
|
{
|
2019-06-30 10:31:07 +02:00
|
|
|
parser.event_id_v3 >> eoi
|
2022-06-24 03:12:21 +02:00
|
|
|
,"is v3"
|
2019-06-28 07:56:50 +02:00
|
|
|
};
|
|
|
|
|
2022-07-05 02:28:19 +02:00
|
|
|
const char *start(id.begin()), *const stop(id.end());
|
2022-06-24 00:23:39 +02:00
|
|
|
return ircd::parse(std::nothrow, start, stop, valid);
|
2019-06-28 07:56:50 +02:00
|
|
|
}
|
|
|
|
|
2019-06-26 13:15:56 +02:00
|
|
|
//
|
|
|
|
// id::event::v4
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::id::event::v4::v4(const string_view &id)
|
|
|
|
:id::event{id}
|
|
|
|
{
|
2020-04-03 20:30:37 +02:00
|
|
|
if(unlikely(!is(id)))
|
2019-06-26 13:15:56 +02:00
|
|
|
throw m::INVALID_MXID
|
|
|
|
{
|
2019-08-10 17:03:41 +02:00
|
|
|
"'%s' is not a version 4 event mxid; maybe version %s?",
|
|
|
|
id,
|
|
|
|
version(),
|
2019-06-26 13:15:56 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::id::event::v4::v4(const mutable_buffer &out,
|
2019-06-28 04:46:37 +02:00
|
|
|
const m::event &event)
|
2019-06-26 13:15:56 +02:00
|
|
|
:v4{[&out, event]
|
|
|
|
{
|
|
|
|
if(unlikely(buffer::size(out) < 44))
|
|
|
|
throw std::out_of_range
|
|
|
|
{
|
|
|
|
"Output buffer insufficient for v4 event_id"
|
|
|
|
};
|
|
|
|
|
2019-06-28 04:46:37 +02:00
|
|
|
thread_local char content_buffer[m::event::MAX_SIZE];
|
|
|
|
const m::event essential
|
|
|
|
{
|
|
|
|
m::essential(event, content_buffer)
|
|
|
|
};
|
|
|
|
|
|
|
|
thread_local char preimage_buffer[m::event::MAX_SIZE];
|
|
|
|
const json::object &preimage
|
|
|
|
{
|
|
|
|
json::stringify(preimage_buffer, essential)
|
|
|
|
};
|
|
|
|
|
2019-06-26 13:15:56 +02:00
|
|
|
const sha256::buf hash
|
|
|
|
{
|
2019-06-28 04:46:37 +02:00
|
|
|
sha256{preimage}
|
2019-06-26 13:15:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
out[0] = '$';
|
2020-08-10 14:14:43 +02:00
|
|
|
const string_view hashb64
|
|
|
|
{
|
2023-03-04 07:20:27 +01:00
|
|
|
b64::encode_unpadded(out + 1, hash, b64::urlsafe)
|
2020-08-10 14:14:43 +02:00
|
|
|
};
|
|
|
|
|
2019-06-28 04:31:50 +02:00
|
|
|
return string_view
|
|
|
|
{
|
|
|
|
ircd::data(out), 1 + ircd::size(hashb64)
|
|
|
|
};
|
2019-06-26 13:15:56 +02:00
|
|
|
}()}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-28 07:56:50 +02:00
|
|
|
bool
|
|
|
|
ircd::m::id::event::v4::is(const string_view &id)
|
|
|
|
noexcept
|
|
|
|
{
|
2019-06-30 10:31:07 +02:00
|
|
|
static const parser::rule<> valid
|
2019-06-28 07:56:50 +02:00
|
|
|
{
|
2019-06-30 10:31:07 +02:00
|
|
|
parser.event_id_v4 >> eoi
|
2022-06-24 03:12:21 +02:00
|
|
|
,"is v4"
|
2019-06-28 07:56:50 +02:00
|
|
|
};
|
|
|
|
|
2022-07-05 02:28:19 +02:00
|
|
|
const char *start(id.begin()), *const stop(id.end());
|
2022-06-24 00:23:39 +02:00
|
|
|
return ircd::parse(std::nothrow, start, stop, valid);
|
2019-06-28 07:56:50 +02:00
|
|
|
}
|
|
|
|
|
2019-06-26 12:35:34 +02:00
|
|
|
//
|
|
|
|
// util
|
|
|
|
//
|
|
|
|
|
2017-12-12 21:13:06 +01:00
|
|
|
bool
|
|
|
|
ircd::m::my(const id &id)
|
|
|
|
{
|
2019-07-14 05:01:12 +02:00
|
|
|
assert(id.host());
|
2017-12-12 21:13:06 +01:00
|
|
|
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)
|
|
|
|
{
|
2019-07-09 10:59:00 +02:00
|
|
|
id::valid(sigil, id);
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::valid(const id::sigil &sigil,
|
|
|
|
const string_view &id)
|
2019-07-09 10:59:00 +02:00
|
|
|
noexcept
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2019-07-09 10:59:00 +02:00
|
|
|
return !empty(id) && id::valid(std::nothrow, sigil, id);
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
|
2018-03-03 11:34:42 +01:00
|
|
|
bool
|
|
|
|
ircd::m::valid_local_only(const id::sigil &sigil,
|
|
|
|
const string_view &id)
|
2022-06-29 00:42:19 +02:00
|
|
|
noexcept
|
2018-03-03 11:34:42 +01:00
|
|
|
{
|
2019-06-30 10:31:07 +02:00
|
|
|
static const id::parser::rule<> rule
|
2018-03-03 11:34:42 +01:00
|
|
|
{
|
2019-06-26 12:35:10 +02:00
|
|
|
id::parser.prefix
|
2019-06-30 10:31:07 +02:00
|
|
|
| id::parser.event_id_v4
|
|
|
|
| id::parser.event_id_v3
|
2022-06-24 03:12:21 +02:00
|
|
|
,"valid local only"
|
2019-06-30 10:31:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const id::parser::rule<> test
|
|
|
|
{
|
|
|
|
rule >> eoi
|
2022-06-24 03:12:21 +02:00
|
|
|
,"valid local only"
|
2018-03-03 11:34:42 +01:00
|
|
|
};
|
|
|
|
|
2019-02-05 10:30:55 +01:00
|
|
|
const char *start(data(id)), *const &stop
|
2018-04-25 04:18:50 +02:00
|
|
|
{
|
2018-04-28 06:26:33 +02:00
|
|
|
start + std::min(size(id), id::MAX_SIZE)
|
2018-04-25 04:18:50 +02:00
|
|
|
};
|
|
|
|
|
2019-02-05 10:31:12 +01:00
|
|
|
return !empty(id) &&
|
2022-06-29 00:42:19 +02:00
|
|
|
id[0] == sigil &&
|
2022-06-24 00:23:39 +02:00
|
|
|
ircd::parse(std::nothrow, start, stop, test);
|
2018-03-03 11:34:42 +01:00
|
|
|
}
|
|
|
|
|
2017-11-16 02:37:09 +01:00
|
|
|
bool
|
|
|
|
ircd::m::valid_local(const id::sigil &sigil,
|
|
|
|
const string_view &id)
|
2022-06-29 00:42:19 +02:00
|
|
|
noexcept
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2022-06-29 00:42:19 +02:00
|
|
|
static const id::parser::rule<> rule
|
2017-12-12 21:13:06 +01:00
|
|
|
{
|
2019-06-26 12:35:10 +02:00
|
|
|
id::parser.prefix
|
2019-06-30 10:31:07 +02:00
|
|
|
| id::parser.event_id_v4
|
|
|
|
| id::parser.event_id_v3
|
2022-06-24 03:12:21 +02:00
|
|
|
,"valid local"
|
2017-12-12 21:13:06 +01:00
|
|
|
};
|
2017-11-16 02:37:09 +01:00
|
|
|
|
2022-06-29 00:42:19 +02:00
|
|
|
static const id::parser::rule<> test
|
|
|
|
{
|
|
|
|
rule >> (lit(':') | eoi)
|
|
|
|
,"valid local"
|
|
|
|
};
|
|
|
|
|
2019-02-05 10:30:55 +01:00
|
|
|
const char *start(data(id)), *const &stop
|
2018-04-25 04:18:50 +02:00
|
|
|
{
|
2018-04-28 06:26:33 +02:00
|
|
|
start + std::min(size(id), id::MAX_SIZE)
|
2018-04-25 04:18:50 +02:00
|
|
|
};
|
|
|
|
|
2019-02-05 10:31:12 +01:00
|
|
|
return !empty(id) &&
|
2022-06-29 00:42:19 +02:00
|
|
|
id[0] == sigil &&
|
|
|
|
ircd::parse(std::nothrow, start, stop, test);
|
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
|
|
|
{
|
2019-02-05 10:30:55 +01:00
|
|
|
const char *start(&c), *const stop(start + 1);
|
2022-06-24 00:23:39 +02:00
|
|
|
return ircd::parse(std::nothrow, 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)
|
|
|
|
{
|
2019-02-05 10:30:55 +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;
|
2019-02-05 10:30:55 +01:00
|
|
|
const char *start(&c), *const stop(&c + 1);
|
2022-06-29 00:42:19 +02:00
|
|
|
if(!ircd::parse(std::nothrow, start, stop, id::parser.sigil, ret))
|
2019-02-05 10:30:55 +01:00
|
|
|
throw BAD_SIGIL
|
|
|
|
{
|
2019-02-06 08:17:26 +01:00
|
|
|
"not a valid sigil"
|
2019-02-05 10:30:55 +01:00
|
|
|
};
|
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;
|
|
|
|
case id::DEVICE: return "DEVICE"_sv;
|
2019-08-12 07:51:26 +02:00
|
|
|
case id::NODE: return "NODE"_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
|
|
|
|
{
|
2019-02-05 10:30:55 +01:00
|
|
|
"Not a valid %s because of an invalid %s.",
|
|
|
|
goal,
|
|
|
|
between(rule, '<', '>')
|
2017-12-12 21:13:06 +01:00
|
|
|
};
|
|
|
|
}
|