0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-09-27 11:18:51 +02:00

ircd:Ⓜ️:app: Implement stdin to app from room.

This commit is contained in:
Jason Volk 2020-10-28 04:57:01 -07:00
parent ca69e70653
commit f6a5e8daf3
2 changed files with 71 additions and 0 deletions

View file

@ -33,8 +33,11 @@ struct ircd::m::app
id::user::buf user_id;
id::room::buf room_id;
id::event::buf event_id;
hookfn<vm::eval &> 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();

View file

@ -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? "...": "",
};
}