bot: pass in Config as a parameter
In an attempt to pass in more stuff to the bot Signed-off-by: pep <pep@bouah.net>
This commit is contained in:
parent
284aa7dfd1
commit
849ecbd5dc
3 changed files with 14 additions and 22 deletions
27
src/bot.rs
27
src/bot.rs
|
|
@ -14,6 +14,7 @@
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::Error;
|
use crate::Error;
|
||||||
|
use crate::config::Config;
|
||||||
use crate::hooks::{Hook, IssueAction, MergeRequestAction, format_hook};
|
use crate::hooks::{Hook, IssueAction, MergeRequestAction, format_hook};
|
||||||
|
|
||||||
use chrono::{TimeDelta, Utc};
|
use chrono::{TimeDelta, Utc};
|
||||||
|
|
@ -29,28 +30,21 @@ use xmpp::{
|
||||||
pub struct XmppClient {
|
pub struct XmppClient {
|
||||||
is_online: bool,
|
is_online: bool,
|
||||||
agent: Agent,
|
agent: Agent,
|
||||||
rooms: Vec<BareJid>,
|
|
||||||
nickname: ResourcePart,
|
nickname: ResourcePart,
|
||||||
admins: Vec<BareJid>,
|
config: Config,
|
||||||
/// Keep around messages we've sent recently so we're able to prevent spamming the same type of
|
/// Keep around messages we've sent recently so we're able to prevent spamming the same type of
|
||||||
/// messages
|
/// messages
|
||||||
recent_hooks: Vec<Hook>,
|
recent_hooks: Vec<Hook>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl XmppClient {
|
impl XmppClient {
|
||||||
pub fn new(
|
pub fn new(jid: BareJid, password: &str, nickname: ResourcePart, config: Config) -> XmppClient {
|
||||||
jid: BareJid,
|
let agent_config = AgentConfig {
|
||||||
password: &str,
|
|
||||||
rooms: Vec<BareJid>,
|
|
||||||
nickname: ResourcePart,
|
|
||||||
admins: Vec<BareJid>,
|
|
||||||
) -> XmppClient {
|
|
||||||
let config = AgentConfig {
|
|
||||||
bookmarks_autojoin: false,
|
bookmarks_autojoin: false,
|
||||||
..AgentConfig::default()
|
..AgentConfig::default()
|
||||||
};
|
};
|
||||||
let agent = ClientBuilder::new(jid, password)
|
let agent = ClientBuilder::new(jid, password)
|
||||||
.set_config(config)
|
.set_config(agent_config)
|
||||||
.set_client(ClientType::Bot, "xmpp-rs")
|
.set_client(ClientType::Bot, "xmpp-rs")
|
||||||
.set_website("https://gitlab.com/xmpp-rs/xmpp-rs")
|
.set_website("https://gitlab.com/xmpp-rs/xmpp-rs")
|
||||||
.set_default_nick(&nickname)
|
.set_default_nick(&nickname)
|
||||||
|
|
@ -60,9 +54,8 @@ impl XmppClient {
|
||||||
XmppClient {
|
XmppClient {
|
||||||
is_online: false,
|
is_online: false,
|
||||||
agent,
|
agent,
|
||||||
rooms,
|
|
||||||
nickname,
|
nickname,
|
||||||
admins,
|
config,
|
||||||
recent_hooks: Vec::new(),
|
recent_hooks: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -73,7 +66,7 @@ impl XmppClient {
|
||||||
Event::Online => {
|
Event::Online => {
|
||||||
self.is_online = true;
|
self.is_online = true;
|
||||||
debug!("XMPP Online");
|
debug!("XMPP Online");
|
||||||
for room in &self.rooms {
|
for room in &self.config.rooms {
|
||||||
self.agent
|
self.agent
|
||||||
.join_room(JoinRoomSettings {
|
.join_room(JoinRoomSettings {
|
||||||
nick: Some(RoomNick::from_resource_ref(self.nickname.as_ref())),
|
nick: Some(RoomNick::from_resource_ref(self.nickname.as_ref())),
|
||||||
|
|
@ -83,7 +76,7 @@ impl XmppClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::ChatMessage(_id, bare, message, _timeinfo) => {
|
Event::ChatMessage(_id, bare, message, _timeinfo) => {
|
||||||
if !self.admins.contains(&bare) {
|
if !self.config.admins.contains(&bare) {
|
||||||
debug!("Received chat message from {}, not in admins", bare);
|
debug!("Received chat message from {}, not in admins", bare);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -91,7 +84,7 @@ impl XmppClient {
|
||||||
debug!("Received chat message from {}: {}", bare, message);
|
debug!("Received chat message from {}: {}", bare, message);
|
||||||
|
|
||||||
if message == "rejoin" {
|
if message == "rejoin" {
|
||||||
for room in &self.rooms {
|
for room in &self.config.rooms {
|
||||||
self.agent
|
self.agent
|
||||||
.join_room(JoinRoomSettings {
|
.join_room(JoinRoomSettings {
|
||||||
nick: Some(RoomNick::from_resource_ref(self.nickname.as_ref())),
|
nick: Some(RoomNick::from_resource_ref(self.nickname.as_ref())),
|
||||||
|
|
@ -209,7 +202,7 @@ impl XmppClient {
|
||||||
}
|
}
|
||||||
if let Some(display) = format_hook(&wh) {
|
if let Some(display) = format_hook(&wh) {
|
||||||
debug!("Hook: {}", display);
|
debug!("Hook: {}", display);
|
||||||
for room in &self.rooms {
|
for room in &self.config.rooms {
|
||||||
self.agent
|
self.agent
|
||||||
.send_raw_message(RawMessageSettings {
|
.send_raw_message(RawMessageSettings {
|
||||||
recipient: Jid::from(room.clone()),
|
recipient: Jid::from(room.clone()),
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ use xmpp::jid::{BareJid, ResourcePart};
|
||||||
|
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
/// Accounts trusted for admin tasks
|
/// Accounts trusted for admin tasks
|
||||||
#[serde(default = "Vec::new")]
|
#[serde(default = "Vec::new")]
|
||||||
|
|
|
||||||
|
|
@ -56,11 +56,10 @@ async fn main() -> Result<(), Error> {
|
||||||
let (value_tx, value_rx) = mpsc::unbounded_channel::<Hook>();
|
let (value_tx, value_rx) = mpsc::unbounded_channel::<Hook>();
|
||||||
|
|
||||||
let mut bot = XmppClient::new(
|
let mut bot = XmppClient::new(
|
||||||
config.jid,
|
config.jid.clone(),
|
||||||
config.password.as_str(),
|
config.password.as_str(),
|
||||||
config.rooms,
|
config.nickname.clone(),
|
||||||
config.nickname,
|
config.clone(),
|
||||||
config.admins,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
let xmpp_handle = tokio::task::spawn(async move {
|
let xmpp_handle = tokio::task::spawn(async move {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue