2018-03-04 16:15:50 +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.
|
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
2019-02-11 23:13:13 +01:00
|
|
|
static void handle_edu_m_presence_object(const m::event &, const m::presence &edu);
|
|
|
|
static void handle_edu_m_presence(const m::event &, m::vm::eval &);
|
|
|
|
|
2018-03-04 16:15:50 +01:00
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Matrix Presence"
|
|
|
|
};
|
|
|
|
|
2019-03-21 04:06:09 +01:00
|
|
|
conf::item<bool>
|
|
|
|
federation_incoming
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.presence.federation.incoming" },
|
|
|
|
{ "default", true },
|
|
|
|
};
|
|
|
|
|
2019-03-10 00:53:43 +01:00
|
|
|
log::log
|
|
|
|
presence_log
|
|
|
|
{
|
|
|
|
"m.presence"
|
|
|
|
};
|
|
|
|
|
2019-02-11 23:13:13 +01:00
|
|
|
extern const string_view
|
|
|
|
valid_states[];
|
2018-03-07 20:06:17 +01:00
|
|
|
|
2018-09-14 04:11:40 +02:00
|
|
|
const m::hookfn<m::vm::eval &>
|
2018-03-07 20:06:17 +01:00
|
|
|
_m_presence_eval
|
|
|
|
{
|
|
|
|
handle_edu_m_presence,
|
|
|
|
{
|
|
|
|
{ "_site", "vm.eval" },
|
|
|
|
{ "type", "m.presence" },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2018-09-14 04:11:40 +02:00
|
|
|
handle_edu_m_presence(const m::event &event,
|
|
|
|
m::vm::eval &eval)
|
2018-04-16 04:57:32 +02:00
|
|
|
try
|
2018-03-07 20:06:17 +01:00
|
|
|
{
|
2019-03-21 04:06:09 +01:00
|
|
|
if(!federation_incoming)
|
|
|
|
return;
|
|
|
|
|
2018-04-07 17:30:40 +02:00
|
|
|
if(m::my_host(at<"origin"_>(event)))
|
|
|
|
return;
|
|
|
|
|
2018-03-07 20:06:17 +01:00
|
|
|
const json::object &content
|
|
|
|
{
|
|
|
|
at<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::array &push
|
|
|
|
{
|
|
|
|
content.get("push")
|
|
|
|
};
|
|
|
|
|
|
|
|
for(const json::object &presence : push)
|
2019-02-11 23:13:13 +01:00
|
|
|
handle_edu_m_presence_object(event, presence);
|
2018-03-07 20:06:17 +01:00
|
|
|
}
|
2018-04-16 04:57:32 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-04-28 11:47:21 +02:00
|
|
|
log::derror
|
2018-04-16 04:57:32 +02:00
|
|
|
{
|
2018-12-23 02:44:18 +01:00
|
|
|
m::log, "Presence from %s :%s",
|
2018-04-16 04:57:32 +02:00
|
|
|
json::get<"origin"_>(event),
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|
2018-03-07 20:06:17 +01:00
|
|
|
|
|
|
|
void
|
2019-02-11 23:13:13 +01:00
|
|
|
handle_edu_m_presence_object(const m::event &event,
|
|
|
|
const m::presence &object)
|
2018-04-16 04:57:32 +02:00
|
|
|
try
|
2018-03-07 20:06:17 +01:00
|
|
|
{
|
|
|
|
const m::user::id &user_id
|
|
|
|
{
|
|
|
|
at<"user_id"_>(object)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(user_id.host() != at<"origin"_>(event))
|
|
|
|
{
|
2018-04-09 23:20:13 +02:00
|
|
|
log::dwarning
|
2018-03-07 20:06:17 +01:00
|
|
|
{
|
2018-12-23 02:44:18 +01:00
|
|
|
m::log, "Ignoring %s from %s for user %s",
|
2018-03-07 20:06:17 +01:00
|
|
|
at<"type"_>(event),
|
|
|
|
at<"origin"_>(event),
|
|
|
|
string_view{user_id}
|
|
|
|
};
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-28 00:27:30 +02:00
|
|
|
bool useful{true};
|
2019-01-27 00:24:29 +01:00
|
|
|
const auto closure{[&event, &object, &useful]
|
|
|
|
(const m::event &existing_event)
|
2018-03-07 20:06:17 +01:00
|
|
|
{
|
2019-01-27 00:24:29 +01:00
|
|
|
const json::object &existing_object
|
|
|
|
{
|
|
|
|
json::get<"content"_>(existing_event)
|
|
|
|
};
|
|
|
|
|
2018-09-05 08:05:05 +02:00
|
|
|
// This check shouldn't have to exist. I think this was a result of corrupting
|
|
|
|
// my DB during development and it fails on only one user. Nevertheless, it's
|
|
|
|
// a valid assertion so might as well keep it.
|
|
|
|
if(unlikely(json::get<"user_id"_>(object) != unquote(existing_object.get("user_id"))))
|
|
|
|
{
|
|
|
|
//log::critical("%s != %s", json::get<"user_id"_>(object), unquote(existing_object.get("user_id")));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-28 00:27:30 +02:00
|
|
|
assert(json::get<"user_id"_>(object) == unquote(existing_object.get("user_id")));
|
|
|
|
|
|
|
|
const auto &prev_active_ago
|
|
|
|
{
|
|
|
|
existing_object.get<time_t>("last_active_ago")
|
|
|
|
};
|
|
|
|
|
|
|
|
const time_t &now_active_ago
|
|
|
|
{
|
|
|
|
json::get<"last_active_ago"_>(object)
|
|
|
|
};
|
|
|
|
|
|
|
|
const time_t &prev_active_absolute
|
|
|
|
{
|
|
|
|
json::get<"origin_server_ts"_>(existing_event) - prev_active_ago
|
|
|
|
};
|
|
|
|
|
|
|
|
const time_t &now_active_absolute
|
|
|
|
{
|
|
|
|
json::get<"origin_server_ts"_>(event) - now_active_ago
|
|
|
|
};
|
|
|
|
|
2018-05-29 21:11:36 +02:00
|
|
|
// First way to filter out the synapse presence spam bug is seeing
|
|
|
|
// if the update is older than the last update.
|
|
|
|
if(now_active_absolute < prev_active_absolute)
|
|
|
|
useful = false;
|
|
|
|
else if(json::get<"presence"_>(object) != unquote(existing_object.get("presence")))
|
|
|
|
useful = true;
|
|
|
|
else if(json::get<"currently_active"_>(object) != existing_object.get<bool>("currently_active"))
|
|
|
|
useful = true;
|
|
|
|
else if(json::get<"currently_active"_>(object))
|
|
|
|
useful = true;
|
|
|
|
else
|
|
|
|
useful = false;
|
2019-01-27 00:24:29 +01:00
|
|
|
}};
|
|
|
|
|
|
|
|
static const m::event::fetch::opts fopts
|
|
|
|
{
|
|
|
|
m::event::keys::include {"content", "origin_server_ts"}
|
|
|
|
};
|
2018-04-28 00:27:30 +02:00
|
|
|
|
2019-01-27 00:24:29 +01:00
|
|
|
m::presence::get(std::nothrow, user_id, closure, &fopts);
|
2019-03-10 00:53:43 +01:00
|
|
|
|
2018-04-28 00:27:30 +02:00
|
|
|
if(!useful)
|
2019-01-27 01:38:09 +01:00
|
|
|
{
|
2019-02-07 11:53:57 +01:00
|
|
|
log::debug
|
2019-01-27 01:38:09 +01:00
|
|
|
{
|
2019-03-10 00:53:43 +01:00
|
|
|
presence_log, "presence spam from %s %s is %s and %s %zd seconds ago",
|
2019-01-27 01:38:09 +01:00
|
|
|
at<"origin"_>(event),
|
|
|
|
string_view{user_id},
|
|
|
|
json::get<"currently_active"_>(object)? "active"_sv : "inactive"_sv,
|
|
|
|
json::get<"presence"_>(object),
|
|
|
|
json::get<"last_active_ago"_>(object) / 1000L
|
|
|
|
};
|
2019-02-07 11:53:57 +01:00
|
|
|
|
2018-04-28 00:27:30 +02:00
|
|
|
return;
|
2019-01-27 01:38:09 +01:00
|
|
|
}
|
2018-03-07 20:06:17 +01:00
|
|
|
|
|
|
|
const auto evid
|
|
|
|
{
|
|
|
|
m::presence::set(object)
|
|
|
|
};
|
|
|
|
|
2018-04-12 10:12:02 +02:00
|
|
|
log::info
|
2018-03-07 20:06:17 +01:00
|
|
|
{
|
2019-03-10 00:53:43 +01:00
|
|
|
presence_log, "%s %s is %s and %s %zd seconds ago",
|
2018-03-07 20:06:17 +01:00
|
|
|
at<"origin"_>(event),
|
|
|
|
string_view{user_id},
|
|
|
|
json::get<"currently_active"_>(object)? "active"_sv : "inactive"_sv,
|
2018-04-28 00:27:30 +02:00
|
|
|
json::get<"presence"_>(object),
|
2018-04-12 10:12:02 +02:00
|
|
|
json::get<"last_active_ago"_>(object) / 1000L
|
2018-03-07 20:06:17 +01:00
|
|
|
};
|
|
|
|
}
|
2018-04-16 04:57:32 +02:00
|
|
|
catch(const m::error &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2018-12-23 02:44:18 +01:00
|
|
|
m::log, "Presence from %s :%s :%s",
|
2018-04-16 04:57:32 +02:00
|
|
|
json::get<"origin"_>(event),
|
|
|
|
e.what(),
|
|
|
|
e.content
|
|
|
|
};
|
|
|
|
}
|
2018-05-28 10:31:45 +02:00
|
|
|
|
2019-02-11 23:13:13 +01:00
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::presence::get(const std::nothrow_t,
|
|
|
|
const m::user &user,
|
|
|
|
const m::presence::closure_event &closure,
|
|
|
|
const m::event::fetch::opts *const &fopts_p)
|
2019-01-27 00:24:29 +01:00
|
|
|
{
|
2019-02-11 23:13:13 +01:00
|
|
|
const m::event::idx event_idx
|
2019-01-27 00:24:29 +01:00
|
|
|
{
|
2019-02-11 23:13:13 +01:00
|
|
|
m::presence::get(std::nothrow, user)
|
2019-01-27 00:24:29 +01:00
|
|
|
};
|
|
|
|
|
2019-02-11 23:13:13 +01:00
|
|
|
if(!event_idx)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const auto &fopts
|
2019-01-27 00:24:29 +01:00
|
|
|
{
|
2019-02-11 23:13:13 +01:00
|
|
|
fopts_p? *fopts_p : event::fetch::default_opts
|
2019-01-27 00:24:29 +01:00
|
|
|
};
|
|
|
|
|
2019-02-11 23:13:13 +01:00
|
|
|
const m::event::fetch event
|
2019-01-27 00:24:29 +01:00
|
|
|
{
|
2019-02-11 23:13:13 +01:00
|
|
|
event_idx, std::nothrow, fopts
|
|
|
|
};
|
2019-01-27 00:24:29 +01:00
|
|
|
|
2019-02-11 23:13:13 +01:00
|
|
|
if(event.valid)
|
|
|
|
closure(event);
|
|
|
|
|
|
|
|
return event.valid;
|
2019-01-27 00:24:29 +01:00
|
|
|
}
|
|
|
|
|
2019-02-11 23:13:13 +01:00
|
|
|
m::event::idx
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::presence::get(const std::nothrow_t,
|
|
|
|
const m::user &user)
|
2018-05-28 10:31:45 +02:00
|
|
|
{
|
2019-02-11 23:13:13 +01:00
|
|
|
const m::user::room user_room
|
2018-05-28 10:31:45 +02:00
|
|
|
{
|
2019-02-11 23:13:13 +01:00
|
|
|
user
|
2018-05-28 10:31:45 +02:00
|
|
|
};
|
|
|
|
|
2019-02-11 23:13:13 +01:00
|
|
|
const m::room::state state
|
2018-05-28 10:31:45 +02:00
|
|
|
{
|
2019-02-11 23:13:13 +01:00
|
|
|
user_room
|
2018-05-28 10:31:45 +02:00
|
|
|
};
|
|
|
|
|
2019-02-11 23:13:13 +01:00
|
|
|
m::event::idx ret{0};
|
|
|
|
state.get(std::nothrow, "ircd.presence", "", [&ret]
|
|
|
|
(const m::event::idx &event_idx)
|
|
|
|
{
|
|
|
|
ret = event_idx;
|
|
|
|
});
|
2019-01-27 00:24:29 +01:00
|
|
|
|
2019-02-11 23:13:13 +01:00
|
|
|
return ret;
|
2018-05-28 10:31:45 +02:00
|
|
|
}
|
|
|
|
|
2019-02-11 23:13:13 +01:00
|
|
|
m::event::id::buf
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::presence::set(const m::presence &content)
|
2018-05-28 10:31:45 +02:00
|
|
|
{
|
|
|
|
const m::user user
|
|
|
|
{
|
|
|
|
at<"user_id"_>(content)
|
|
|
|
};
|
|
|
|
|
|
|
|
//TODO: ABA
|
|
|
|
if(!exists(user))
|
|
|
|
create(user.user_id);
|
|
|
|
|
2019-01-16 22:48:28 +01:00
|
|
|
m::vm::copts copts;
|
|
|
|
copts.history = false;
|
2018-05-28 10:31:45 +02:00
|
|
|
const m::user::room user_room
|
|
|
|
{
|
2019-01-16 22:48:28 +01:00
|
|
|
user, &copts
|
2018-05-28 10:31:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
//TODO: ABA
|
|
|
|
return send(user_room, user.user_id, "ircd.presence", "", json::strung{content});
|
|
|
|
}
|
2019-02-11 23:13:13 +01:00
|
|
|
|
|
|
|
const string_view
|
|
|
|
valid_states[]
|
|
|
|
{
|
|
|
|
"online", "offline", "unavailable",
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::presence::valid_state(const string_view &state)
|
|
|
|
{
|
|
|
|
return std::any_of(begin(valid_states), end(valid_states), [&state]
|
|
|
|
(const string_view &valid)
|
|
|
|
{
|
|
|
|
return state == valid;
|
|
|
|
});
|
|
|
|
}
|