2019-09-27 04:00:19 +02: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.
|
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::m::room::origins::random(const mutable_buffer &buf,
|
|
|
|
const closure_bool &proffer)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
string_view ret;
|
|
|
|
const auto closure{[&buf, &proffer, &ret]
|
|
|
|
(const string_view &origin)
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
ret = { data(buf), copy(buf, origin) };
|
|
|
|
}};
|
2019-09-27 04:00:19 +02:00
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
random(closure, proffer);
|
2019-09-27 04:00:19 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2019-09-27 04:28:25 +02:00
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::origins::random(const closure &view,
|
|
|
|
const closure_bool &proffer)
|
|
|
|
const
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
return random(*this, view, proffer);
|
2019-09-27 04:00:19 +02:00
|
|
|
}
|
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
bool
|
|
|
|
ircd::m::room::origins::random(const origins &origins,
|
|
|
|
const closure &view,
|
|
|
|
const closure_bool &proffer)
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
bool ret{false};
|
|
|
|
const size_t max
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
origins.count()
|
2019-09-27 04:00:19 +02:00
|
|
|
};
|
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
if(unlikely(!max))
|
|
|
|
return ret;
|
2019-09-27 04:00:19 +02:00
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
auto select
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
ssize_t(rand::integer(0, max - 1))
|
2019-09-27 04:00:19 +02:00
|
|
|
};
|
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
const closure_bool closure{[&proffer, &view, &select]
|
|
|
|
(const string_view &origin)
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
if(select-- > 0)
|
|
|
|
return true;
|
2019-09-27 04:00:19 +02:00
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
// Test if this random selection is "ok" e.g. the callback allows the
|
|
|
|
// user to test a blacklist for this origin. Skip to next if not.
|
|
|
|
if(proffer && !proffer(origin))
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
++select;
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-27 04:00:19 +02:00
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
view(origin);
|
|
|
|
return false;
|
2019-09-27 04:00:19 +02:00
|
|
|
}};
|
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
const auto iteration{[&origins, &closure, &ret]
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
ret = !origins.for_each(closure);
|
|
|
|
}};
|
2019-09-27 04:00:19 +02:00
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
// Attempt select on first iteration
|
|
|
|
iteration();
|
2019-09-27 04:00:19 +02:00
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
// If nothing was OK between the random int and the end of the iteration
|
|
|
|
// then start again and pick the first OK.
|
|
|
|
if(!ret && select >= 0)
|
|
|
|
iteration();
|
2019-09-27 04:00:19 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2019-09-27 04:28:25 +02:00
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::origins::empty()
|
|
|
|
const
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
return for_each(closure_bool{[]
|
|
|
|
(const string_view &)
|
|
|
|
{
|
|
|
|
// return false to break and return false.
|
|
|
|
return false;
|
|
|
|
}});
|
2019-09-27 04:00:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2019-09-27 04:28:25 +02:00
|
|
|
ircd::m::room::origins::count()
|
2019-09-27 04:00:19 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t ret{0};
|
2019-09-27 04:28:25 +02:00
|
|
|
for_each([&ret](const string_view &)
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2019-09-27 04:28:25 +02:00
|
|
|
ircd::m::room::origins::count_error()
|
2019-09-27 04:00:19 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t ret{0};
|
2019-09-27 04:28:25 +02:00
|
|
|
for_each([&ret](const string_view &server)
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
ret += !ircd::empty(server::errmsg(server));
|
2019-09-27 04:00:19 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2019-09-27 04:28:25 +02:00
|
|
|
ircd::m::room::origins::count_online()
|
2019-09-27 04:00:19 +02:00
|
|
|
const
|
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
ssize_t ret
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
0 - ssize_t(count_error())
|
|
|
|
};
|
2019-09-27 04:00:19 +02:00
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
for_each([&ret](const string_view &hostport)
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
ret += bool(server::exists(hostport));
|
2019-09-27 04:00:19 +02:00
|
|
|
});
|
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
assert(ret >= 0L);
|
|
|
|
return std::max(ret, 0L);
|
2019-09-27 04:00:19 +02:00
|
|
|
}
|
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
/// Tests if argument is the only origin in the room.
|
|
|
|
/// If a zero or more than one origins exist, returns false. If the only origin
|
|
|
|
/// in the room is the argument origin, returns true.
|
2019-09-27 04:00:19 +02:00
|
|
|
bool
|
2019-09-27 04:28:25 +02:00
|
|
|
ircd::m::room::origins::only(const string_view &origin)
|
|
|
|
const
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
ushort ret{2};
|
|
|
|
for_each(closure_bool{[&ret, &origin]
|
|
|
|
(const string_view &origin_) -> bool
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
if(origin == origin_)
|
|
|
|
ret = 1;
|
|
|
|
else
|
|
|
|
ret = 0;
|
2019-09-27 04:00:19 +02:00
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
return ret;
|
|
|
|
}});
|
2019-09-27 04:00:19 +02:00
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
return ret == 1;
|
2019-09-27 04:00:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-09-27 04:28:25 +02:00
|
|
|
ircd::m::room::origins::has(const string_view &origin)
|
2019-09-27 04:00:19 +02:00
|
|
|
const
|
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
db::domain &index
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
dbs::room_joined
|
|
|
|
};
|
2019-09-27 04:00:19 +02:00
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
char querybuf[dbs::ROOM_JOINED_KEY_MAX_SIZE];
|
|
|
|
const auto query
|
|
|
|
{
|
|
|
|
dbs::room_joined_key(querybuf, room.room_id, origin)
|
|
|
|
};
|
2019-09-27 04:00:19 +02:00
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
auto it
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
index.begin(query)
|
|
|
|
};
|
2019-09-27 04:00:19 +02:00
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
if(!it)
|
|
|
|
return false;
|
2019-09-27 04:00:19 +02:00
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
const string_view &key
|
|
|
|
{
|
|
|
|
lstrip(it->first, "\0"_sv)
|
|
|
|
};
|
2019-09-27 04:00:19 +02:00
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
const string_view &key_origin
|
|
|
|
{
|
|
|
|
std::get<0>(dbs::room_joined_key(key))
|
|
|
|
};
|
|
|
|
|
|
|
|
return key_origin == origin;
|
2019-09-27 04:00:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-27 04:28:25 +02:00
|
|
|
ircd::m::room::origins::for_each(const closure &view)
|
2019-09-27 04:00:19 +02:00
|
|
|
const
|
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
for_each(closure_bool{[&view]
|
|
|
|
(const string_view &origin)
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
view(origin);
|
2019-09-27 04:00:19 +02:00
|
|
|
return true;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-09-27 04:28:25 +02:00
|
|
|
ircd::m::room::origins::for_each(const closure_bool &view)
|
2019-09-27 04:00:19 +02:00
|
|
|
const
|
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
string_view last;
|
|
|
|
char lastbuf[rfc1035::NAME_BUFSIZE];
|
|
|
|
return _for_each(*this, [&last, &lastbuf, &view]
|
|
|
|
(const string_view &key)
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
const string_view &origin
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
std::get<0>(dbs::room_joined_key(key))
|
2019-09-27 04:00:19 +02:00
|
|
|
};
|
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
if(origin == last)
|
|
|
|
return true;
|
2019-09-27 04:00:19 +02:00
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
if(!view(origin))
|
|
|
|
return false;
|
2019-09-27 04:00:19 +02:00
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
last = { lastbuf, copy(lastbuf, origin) };
|
|
|
|
return true;
|
2019-09-27 04:00:19 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-09-27 04:28:25 +02:00
|
|
|
ircd::m::room::origins::_for_each(const origins &origins,
|
|
|
|
const closure_bool &view)
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
db::domain &index
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
dbs::room_joined
|
2019-09-27 04:00:19 +02:00
|
|
|
};
|
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
auto it
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
index.begin(origins.room.room_id)
|
2019-09-27 04:00:19 +02:00
|
|
|
};
|
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
for(; bool(it); ++it)
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
const string_view &key
|
2019-09-27 04:00:19 +02:00
|
|
|
{
|
2019-09-27 04:28:25 +02:00
|
|
|
lstrip(it->first, "\0"_sv)
|
2019-09-27 04:00:19 +02:00
|
|
|
};
|
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
if(!view(key))
|
|
|
|
return false;
|
2019-09-27 04:00:19 +02:00
|
|
|
}
|
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
return true;
|
2019-09-27 04:00:19 +02:00
|
|
|
}
|