2017-12-12 21:33:14 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Charybdis Development Team
|
|
|
|
* Copyright (C) 2016 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ircd/m/m.h>
|
|
|
|
|
|
|
|
ircd::database *
|
|
|
|
ircd::m::event::events
|
|
|
|
{};
|
|
|
|
|
2018-01-20 14:48:39 +01:00
|
|
|
ircd::m::id::event
|
|
|
|
ircd::m::event_id(const event &event,
|
|
|
|
id::event::buf &buf)
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
2018-01-20 14:48:39 +01:00
|
|
|
const json::strung preimage
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
2018-01-20 14:48:39 +01:00
|
|
|
event
|
2017-12-12 21:33:14 +01:00
|
|
|
};
|
|
|
|
|
2018-01-20 14:48:39 +01:00
|
|
|
const fixed_buffer<const_raw_buffer, sha256::digest_size> hash
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
2018-01-20 14:48:39 +01:00
|
|
|
sha256{const_buffer{preimage}}
|
2017-12-12 21:33:14 +01:00
|
|
|
};
|
|
|
|
|
2018-01-20 14:48:39 +01:00
|
|
|
return event_id(event, buf, hash);
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
2018-01-20 14:48:39 +01:00
|
|
|
ircd::m::id::event
|
|
|
|
ircd::m::event_id(const event &event,
|
|
|
|
id::event::buf &buf,
|
|
|
|
const const_raw_buffer &hash)
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
2018-01-20 14:48:39 +01:00
|
|
|
char readable[b58encode_size(sha256::digest_size)];
|
|
|
|
return id::event
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
2018-01-20 14:48:39 +01:00
|
|
|
buf, b58encode(readable, hash), my_host()
|
2017-12-12 21:33:14 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-20 14:48:39 +01:00
|
|
|
ircd::m::id::event
|
|
|
|
ircd::m::event_id(const event &event)
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
2018-01-20 14:48:39 +01:00
|
|
|
return at<"event_id"_>(event);
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::degree(const event &event)
|
|
|
|
{
|
|
|
|
return degree(event::prev{event});
|
|
|
|
}
|
|
|
|
|
|
|
|
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 std::function<void (const event::id &)> &closure)
|
|
|
|
{
|
|
|
|
json::for_each(prev, [&closure]
|
|
|
|
(const auto &key, const json::array &prevs)
|
|
|
|
{
|
|
|
|
for(const json::array &prev : prevs)
|
|
|
|
{
|
|
|
|
const event::id &id{unquote(prev[0])};
|
|
|
|
closure(id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::m::pretty(const event::prev &prev)
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
std::stringstream s;
|
|
|
|
pubsetbuf(s, ret, 2048);
|
|
|
|
|
|
|
|
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)
|
|
|
|
out("auth_event", unquote(auth_event[0]));
|
|
|
|
|
|
|
|
const auto &prev_states{json::get<"prev_state"_>(prev)};
|
|
|
|
for(const json::array prev_state : prev_states)
|
|
|
|
out("prev_state", unquote(prev_state[0]));
|
|
|
|
|
|
|
|
const auto &prev_events{json::get<"prev_events"_>(prev)};
|
|
|
|
for(const json::array prev_event : prev_events)
|
|
|
|
out("prev_event", unquote(prev_event[0]));
|
|
|
|
|
|
|
|
resizebuf(s, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::m::pretty_oneline(const event::prev &prev)
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
std::stringstream s;
|
|
|
|
pubsetbuf(s, ret, 1024);
|
|
|
|
|
|
|
|
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 << "] ";
|
|
|
|
|
|
|
|
resizebuf(s, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::m::pretty(const event &event)
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
std::stringstream s;
|
|
|
|
pubsetbuf(s, ret, 2048);
|
|
|
|
|
|
|
|
const auto out{[&s]
|
|
|
|
(const string_view &key, auto&& val)
|
|
|
|
{
|
|
|
|
if(json::defined(val))
|
|
|
|
s << std::setw(16) << std::right << key << ": " << val << std::endl;
|
|
|
|
}};
|
|
|
|
|
|
|
|
const string_view top_keys[]
|
|
|
|
{
|
|
|
|
"origin",
|
|
|
|
"event_id",
|
|
|
|
"room_id",
|
|
|
|
"sender",
|
|
|
|
"type",
|
|
|
|
"depth",
|
|
|
|
"state_key",
|
|
|
|
"membership",
|
|
|
|
};
|
|
|
|
|
|
|
|
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];
|
|
|
|
s << std::setw(16) << std::right << "origin_server_ts" << ": "
|
|
|
|
<< timef(buf, ts / 1000L, localtime)
|
|
|
|
<< " (" << ts << ")"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
const auto &hashes{json::get<"hashes"_>(event)};
|
|
|
|
for(const auto &hash : hashes)
|
|
|
|
{
|
|
|
|
s << std::setw(16) << std::right << "[hash]" << ": "
|
|
|
|
<< hash.first
|
|
|
|
//<< " "
|
|
|
|
//<< hash.second
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &signatures{json::get<"signatures"_>(event)};
|
|
|
|
for(const auto &signature : signatures)
|
|
|
|
{
|
|
|
|
s << std::setw(16) << std::right << "[signature]" << ": "
|
|
|
|
<< signature.first << " ";
|
|
|
|
|
|
|
|
for(const auto &key : json::object{signature.second})
|
|
|
|
s << key.first << " ";
|
|
|
|
|
|
|
|
s << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
const json::object &contents{json::get<"content"_>(event)};
|
|
|
|
if(!contents.empty())
|
|
|
|
{
|
|
|
|
s << std::setw(16) << std::right << "[content]" << ": ";
|
|
|
|
for(const auto &content : contents)
|
|
|
|
s << content.first << ", ";
|
|
|
|
s << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &auth_events{json::get<"auth_events"_>(event)};
|
|
|
|
for(const json::array auth_event : auth_events)
|
|
|
|
out("[auth_event]", unquote(auth_event[0]));
|
|
|
|
|
|
|
|
const auto &prev_states{json::get<"prev_state"_>(event)};
|
|
|
|
for(const json::array prev_state : prev_states)
|
|
|
|
out("[prev_state]", unquote(prev_state[0]));
|
|
|
|
|
|
|
|
const auto &prev_events{json::get<"prev_events"_>(event)};
|
|
|
|
for(const json::array prev_event : prev_events)
|
|
|
|
out("[prev_event]", unquote(prev_event[0]));
|
|
|
|
|
|
|
|
resizebuf(s, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::m::pretty_oneline(const event &event)
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
std::stringstream s;
|
|
|
|
pubsetbuf(s, ret, 1024);
|
|
|
|
|
|
|
|
const auto out{[&s]
|
|
|
|
(const string_view &key, auto&& val)
|
|
|
|
{
|
|
|
|
if(defined(val))
|
|
|
|
s << val << " ";
|
|
|
|
else
|
|
|
|
s << "* ";
|
|
|
|
}};
|
|
|
|
|
|
|
|
const string_view top_keys[]
|
|
|
|
{
|
|
|
|
"origin",
|
|
|
|
"event_id",
|
|
|
|
"room_id",
|
|
|
|
"sender",
|
|
|
|
"depth",
|
|
|
|
};
|
|
|
|
|
|
|
|
s << ':';
|
|
|
|
json::for_each(event, top_keys, out);
|
|
|
|
|
|
|
|
const auto &auth_events{json::get<"auth_events"_>(event)};
|
|
|
|
s << "pa:" << auth_events.count() << " ";
|
|
|
|
|
|
|
|
const auto &prev_states{json::get<"prev_state"_>(event)};
|
|
|
|
s << "ps:" << prev_states.count() << " ";
|
|
|
|
|
|
|
|
const auto &prev_events{json::get<"prev_events"_>(event)};
|
|
|
|
s << "pe:" << prev_events.count() << " ";
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
const json::object &contents{json::get<"content"_>(event)};
|
|
|
|
if(!contents.empty())
|
|
|
|
{
|
|
|
|
s << "+" << string_view{contents}.size() << " bytes :";
|
|
|
|
for(const auto &content : contents)
|
|
|
|
s << content.first << " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
resizebuf(s, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-01-20 14:48:39 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// event
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::event::event(const id &id,
|
|
|
|
const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
fetch tab
|
|
|
|
{
|
|
|
|
id, buf
|
|
|
|
};
|
|
|
|
|
|
|
|
new (this) event{tab};
|
|
|
|
*/
|
|
|
|
}
|