Skip to content

Commit

Permalink
on connect
Browse files Browse the repository at this point in the history
  • Loading branch information
casperstorm committed Aug 2, 2023
1 parent e68730f commit 27119d5
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Added:
cycling channels in the buffer and more! A new `keys` section has been added to the config file, reference the
[wiki](https://github.com/squidowl/halloy/wiki/Keyboard-shortcuts) for more details.
- Single clicking on a user will insert nickname to input
- Configuration option `on_connect` to execute commands once connected to a server, reference the
[wiki](https://github.com/squidowl/halloy/wiki/Configuration#on-connect) for more details.

Fixed:

Expand Down
9 changes: 9 additions & 0 deletions data/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,15 @@ impl Client {
let nick = args.first()?;
self.resolved_nick = Some(nick.to_string());

// Loop on connect commands.
for command in self.config.on_connect.iter() {
if let Ok(cmd) = crate::command::parse(command, None) {
if let Ok(command) = proto::Command::try_from(cmd) {
let _ = self.sender.try_send(command.into());
};
};
}

// Send nick password & ghost
if let Some(nick_pass) = self.config.nick_password.as_ref() {
// Try ghost recovery if we couldn't claim our nick
Expand Down
4 changes: 2 additions & 2 deletions data/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub enum Command {
Unknown(String, Vec<String>),
}

pub fn parse(s: &str, buffer: &Buffer) -> Result<Command, Error> {
pub fn parse(s: &str, buffer: Option<&Buffer>) -> Result<Command, Error> {
let (head, rest) = s.split_once('/').ok_or(Error::MissingSlash)?;
// Don't allow leading whitespace before slash
if !head.is_empty() {
Expand Down Expand Up @@ -88,7 +88,7 @@ pub fn parse(s: &str, buffer: &Buffer) -> Result<Command, Error> {
validated::<2, 0, true>(args, |[target, msg], []| Command::Msg(target, msg))
}
Kind::Me => {
if let Some(target) = buffer.target() {
if let Some(target) = buffer.and_then(|b| b.target()) {
validated::<1, 0, true>(args, |[text], _| Command::Me(target, text))
} else {
Ok(unknown())
Expand Down
3 changes: 3 additions & 0 deletions data/src/config/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ pub struct Server {
root_cert_path: Option<PathBuf>,
/// Sasl authentication
pub sasl: Option<Sasl>,
/// Commands which are executed once connected.
#[serde(default)]
pub on_connect: Vec<String>,
}

impl Server {
Expand Down
2 changes: 1 addition & 1 deletion data/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::user::NickRef;
use crate::{command, message, Buffer, Command, Message, Server, User};

pub fn parse(buffer: Buffer, input: &str) -> Result<Input, command::Error> {
let content = match command::parse(input, &buffer) {
let content = match command::parse(input, Some(&buffer)) {
Ok(command) => Content::Command(command),
Err(command::Error::MissingSlash) => Content::Text(input.to_string()),
Err(error) => return Err(error),
Expand Down

0 comments on commit 27119d5

Please sign in to comment.