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-12-12 21:33:14 +01:00
|
|
|
|
|
|
|
std::string
|
2018-05-04 23:22:17 +02:00
|
|
|
ircd::m::pretty(const event &event)
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
std::stringstream s;
|
2018-04-26 08:34:39 +02:00
|
|
|
pubsetbuf(s, ret, 4096);
|
2018-05-04 23:22:17 +02:00
|
|
|
pretty(s, event);
|
2017-12-12 21:33:14 +01:00
|
|
|
resizebuf(s, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-05-04 23:22:17 +02:00
|
|
|
std::ostream &
|
|
|
|
ircd::m::pretty(std::ostream &s,
|
|
|
|
const event &event)
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
|
|
|
const auto out{[&s]
|
|
|
|
(const string_view &key, auto&& val)
|
|
|
|
{
|
2018-04-12 07:57:10 +02:00
|
|
|
if(defined(json::value(val)))
|
2018-04-04 07:44:57 +02:00
|
|
|
s << std::setw(16) << std::right << key << " :" << val << std::endl;
|
2017-12-12 21:33:14 +01:00
|
|
|
}};
|
|
|
|
|
|
|
|
const string_view top_keys[]
|
|
|
|
{
|
|
|
|
"origin",
|
|
|
|
"event_id",
|
|
|
|
"room_id",
|
|
|
|
"sender",
|
|
|
|
"type",
|
|
|
|
"depth",
|
|
|
|
"state_key",
|
|
|
|
"membership",
|
2018-02-15 23:57:56 +01:00
|
|
|
"redacts",
|
2017-12-12 21:33:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
json::for_each(event, top_keys, out);
|
|
|
|
|
2018-01-21 12:19:36 +01:00
|
|
|
const auto &ts{json::get<"origin_server_ts"_>(event)};
|
|
|
|
{
|
|
|
|
thread_local char buf[128];
|
2018-04-04 07:44:57 +02:00
|
|
|
s << std::setw(16) << std::right << "origin_server_ts" << " :"
|
2018-01-21 12:19:36 +01:00
|
|
|
<< timef(buf, ts / 1000L, localtime)
|
|
|
|
<< " (" << ts << ")"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
|
2018-04-22 23:24:48 +02:00
|
|
|
const json::object &contents{json::get<"content"_>(event)};
|
|
|
|
if(!contents.empty())
|
|
|
|
s << std::setw(16) << std::right << "content" << " :"
|
|
|
|
<< size(contents) << " keys; "
|
|
|
|
<< size(string_view{contents}) << " bytes."
|
|
|
|
<< std::endl;
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
const auto &hashes{json::get<"hashes"_>(event)};
|
|
|
|
for(const auto &hash : hashes)
|
|
|
|
{
|
2018-04-15 13:16:40 +02:00
|
|
|
s << std::setw(16) << std::right << "[hash]" << " :"
|
2017-12-12 21:33:14 +01:00
|
|
|
<< hash.first
|
2018-01-24 00:03:21 +01:00
|
|
|
<< " "
|
|
|
|
<< unquote(hash.second)
|
2017-12-12 21:33:14 +01:00
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &signatures{json::get<"signatures"_>(event)};
|
|
|
|
for(const auto &signature : signatures)
|
|
|
|
{
|
2018-04-04 07:44:57 +02:00
|
|
|
s << std::setw(16) << std::right << "[signature]" << " :"
|
2017-12-12 21:33:14 +01:00
|
|
|
<< signature.first << " ";
|
|
|
|
|
|
|
|
for(const auto &key : json::object{signature.second})
|
|
|
|
s << key.first << " ";
|
|
|
|
|
|
|
|
s << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &auth_events{json::get<"auth_events"_>(event)};
|
|
|
|
for(const json::array auth_event : auth_events)
|
2018-04-04 07:47:45 +02:00
|
|
|
{
|
|
|
|
s << std::setw(16) << std::right << "[auth event]"
|
|
|
|
<< " :" << unquote(auth_event[0]);
|
|
|
|
|
|
|
|
for(const auto &hash : json::object{auth_event[1]})
|
|
|
|
s << " " << unquote(hash.first)
|
|
|
|
<< ": " << unquote(hash.second);
|
|
|
|
|
|
|
|
s << std::endl;
|
|
|
|
}
|
2017-12-12 21:33:14 +01:00
|
|
|
|
|
|
|
const auto &prev_states{json::get<"prev_state"_>(event)};
|
|
|
|
for(const json::array prev_state : prev_states)
|
2018-04-04 07:47:45 +02:00
|
|
|
{
|
|
|
|
s << std::setw(16) << std::right << "[prev state]"
|
|
|
|
<< " :" << unquote(prev_state[0]);
|
|
|
|
|
|
|
|
for(const auto &hash : json::object{prev_state[1]})
|
|
|
|
s << " " << unquote(hash.first)
|
|
|
|
<< ": " << unquote(hash.second);
|
2018-04-07 09:45:37 +02:00
|
|
|
|
|
|
|
s << std::endl;
|
2018-04-04 07:47:45 +02:00
|
|
|
}
|
2017-12-12 21:33:14 +01:00
|
|
|
|
|
|
|
const auto &prev_events{json::get<"prev_events"_>(event)};
|
|
|
|
for(const json::array prev_event : prev_events)
|
2018-04-04 07:47:45 +02:00
|
|
|
{
|
|
|
|
s << std::setw(16) << std::right << "[prev_event]"
|
|
|
|
<< " :" << unquote(prev_event[0]);
|
|
|
|
|
|
|
|
for(const auto &hash : json::object{prev_event[1]})
|
|
|
|
s << " " << unquote(hash.first)
|
|
|
|
<< ": " << unquote(hash.second);
|
|
|
|
|
|
|
|
s << std::endl;
|
|
|
|
}
|
2017-12-12 21:33:14 +01:00
|
|
|
|
2018-04-22 23:24:48 +02:00
|
|
|
if(!contents.empty())
|
|
|
|
for(const json::object::member &content : contents)
|
|
|
|
s << std::setw(16) << std::right << "[content]" << " :"
|
|
|
|
<< std::setw(7) << std::left << reflect(json::type(content.second)) << " "
|
|
|
|
<< std::setw(5) << std::right << size(string_view{content.second}) << " bytes "
|
|
|
|
<< ':' << content.first
|
|
|
|
<< std::endl;
|
|
|
|
|
2018-05-04 23:22:17 +02:00
|
|
|
return s;
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
2018-03-15 07:58:34 +01:00
|
|
|
ircd::m::pretty_oneline(const event &event,
|
|
|
|
const bool &content_keys)
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
std::stringstream s;
|
2018-05-04 23:22:17 +02:00
|
|
|
pubsetbuf(s, ret, 4096);
|
|
|
|
pretty_oneline(s, event, content_keys);
|
|
|
|
resizebuf(s, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-12-12 21:33:14 +01:00
|
|
|
|
2018-05-04 23:22:17 +02:00
|
|
|
std::ostream &
|
|
|
|
ircd::m::pretty_oneline(std::ostream &s,
|
|
|
|
const event &event,
|
|
|
|
const bool &content_keys)
|
|
|
|
{
|
2017-12-12 21:33:14 +01:00
|
|
|
const auto out{[&s]
|
|
|
|
(const string_view &key, auto&& val)
|
|
|
|
{
|
2018-04-12 07:57:10 +02:00
|
|
|
if(defined(json::value(val)))
|
2017-12-12 21:33:14 +01:00
|
|
|
s << val << " ";
|
|
|
|
else
|
|
|
|
s << "* ";
|
|
|
|
}};
|
|
|
|
|
|
|
|
const string_view top_keys[]
|
|
|
|
{
|
|
|
|
"origin",
|
|
|
|
"event_id",
|
|
|
|
"sender",
|
|
|
|
};
|
|
|
|
|
2018-04-07 09:45:37 +02:00
|
|
|
s << json::get<"room_id"_>(event) << " ";
|
2018-03-08 02:32:42 +01:00
|
|
|
s << json::get<"depth"_>(event) << " :";
|
2017-12-12 21:33:14 +01:00
|
|
|
json::for_each(event, top_keys, out);
|
|
|
|
|
|
|
|
const auto &auth_events{json::get<"auth_events"_>(event)};
|
2018-03-08 02:32:42 +01:00
|
|
|
s << "A:" << auth_events.count() << " ";
|
2017-12-12 21:33:14 +01:00
|
|
|
|
|
|
|
const auto &prev_states{json::get<"prev_state"_>(event)};
|
2018-03-08 02:32:42 +01:00
|
|
|
s << "S:" << prev_states.count() << " ";
|
2017-12-12 21:33:14 +01:00
|
|
|
|
|
|
|
const auto &prev_events{json::get<"prev_events"_>(event)};
|
2018-03-08 02:32:42 +01:00
|
|
|
s << "E:" << prev_events.count() << " ";
|
2017-12-12 21:33:14 +01:00
|
|
|
|
|
|
|
const auto &hashes{json::get<"hashes"_>(event)};
|
|
|
|
s << "[ ";
|
|
|
|
for(const auto &hash : hashes)
|
|
|
|
s << hash.first << " ";
|
|
|
|
s << "] ";
|
|
|
|
|
|
|
|
const auto &signatures{json::get<"signatures"_>(event)};
|
|
|
|
s << "[ ";
|
|
|
|
for(const auto &signature : signatures)
|
|
|
|
{
|
|
|
|
s << signature.first << "[ ";
|
|
|
|
for(const auto &key : json::object{signature.second})
|
|
|
|
s << key.first << " ";
|
|
|
|
|
|
|
|
s << "] ";
|
|
|
|
}
|
|
|
|
s << "] ";
|
|
|
|
|
|
|
|
out("type", json::get<"type"_>(event));
|
|
|
|
|
|
|
|
const auto &state_key
|
|
|
|
{
|
|
|
|
json::get<"state_key"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(defined(state_key) && empty(state_key))
|
|
|
|
s << "\"\"" << " ";
|
|
|
|
else if(defined(state_key))
|
|
|
|
s << state_key << " ";
|
|
|
|
else
|
|
|
|
s << "*" << " ";
|
|
|
|
|
|
|
|
out("membership", json::get<"membership"_>(event));
|
2018-02-15 23:57:56 +01:00
|
|
|
out("redacts", json::get<"redacts"_>(event));
|
2017-12-12 21:33:14 +01:00
|
|
|
|
2018-03-15 07:58:34 +01:00
|
|
|
const json::object &contents
|
|
|
|
{
|
|
|
|
content_keys?
|
|
|
|
json::get<"content"_>(event):
|
|
|
|
json::object{}
|
|
|
|
};
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
if(!contents.empty())
|
|
|
|
{
|
|
|
|
s << "+" << string_view{contents}.size() << " bytes :";
|
|
|
|
for(const auto &content : contents)
|
|
|
|
s << content.first << " ";
|
|
|
|
}
|
|
|
|
|
2018-05-04 23:22:17 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::id::event
|
|
|
|
ircd::m::make_id(const event &event,
|
|
|
|
id::event::buf &buf)
|
|
|
|
{
|
|
|
|
const crh::sha256::buf hash{event};
|
|
|
|
return make_id(event, buf, hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::id::event
|
|
|
|
ircd::m::make_id(const event &event,
|
|
|
|
id::event::buf &buf,
|
|
|
|
const const_buffer &hash)
|
|
|
|
{
|
|
|
|
char readable[b58encode_size(sha256::digest_size)];
|
|
|
|
const id::event ret
|
|
|
|
{
|
|
|
|
buf, b58encode(readable, hash), my_host()
|
|
|
|
};
|
|
|
|
|
|
|
|
buf.assigned(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-06-09 23:32:46 +02:00
|
|
|
ircd::m::before(const event &a,
|
|
|
|
const event &b)
|
2018-05-04 23:22:17 +02:00
|
|
|
{
|
2018-08-26 08:28:23 +02:00
|
|
|
const event::prev prev{b};
|
|
|
|
return prev.prev_events_has(at<"event_id"_>(a));
|
2018-05-04 23:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::operator>=(const event &a, const event &b)
|
|
|
|
{
|
|
|
|
assert(json::get<"room_id"_>(a) == json::get<"room_id"_>(b));
|
|
|
|
return at<"depth"_>(a) >= at<"depth"_>(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::operator<=(const event &a, const event &b)
|
|
|
|
{
|
|
|
|
assert(json::get<"room_id"_>(a) == json::get<"room_id"_>(b));
|
|
|
|
return at<"depth"_>(a) <= at<"depth"_>(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::operator>(const event &a, const event &b)
|
|
|
|
{
|
|
|
|
assert(json::get<"room_id"_>(a) == json::get<"room_id"_>(b));
|
|
|
|
return at<"depth"_>(a) > at<"depth"_>(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::operator<(const event &a, const event &b)
|
|
|
|
{
|
|
|
|
assert(json::get<"room_id"_>(a) == json::get<"room_id"_>(b));
|
|
|
|
return at<"depth"_>(a) < at<"depth"_>(b);
|
|
|
|
}
|
|
|
|
|
2018-06-09 23:32:46 +02:00
|
|
|
bool
|
|
|
|
ircd::m::operator==(const event &a, const event &b)
|
|
|
|
{
|
|
|
|
assert(json::get<"room_id"_>(a) == json::get<"room_id"_>(b));
|
|
|
|
return at<"event_id"_>(a) == at<"event_id"_>(b);
|
|
|
|
}
|
|
|
|
|
2018-05-05 01:13:10 +02:00
|
|
|
bool
|
|
|
|
ircd::m::bad(const id::event &event_id)
|
|
|
|
{
|
|
|
|
auto &column
|
|
|
|
{
|
|
|
|
dbs::event_bad
|
|
|
|
};
|
|
|
|
|
|
|
|
return has(column, event_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::bad(const id::event &event_id,
|
|
|
|
uint64_t &idx)
|
|
|
|
{
|
|
|
|
auto &column
|
|
|
|
{
|
|
|
|
dbs::event_bad
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ret;
|
|
|
|
const string_view value
|
|
|
|
{
|
|
|
|
read(column, event_id, ret, mutable_buffer
|
|
|
|
{
|
|
|
|
reinterpret_cast<char *>(&idx), sizeof(idx)
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!value)
|
|
|
|
idx = uint64_t(-1);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-05-04 23:22:17 +02:00
|
|
|
bool
|
|
|
|
ircd::m::exists(const id::event &event_id)
|
|
|
|
{
|
|
|
|
auto &column
|
|
|
|
{
|
|
|
|
dbs::event_idx
|
|
|
|
};
|
|
|
|
|
|
|
|
return has(column, event_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::check_size(const event &event)
|
|
|
|
{
|
|
|
|
const size_t &event_size
|
|
|
|
{
|
|
|
|
serialized(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(event_size > size_t(event::max_size))
|
|
|
|
throw m::BAD_JSON
|
|
|
|
{
|
|
|
|
"Event is %zu bytes which is larger than the maximum %zu bytes",
|
|
|
|
event_size,
|
|
|
|
size_t(event::max_size)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::check_size(std::nothrow_t,
|
|
|
|
const event &event)
|
|
|
|
{
|
|
|
|
const size_t &event_size
|
|
|
|
{
|
|
|
|
serialized(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
return event_size <= size_t(event::max_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::membership(const event &event)
|
|
|
|
{
|
|
|
|
return json::get<"membership"_>(event)?
|
|
|
|
string_view{json::get<"membership"_>(event)}:
|
|
|
|
unquote(json::get<"content"_>(event).get("membership"));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::degree(const event &event)
|
|
|
|
{
|
|
|
|
return degree(event::prev{event});
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::m::pretty(const event::prev &prev)
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
std::stringstream s;
|
|
|
|
pubsetbuf(s, ret, 4096);
|
|
|
|
pretty(s, prev);
|
|
|
|
resizebuf(s, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream &
|
|
|
|
ircd::m::pretty(std::ostream &s,
|
|
|
|
const event::prev &prev)
|
|
|
|
{
|
|
|
|
const auto out{[&s]
|
|
|
|
(const string_view &key, auto&& val)
|
|
|
|
{
|
|
|
|
if(json::defined(val))
|
|
|
|
s << key << " :" << val << std::endl;
|
|
|
|
}};
|
|
|
|
|
|
|
|
const auto &auth_events{json::get<"auth_events"_>(prev)};
|
|
|
|
for(const json::array auth_event : auth_events)
|
|
|
|
{
|
|
|
|
s << std::setw(16) << std::right << "[auth event]"
|
|
|
|
<< " :" << unquote(auth_event[0]);
|
|
|
|
|
|
|
|
for(const auto &hash : json::object{auth_event[1]})
|
|
|
|
s << " " << unquote(hash.first)
|
|
|
|
<< ": " << unquote(hash.second);
|
|
|
|
|
|
|
|
s << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &prev_states{json::get<"prev_state"_>(prev)};
|
|
|
|
for(const json::array prev_state : prev_states)
|
|
|
|
{
|
|
|
|
s << std::setw(16) << std::right << "[prev state]"
|
|
|
|
<< " :" << unquote(prev_state[0]);
|
|
|
|
|
|
|
|
for(const auto &hash : json::object{prev_state[1]})
|
|
|
|
s << " " << unquote(hash.first)
|
|
|
|
<< ": " << unquote(hash.second);
|
|
|
|
|
|
|
|
s << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &prev_events{json::get<"prev_events"_>(prev)};
|
|
|
|
for(const json::array prev_event : prev_events)
|
|
|
|
{
|
|
|
|
s << std::setw(16) << std::right << "[prev_event]"
|
|
|
|
<< " :" << unquote(prev_event[0]);
|
|
|
|
|
|
|
|
for(const auto &hash : json::object{prev_event[1]})
|
|
|
|
s << " " << unquote(hash.first)
|
|
|
|
<< ": " << unquote(hash.second);
|
|
|
|
|
|
|
|
s << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::m::pretty_oneline(const event::prev &prev)
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
std::stringstream s;
|
|
|
|
pubsetbuf(s, ret, 4096);
|
|
|
|
pretty_oneline(s, prev);
|
2017-12-12 21:33:14 +01:00
|
|
|
resizebuf(s, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-01-20 14:48:39 +01:00
|
|
|
|
2018-05-04 23:22:17 +02:00
|
|
|
std::ostream &
|
|
|
|
ircd::m::pretty_oneline(std::ostream &s,
|
|
|
|
const event::prev &prev)
|
|
|
|
{
|
|
|
|
const auto &auth_events{json::get<"auth_events"_>(prev)};
|
|
|
|
s << "A[ ";
|
|
|
|
for(const json::array auth_event : auth_events)
|
|
|
|
s << unquote(auth_event[0]) << " ";
|
|
|
|
s << "] ";
|
|
|
|
|
|
|
|
const auto &prev_states{json::get<"prev_state"_>(prev)};
|
|
|
|
s << "S[ ";
|
|
|
|
for(const json::array prev_state : prev_states)
|
|
|
|
s << unquote(prev_state[0]) << " ";
|
|
|
|
s << "] ";
|
|
|
|
|
|
|
|
const auto &prev_events{json::get<"prev_events"_>(prev)};
|
|
|
|
s << "E[ ";
|
|
|
|
for(const json::array prev_event : prev_events)
|
|
|
|
s << unquote(prev_event[0]) << " ";
|
|
|
|
s << "] ";
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-06-09 23:32:46 +02:00
|
|
|
size_t
|
|
|
|
ircd::m::degree(const event::prev &prev)
|
|
|
|
{
|
|
|
|
size_t ret{0};
|
|
|
|
json::for_each(prev, [&ret]
|
|
|
|
(const auto &, const json::array &prevs)
|
|
|
|
{
|
|
|
|
ret += prevs.count();
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::count(const event::prev &prev)
|
|
|
|
{
|
|
|
|
size_t ret{0};
|
|
|
|
m::for_each(prev, [&ret](const event::id &event_id)
|
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::for_each(const event::prev &prev,
|
|
|
|
const event::id::closure &closure)
|
|
|
|
{
|
|
|
|
m::for_each(prev, event::id::closure_bool{[&closure]
|
|
|
|
(const event::id &event_id)
|
|
|
|
{
|
|
|
|
closure(event_id);
|
|
|
|
return true;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::for_each(const event::prev &prev,
|
|
|
|
const event::id::closure_bool &closure)
|
|
|
|
{
|
|
|
|
return json::until(prev, [&closure]
|
|
|
|
(const auto &key, const json::array &prevs)
|
|
|
|
{
|
|
|
|
for(const json::array &prev : prevs)
|
|
|
|
if(!closure(event::id(unquote(prev.at(0)))))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-21 02:04:17 +01:00
|
|
|
bool
|
|
|
|
ircd::m::my(const event &event)
|
|
|
|
{
|
2018-03-14 20:27:44 +01:00
|
|
|
const auto &origin(json::get<"origin"_>(event));
|
|
|
|
const auto &eid(json::get<"event_id"_>(event));
|
|
|
|
return
|
|
|
|
origin?
|
|
|
|
my_host(origin):
|
|
|
|
eid?
|
|
|
|
my(event::id(eid)):
|
|
|
|
false;
|
2018-02-21 02:04:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::my(const id::event &event_id)
|
|
|
|
{
|
|
|
|
return self::host(event_id.host());
|
|
|
|
}
|
|
|
|
|
2018-01-20 14:48:39 +01:00
|
|
|
//
|
|
|
|
// event
|
|
|
|
//
|
|
|
|
|
2018-03-18 04:47:21 +01:00
|
|
|
/// The maximum size of an event we will create. This may also be used in
|
|
|
|
/// some contexts for what we will accept, but the protocol limit and hard
|
|
|
|
/// worst-case buffer size is still event::MAX_SIZE.
|
|
|
|
ircd::conf::item<size_t>
|
|
|
|
ircd::m::event::max_size
|
|
|
|
{
|
|
|
|
{ "name", "m.event.max_size" },
|
|
|
|
{ "default", 65507L },
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// event::event
|
|
|
|
//
|
|
|
|
|
2018-01-20 14:48:39 +01:00
|
|
|
ircd::m::event::event(const id &id,
|
|
|
|
const mutable_buffer &buf)
|
2018-04-18 04:14:39 +02:00
|
|
|
:event
|
|
|
|
{
|
2018-05-10 01:38:11 +02:00
|
|
|
index(id), buf
|
2018-04-18 04:14:39 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::event(const idx &idx,
|
|
|
|
const mutable_buffer &buf)
|
2018-01-20 14:48:39 +01:00
|
|
|
{
|
2018-02-08 22:23:01 +01:00
|
|
|
assert(bool(dbs::events));
|
2018-02-06 00:19:20 +01:00
|
|
|
|
|
|
|
db::gopts opts;
|
2018-02-08 22:23:01 +01:00
|
|
|
for(size_t i(0); i < dbs::event_column.size(); ++i)
|
2018-01-20 14:48:39 +01:00
|
|
|
{
|
2018-02-06 00:19:20 +01:00
|
|
|
const db::cell cell
|
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
dbs::event_column[i], byte_view<string_view>{idx}, opts
|
2018-02-06 00:19:20 +01:00
|
|
|
};
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
db::assign(*this, cell, byte_view<string_view>{idx});
|
2018-02-06 00:19:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const json::object obj
|
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
string_view
|
|
|
|
{
|
|
|
|
data(buf), json::print(buf, *this)
|
|
|
|
}
|
2018-01-20 14:48:39 +01:00
|
|
|
};
|
|
|
|
|
2018-02-06 00:19:20 +01:00
|
|
|
new (this) m::event(obj);
|
2018-01-20 14:48:39 +01:00
|
|
|
}
|
2018-02-09 20:22:46 +01:00
|
|
|
|
2018-05-27 11:02:08 +02:00
|
|
|
namespace ircd::m
|
|
|
|
{
|
|
|
|
static json::object make_hashes(const mutable_buffer &out, const sha256::buf &hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::object
|
|
|
|
ircd::m::hashes(const mutable_buffer &out,
|
|
|
|
const event &event)
|
|
|
|
{
|
|
|
|
const sha256::buf hash_
|
|
|
|
{
|
|
|
|
hash(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
return make_hashes(out, hash_);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::object
|
2018-03-07 20:54:56 +01:00
|
|
|
ircd::m::event::hashes(const mutable_buffer &out,
|
|
|
|
json::iov &event,
|
|
|
|
const string_view &content)
|
|
|
|
{
|
|
|
|
const sha256::buf hash_
|
|
|
|
{
|
|
|
|
hash(event, content)
|
|
|
|
};
|
|
|
|
|
2018-05-27 11:02:08 +02:00
|
|
|
return make_hashes(out, hash_);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::object
|
|
|
|
ircd::m::make_hashes(const mutable_buffer &out,
|
|
|
|
const sha256::buf &hash)
|
|
|
|
{
|
|
|
|
static const auto b64bufsz(b64encode_size(sizeof(hash)));
|
|
|
|
thread_local char hashb64buf[b64bufsz];
|
2018-03-07 20:54:56 +01:00
|
|
|
const json::members hashes
|
|
|
|
{
|
2018-05-27 11:02:08 +02:00
|
|
|
{ "sha256", b64encode_unpadded(hashb64buf, hash) }
|
2018-03-07 20:54:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return json::stringify(mutable_buffer{out}, hashes);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::sha256::buf
|
|
|
|
ircd::m::event::hash(json::iov &event,
|
|
|
|
const string_view &content)
|
|
|
|
{
|
|
|
|
const json::iov::push _content
|
|
|
|
{
|
|
|
|
event, { "content", content }
|
|
|
|
};
|
|
|
|
|
2018-03-23 07:53:11 +01:00
|
|
|
return m::hash(event);
|
2018-03-07 20:54:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::sha256::buf
|
2018-03-23 07:53:11 +01:00
|
|
|
ircd::m::hash(const event &event)
|
2018-03-07 20:54:56 +01:00
|
|
|
{
|
|
|
|
thread_local char buf[64_KiB];
|
2018-03-21 08:22:20 +01:00
|
|
|
string_view preimage;
|
|
|
|
|
|
|
|
//TODO: tuple::keys::selection
|
|
|
|
if(defined(json::get<"signatures"_>(event)) ||
|
|
|
|
defined(json::get<"hashes"_>(event)))
|
|
|
|
{
|
|
|
|
m::event event_{event};
|
|
|
|
json::get<"signatures"_>(event_) = {};
|
|
|
|
json::get<"hashes"_>(event_) = {};
|
|
|
|
preimage =
|
|
|
|
{
|
|
|
|
stringify(buf, event_)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else preimage =
|
2018-03-07 20:54:56 +01:00
|
|
|
{
|
|
|
|
stringify(buf, event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const sha256::buf hash
|
|
|
|
{
|
|
|
|
sha256{preimage}
|
|
|
|
};
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2018-03-23 07:53:11 +01:00
|
|
|
bool
|
|
|
|
ircd::m::verify_hash(const event &event)
|
|
|
|
{
|
|
|
|
const sha256::buf hash
|
|
|
|
{
|
|
|
|
m::hash(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
return verify_hash(event, hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::verify_hash(const event &event,
|
|
|
|
const sha256::buf &hash)
|
|
|
|
{
|
|
|
|
static const size_t hashb64sz
|
|
|
|
{
|
|
|
|
size_t(hash.size() * 1.34) + 1
|
|
|
|
};
|
|
|
|
|
|
|
|
thread_local char b64buf[hashb64sz];
|
|
|
|
return verify_sha256b64(event, b64encode_unpadded(b64buf, hash));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::verify_sha256b64(const event &event,
|
|
|
|
const string_view &b64)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const json::object &object
|
|
|
|
{
|
|
|
|
at<"hashes"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &hash
|
|
|
|
{
|
|
|
|
unquote(object.at("sha256"))
|
|
|
|
};
|
|
|
|
|
|
|
|
return hash == b64;
|
|
|
|
}
|
|
|
|
catch(const json::not_found &)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-27 11:02:08 +02:00
|
|
|
ircd::json::object
|
2018-03-07 20:54:56 +01:00
|
|
|
ircd::m::event::signatures(const mutable_buffer &out,
|
|
|
|
json::iov &event,
|
|
|
|
const json::iov &content)
|
|
|
|
{
|
|
|
|
const ed25519::sig sig
|
|
|
|
{
|
|
|
|
sign(event, content)
|
|
|
|
};
|
|
|
|
|
2018-05-15 09:55:01 +02:00
|
|
|
thread_local char sigb64buf[b64encode_size(sizeof(sig))];
|
2018-03-07 20:54:56 +01:00
|
|
|
const json::members sigb64
|
|
|
|
{
|
|
|
|
{ self::public_key_id, b64encode_unpadded(sigb64buf, sig) }
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::members sigs
|
|
|
|
{
|
|
|
|
{ my_host(), sigb64 }
|
|
|
|
};
|
|
|
|
|
|
|
|
return json::stringify(mutable_buffer{out}, sigs);
|
|
|
|
}
|
|
|
|
|
2018-05-16 04:53:08 +02:00
|
|
|
ircd::m::event
|
2018-05-19 01:42:25 +02:00
|
|
|
ircd::m::signatures(const mutable_buffer &out_,
|
2018-05-16 04:53:08 +02:00
|
|
|
const m::event &event_)
|
|
|
|
{
|
|
|
|
thread_local char content[64_KiB];
|
|
|
|
m::event event
|
|
|
|
{
|
|
|
|
essential(event_, content)
|
|
|
|
};
|
|
|
|
|
|
|
|
thread_local char buf[64_KiB];
|
|
|
|
const json::object &preimage
|
|
|
|
{
|
|
|
|
stringify(buf, event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const ed25519::sig sig
|
|
|
|
{
|
|
|
|
sign(preimage)
|
|
|
|
};
|
|
|
|
|
|
|
|
thread_local char sigb64buf[b64encode_size(sizeof(sig))];
|
|
|
|
const json::member my_sig
|
|
|
|
{
|
|
|
|
my_host(), json::members
|
|
|
|
{
|
|
|
|
{ self::public_key_id, b64encode_unpadded(sigb64buf, sig) }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const size_t SIG_MAX{64};
|
|
|
|
thread_local std::array<json::member, SIG_MAX> sigs;
|
|
|
|
|
|
|
|
size_t i(0);
|
|
|
|
sigs.at(i++) = my_sig;
|
|
|
|
for(const auto &other : json::get<"signatures"_>(event_))
|
2018-05-19 01:42:25 +02:00
|
|
|
if(!my_host(unquote(other.first)))
|
|
|
|
sigs.at(i++) = { other.first, other.second };
|
2018-05-16 04:53:08 +02:00
|
|
|
|
2018-05-27 13:19:01 +02:00
|
|
|
event = event_;
|
2018-05-19 01:42:25 +02:00
|
|
|
mutable_buffer out{out_};
|
|
|
|
json::get<"signatures"_>(event) = json::stringify(out, sigs.data(), sigs.data() + i);
|
2018-05-16 04:53:08 +02:00
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
2018-03-07 20:54:56 +01:00
|
|
|
ircd::ed25519::sig
|
|
|
|
ircd::m::event::sign(json::iov &event,
|
2018-03-08 23:17:47 +01:00
|
|
|
const json::iov &contents)
|
2018-03-21 01:59:08 +01:00
|
|
|
{
|
|
|
|
return sign(event, contents, self::secret_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ed25519::sig
|
|
|
|
ircd::m::event::sign(json::iov &event,
|
|
|
|
const json::iov &contents,
|
|
|
|
const ed25519::sk &sk)
|
2018-03-07 20:54:56 +01:00
|
|
|
{
|
2018-03-21 06:03:53 +01:00
|
|
|
ed25519::sig sig;
|
|
|
|
essential(event, contents, [&sk, &sig]
|
|
|
|
(json::iov &event)
|
2018-03-08 23:17:47 +01:00
|
|
|
{
|
2018-03-23 07:53:11 +01:00
|
|
|
sig = m::sign(event, sk);
|
2018-03-21 06:03:53 +01:00
|
|
|
});
|
2018-03-08 23:17:47 +01:00
|
|
|
|
2018-03-21 06:03:53 +01:00
|
|
|
return sig;
|
2018-03-07 20:54:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ed25519::sig
|
2018-03-23 07:53:11 +01:00
|
|
|
ircd::m::sign(const event &event)
|
2018-03-07 20:54:56 +01:00
|
|
|
{
|
2018-03-21 01:59:08 +01:00
|
|
|
return sign(event, self::secret_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ed25519::sig
|
2018-03-23 07:53:11 +01:00
|
|
|
ircd::m::sign(const event &event,
|
|
|
|
const ed25519::sk &sk)
|
2018-03-21 01:59:08 +01:00
|
|
|
{
|
|
|
|
thread_local char buf[64_KiB];
|
|
|
|
const string_view preimage
|
|
|
|
{
|
|
|
|
stringify(buf, event)
|
|
|
|
};
|
|
|
|
|
2018-03-23 07:53:11 +01:00
|
|
|
return event::sign(preimage, sk);
|
2018-03-21 01:59:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ed25519::sig
|
|
|
|
ircd::m::event::sign(const json::object &event)
|
|
|
|
{
|
|
|
|
return sign(event, self::secret_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ed25519::sig
|
|
|
|
ircd::m::event::sign(const json::object &event,
|
|
|
|
const ed25519::sk &sk)
|
|
|
|
{
|
|
|
|
//TODO: skip rewrite
|
2018-03-07 20:54:56 +01:00
|
|
|
thread_local char buf[64_KiB];
|
|
|
|
const string_view preimage
|
|
|
|
{
|
|
|
|
stringify(buf, event)
|
|
|
|
};
|
|
|
|
|
2018-03-21 01:59:08 +01:00
|
|
|
return sign(preimage, sk);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ed25519::sig
|
|
|
|
ircd::m::event::sign(const string_view &event)
|
|
|
|
{
|
|
|
|
return sign(event, self::secret_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ed25519::sig
|
|
|
|
ircd::m::event::sign(const string_view &event,
|
|
|
|
const ed25519::sk &sk)
|
|
|
|
{
|
2018-03-07 20:54:56 +01:00
|
|
|
const ed25519::sig sig
|
|
|
|
{
|
2018-03-21 01:59:08 +01:00
|
|
|
sk.sign(event)
|
2018-03-07 20:54:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return sig;
|
|
|
|
}
|
2018-03-21 03:19:55 +01:00
|
|
|
bool
|
2018-03-23 07:53:11 +01:00
|
|
|
ircd::m::verify(const event &event)
|
2018-03-21 03:19:55 +01:00
|
|
|
{
|
|
|
|
const string_view &origin
|
|
|
|
{
|
|
|
|
at<"origin"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
return verify(event, origin);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-03-23 07:53:11 +01:00
|
|
|
ircd::m::verify(const event &event,
|
|
|
|
const string_view &origin)
|
2018-03-21 03:19:55 +01:00
|
|
|
{
|
|
|
|
const json::object &signatures
|
|
|
|
{
|
|
|
|
at<"signatures"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &origin_sigs
|
|
|
|
{
|
|
|
|
signatures.at(origin)
|
|
|
|
};
|
|
|
|
|
|
|
|
for(const auto &p : origin_sigs)
|
|
|
|
if(verify(event, origin, unquote(p.first)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-03-23 07:53:11 +01:00
|
|
|
ircd::m::verify(const event &event,
|
|
|
|
const string_view &origin,
|
|
|
|
const string_view &keyid)
|
2018-03-21 03:19:55 +01:00
|
|
|
try
|
|
|
|
{
|
2018-05-11 11:05:08 +02:00
|
|
|
const m::node::id::buf node_id
|
|
|
|
{
|
|
|
|
"", origin
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::node node
|
|
|
|
{
|
|
|
|
node_id
|
|
|
|
};
|
|
|
|
|
2018-03-21 03:19:55 +01:00
|
|
|
bool ret{false};
|
2018-05-11 11:05:08 +02:00
|
|
|
node.key(keyid, [&ret, &event, &origin, &keyid]
|
2018-03-21 03:19:55 +01:00
|
|
|
(const ed25519::pk &pk)
|
|
|
|
{
|
|
|
|
ret = verify(event, pk, origin, keyid);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const m::NOT_FOUND &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
"Failed to verify %s because key %s for %s :%s",
|
|
|
|
string_view{json::get<"event_id"_>(event)},
|
|
|
|
keyid,
|
|
|
|
origin,
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-07 20:54:56 +01:00
|
|
|
|
2018-03-21 03:01:47 +01:00
|
|
|
bool
|
2018-03-23 07:53:11 +01:00
|
|
|
ircd::m::verify(const event &event,
|
|
|
|
const ed25519::pk &pk,
|
|
|
|
const string_view &origin,
|
|
|
|
const string_view &keyid)
|
2018-03-21 03:01:47 +01:00
|
|
|
{
|
|
|
|
const json::object &signatures
|
|
|
|
{
|
|
|
|
at<"signatures"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &origin_sigs
|
|
|
|
{
|
|
|
|
signatures.at(origin)
|
|
|
|
};
|
|
|
|
|
|
|
|
const ed25519::sig sig
|
|
|
|
{
|
|
|
|
[&origin_sigs, &keyid](auto &buf)
|
|
|
|
{
|
|
|
|
b64decode(buf, unquote(origin_sigs.at(keyid)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return verify(event, pk, sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-03-23 07:53:11 +01:00
|
|
|
ircd::m::verify(const event &event_,
|
|
|
|
const ed25519::pk &pk,
|
|
|
|
const ed25519::sig &sig)
|
2018-03-21 03:01:47 +01:00
|
|
|
{
|
2018-03-21 06:03:53 +01:00
|
|
|
thread_local char content[64_KiB];
|
|
|
|
m::event event
|
|
|
|
{
|
|
|
|
essential(event_, content)
|
|
|
|
};
|
|
|
|
|
|
|
|
thread_local char buf[64_KiB];
|
2018-05-11 11:05:08 +02:00
|
|
|
const json::object &preimage
|
2018-03-21 06:03:53 +01:00
|
|
|
{
|
|
|
|
stringify(buf, event)
|
|
|
|
};
|
|
|
|
|
2018-03-23 07:53:11 +01:00
|
|
|
return event::verify(preimage, pk, sig);
|
2018-03-21 06:03:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::event::verify(const json::object &event,
|
|
|
|
const ed25519::pk &pk,
|
|
|
|
const ed25519::sig &sig)
|
|
|
|
{
|
|
|
|
//TODO: skip rewrite
|
|
|
|
thread_local char buf[64_KiB];
|
|
|
|
const string_view preimage
|
|
|
|
{
|
|
|
|
stringify(buf, event)
|
|
|
|
};
|
|
|
|
|
|
|
|
return verify(preimage, pk, sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::event::verify(const string_view &event,
|
|
|
|
const ed25519::pk &pk,
|
|
|
|
const ed25519::sig &sig)
|
|
|
|
{
|
|
|
|
return pk.verify(event, sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::event::essential(json::iov &event,
|
|
|
|
const json::iov &contents,
|
|
|
|
const closure_iov_mutable &closure)
|
|
|
|
{
|
|
|
|
const auto &type
|
|
|
|
{
|
|
|
|
event.at("type")
|
|
|
|
};
|
|
|
|
|
|
|
|
if(type == "m.room.aliases")
|
|
|
|
{
|
|
|
|
const json::iov::push _content{event,
|
|
|
|
{
|
|
|
|
"content", json::members
|
|
|
|
{
|
|
|
|
{ "aliases", contents.at("aliases") }
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
|
|
|
closure(event);
|
|
|
|
}
|
|
|
|
else if(type == "m.room.create")
|
|
|
|
{
|
|
|
|
const json::iov::push _content{event,
|
|
|
|
{
|
|
|
|
"content", json::members
|
|
|
|
{
|
|
|
|
{ "creator", contents.at("creator") }
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
|
|
|
closure(event);
|
|
|
|
}
|
|
|
|
else if(type == "m.room.history_visibility")
|
|
|
|
{
|
|
|
|
const json::iov::push _content{event,
|
|
|
|
{
|
|
|
|
"content", json::members
|
|
|
|
{
|
|
|
|
{ "history_visibility", contents.at("history_visibility") }
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
|
|
|
closure(event);
|
|
|
|
}
|
|
|
|
else if(type == "m.room.join_rules")
|
|
|
|
{
|
|
|
|
const json::iov::push _content{event,
|
|
|
|
{
|
|
|
|
"content", json::members
|
|
|
|
{
|
|
|
|
{ "join_rule", contents.at("join_rule") }
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
|
|
|
closure(event);
|
|
|
|
}
|
|
|
|
else if(type == "m.room.member")
|
|
|
|
{
|
|
|
|
const json::iov::push _content{event,
|
|
|
|
{
|
|
|
|
"content", json::members
|
|
|
|
{
|
|
|
|
{ "membership", contents.at("membership") }
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
|
|
|
closure(event);
|
|
|
|
}
|
|
|
|
else if(type == "m.room.power_levels")
|
|
|
|
{
|
|
|
|
const json::iov::push _content{event,
|
|
|
|
{
|
|
|
|
"content", json::members
|
|
|
|
{
|
|
|
|
{ "ban", contents.at("ban") },
|
|
|
|
{ "events", contents.at("events") },
|
|
|
|
{ "events_default", contents.at("events_default") },
|
|
|
|
{ "kick", contents.at("kick") },
|
|
|
|
{ "redact", contents.at("redact") },
|
|
|
|
{ "state_default", contents.at("state_default") },
|
|
|
|
{ "users", contents.at("users") },
|
|
|
|
{ "users_default", contents.at("users_default") },
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
|
|
|
closure(event);
|
|
|
|
}
|
2018-05-26 02:50:51 +02:00
|
|
|
else if(type == "m.room.redaction")
|
|
|
|
{
|
|
|
|
// This simply finds the redacts key and swaps it with jsundefined for
|
|
|
|
// the scope's duration. The redacts key will still be present and
|
|
|
|
// visible in the json::iov which is incorrect if directly serialized.
|
|
|
|
// However, this iov is turned into a json::tuple (m::event) which ends
|
|
|
|
// up being serialized for signing. That serialization is where the
|
|
|
|
// jsundefined redacts value is ignored.
|
|
|
|
auto &redacts{event.at("redacts")};
|
|
|
|
json::value temp(std::move(redacts));
|
|
|
|
redacts = json::value{};
|
|
|
|
const unwind _{[&redacts, &temp]
|
|
|
|
{
|
|
|
|
redacts = std::move(temp);
|
|
|
|
}};
|
|
|
|
|
|
|
|
const json::iov::push _content
|
|
|
|
{
|
|
|
|
event, { "content", "{}" }
|
|
|
|
};
|
|
|
|
|
|
|
|
closure(event);
|
|
|
|
}
|
2018-03-21 06:03:53 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
const json::iov::push _content
|
|
|
|
{
|
|
|
|
event, { "content", "{}" }
|
|
|
|
};
|
|
|
|
|
|
|
|
closure(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event
|
2018-03-23 07:53:11 +01:00
|
|
|
ircd::m::essential(m::event event,
|
|
|
|
const mutable_buffer &contentbuf)
|
2018-03-21 06:03:53 +01:00
|
|
|
{
|
2018-03-21 03:01:47 +01:00
|
|
|
const auto &type
|
|
|
|
{
|
|
|
|
json::at<"type"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
json::object &content
|
|
|
|
{
|
|
|
|
json::get<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
2018-03-21 06:03:53 +01:00
|
|
|
mutable_buffer essential
|
|
|
|
{
|
|
|
|
contentbuf
|
|
|
|
};
|
2018-03-21 03:01:47 +01:00
|
|
|
|
|
|
|
if(type == "m.room.aliases")
|
|
|
|
{
|
|
|
|
content = json::stringify(essential, json::members
|
|
|
|
{
|
|
|
|
{ "aliases", unquote(content.at("aliases")) }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if(type == "m.room.create")
|
|
|
|
{
|
|
|
|
content = json::stringify(essential, json::members
|
|
|
|
{
|
|
|
|
{ "creator", unquote(content.at("creator")) }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if(type == "m.room.history_visibility")
|
|
|
|
{
|
|
|
|
content = json::stringify(essential, json::members
|
|
|
|
{
|
|
|
|
{ "history_visibility", unquote(content.at("history_visibility")) }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if(type == "m.room.join_rules")
|
|
|
|
{
|
|
|
|
content = json::stringify(essential, json::members
|
|
|
|
{
|
|
|
|
{ "join_rule", unquote(content.at("join_rule")) }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if(type == "m.room.member")
|
|
|
|
{
|
|
|
|
content = json::stringify(essential, json::members
|
|
|
|
{
|
|
|
|
{ "membership", unquote(content.at("membership")) }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if(type == "m.room.power_levels")
|
|
|
|
{
|
|
|
|
content = json::stringify(essential, json::members
|
|
|
|
{
|
|
|
|
{ "ban", unquote(content.at("ban")) },
|
|
|
|
{ "events", unquote(content.at("events")) },
|
|
|
|
{ "events_default", unquote(content.at("events_default")) },
|
|
|
|
{ "kick", unquote(content.at("kick")) },
|
|
|
|
{ "redact", unquote(content.at("redact")) },
|
|
|
|
{ "state_default", unquote(content.at("state_default")) },
|
|
|
|
{ "users", unquote(content.at("users")) },
|
|
|
|
{ "users_default", unquote(content.at("users_default")) },
|
|
|
|
});
|
|
|
|
}
|
2018-05-26 02:50:51 +02:00
|
|
|
else if(type == "m.room.redaction")
|
|
|
|
{
|
|
|
|
json::get<"redacts"_>(event) = string_view{};
|
|
|
|
content = "{}"_sv;
|
|
|
|
}
|
2018-03-21 03:01:47 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
content = "{}"_sv;
|
|
|
|
}
|
|
|
|
|
2018-05-16 04:52:37 +02:00
|
|
|
json::get<"signatures"_>(event) = {};
|
2018-03-21 06:03:53 +01:00
|
|
|
return event;
|
2018-03-21 03:01:47 +01:00
|
|
|
}
|
|
|
|
|
2018-02-10 22:28:22 +01:00
|
|
|
//
|
|
|
|
// event::prev
|
|
|
|
//
|
|
|
|
|
2018-08-26 08:28:23 +02:00
|
|
|
bool
|
|
|
|
ircd::m::event::prev::prev_events_has(const event::id &event_id)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for(const json::array &p : json::get<"prev_events"_>(*this))
|
|
|
|
if(unquote(p.at(0)) == event_id)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::event::prev::prev_states_has(const event::id &event_id)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for(const json::array &p : json::get<"prev_state"_>(*this))
|
|
|
|
if(unquote(p.at(0)) == event_id)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::event::prev::auth_events_has(const event::id &event_id)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for(const json::array &p : json::get<"auth_events"_>(*this))
|
|
|
|
if(unquote(p.at(0)) == event_id)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::event::prev::prev_events_count()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return json::get<"prev_events"_>(*this).count();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::event::prev::prev_states_count()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return json::get<"prev_state"_>(*this).count();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::event::prev::auth_events_count()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return json::get<"auth_events"_>(*this).count();
|
|
|
|
}
|
|
|
|
|
2018-02-10 22:28:22 +01:00
|
|
|
ircd::m::event::id
|
|
|
|
ircd::m::event::prev::auth_event(const uint &idx)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return std::get<0>(auth_events(idx));
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id
|
|
|
|
ircd::m::event::prev::prev_state(const uint &idx)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return std::get<0>(prev_states(idx));
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id
|
|
|
|
ircd::m::event::prev::prev_event(const uint &idx)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return std::get<0>(prev_events(idx));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::tuple<ircd::m::event::id, ircd::string_view>
|
|
|
|
ircd::m::event::prev::auth_events(const uint &idx)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const json::array &auth_event
|
|
|
|
{
|
|
|
|
at<"auth_events"_>(*this).at(idx)
|
|
|
|
};
|
|
|
|
|
|
|
|
return
|
|
|
|
{
|
|
|
|
unquote(auth_event.at(0)), unquote(auth_event[1])
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::tuple<ircd::m::event::id, ircd::string_view>
|
|
|
|
ircd::m::event::prev::prev_states(const uint &idx)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const json::array &state_event
|
|
|
|
{
|
|
|
|
at<"prev_state"_>(*this).at(idx)
|
|
|
|
};
|
|
|
|
|
|
|
|
return
|
|
|
|
{
|
|
|
|
unquote(state_event.at(0)), unquote(state_event[1])
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::tuple<ircd::m::event::id, ircd::string_view>
|
|
|
|
ircd::m::event::prev::prev_events(const uint &idx)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const json::array &prev_event
|
|
|
|
{
|
|
|
|
at<"prev_events"_>(*this).at(idx)
|
|
|
|
};
|
|
|
|
|
|
|
|
return
|
|
|
|
{
|
|
|
|
unquote(prev_event.at(0)), unquote(prev_event[1])
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-09 20:22:46 +01:00
|
|
|
//
|
|
|
|
// event::fetch
|
|
|
|
//
|
|
|
|
|
2018-05-20 12:01:58 +02:00
|
|
|
decltype(ircd::m::event::fetch::default_opts)
|
|
|
|
ircd::m::event::fetch::default_opts
|
|
|
|
{};
|
|
|
|
|
2018-09-01 13:59:39 +02:00
|
|
|
void
|
|
|
|
ircd::m::prefetch(const event::id &event_id,
|
|
|
|
const event::fetch::opts &opts)
|
|
|
|
{
|
|
|
|
prefetch(index(event_id), opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::prefetch(const event::id &event_id,
|
|
|
|
const string_view &key)
|
|
|
|
{
|
|
|
|
prefetch(index(event_id), key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::prefetch(const event::idx &event_idx,
|
|
|
|
const event::fetch::opts &opts)
|
|
|
|
{
|
|
|
|
const vector_view<const string_view> cols
|
|
|
|
{
|
|
|
|
opts.keys
|
|
|
|
};
|
|
|
|
|
|
|
|
for(const auto &col : cols)
|
|
|
|
if(col)
|
|
|
|
prefetch(event_idx, col);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::prefetch(const event::idx &event_idx,
|
|
|
|
const string_view &key)
|
|
|
|
{
|
|
|
|
const auto &column_idx
|
|
|
|
{
|
|
|
|
json::indexof<event>(key)
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &column
|
|
|
|
{
|
|
|
|
dbs::event_column.at(column_idx)
|
|
|
|
};
|
|
|
|
|
|
|
|
db::prefetch(column, byte_view<string_view>{event_idx});
|
|
|
|
}
|
|
|
|
|
2018-05-12 04:21:56 +02:00
|
|
|
ircd::const_buffer
|
|
|
|
ircd::m::get(const event::id &event_id,
|
|
|
|
const string_view &key,
|
|
|
|
const mutable_buffer &out)
|
|
|
|
{
|
|
|
|
const auto &ret
|
|
|
|
{
|
|
|
|
get(std::nothrow, index(event_id), key, out)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!ret)
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"%s for %s not found in database",
|
|
|
|
key,
|
|
|
|
string_view{event_id}
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::m::get(const event::idx &event_idx,
|
|
|
|
const string_view &key,
|
|
|
|
const mutable_buffer &out)
|
|
|
|
{
|
|
|
|
const const_buffer ret
|
|
|
|
{
|
|
|
|
get(std::nothrow, event_idx, key, out)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!ret)
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"%s for event_idx[%lu] not found in database",
|
|
|
|
key,
|
|
|
|
event_idx
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::m::get(std::nothrow_t,
|
|
|
|
const event::id &event_id,
|
|
|
|
const string_view &key,
|
|
|
|
const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
return get(std::nothrow, index(event_id), key, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::m::get(std::nothrow_t,
|
|
|
|
const event::idx &event_idx,
|
|
|
|
const string_view &key,
|
|
|
|
const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
const_buffer ret;
|
|
|
|
get(std::nothrow, event_idx, key, [&buf, &ret]
|
|
|
|
(const string_view &value)
|
|
|
|
{
|
|
|
|
ret = { data(buf), copy(buf, value) };
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::get(const event::id &event_id,
|
|
|
|
const string_view &key,
|
|
|
|
const event::fetch::view_closure &closure)
|
|
|
|
{
|
|
|
|
if(!get(std::nothrow, index(event_id), key, closure))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"%s for %s not found in database",
|
|
|
|
key,
|
|
|
|
string_view{event_id}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::get(const event::idx &event_idx,
|
|
|
|
const string_view &key,
|
|
|
|
const event::fetch::view_closure &closure)
|
|
|
|
{
|
|
|
|
if(!get(std::nothrow, event_idx, key, closure))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"%s for event_idx[%lu] not found in database",
|
|
|
|
key,
|
|
|
|
event_idx
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::get(std::nothrow_t,
|
|
|
|
const event::id &event_id,
|
|
|
|
const string_view &key,
|
|
|
|
const event::fetch::view_closure &closure)
|
|
|
|
{
|
|
|
|
return get(std::nothrow, index(event_id), key, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::get(std::nothrow_t,
|
|
|
|
const event::idx &event_idx,
|
|
|
|
const string_view &key,
|
|
|
|
const event::fetch::view_closure &closure)
|
|
|
|
{
|
|
|
|
const auto &column_idx
|
|
|
|
{
|
|
|
|
json::indexof<event>(key)
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &column
|
|
|
|
{
|
|
|
|
dbs::event_column.at(column_idx)
|
|
|
|
};
|
|
|
|
|
|
|
|
return column(byte_view<string_view>{event_idx}, std::nothrow, closure);
|
|
|
|
}
|
|
|
|
|
2018-02-09 20:22:46 +01:00
|
|
|
void
|
|
|
|
ircd::m::seek(event::fetch &fetch,
|
|
|
|
const event::id &event_id)
|
|
|
|
{
|
|
|
|
if(!seek(fetch, event_id, std::nothrow))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"%s not found in database", event_id
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::seek(event::fetch &fetch,
|
|
|
|
const event::id &event_id,
|
|
|
|
std::nothrow_t)
|
|
|
|
{
|
2018-05-10 01:38:11 +02:00
|
|
|
const auto &event_idx
|
2018-04-18 04:14:39 +02:00
|
|
|
{
|
2018-05-10 01:38:11 +02:00
|
|
|
index(event_id, std::nothrow)
|
2018-04-18 04:14:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return seek(fetch, event_idx, std::nothrow);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::seek(event::fetch &fetch,
|
|
|
|
const event::idx &event_idx)
|
|
|
|
{
|
|
|
|
if(!seek(fetch, event_idx, std::nothrow))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"%lu not found in database", event_idx
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::seek(event::fetch &fetch,
|
|
|
|
const event::idx &event_idx,
|
|
|
|
std::nothrow_t)
|
|
|
|
{
|
|
|
|
const string_view &key
|
|
|
|
{
|
|
|
|
byte_view<string_view>(event_idx)
|
|
|
|
};
|
|
|
|
|
|
|
|
db::seek(fetch.row, key);
|
|
|
|
fetch.valid = fetch.row.valid(key);
|
|
|
|
if(!fetch.valid)
|
2018-02-09 20:22:46 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
auto &event{static_cast<m::event &>(fetch)};
|
2018-04-18 04:14:39 +02:00
|
|
|
assign(event, fetch.row, key);
|
2018-02-09 20:22:46 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
ircd::m::event::idx
|
2018-05-10 01:38:11 +02:00
|
|
|
ircd::m::index(const event &event)
|
2018-04-18 04:14:39 +02:00
|
|
|
{
|
|
|
|
return index(at<"event_id"_>(event));
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::idx
|
2018-05-10 01:38:11 +02:00
|
|
|
ircd::m::index(const event &event,
|
|
|
|
std::nothrow_t)
|
2018-04-18 04:14:39 +02:00
|
|
|
{
|
|
|
|
return index(at<"event_id"_>(event), std::nothrow);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::idx
|
2018-05-10 01:38:11 +02:00
|
|
|
ircd::m::index(const event::id &event_id)
|
2018-04-18 04:14:39 +02:00
|
|
|
{
|
|
|
|
auto &column
|
|
|
|
{
|
|
|
|
dbs::event_idx
|
|
|
|
};
|
|
|
|
|
|
|
|
event::idx ret;
|
|
|
|
column(event_id, [&ret]
|
|
|
|
(const string_view &value)
|
|
|
|
{
|
|
|
|
ret = byte_view<event::idx>(value);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::idx
|
2018-05-10 01:38:11 +02:00
|
|
|
ircd::m::index(const event::id &event_id,
|
|
|
|
std::nothrow_t)
|
2018-04-18 04:14:39 +02:00
|
|
|
{
|
|
|
|
auto &column
|
|
|
|
{
|
|
|
|
dbs::event_idx
|
|
|
|
};
|
|
|
|
|
|
|
|
event::idx ret{0};
|
|
|
|
column(event_id, std::nothrow, [&ret]
|
|
|
|
(const string_view &value)
|
|
|
|
{
|
|
|
|
ret = byte_view<event::idx>(value);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::event::fetch::event_id(const idx &idx,
|
|
|
|
const id::closure &closure)
|
|
|
|
{
|
|
|
|
if(!event_id(idx, std::nothrow, closure))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"%lu not found in database", idx
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::event::fetch::event_id(const idx &idx,
|
|
|
|
std::nothrow_t,
|
|
|
|
const id::closure &closure)
|
|
|
|
{
|
|
|
|
static constexpr auto column_idx
|
|
|
|
{
|
|
|
|
json::indexof<event, "event_id"_>()
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &column
|
|
|
|
{
|
|
|
|
dbs::event_column.at(column_idx)
|
|
|
|
};
|
|
|
|
|
|
|
|
return column(byte_view<string_view>{idx}, std::nothrow, [&closure]
|
|
|
|
(const string_view &value)
|
|
|
|
{
|
|
|
|
closure(value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-09 20:22:46 +01:00
|
|
|
/// Seekless constructor.
|
2018-05-20 12:01:58 +02:00
|
|
|
ircd::m::event::fetch::fetch(const opts *const &opts)
|
2018-02-09 20:22:46 +01:00
|
|
|
:row
|
|
|
|
{
|
2018-05-20 12:01:58 +02:00
|
|
|
*dbs::events,
|
|
|
|
string_view{},
|
|
|
|
opts? opts->keys : default_opts.keys,
|
|
|
|
cell,
|
|
|
|
opts? opts->gopts : default_opts.gopts
|
2018-05-20 07:39:03 +02:00
|
|
|
}
|
|
|
|
,valid
|
|
|
|
{
|
|
|
|
false
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-09 20:22:46 +01:00
|
|
|
/// Seek to event_id and populate this event from database.
|
|
|
|
/// Throws if event not in database.
|
2018-04-18 04:14:39 +02:00
|
|
|
ircd::m::event::fetch::fetch(const event::id &event_id,
|
2018-05-20 12:01:58 +02:00
|
|
|
const opts *const &opts)
|
2018-04-18 04:14:39 +02:00
|
|
|
:fetch
|
2018-02-09 20:22:46 +01:00
|
|
|
{
|
2018-05-20 12:01:58 +02:00
|
|
|
index(event_id), opts
|
2018-02-09 20:22:46 +01:00
|
|
|
}
|
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
}
|
|
|
|
|
2018-05-20 06:20:44 +02:00
|
|
|
/// Seek to event_id and populate this event from database.
|
|
|
|
/// Event is not populated if not found in database.
|
|
|
|
ircd::m::event::fetch::fetch(const event::id &event_id,
|
|
|
|
std::nothrow_t,
|
2018-05-20 12:01:58 +02:00
|
|
|
const opts *const &opts)
|
2018-05-20 06:20:44 +02:00
|
|
|
:fetch
|
|
|
|
{
|
2018-05-20 12:01:58 +02:00
|
|
|
index(event_id, std::nothrow), std::nothrow, opts
|
2018-05-20 06:20:44 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Seek to event_idx and populate this event from database.
|
|
|
|
/// Throws if event not in database.
|
|
|
|
ircd::m::event::fetch::fetch(const event::idx &event_idx,
|
2018-05-20 12:01:58 +02:00
|
|
|
const opts *const &opts)
|
2018-05-20 06:20:44 +02:00
|
|
|
:fetch
|
|
|
|
{
|
2018-05-20 12:01:58 +02:00
|
|
|
event_idx, std::nothrow, opts
|
2018-05-20 06:20:44 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
if(!valid)
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"idx %zu not found in database", event_idx
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Seek to event_idx and populate this event from database.
|
|
|
|
/// Event is not populated if not found in database.
|
|
|
|
ircd::m::event::fetch::fetch(const event::idx &event_idx,
|
|
|
|
std::nothrow_t,
|
2018-05-20 12:01:58 +02:00
|
|
|
const opts *const &opts)
|
2018-05-20 06:20:44 +02:00
|
|
|
:row
|
|
|
|
{
|
2018-05-20 12:01:58 +02:00
|
|
|
*dbs::events,
|
|
|
|
byte_view<string_view>{event_idx},
|
|
|
|
opts? opts->keys : default_opts.keys,
|
|
|
|
cell,
|
|
|
|
opts? opts->gopts : default_opts.gopts
|
2018-05-20 06:20:44 +02:00
|
|
|
}
|
|
|
|
,valid
|
|
|
|
{
|
|
|
|
row.valid(byte_view<string_view>{event_idx})
|
|
|
|
}
|
|
|
|
{
|
|
|
|
if(valid)
|
|
|
|
assign(*this, row, byte_view<string_view>{event_idx});
|
|
|
|
}
|
|
|
|
|
2018-05-20 12:01:58 +02:00
|
|
|
//
|
|
|
|
// event::fetch::opts
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::event::fetch::opts::opts(const db::gopts &gopts,
|
|
|
|
const event::keys::selection &selection)
|
|
|
|
:opts
|
|
|
|
{
|
|
|
|
selection, gopts
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::fetch::opts::opts(const event::keys::selection &selection,
|
|
|
|
const db::gopts &gopts)
|
|
|
|
:opts
|
|
|
|
{
|
|
|
|
event::keys{selection}, gopts
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::fetch::opts::opts(const event::keys &keys,
|
|
|
|
const db::gopts &gopts)
|
|
|
|
:keys{keys}
|
|
|
|
,gopts{gopts}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-21 03:09:39 +01:00
|
|
|
//
|
2018-02-21 22:43:33 +01:00
|
|
|
// event::conforms
|
2018-02-21 03:09:39 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd::m
|
|
|
|
{
|
2018-02-21 22:43:33 +01:00
|
|
|
const size_t event_conforms_num{num_of<event::conforms::code>()};
|
|
|
|
extern const std::array<string_view, event_conforms_num> event_conforms_reflects;
|
2018-02-21 03:09:39 +01:00
|
|
|
}
|
|
|
|
|
2018-02-21 22:43:33 +01:00
|
|
|
decltype(ircd::m::event_conforms_reflects)
|
|
|
|
ircd::m::event_conforms_reflects
|
2018-02-21 03:09:39 +01:00
|
|
|
{
|
2018-02-21 23:24:34 +01:00
|
|
|
"INVALID_OR_MISSING_EVENT_ID",
|
|
|
|
"INVALID_OR_MISSING_ROOM_ID",
|
|
|
|
"INVALID_OR_MISSING_SENDER_ID",
|
|
|
|
"MISSING_TYPE",
|
|
|
|
"MISSING_ORIGIN",
|
|
|
|
"INVALID_ORIGIN",
|
|
|
|
"INVALID_OR_MISSING_REDACTS_ID",
|
|
|
|
"MISSING_MEMBERSHIP",
|
|
|
|
"INVALID_MEMBERSHIP",
|
|
|
|
"MISSING_CONTENT_MEMBERSHIP",
|
|
|
|
"INVALID_CONTENT_MEMBERSHIP",
|
|
|
|
"MISSING_PREV_EVENTS",
|
|
|
|
"MISSING_PREV_STATE",
|
2018-02-21 03:09:39 +01:00
|
|
|
"DEPTH_NEGATIVE",
|
2018-02-21 23:24:34 +01:00
|
|
|
"DEPTH_ZERO",
|
2018-03-09 16:11:00 +01:00
|
|
|
"MISSING_SIGNATURES",
|
|
|
|
"MISSING_ORIGIN_SIGNATURE",
|
|
|
|
"MISMATCH_ORIGIN_SENDER",
|
|
|
|
"MISMATCH_ORIGIN_EVENT_ID",
|
2018-03-12 04:48:34 +01:00
|
|
|
"SELF_REDACTS",
|
|
|
|
"SELF_PREV_EVENT",
|
|
|
|
"SELF_PREV_STATE",
|
|
|
|
"DUP_PREV_EVENT",
|
|
|
|
"DUP_PREV_STATE",
|
2018-02-21 03:09:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream &
|
2018-02-21 22:43:33 +01:00
|
|
|
ircd::m::operator<<(std::ostream &s, const event::conforms &conforms)
|
2018-02-21 03:09:39 +01:00
|
|
|
{
|
|
|
|
thread_local char buf[1024];
|
2018-02-21 22:43:33 +01:00
|
|
|
s << conforms.string(buf);
|
2018-02-21 03:09:39 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
2018-02-21 22:43:33 +01:00
|
|
|
ircd::m::reflect(const event::conforms::code &code)
|
2018-02-21 03:09:39 +01:00
|
|
|
try
|
|
|
|
{
|
2018-02-21 22:43:33 +01:00
|
|
|
return event_conforms_reflects.at(code);
|
2018-02-21 03:09:39 +01:00
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
return "??????"_sv;
|
|
|
|
}
|
|
|
|
|
2018-02-22 00:00:02 +01:00
|
|
|
ircd::m::event::conforms::code
|
|
|
|
ircd::m::event::conforms::reflect(const string_view &name)
|
|
|
|
{
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
std::find(begin(event_conforms_reflects), end(event_conforms_reflects), name)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(it == end(event_conforms_reflects))
|
|
|
|
throw std::out_of_range
|
|
|
|
{
|
|
|
|
"There is no event::conforms code by that name."
|
|
|
|
};
|
|
|
|
|
|
|
|
return code(std::distance(begin(event_conforms_reflects), it));
|
|
|
|
}
|
|
|
|
|
2018-02-21 23:24:34 +01:00
|
|
|
ircd::m::event::conforms::conforms(const event &e,
|
|
|
|
const uint64_t &skip)
|
|
|
|
:conforms{e}
|
|
|
|
{
|
|
|
|
report &= ~skip;
|
|
|
|
}
|
|
|
|
|
2018-02-21 22:43:33 +01:00
|
|
|
ircd::m::event::conforms::conforms(const event &e)
|
2018-02-21 03:09:39 +01:00
|
|
|
:report{0}
|
|
|
|
{
|
|
|
|
if(!valid(m::id::EVENT, json::get<"event_id"_>(e)))
|
2018-02-21 23:24:34 +01:00
|
|
|
set(INVALID_OR_MISSING_EVENT_ID);
|
2018-02-21 03:09:39 +01:00
|
|
|
|
|
|
|
if(!valid(m::id::ROOM, json::get<"room_id"_>(e)))
|
2018-02-21 23:24:34 +01:00
|
|
|
set(INVALID_OR_MISSING_ROOM_ID);
|
2018-02-21 03:09:39 +01:00
|
|
|
|
|
|
|
if(!valid(m::id::USER, json::get<"sender"_>(e)))
|
2018-02-21 23:24:34 +01:00
|
|
|
set(INVALID_OR_MISSING_SENDER_ID);
|
|
|
|
|
|
|
|
if(empty(json::get<"type"_>(e)))
|
|
|
|
set(MISSING_TYPE);
|
|
|
|
|
|
|
|
if(empty(json::get<"origin"_>(e)))
|
|
|
|
set(MISSING_ORIGIN);
|
|
|
|
|
|
|
|
//TODO: XXX
|
|
|
|
if(false)
|
|
|
|
set(INVALID_ORIGIN);
|
2018-02-21 03:09:39 +01:00
|
|
|
|
2018-03-09 16:11:00 +01:00
|
|
|
if(empty(json::get<"signatures"_>(e)))
|
|
|
|
set(MISSING_SIGNATURES);
|
|
|
|
|
|
|
|
if(empty(json::object{json::get<"signatures"_>(e).get(json::get<"origin"_>(e))}))
|
|
|
|
set(MISSING_ORIGIN_SIGNATURE);
|
|
|
|
|
|
|
|
if(!has(INVALID_OR_MISSING_SENDER_ID))
|
|
|
|
if(json::get<"origin"_>(e) != m::id::user{json::get<"sender"_>(e)}.host())
|
|
|
|
set(MISMATCH_ORIGIN_SENDER);
|
|
|
|
|
|
|
|
if(!has(INVALID_OR_MISSING_EVENT_ID))
|
|
|
|
if(json::get<"origin"_>(e) != m::id::event{json::get<"event_id"_>(e)}.host())
|
|
|
|
set(MISMATCH_ORIGIN_EVENT_ID);
|
|
|
|
|
2018-02-21 03:09:39 +01:00
|
|
|
if(json::get<"type"_>(e) == "m.room.redaction")
|
|
|
|
if(!valid(m::id::EVENT, json::get<"redacts"_>(e)))
|
2018-02-21 23:24:34 +01:00
|
|
|
set(INVALID_OR_MISSING_REDACTS_ID);
|
2018-02-21 03:09:39 +01:00
|
|
|
|
2018-03-12 04:48:34 +01:00
|
|
|
if(json::get<"redacts"_>(e))
|
|
|
|
if(json::get<"redacts"_>(e) == json::get<"event_id"_>(e))
|
|
|
|
set(SELF_REDACTS);
|
|
|
|
|
2018-02-21 23:24:34 +01:00
|
|
|
if(json::get<"type"_>(e) == "m.room.member")
|
|
|
|
if(empty(json::get<"membership"_>(e)))
|
|
|
|
set(MISSING_MEMBERSHIP);
|
|
|
|
|
|
|
|
if(json::get<"type"_>(e) == "m.room.member")
|
|
|
|
if(!all_of<std::islower>(json::get<"membership"_>(e)))
|
|
|
|
set(INVALID_MEMBERSHIP);
|
|
|
|
|
|
|
|
if(json::get<"type"_>(e) == "m.room.member")
|
|
|
|
if(empty(unquote(json::get<"content"_>(e).get("membership"))))
|
|
|
|
set(MISSING_CONTENT_MEMBERSHIP);
|
|
|
|
|
|
|
|
if(json::get<"type"_>(e) == "m.room.member")
|
|
|
|
if(!all_of<std::islower>(unquote(json::get<"content"_>(e).get("membership"))))
|
|
|
|
set(INVALID_CONTENT_MEMBERSHIP);
|
|
|
|
|
|
|
|
if(json::get<"type"_>(e) != "m.room.create")
|
|
|
|
if(empty(json::get<"prev_events"_>(e)))
|
|
|
|
set(MISSING_PREV_EVENTS);
|
|
|
|
|
|
|
|
if(json::get<"type"_>(e) != "m.room.create")
|
|
|
|
if(!empty(json::get<"state_key"_>(e)))
|
|
|
|
if(empty(json::get<"prev_state"_>(e)))
|
|
|
|
set(MISSING_PREV_STATE);
|
2018-02-21 03:09:39 +01:00
|
|
|
|
2018-05-23 09:29:26 +02:00
|
|
|
if(json::get<"depth"_>(e) != json::undefined_number && json::get<"depth"_>(e) < 0)
|
2018-02-21 03:09:39 +01:00
|
|
|
set(DEPTH_NEGATIVE);
|
|
|
|
|
|
|
|
if(json::get<"type"_>(e) != "m.room.create")
|
|
|
|
if(json::get<"depth"_>(e) == 0)
|
2018-02-21 23:24:34 +01:00
|
|
|
set(DEPTH_ZERO);
|
2018-03-12 04:48:34 +01:00
|
|
|
|
|
|
|
const prev p{e};
|
|
|
|
size_t i{0}, j{0};
|
|
|
|
for(const json::array &pe : json::get<"prev_events"_>(p))
|
|
|
|
{
|
|
|
|
if(unquote(pe.at(0)) == json::get<"event_id"_>(e))
|
|
|
|
set(SELF_PREV_EVENT);
|
|
|
|
|
|
|
|
j = 0;
|
|
|
|
for(const json::array &pe_ : json::get<"prev_events"_>(p))
|
|
|
|
if(i != j++)
|
|
|
|
if(pe_.at(0) == pe.at(0))
|
|
|
|
set(DUP_PREV_EVENT);
|
|
|
|
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
for(const json::array &ps : json::get<"prev_state"_>(p))
|
|
|
|
{
|
|
|
|
if(unquote(ps.at(0)) == json::get<"event_id"_>(e))
|
|
|
|
set(SELF_PREV_STATE);
|
|
|
|
|
|
|
|
j = 0;
|
|
|
|
for(const json::array &ps_ : json::get<"prev_state"_>(p))
|
|
|
|
if(i != j++)
|
|
|
|
if(ps_.at(0) == ps.at(0))
|
|
|
|
set(DUP_PREV_STATE);
|
|
|
|
|
|
|
|
++i;
|
|
|
|
}
|
2018-02-21 03:09:39 +01:00
|
|
|
}
|
|
|
|
|
2018-02-28 05:55:59 +01:00
|
|
|
void
|
|
|
|
ircd::m::event::conforms::operator|=(const code &code)
|
|
|
|
&
|
|
|
|
{
|
|
|
|
set(code);
|
|
|
|
}
|
|
|
|
|
2018-02-21 03:09:39 +01:00
|
|
|
void
|
2018-02-21 22:43:33 +01:00
|
|
|
ircd::m::event::conforms::del(const code &code)
|
2018-02-21 03:09:39 +01:00
|
|
|
{
|
|
|
|
report &= ~(1UL << code);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-02-21 22:43:33 +01:00
|
|
|
ircd::m::event::conforms::set(const code &code)
|
2018-02-21 03:09:39 +01:00
|
|
|
{
|
|
|
|
report |= (1UL << code);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
2018-02-21 22:43:33 +01:00
|
|
|
ircd::m::event::conforms::string(const mutable_buffer &out)
|
2018-02-21 03:09:39 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
mutable_buffer buf{out};
|
|
|
|
for(uint64_t i(0); i < num_of<code>(); ++i)
|
|
|
|
{
|
|
|
|
if(!has(code(i)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(begin(buf) != begin(out))
|
|
|
|
consume(buf, copy(buf, " "_sv));
|
|
|
|
|
2018-02-22 00:00:02 +01:00
|
|
|
consume(buf, copy(buf, m::reflect(code(i))));
|
2018-02-21 03:09:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return { data(out), begin(buf) };
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-02-21 22:43:33 +01:00
|
|
|
ircd::m::event::conforms::has(const code &code)
|
2018-02-21 03:09:39 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return report & (1UL << code);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-02-21 22:43:33 +01:00
|
|
|
ircd::m::event::conforms::has(const uint &code)
|
2018-02-21 03:09:39 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return (report & (1UL << code)) == code;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-02-21 22:43:33 +01:00
|
|
|
ircd::m::event::conforms::operator!()
|
2018-02-21 03:09:39 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return clean();
|
|
|
|
}
|
|
|
|
|
2018-02-21 22:43:33 +01:00
|
|
|
ircd::m::event::conforms::operator bool()
|
2018-02-21 03:09:39 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return !clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-02-21 22:43:33 +01:00
|
|
|
ircd::m::event::conforms::clean()
|
2018-02-21 03:09:39 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return report == 0;
|
|
|
|
}
|