2021-05-14 22:07:16 +00:00
|
|
|
/**
|
|
|
|
* In addition to the terms of the AGPL, portions of this file
|
|
|
|
* are governed by the terms of the MIT license, from the Rust Matrix
|
|
|
|
* SDK example code.
|
|
|
|
*/
|
2020-11-22 20:52:44 +00:00
|
|
|
use super::DiceBot;
|
2021-05-15 23:45:26 +00:00
|
|
|
use crate::db::sqlite::Database;
|
2021-05-21 14:05:25 +00:00
|
|
|
use crate::db::Rooms;
|
2020-11-07 14:37:56 +00:00
|
|
|
use crate::error::BotError;
|
|
|
|
use async_trait::async_trait;
|
|
|
|
use log::{debug, error, info, warn};
|
|
|
|
use matrix_sdk::{
|
|
|
|
self,
|
|
|
|
events::{
|
2021-05-24 21:32:00 +00:00
|
|
|
room::member::MemberEventContent,
|
2021-03-15 20:10:42 +00:00
|
|
|
room::message::{MessageEventContent, MessageType, TextMessageEventContent},
|
2021-05-24 21:32:00 +00:00
|
|
|
StrippedStateEvent, SyncMessageEvent,
|
2020-11-07 14:37:56 +00:00
|
|
|
},
|
2021-03-22 15:01:49 +00:00
|
|
|
room::Room,
|
|
|
|
EventHandler,
|
2020-11-07 14:37:56 +00:00
|
|
|
};
|
|
|
|
use std::ops::Sub;
|
|
|
|
use std::time::{Duration, SystemTime};
|
2021-05-16 22:22:06 +00:00
|
|
|
use std::{clone::Clone, time::UNIX_EPOCH};
|
2020-11-07 14:37:56 +00:00
|
|
|
|
|
|
|
/// Check if a message is recent enough to actually process. If the
|
|
|
|
/// message is within "oldest_message_age" seconds, this function
|
|
|
|
/// returns true. If it's older than that, it returns false and logs a
|
|
|
|
/// debug message.
|
|
|
|
fn check_message_age(
|
|
|
|
event: &SyncMessageEvent<MessageEventContent>,
|
|
|
|
oldest_message_age: u64,
|
|
|
|
) -> bool {
|
2021-05-16 22:22:06 +00:00
|
|
|
let sending_time = event
|
|
|
|
.origin_server_ts
|
|
|
|
.to_system_time()
|
|
|
|
.unwrap_or(UNIX_EPOCH);
|
|
|
|
|
2021-02-10 20:30:17 +00:00
|
|
|
let oldest_timestamp = SystemTime::now().sub(Duration::from_secs(oldest_message_age));
|
2020-11-07 14:37:56 +00:00
|
|
|
|
|
|
|
if sending_time > oldest_timestamp {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
let age = match oldest_timestamp.duration_since(sending_time) {
|
|
|
|
Ok(n) => format!("{} seconds too old", n.as_secs()),
|
|
|
|
Err(_) => "before the UNIX epoch".to_owned(),
|
|
|
|
};
|
|
|
|
|
|
|
|
debug!("Ignoring message because it is {}: {:?}", age, event);
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 21:05:44 +00:00
|
|
|
/// Determine whether or not to process a received message. This check
|
|
|
|
/// is necessary in addition to the event processing check because we
|
|
|
|
/// may receive message events when entering a room for the first
|
|
|
|
/// time, and we don't want to respond to things before the bot was in
|
|
|
|
/// the channel, but we do want to respond to things that were sent if
|
|
|
|
/// the bot left and rejoined quickly.
|
|
|
|
async fn should_process_message<'a>(
|
2020-11-07 14:37:56 +00:00
|
|
|
bot: &DiceBot,
|
|
|
|
event: &SyncMessageEvent<MessageEventContent>,
|
|
|
|
) -> Result<(String, String), BotError> {
|
|
|
|
//Ignore messages that are older than configured duration.
|
|
|
|
if !check_message_age(event, bot.config.oldest_message_age()) {
|
|
|
|
let state_check = bot.state.read().unwrap();
|
|
|
|
if !((*state_check).logged_skipped_old_messages()) {
|
|
|
|
drop(state_check);
|
|
|
|
let mut state = bot.state.write().unwrap();
|
|
|
|
(*state).skipped_old_messages();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Err(BotError::ShouldNotProcessError);
|
|
|
|
}
|
|
|
|
|
|
|
|
let (msg_body, sender_username) = if let SyncMessageEvent {
|
2021-03-15 20:10:42 +00:00
|
|
|
content:
|
|
|
|
MessageEventContent {
|
|
|
|
msgtype: MessageType::Text(TextMessageEventContent { body, .. }),
|
|
|
|
..
|
|
|
|
},
|
2020-11-07 14:37:56 +00:00
|
|
|
sender,
|
|
|
|
..
|
|
|
|
} = event
|
|
|
|
{
|
|
|
|
(
|
|
|
|
body.clone(),
|
|
|
|
format!("@{}:{}", sender.localpart(), sender.server_name()),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(String::new(), String::new())
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((msg_body, sender_username))
|
|
|
|
}
|
|
|
|
|
2021-05-15 23:45:26 +00:00
|
|
|
async fn should_process_event(db: &Database, room_id: &str, event_id: &str) -> bool {
|
|
|
|
db.should_process(room_id, event_id)
|
|
|
|
.await
|
2020-11-09 21:13:47 +00:00
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
error!(
|
|
|
|
"Database error when checking if we should process an event: {}",
|
|
|
|
e.to_string()
|
|
|
|
);
|
|
|
|
false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:37:56 +00:00
|
|
|
/// This event emitter listens for messages with dice rolling commands.
|
|
|
|
/// Originally adapted from the matrix-rust-sdk examples.
|
|
|
|
#[async_trait]
|
2021-03-15 20:10:42 +00:00
|
|
|
impl EventHandler for DiceBot {
|
2020-11-07 14:37:56 +00:00
|
|
|
async fn on_stripped_state_member(
|
|
|
|
&self,
|
2021-03-15 20:10:42 +00:00
|
|
|
room: Room,
|
2020-11-09 21:05:44 +00:00
|
|
|
event: &StrippedStateEvent<MemberEventContent>,
|
2020-11-07 14:37:56 +00:00
|
|
|
_: Option<MemberEventContent>,
|
|
|
|
) {
|
2021-03-22 15:01:49 +00:00
|
|
|
let room = match room {
|
|
|
|
Room::Invited(invited_room) => invited_room,
|
2021-03-15 20:10:42 +00:00
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
if room.own_user_id().as_str() != event.state_key {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
info!(
|
|
|
|
"Autojoining room {}",
|
|
|
|
room.display_name().await.ok().unwrap_or_default()
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Err(e) = self.client.join_room_by_id(&room.room_id()).await {
|
|
|
|
warn!("Could not join room: {}", e.to_string())
|
2020-11-07 14:37:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 20:10:42 +00:00
|
|
|
async fn on_room_message(&self, room: Room, event: &SyncMessageEvent<MessageEventContent>) {
|
2021-03-22 15:01:49 +00:00
|
|
|
let room = match room {
|
|
|
|
Room::Joined(joined_room) => joined_room,
|
2021-03-15 20:10:42 +00:00
|
|
|
_ => return,
|
|
|
|
};
|
2020-11-07 14:37:56 +00:00
|
|
|
|
2021-03-15 20:10:42 +00:00
|
|
|
let room_id = room.room_id().as_str();
|
2021-05-15 23:45:26 +00:00
|
|
|
if !should_process_event(&self.db, room_id, event.event_id.as_str()).await {
|
2021-03-15 20:10:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let (msg_body, sender_username) =
|
|
|
|
if let Ok((msg_body, sender_username)) = should_process_message(self, &event).await {
|
2020-11-09 21:05:44 +00:00
|
|
|
(msg_body, sender_username)
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
2020-11-07 14:37:56 +00:00
|
|
|
|
2021-03-15 20:27:57 +00:00
|
|
|
let results = self
|
|
|
|
.execute_commands(&room, &sender_username, &msg_body)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
self.handle_results(&room, &sender_username, event.event_id.clone(), results)
|
2021-03-15 20:10:42 +00:00
|
|
|
.await;
|
2020-11-07 14:37:56 +00:00
|
|
|
}
|
|
|
|
}
|