Compare commits

..

No commits in common. "5af13e5a07cbec5b96318e2798bfe2d79e5ce1e4" and "1bbd542b9db3af4a1061fd41bb7efa692945b924" have entirely different histories.

3 changed files with 38 additions and 34 deletions

View file

@ -8,16 +8,16 @@ lto = "fat"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
matrix-sdk = {version = "0.7.1", features = ["anyhow", "e2e-encryption", "socks"]} matrix-sdk = {version = "0.6.2", features = ["anyhow", "e2e-encryption", "socks"]}
anyhow = "1.0.86" anyhow = "1.0.75"
clap = "4.5.8" clap = "4.4.6"
toml = "0.8.14" toml = "0.8.2"
log = "0.4.22" log = "0.4.20"
env_logger = "0.11.3" env_logger = "0.10.0"
tokio = {version = "1.38.0", features = ["parking_lot", "rt-multi-thread", "macros"]} tokio = {version = "1.32.0", features = ["parking_lot", "rt-multi-thread", "macros"]}
serde = {version = "1.0.204", features = ["derive"]} serde = {version = "1.0.188", features = ["derive"]}
tracing-subscriber = "0.3.18" tracing-subscriber = "0.3.17"
scraper = "0.19.0" scraper = "0.17.1"
reqwest = "0.12.5" reqwest = "0.11.22"
regex = "1.10.5" regex = "1.9.6"
lazy_static = "1.5.0" lazy_static = "1.4.0"

View file

@ -5,10 +5,9 @@
use lazy_static::lazy_static; use lazy_static::lazy_static;
use log::warn; use log::warn;
use matrix_sdk::{ use matrix_sdk::{
RoomState,
room::Room, room::Room,
ruma::events::room::message::{ ruma::events::room::message::{
MessageType, OriginalSyncRoomMessageEvent, Relation, ForwardThread, AddMentions, RoomMessageEventContent, MessageType, OriginalSyncRoomMessageEvent, Relation, RoomMessageEventContent,
}, },
Client, Client,
}; };
@ -98,7 +97,7 @@ fn get_urls_from_message(message: &str) -> Vec<&str> {
pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, client: Client) { pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, client: Client) {
let fn_start = Instant::now(); let fn_start = Instant::now();
if room.state() == RoomState::Joined { if let Room::Joined(room) = room {
let full_reply_event = event.clone().into_full_event(room.room_id().to_owned()); let full_reply_event = event.clone().into_full_event(room.room_id().to_owned());
// If the sender ID matches our client, ignore the message // If the sender ID matches our client, ignore the message
@ -143,11 +142,11 @@ pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, clie
&embed.title, &embed.description &embed.title, &embed.description
), ),
) )
.make_reply_to(&full_reply_event, ForwardThread::Yes, AddMentions::Yes); .make_reply_to(&full_reply_event);
// Finally send the reply to the room // Finally send the reply to the room
warn!("Sending embed for URL: '{}'", &url); warn!("Sending embed for URL: '{}'", &url);
if room.send(bot_reply).await.is_err() { if room.send(bot_reply, None).await.is_err() {
warn!("Failed to send embed for URL: '{}'", &url); warn!("Failed to send embed for URL: '{}'", &url);
} }
warn!("Ran fn room.send after: '{:#?}'", fn_start.elapsed()); warn!("Ran fn room.send after: '{:#?}'", fn_start.elapsed());
@ -157,10 +156,10 @@ pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, clie
"Couldn't parse metadata for URL", "Couldn't parse metadata for URL",
"<blockquote><h5>Couldn't parse metadata for URL</h5></blockquote>", "<blockquote><h5>Couldn't parse metadata for URL</h5></blockquote>",
) )
.make_reply_to(&full_reply_event, ForwardThread::Yes, AddMentions::Yes); .make_reply_to(&full_reply_event);
// Send the reply to the room // Send the reply to the room
warn!("Sending 'No metadata' embed for URL: '{}'", &url); warn!("Sending 'No metadata' embed for URL: '{}'", &url);
if room.send(bot_reply).await.is_err() { if room.send(bot_reply, None).await.is_err() {
warn!("Failed to send embed for URL: '{}'", &url); warn!("Failed to send embed for URL: '{}'", &url);
} }
warn!("Ran fn room.send after: '{:#?}'", fn_start.elapsed()); warn!("Ran fn room.send after: '{:#?}'", fn_start.elapsed());

View file

@ -2,10 +2,9 @@
#![deny(missing_docs)] #![deny(missing_docs)]
pub mod embeds; pub mod embeds;
use log::warn; use log::{error, warn};
use matrix_sdk::{ use matrix_sdk::{
config::SyncSettings, config::SyncSettings,
RoomState,
room::Room, room::Room,
ruma::{ ruma::{
api::client::uiaa, events::room::member::StrippedRoomMemberEvent, OwnedDeviceId, api::client::uiaa, events::room::member::StrippedRoomMemberEvent, OwnedDeviceId,
@ -71,12 +70,12 @@ pub async fn delete_old_encryption_devices(client: &Client, config: &Config) ->
// Deleting these devices needs "user interaction" or something, so we just send password again // Deleting these devices needs "user interaction" or something, so we just send password again
// and it works :D // and it works :D
if let Err(e) = client.delete_devices(&old_devices, None).await { if let Err(e) = client.delete_devices(&old_devices, None).await {
if let Some(info) = e.as_uiaa_response() { if let Some(info) = e.uiaa_response() {
let mut password = uiaa::Password::new( let mut password = uiaa::Password::new(
uiaa::UserIdentifier::UserIdOrLocalpart(config.username.clone()), uiaa::UserIdentifier::UserIdOrLocalpart(&config.username),
config.password.clone(), &config.password,
); );
password.session = info.session.clone(); password.session = info.session.as_deref();
client client
.delete_devices(&old_devices, Some(uiaa::AuthData::Password(password))) .delete_devices(&old_devices, Some(uiaa::AuthData::Password(password)))
.await?; .await?;
@ -95,17 +94,24 @@ pub async fn reject_stale_invites(client: &Client, config: &Config) {
for room in client.invited_rooms() { for room in client.invited_rooms() {
let room_name = room.name().unwrap_or_default(); let room_name = room.name().unwrap_or_default();
if !room.is_space() if !room.is_space()
&& !room.is_direct().await.expect("Failed to check if room is DM") && !room.is_direct()
&& config.room_ids.iter().any(|r| *r == room.room_id()) && config.room_ids.iter().any(|r| *r == room.room_id())
{ {
warn!("Got invite to room: '{}'", room_name); warn!("Got invite to room: '{}'", room_name);
room.join() room.accept_invitation()
.await .await
.expect("Failed to accept invite"); .expect("Failed to accept invite");
warn!("Joined room: '{}'!", room_name); warn!("Joining room!");
if let Err(e) = client.join_room_by_id(room.room_id()).await {
error!(
"Failed to join room with id: {} and error: {}",
room.room_id(),
e
);
}
} else { } else {
warn!("Rejecting invite to room: '{}'", room_name); warn!("Rejecting invite to room: '{}'", room_name);
room.leave().await.unwrap_or_default(); room.reject_invitation().await.unwrap_or_default();
} }
} }
warn!("Finished checking old invites"); warn!("Finished checking old invites");
@ -129,7 +135,6 @@ pub async fn run(config: Config) -> anyhow::Result<()> {
// Attempt to log into the server // Attempt to log into the server
client client
.matrix_auth()
.login_username(&config.username, &config.password) .login_username(&config.username, &config.password)
.initial_device_display_name(&config.display_name) .initial_device_display_name(&config.display_name)
.send() .send()
@ -160,13 +165,13 @@ pub async fn run(config: Config) -> anyhow::Result<()> {
// Add handler to log new room invites as they're recieved // Add handler to log new room invites as they're recieved
client.add_event_handler(|ev: StrippedRoomMemberEvent, room: Room| async move { client.add_event_handler(|ev: StrippedRoomMemberEvent, room: Room| async move {
if room.state() == RoomState::Invited { if let Room::Invited(invited_room) = room {
warn!( warn!(
"Got invite to room: '{}' sent by '{}'", "Got invite to room: '{}' sent by '{}'",
room.name().unwrap_or_default(), invited_room.name().unwrap_or_default(),
ev.sender ev.sender
); );
}; }
}); });
// Add handler to detect and create embeds for HTTP links in chat // Add handler to detect and create embeds for HTTP links in chat