Compare commits
2 commits
1bbd542b9d
...
5af13e5a07
Author | SHA1 | Date | |
---|---|---|---|
5af13e5a07 | |||
f8e983839e |
3 changed files with 34 additions and 38 deletions
26
Cargo.toml
26
Cargo.toml
|
@ -8,16 +8,16 @@ lto = "fat"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
matrix-sdk = {version = "0.6.2", features = ["anyhow", "e2e-encryption", "socks"]}
|
||||
anyhow = "1.0.75"
|
||||
clap = "4.4.6"
|
||||
toml = "0.8.2"
|
||||
log = "0.4.20"
|
||||
env_logger = "0.10.0"
|
||||
tokio = {version = "1.32.0", features = ["parking_lot", "rt-multi-thread", "macros"]}
|
||||
serde = {version = "1.0.188", features = ["derive"]}
|
||||
tracing-subscriber = "0.3.17"
|
||||
scraper = "0.17.1"
|
||||
reqwest = "0.11.22"
|
||||
regex = "1.9.6"
|
||||
lazy_static = "1.4.0"
|
||||
matrix-sdk = {version = "0.7.1", features = ["anyhow", "e2e-encryption", "socks"]}
|
||||
anyhow = "1.0.86"
|
||||
clap = "4.5.8"
|
||||
toml = "0.8.14"
|
||||
log = "0.4.22"
|
||||
env_logger = "0.11.3"
|
||||
tokio = {version = "1.38.0", features = ["parking_lot", "rt-multi-thread", "macros"]}
|
||||
serde = {version = "1.0.204", features = ["derive"]}
|
||||
tracing-subscriber = "0.3.18"
|
||||
scraper = "0.19.0"
|
||||
reqwest = "0.12.5"
|
||||
regex = "1.10.5"
|
||||
lazy_static = "1.5.0"
|
||||
|
|
|
@ -5,9 +5,10 @@
|
|||
use lazy_static::lazy_static;
|
||||
use log::warn;
|
||||
use matrix_sdk::{
|
||||
RoomState,
|
||||
room::Room,
|
||||
ruma::events::room::message::{
|
||||
MessageType, OriginalSyncRoomMessageEvent, Relation, RoomMessageEventContent,
|
||||
MessageType, OriginalSyncRoomMessageEvent, Relation, ForwardThread, AddMentions, RoomMessageEventContent,
|
||||
},
|
||||
Client,
|
||||
};
|
||||
|
@ -97,7 +98,7 @@ fn get_urls_from_message(message: &str) -> Vec<&str> {
|
|||
pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, client: Client) {
|
||||
let fn_start = Instant::now();
|
||||
|
||||
if let Room::Joined(room) = room {
|
||||
if room.state() == RoomState::Joined {
|
||||
let full_reply_event = event.clone().into_full_event(room.room_id().to_owned());
|
||||
|
||||
// If the sender ID matches our client, ignore the message
|
||||
|
@ -142,11 +143,11 @@ pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, clie
|
|||
&embed.title, &embed.description
|
||||
),
|
||||
)
|
||||
.make_reply_to(&full_reply_event);
|
||||
.make_reply_to(&full_reply_event, ForwardThread::Yes, AddMentions::Yes);
|
||||
|
||||
// Finally send the reply to the room
|
||||
warn!("Sending embed for URL: '{}'", &url);
|
||||
if room.send(bot_reply, None).await.is_err() {
|
||||
if room.send(bot_reply).await.is_err() {
|
||||
warn!("Failed to send embed for URL: '{}'", &url);
|
||||
}
|
||||
warn!("Ran fn room.send after: '{:#?}'", fn_start.elapsed());
|
||||
|
@ -156,10 +157,10 @@ pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, clie
|
|||
"Couldn't parse metadata for URL",
|
||||
"<blockquote><h5>Couldn't parse metadata for URL</h5></blockquote>",
|
||||
)
|
||||
.make_reply_to(&full_reply_event);
|
||||
.make_reply_to(&full_reply_event, ForwardThread::Yes, AddMentions::Yes);
|
||||
// Send the reply to the room
|
||||
warn!("Sending 'No metadata' embed for URL: '{}'", &url);
|
||||
if room.send(bot_reply, None).await.is_err() {
|
||||
if room.send(bot_reply).await.is_err() {
|
||||
warn!("Failed to send embed for URL: '{}'", &url);
|
||||
}
|
||||
warn!("Ran fn room.send after: '{:#?}'", fn_start.elapsed());
|
||||
|
|
33
src/lib.rs
33
src/lib.rs
|
@ -2,9 +2,10 @@
|
|||
#![deny(missing_docs)]
|
||||
pub mod embeds;
|
||||
|
||||
use log::{error, warn};
|
||||
use log::warn;
|
||||
use matrix_sdk::{
|
||||
config::SyncSettings,
|
||||
RoomState,
|
||||
room::Room,
|
||||
ruma::{
|
||||
api::client::uiaa, events::room::member::StrippedRoomMemberEvent, OwnedDeviceId,
|
||||
|
@ -70,12 +71,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
|
||||
// and it works :D
|
||||
if let Err(e) = client.delete_devices(&old_devices, None).await {
|
||||
if let Some(info) = e.uiaa_response() {
|
||||
if let Some(info) = e.as_uiaa_response() {
|
||||
let mut password = uiaa::Password::new(
|
||||
uiaa::UserIdentifier::UserIdOrLocalpart(&config.username),
|
||||
&config.password,
|
||||
uiaa::UserIdentifier::UserIdOrLocalpart(config.username.clone()),
|
||||
config.password.clone(),
|
||||
);
|
||||
password.session = info.session.as_deref();
|
||||
password.session = info.session.clone();
|
||||
client
|
||||
.delete_devices(&old_devices, Some(uiaa::AuthData::Password(password)))
|
||||
.await?;
|
||||
|
@ -94,24 +95,17 @@ pub async fn reject_stale_invites(client: &Client, config: &Config) {
|
|||
for room in client.invited_rooms() {
|
||||
let room_name = room.name().unwrap_or_default();
|
||||
if !room.is_space()
|
||||
&& !room.is_direct()
|
||||
&& !room.is_direct().await.expect("Failed to check if room is DM")
|
||||
&& config.room_ids.iter().any(|r| *r == room.room_id())
|
||||
{
|
||||
warn!("Got invite to room: '{}'", room_name);
|
||||
room.accept_invitation()
|
||||
room.join()
|
||||
.await
|
||||
.expect("Failed to accept invite");
|
||||
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
|
||||
);
|
||||
}
|
||||
warn!("Joined room: '{}'!", room_name);
|
||||
} else {
|
||||
warn!("Rejecting invite to room: '{}'", room_name);
|
||||
room.reject_invitation().await.unwrap_or_default();
|
||||
room.leave().await.unwrap_or_default();
|
||||
}
|
||||
}
|
||||
warn!("Finished checking old invites");
|
||||
|
@ -135,6 +129,7 @@ pub async fn run(config: Config) -> anyhow::Result<()> {
|
|||
|
||||
// Attempt to log into the server
|
||||
client
|
||||
.matrix_auth()
|
||||
.login_username(&config.username, &config.password)
|
||||
.initial_device_display_name(&config.display_name)
|
||||
.send()
|
||||
|
@ -165,13 +160,13 @@ pub async fn run(config: Config) -> anyhow::Result<()> {
|
|||
|
||||
// Add handler to log new room invites as they're recieved
|
||||
client.add_event_handler(|ev: StrippedRoomMemberEvent, room: Room| async move {
|
||||
if let Room::Invited(invited_room) = room {
|
||||
if room.state() == RoomState::Invited {
|
||||
warn!(
|
||||
"Got invite to room: '{}' sent by '{}'",
|
||||
invited_room.name().unwrap_or_default(),
|
||||
room.name().unwrap_or_default(),
|
||||
ev.sender
|
||||
);
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
// Add handler to detect and create embeds for HTTP links in chat
|
||||
|
|
Loading…
Reference in a new issue