diff --git a/include/ircd/m/v1/v1.h b/include/ircd/m/v1/v1.h index a2805bdcf..5534c0fc7 100644 --- a/include/ircd/m/v1/v1.h +++ b/include/ircd/m/v1/v1.h @@ -11,6 +11,7 @@ #pragma once #define HAVE_IRCD_M_V1_H +#include "version.h" #include "make_join.h" #include "send_join.h" #include "event.h" diff --git a/include/ircd/m/v1/version.h b/include/ircd/m/v1/version.h new file mode 100644 index 000000000..3e8f5bd54 --- /dev/null +++ b/include/ircd/m/v1/version.h @@ -0,0 +1,39 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2018 Jason Volk +// +// 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. + +#pragma once +#define HAVE_IRCD_M_V1_VERSION_H + +namespace ircd::m::v1 +{ + struct version; +}; + +struct ircd::m::v1::version +:server::request +{ + struct opts; + + operator json::object() const + { + return json::object{in.content}; + } + + version(const mutable_buffer &, opts); +}; + +struct ircd::m::v1::version::opts +{ + net::hostport remote; + m::request request; + server::out out; + server::in in; + const struct server::request::opts *sopts {nullptr}; +}; diff --git a/ircd/m/v1.cc b/ircd/m/v1.cc index f494d4bfe..f6d78e702 100644 --- a/ircd/m/v1.cc +++ b/ircd/m/v1.cc @@ -494,3 +494,46 @@ ircd::m::v1::make_join::make_join(const room::id &room_id, }()} { } + +/////////////////////////////////////////////////////////////////////////////// +// +// v1/version.h +// + +ircd::m::v1::version::version(const mutable_buffer &buf, + opts opts) +:server::request{[&] +{ + assert(!!opts.remote); + + if(!defined(json::get<"origin"_>(opts.request))) + json::get<"origin"_>(opts.request) = my_host(); + + if(!defined(json::get<"destination"_>(opts.request))) + json::get<"destination"_>(opts.request) = host(opts.remote); + + if(!defined(json::get<"uri"_>(opts.request))) + json::get<"uri"_>(opts.request) = "/_matrix/federation/v1/version"; + + json::get<"method"_>(opts.request) = "GET"; + opts.out.head = opts.request(buf); + + if(!size(opts.in)) + { + const auto in_max + { + std::max(ssize_t(size(buf) - size(opts.out.head)), ssize_t(0)) + }; + + assert(in_max >= ssize_t(size(buf) / 2)); + opts.in.head = { data(buf) + size(opts.out.head), size_t(in_max) }; + opts.in.content = opts.in.head; + } + + return server::request + { + opts.remote, std::move(opts.out), std::move(opts.in), opts.sopts + }; +}()} +{ +}