From f6a5e8daf3c9998861655babe094e5c6ee0fed03 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 28 Oct 2020 04:57:01 -0700 Subject: [PATCH] ircd::m::app: Implement stdin to app from room. --- include/ircd/m/app.h | 3 ++ matrix/app.cc | 68 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/include/ircd/m/app.h b/include/ircd/m/app.h index 5b085a65c..5caedb347 100644 --- a/include/ircd/m/app.h +++ b/include/ircd/m/app.h @@ -33,8 +33,11 @@ struct ircd::m::app id::user::buf user_id; id::room::buf room_id; id::event::buf event_id; + hookfn room_hook; context worker_context; + void handle_stdin(const event &, const string_view &); + void handle_room_message(const event &, vm::eval &); bool handle_stdout(); void worker(); diff --git a/matrix/app.cc b/matrix/app.cc index 1af78d548..d8384247d 100644 --- a/matrix/app.cc +++ b/matrix/app.cc @@ -248,6 +248,15 @@ ircd::m::app::app(const m::event::idx &event_idx) { m::event_id(event_idx) } +,room_hook +{ + std::bind(&app::handle_room_message, this, ph::_1, ph::_2), + { + { "_site", "vm.eval" }, + { "type", "m.room.message" }, + { "room_id", this->room_id }, + } +} ,worker_context { "m.app", @@ -361,3 +370,62 @@ ircd::m::app::handle_stdout() return true; } + +void +ircd::m::app::handle_room_message(const event &event, + vm::eval &) +{ + assert(at<"room_id"_>(event) == room_id); + assert(at<"type"_>(event) == "m.room.message"); + + if(at<"sender"_>(event) == user_id) + return; + + const m::room::message msg + { + json::get<"content"_>(event) + }; + + if(json::get<"msgtype"_>(msg) != "m.text") + return; + + if(!startswith(json::get<"body"_>(msg), user_id)) + return; + + string_view text + { + json::get<"body"_>(msg) + }; + + text = lstrip(text, user_id); + text = lstrip(text, ':'); + text = lstrip(text, ' '); + handle_stdin(event, text); +} + +void +ircd::m::app::handle_stdin(const event &event, + const string_view &text) +{ + const string_view wrote + { + write(this->child, text) + }; + + const string_view nl + { + write(this->child, "\n"_sv) + }; + + log::debug + { + log, "app:%lu input %zu of %zu bytes from %s in %s :%s%s", + event_idx, + size(wrote) + size(nl), + size(text) + 1, + string_view{at<"sender"_>(event)}, + string_view{room_id}, + trunc(text, 64), + size(text) > 64? "...": "", + }; +}