diff --git a/include/ircd/m.h b/include/ircd/m.h index feb50643a..3b1f9af70 100644 --- a/include/ircd/m.h +++ b/include/ircd/m.h @@ -35,10 +35,22 @@ namespace m { #include "m/error.h" #include "m/id.h" +#include "m/db.h" #include "m/event.h" #include "m/request.h" +#include "m/accounts.h" #include "m/session.h" namespace ircd { +namespace m { +struct init +{ + db::init db; + + init(); + ~init() noexcept; +}; + +} // namespace m } // namespace ircd diff --git a/modules/client/rooms.h b/include/ircd/m/accounts.h similarity index 79% rename from modules/client/rooms.h rename to include/ircd/m/accounts.h index 84b807bf6..31b4d0eef 100644 --- a/modules/client/rooms.h +++ b/include/ircd/m/accounts.h @@ -1,6 +1,8 @@ /* - * Copyright (C) 2017 Charybdis Development Team - * Copyright (C) 2017 Jason Volk + * charybdis: 21st Century IRC++d + * + * Copyright (C) 2016 Charybdis Development Team + * Copyright (C) 2016 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 @@ -17,14 +19,14 @@ * 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. + * */ -ircd::import_shared rooms_database -{ - "client_rooms", "rooms_database" -}; +#pragma once +#define HAVE_IRCD_M_ACCOUNTS_H -extern ircd::database *const rooms -{ - rooms_database.get() -}; +namespace ircd { +namespace m { + +} // namespace m +} // namespace ircd diff --git a/modules/client/events.h b/include/ircd/m/db.h similarity index 70% rename from modules/client/events.h rename to include/ircd/m/db.h index 14352f322..d12e8ef6e 100644 --- a/modules/client/events.h +++ b/include/ircd/m/db.h @@ -1,6 +1,8 @@ /* - * Copyright (C) 2017 Charybdis Development Team - * Copyright (C) 2017 Jason Volk + * charybdis: 21st Century IRC++d + * + * Copyright (C) 2016 Charybdis Development Team + * Copyright (C) 2016 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 @@ -17,14 +19,26 @@ * 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. + * */ -ircd::import_shared events_database +#pragma once +#define HAVE_IRCD_M_DB_H + +namespace ircd { +namespace m { +namespace db { + +extern database *events; +extern database *accounts; +extern database *rooms; + +struct init { - "client_events", "events_database" + init(); + ~init() noexcept; }; -extern ircd::database *const events -{ - events_database.get() -}; +} // namespace db +} // namespace m +} // namespace ircd diff --git a/modules/Makefile.am b/modules/Makefile.am index 28de69fc3..a6f371ba4 100644 --- a/modules/Makefile.am +++ b/modules/Makefile.am @@ -33,6 +33,18 @@ module_LTLIBRARIES = \ root.la \ ### +# This puts the source in db/ but the installed +# library is client_X.so in the main modules dir. +db_moduledir = @moduledir@ +db_db_events_la_SOURCES = db/events.cc +db_db_accounts_la_SOURCES = db/accounts.cc +db_db_rooms_la_SOURCES = db/rooms.cc +db_module_LTLIBRARIES = \ + db/db_events.la \ + db/db_accounts.la \ + db/db_rooms.la \ + ### + # This puts the source in client/ but the installed # library is client_X.so in the main modules dir. client_moduledir = @moduledir@ diff --git a/modules/client/account.cc b/modules/client/account.cc index 74c5f94de..12b61db75 100644 --- a/modules/client/account.cc +++ b/modules/client/account.cc @@ -21,49 +21,6 @@ using namespace ircd; -const database::descriptor token_descriptor -{ - "token", - "An index of access_token to user_id", - { - // readable key // readable value - typeid(string_view), typeid(string_view) - } -}; - -const database::descriptor account_registered_descriptor -{ - "registered", - "A UNIX epoch timestamp sampled when the account was created.", - { - // readable key // binary value - typeid(string_view), typeid(time_t) - } -}; - -const database::description account_description -{ - { "default" }, - token_descriptor, - account_registered_descriptor, - { "access_token" }, - { "access_token.text" }, - { "password" }, - { "password.text" }, - { "password.hash" }, - { "password.hash.sha256" }, -}; - -std::shared_ptr account_database -{ - std::make_shared("account"s, ""s, account_description) -}; - -extern database *const account -{ - account_database.get() -}; - struct account :resource { diff --git a/modules/client/createroom.cc b/modules/client/createroom.cc index 2477204b2..705d067fb 100644 --- a/modules/client/createroom.cc +++ b/modules/client/createroom.cc @@ -21,16 +21,6 @@ using namespace ircd; -import room_database -{ - "client_room", "room" -}; - -extern database *const room -{ - room_database -}; - resource createroom { "_matrix/client/r0/createRoom", diff --git a/modules/client/events.cc b/modules/client/events.cc index 5ed61fba0..ac4828d19 100644 --- a/modules/client/events.cc +++ b/modules/client/events.cc @@ -21,204 +21,26 @@ using namespace ircd; -const database::descriptor events_type_descriptor -{ - // name - "type", - - // explanation - R"(### protocol note: - - 10.1 - The type of event. This SHOULD be namespaced similar to Java package naming conventions - e.g. 'com.example.subdomain.event.type'. - - 10.4 - MUST NOT exceed 255 bytes. - - ### developer note: - key is event_id - )", - - // typing (key, value) - { - typeid(string_view), typeid(string_view) - } -}; - -const database::descriptor events_content_descriptor -{ - // name - "content", - - // explanation - R"(### protocol note: - - 10.1 - The fields in this object will vary depending on the type of event. When interacting - with the REST API, this is the HTTP body. - - ### developer note: - Since events must not exceed 65 KB the maximum size for the content is the remaining - space after all the other fields for the event are rendered. - - key is event_id - )", - - // typing (key, value) - { - typeid(string_view), typeid(string_view) - } -}; - -const database::descriptor events_room_id_descriptor -{ - // name - "room_id", - - // explanation - R"(### protocol note: - - 10.2 (apropos room events) - Required. The ID of the room associated with this event. - - 10.4 - MUST NOT exceed 255 bytes. - - ### developer note: - key is event_id - )", - - // typing (key, value) - { - typeid(string_view), typeid(string_view) - } -}; - -const database::descriptor events_sender_descriptor -{ - // name - "sender", - - // explanation - R"(### protocol note: - - 10.2 (apropos room events) - Required. Contains the fully-qualified ID of the user who sent this event. - - 10.4 - MUST NOT exceed 255 bytes. - - ### developer note: - key is event_id - )", - - // typing (key, value) - { - typeid(string_view), typeid(string_view) - } -}; - -const database::descriptor events_state_key_descriptor -{ - // name - "state_key", - - // explanation - R"(### protocol note: - - 10.3 (apropos room state events) - A unique key which defines the overwriting semantics for this piece of room state. - This value is often a zero-length string. The presence of this key makes this event a - State Event. The key MUST NOT start with '_'. - - 10.4 - MUST NOT exceed 255 bytes. - - ### developer note: - key is event_id - )", - - // typing (key, value) - { - typeid(string_view), typeid(string_view) - } -}; - -const database::descriptor events_origin_descriptor -{ - // name - "origin", - - // explanation - R"(### protocol note: - - FEDERATION 4.1 - DNS name of homeserver that created this PDU - - ### developer note: - key is event_id - )", - - // typing (key, value) - { - typeid(string_view), typeid(string_view) - } -}; - -const database::descriptor events_origin_server_ts_descriptor -{ - // name - "origin_server_ts", - - // explanation - R"(### protocol note: - - FEDERATION 4.1 - Timestamp in milliseconds on origin homeserver when this PDU was created. - - ### developer note: - key is event_id - value is a machine integer (binary) - - TODO: consider unsigned rather than time_t because of millisecond precision - - )", - - // typing (key, value) - { - typeid(string_view), typeid(time_t) - } -}; - -const database::description events_description -{ - { "default" }, - events_type_descriptor, - events_content_descriptor, - events_room_id_descriptor, - events_sender_descriptor, - events_state_key_descriptor, - events_origin_descriptor, - events_origin_server_ts_descriptor, -}; - -std::shared_ptr events_database -{ - std::make_shared("events"s, ""s, events_description) -}; - -extern database *const events -{ - events_database.get() -}; - resource events_resource { "_matrix/client/r0/events", "Events (6.2.3) (10.x)" }; +resource::response +get_events(client &client, const resource::request &request) +{ + return resource::response + { + client, json::object {} + }; +} + +resource::method method_get +{ + events_resource, "GET", get_events +}; + mapi::header IRCD_MODULE { "registers the resource 'client/events' and hosts the events database" diff --git a/modules/client/login.cc b/modules/client/login.cc index d0c274e81..f5918d6f9 100644 --- a/modules/client/login.cc +++ b/modules/client/login.cc @@ -19,8 +19,6 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "account.h" - using namespace ircd; resource login_resource @@ -36,8 +34,8 @@ const auto home_server "cdc.z" }; -using object = db::object; -template using value = db::value; +using object = db::object; +template using value = db::value; resource::response post_login_password(client &client, const resource::request &request) diff --git a/modules/client/logout.cc b/modules/client/logout.cc index e517a8acc..c618b0261 100644 --- a/modules/client/logout.cc +++ b/modules/client/logout.cc @@ -19,12 +19,10 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "account.h" - using namespace ircd; -using object = db::object; -template using value = db::value; +using object = db::object; +template using value = db::value; resource logout_resource { diff --git a/modules/client/publicrooms.cc b/modules/client/publicrooms.cc index f6b9bbf68..c4ac2576e 100644 --- a/modules/client/publicrooms.cc +++ b/modules/client/publicrooms.cc @@ -19,8 +19,6 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "rooms.h" - using namespace ircd; resource publicrooms_resource diff --git a/modules/client/register.cc b/modules/client/register.cc index 8e9a8c312..9be897f9f 100644 --- a/modules/client/register.cc +++ b/modules/client/register.cc @@ -19,12 +19,10 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "account.h" - using namespace ircd; -using object = db::object; -template using value = db::value; +using object = db::object; +template using value = db::value; const auto home_server { diff --git a/modules/client/rooms.cc b/modules/client/rooms.cc index eec8870c7..003a3168c 100644 --- a/modules/client/rooms.cc +++ b/modules/client/rooms.cc @@ -21,44 +21,6 @@ using namespace ircd; -const database::descriptor rooms_head_descriptor -{ - // name - "head", - - // notes - R"( - ### developer note: - - The latest event for a room. - - key is room_id - value is event_id - - )", - - // typing for key and value - { - typeid(string_view), typeid(string_view) - } -}; - -const database::description rooms_description -{ - { "default" }, - rooms_head_descriptor, -}; - -std::shared_ptr rooms_database -{ - std::make_shared("room"s, ""s, rooms_description) -}; - -extern database *const room -{ - rooms_database.get() -}; - struct room :resource { @@ -72,5 +34,5 @@ rooms_resource mapi::header IRCD_MODULE { - "registers the resource 'client/rooms' and hosts the rooms database" + "registers the resource 'client/rooms'" }; diff --git a/modules/client/sync.cc b/modules/client/sync.cc index 814d7be7d..3ac4a68fc 100644 --- a/modules/client/sync.cc +++ b/modules/client/sync.cc @@ -19,12 +19,10 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "account.h" - using namespace ircd; -using object = db::object; -template using value = db::value; +//using object = db::object; +//template using value = db::value; resource sync_resource { diff --git a/modules/client/user.cc b/modules/client/user.cc index 9e587d198..9e5c3868b 100644 --- a/modules/client/user.cc +++ b/modules/client/user.cc @@ -19,8 +19,6 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "account.h" - using namespace ircd; resource user_resource diff --git a/modules/db/accounts.cc b/modules/db/accounts.cc new file mode 100644 index 000000000..88702ce46 --- /dev/null +++ b/modules/db/accounts.cc @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2017 Charybdis Development Team + * Copyright (C) 2017 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. + * + * 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. + */ + +using namespace ircd; + +const database::descriptor accounts_token_descriptor +{ + "token", + "An index of access_token to user_id", + { + // readable key // readable value + typeid(string_view), typeid(string_view) + } +}; + +const database::descriptor accounts_registered_descriptor +{ + "registered", + "A UNIX epoch timestamp sampled when the account was created.", + { + // readable key // binary value + typeid(string_view), typeid(time_t) + } +}; + +const database::description accounts_description +{ + { "default" }, + accounts_token_descriptor, + accounts_registered_descriptor, + { "access_token" }, + { "access_token.text" }, + { "password" }, + { "password.text" }, + { "password.hash" }, + { "password.hash.sha256" }, +}; + +std::shared_ptr accounts_database +{ + std::make_shared("accounts"s, ""s, accounts_description) +}; + +mapi::header IRCD_MODULE +{ + "Hosts the 'accounts' database" +}; + diff --git a/modules/db/events.cc b/modules/db/events.cc new file mode 100644 index 000000000..0e48795d5 --- /dev/null +++ b/modules/db/events.cc @@ -0,0 +1,214 @@ +/* + * Copyright (C) 2017 Charybdis Development Team + * Copyright (C) 2017 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. + * + * 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. + */ + +using namespace ircd; + +const database::descriptor events_type_descriptor +{ + // name + "type", + + // explanation + R"(### protocol note: + + 10.1 + The type of event. This SHOULD be namespaced similar to Java package naming conventions + e.g. 'com.example.subdomain.event.type'. + + 10.4 + MUST NOT exceed 255 bytes. + + ### developer note: + key is event_id + )", + + // typing (key, value) + { + typeid(string_view), typeid(string_view) + } +}; + +const database::descriptor events_content_descriptor +{ + // name + "content", + + // explanation + R"(### protocol note: + + 10.1 + The fields in this object will vary depending on the type of event. When interacting + with the REST API, this is the HTTP body. + + ### developer note: + Since events must not exceed 65 KB the maximum size for the content is the remaining + space after all the other fields for the event are rendered. + + key is event_id + )", + + // typing (key, value) + { + typeid(string_view), typeid(string_view) + } +}; + +const database::descriptor events_room_id_descriptor +{ + // name + "room_id", + + // explanation + R"(### protocol note: + + 10.2 (apropos room events) + Required. The ID of the room associated with this event. + + 10.4 + MUST NOT exceed 255 bytes. + + ### developer note: + key is event_id + )", + + // typing (key, value) + { + typeid(string_view), typeid(string_view) + } +}; + +const database::descriptor events_sender_descriptor +{ + // name + "sender", + + // explanation + R"(### protocol note: + + 10.2 (apropos room events) + Required. Contains the fully-qualified ID of the user who sent this event. + + 10.4 + MUST NOT exceed 255 bytes. + + ### developer note: + key is event_id + )", + + // typing (key, value) + { + typeid(string_view), typeid(string_view) + } +}; + +const database::descriptor events_state_key_descriptor +{ + // name + "state_key", + + // explanation + R"(### protocol note: + + 10.3 (apropos room state events) + A unique key which defines the overwriting semantics for this piece of room state. + This value is often a zero-length string. The presence of this key makes this event a + State Event. The key MUST NOT start with '_'. + + 10.4 + MUST NOT exceed 255 bytes. + + ### developer note: + key is event_id + )", + + // typing (key, value) + { + typeid(string_view), typeid(string_view) + } +}; + +const database::descriptor events_origin_descriptor +{ + // name + "origin", + + // explanation + R"(### protocol note: + + FEDERATION 4.1 + DNS name of homeserver that created this PDU + + ### developer note: + key is event_id + )", + + // typing (key, value) + { + typeid(string_view), typeid(string_view) + } +}; + +const database::descriptor events_origin_server_ts_descriptor +{ + // name + "origin_server_ts", + + // explanation + R"(### protocol note: + + FEDERATION 4.1 + Timestamp in milliseconds on origin homeserver when this PDU was created. + + ### developer note: + key is event_id + value is a machine integer (binary) + + TODO: consider unsigned rather than time_t because of millisecond precision + + )", + + // typing (key, value) + { + typeid(string_view), typeid(time_t) + } +}; + +const database::description events_description +{ + { "default" }, + events_type_descriptor, + events_content_descriptor, + events_room_id_descriptor, + events_sender_descriptor, + events_state_key_descriptor, + events_origin_descriptor, + events_origin_server_ts_descriptor, +}; + +std::shared_ptr events_database +{ + std::make_shared("events"s, ""s, events_description) +}; + +mapi::header IRCD_MODULE +{ + "Hosts the 'events' database" +}; diff --git a/modules/client/account.h b/modules/db/rooms.cc similarity index 66% rename from modules/client/account.h rename to modules/db/rooms.cc index 96f4f8eb4..717458c13 100644 --- a/modules/client/account.h +++ b/modules/db/rooms.cc @@ -1,4 +1,4 @@ -/* +/* * Copyright (C) 2017 Charybdis Development Team * Copyright (C) 2017 Jason Volk * @@ -19,12 +19,42 @@ * POSSIBILITY OF SUCH DAMAGE. */ -ircd::import_shared account_database +using namespace ircd; + +const database::descriptor rooms_head_descriptor { - "client_account", "account_database" + // name + "head", + + // notes + R"( + ### developer note: + + The latest event for a room. + + key is room_id + value is event_id + + )", + + // typing for key and value + { + typeid(string_view), typeid(string_view) + } }; -extern ircd::database *const account +const database::description rooms_description { - account_database.get() + { "default" }, + rooms_head_descriptor, +}; + +std::shared_ptr rooms_database +{ + std::make_shared("room"s, ""s, rooms_description) +}; + +mapi::header IRCD_MODULE +{ + "Hosts the 'rooms' database" };