Skip to content

Commit 9cf2761

Browse files
committed
Add --autojoin option to select whether to autojoin channels and queries
1 parent 8d45d48 commit 9cf2761

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ matrix-sdk = { version = "0.8", features = ["anyhow", "sso-login"] }
3030
percent-encoding = "2.3.1"
3131
rand_core = { version = "0.6", features = ["getrandom"] }
3232
regex = "1.8"
33-
serde = "1.0"
33+
serde = { version = "1.0", features = ["derive"] }
3434
serde_json = "1.0"
3535
tokio = { version = "1.0.0", features = ["full"] }
3636
tokio-util = { version = "0.7", features = ["codec"] }

src/args.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
use clap::Parser;
22
use lazy_static::lazy_static;
3+
use serde::Deserialize;
34
use std::net::SocketAddr;
45

6+
#[derive(clap::ValueEnum, Clone, Debug, PartialEq, Deserialize)]
7+
#[serde(rename_all = "lowercase")]
8+
pub enum AutoJoinOptions {
9+
None,
10+
Queries,
11+
Channels,
12+
All,
13+
}
14+
15+
impl AutoJoinOptions {
16+
pub fn join_queries(&self) -> bool {
17+
[AutoJoinOptions::Queries, AutoJoinOptions::All].contains(self)
18+
}
19+
pub fn join_channels(&self) -> bool {
20+
[AutoJoinOptions::Channels, AutoJoinOptions::All].contains(self)
21+
}
22+
}
23+
524
#[derive(Parser, Debug)]
625
#[command(author, version, about, long_about = None)]
726
pub struct Args {
@@ -19,6 +38,9 @@ pub struct Args {
1938

2039
#[arg(long, default_value = None)]
2140
pub media_url: Option<String>,
41+
42+
#[arg(long, value_enum, default_value_t = AutoJoinOptions::None)]
43+
pub autojoin: AutoJoinOptions,
2244
}
2345

2446
pub fn args() -> &'static Args {

src/ircd/proto.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use tokio::net::TcpStream;
1010
use tokio::sync::mpsc;
1111
use tokio_util::codec::Framed;
1212

13+
use crate::args::{args, AutoJoinOptions};
14+
use crate::matrix::room_mappings::RoomTargetType;
1315
use crate::{matrirc::Matrirc, matrix::MatrixMessageType};
1416

1517
/// it's a bit of a pain to redo the work twice for notice/privmsg,
@@ -163,6 +165,10 @@ pub async fn ircd_sync_write(
163165
}
164166

165167
pub async fn join_channels(matrirc: &Matrirc) -> Result<()> {
168+
if args().autojoin == AutoJoinOptions::None {
169+
return Ok(());
170+
}
171+
166172
let irc = matrirc.irc();
167173
let matrix = matrirc.matrix();
168174
let mapping = matrirc.mappings();
@@ -178,7 +184,20 @@ pub async fn join_channels(matrirc: &Matrirc) -> Result<()> {
178184
continue;
179185
}
180186
let roomtarget = mapping.try_room_target(&joined).await?;
181-
roomtarget.join_chan(irc).await;
187+
let chantype = roomtarget.target_type().await;
188+
if [RoomTargetType::Chan, RoomTargetType::LeftChan].contains(&chantype)
189+
&& args().autojoin.join_channels()
190+
{
191+
roomtarget.join_chan(irc).await;
192+
} else if chantype == RoomTargetType::Query && args().autojoin.join_queries() {
193+
let _ = irc
194+
.send(privmsg(
195+
roomtarget.target().await,
196+
&irc.nick,
197+
"* <Resumed connection to matrirc>",
198+
))
199+
.await;
200+
}
182201
}
183202
Ok(())
184203
}

0 commit comments

Comments
 (0)