bad-news/src/main.rs

200 lines
5.4 KiB
Rust
Raw Normal View History

2021-02-02 05:37:43 +01:00
use std::{
fs::File,
io::{self, BufReader, BufWriter},
2021-02-02 06:43:23 +01:00
path::PathBuf,
time::Duration,
2021-02-02 05:37:43 +01:00
};
2021-02-02 04:20:27 +01:00
use clap::Clap;
use matrix_sdk::{
self, async_trait,
events::{
room::{
member::MemberEventContent,
message::{MessageEventContent, TextMessageEventContent},
},
StrippedStateEvent, SyncMessageEvent,
},
2021-02-02 05:37:43 +01:00
Client, ClientConfig, EventEmitter, RoomState, Session, SyncSettings,
2021-02-02 04:20:27 +01:00
};
2021-02-02 06:04:06 +01:00
use serde::Deserialize;
use thiserror::Error;
2021-02-02 06:43:23 +01:00
use tokio::time::sleep;
use url::Url;
2021-02-02 04:20:27 +01:00
struct AutoJoinBot {
client: Client,
}
impl AutoJoinBot {
pub fn new(client: Client) -> Self {
Self { client }
}
}
#[async_trait]
impl EventEmitter for AutoJoinBot {
async fn on_room_message(
&self,
room: RoomState,
event: &SyncMessageEvent<MessageEventContent>,
) {
if let RoomState::Joined(room) = room {
if let SyncMessageEvent {
content: MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }),
sender,
..
} = event
{
let member = room.get_member(&sender).await.unwrap().unwrap();
let name = member
.display_name()
.unwrap_or_else(|| member.user_id().as_str());
println!("{}: {}", name, msg_body);
}
}
}
2021-02-02 06:39:05 +01:00
2021-02-02 04:20:27 +01:00
async fn on_stripped_state_member(
&self,
room: RoomState,
room_member: &StrippedStateEvent<MemberEventContent>,
_: Option<MemberEventContent>,
) {
if room_member.state_key != self.client.user_id().await.unwrap() {
return;
}
if let RoomState::Invited(room) = room {
2021-02-02 04:52:43 +01:00
// TODO: only join room if it's the room specified in the configuration
2021-02-02 04:20:27 +01:00
println!("Autojoining room {}", room.room_id());
let mut delay = 2;
while let Err(err) = self.client.join_room_by_id(room.room_id()).await {
// retry autojoin due to synapse sending invites, before the
// invited user can join for more information see
// https://github.com/matrix-org/synapse/issues/4345
eprintln!(
"Failed to join room {} ({:?}), retrying in {}s",
room.room_id(),
err,
delay
);
sleep(Duration::from_secs(delay)).await;
delay *= 2;
if delay > 3600 {
eprintln!("Can't join room {} ({:?})", room.room_id(), err);
break;
}
}
println!("Successfully joined room {}", room.room_id());
}
}
}
2021-02-02 05:37:43 +01:00
// TODO: use nice error handling
async fn load_or_init_session(
client: &Client,
session_file: PathBuf,
username: &str,
password: &str,
) -> anyhow::Result<()> {
2021-02-02 05:37:43 +01:00
if session_file.is_file() {
let reader = BufReader::new(File::open(session_file)?);
2021-02-02 05:37:43 +01:00
let session: Session = serde_yaml::from_reader(reader)?;
2021-02-02 05:37:43 +01:00
client.restore_login(session.clone()).await?;
2021-02-02 05:37:43 +01:00
println!("Reused session: {}, {}", session.user_id, session.device_id);
} else {
let response = client
.login(username, password, None, Some("autojoin bot"))
.await?;
2021-02-02 05:37:43 +01:00
println!("logged in as {}", username);
let session = Session {
access_token: response.access_token,
user_id: response.user_id,
device_id: response.device_id,
};
let writer = BufWriter::new(File::create(session_file)?);
serde_yaml::to_writer(writer, &session)?;
2021-02-02 05:37:43 +01:00
}
Ok(())
2021-02-02 05:37:43 +01:00
}
2021-02-02 04:20:27 +01:00
async fn login_and_sync(
2021-02-02 06:04:06 +01:00
homeserver_url: Url,
2021-02-02 04:20:27 +01:00
username: &str,
password: &str,
2021-02-02 06:04:06 +01:00
state_dir: PathBuf,
) -> anyhow::Result<()> {
2021-02-02 06:04:06 +01:00
let client_config = ClientConfig::new().store_path(state_dir.join("store"));
2021-02-02 04:20:27 +01:00
let client = Client::new_with_config(homeserver_url, client_config)?;
2021-02-02 04:20:27 +01:00
load_or_init_session(&client, state_dir.join("session.yaml"), username, password).await?;
2021-02-02 04:20:27 +01:00
client
.add_event_emitter(Box::new(AutoJoinBot::new(client.clone())))
.await;
client.sync(SyncSettings::default()).await;
Ok(())
}
#[derive(Error, Debug)]
enum BadNewsError {
#[error("problem accessing configuration file")]
ConfigFile(#[from] io::Error),
#[error("Matrix communication error")]
Matrix(#[from] matrix_sdk::Error),
}
2021-02-02 06:04:06 +01:00
#[derive(Clap)]
#[clap(version = "0.1", author = "Antoine Martin")]
struct Opts {
/// File where session information will be saved
#[clap(short, long, parse(from_os_str))]
config: PathBuf,
}
2021-02-02 06:39:05 +01:00
/// Holds the configuration for the bot.
2021-02-02 06:04:06 +01:00
#[derive(Deserialize)]
struct Config {
2021-02-02 06:39:05 +01:00
/// The URL for the homeserver we should connect to
2021-02-02 06:04:06 +01:00
homeserver: Url,
2021-02-02 06:39:05 +01:00
/// The bot's account username
2021-02-02 06:04:06 +01:00
username: String,
2021-02-02 06:39:05 +01:00
/// The bot's account password
2021-02-02 06:04:06 +01:00
password: String,
2021-02-02 06:39:05 +01:00
/// Path to a directory where the bot will store Matrix state and current session information.
2021-02-02 06:04:06 +01:00
state_dir: PathBuf,
}
2021-02-02 04:20:27 +01:00
#[tokio::main]
async fn main() -> anyhow::Result<()> {
2021-02-02 04:20:27 +01:00
tracing_subscriber::fmt::init();
2021-02-02 06:04:06 +01:00
2021-02-02 04:20:27 +01:00
let opts = Opts::parse();
2021-02-02 06:04:06 +01:00
let config_file = opts.config;
let config: Config = serde_yaml::from_reader(BufReader::new(File::open(config_file)?))?;
2021-02-02 04:20:27 +01:00
login_and_sync(
2021-02-02 06:04:06 +01:00
config.homeserver,
&config.username,
&config.password,
config.state_dir,
2021-02-02 04:20:27 +01:00
)
.await
}