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/>.
|
||||
|
||||
use crate::Error;
|
||||
use crate::config::Config;
|
||||
use crate::hooks::{Hook, IssueAction, MergeRequestAction, format_hook};
|
||||
|
||||
use chrono::{TimeDelta, Utc};
|
||||
|
|
@ -29,28 +30,21 @@ use xmpp::{
|
|||
pub struct XmppClient {
|
||||
is_online: bool,
|
||||
agent: Agent,
|
||||
rooms: Vec<BareJid>,
|
||||
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
|
||||
/// messages
|
||||
recent_hooks: Vec<Hook>,
|
||||
}
|
||||
|
||||
impl XmppClient {
|
||||
pub fn new(
|
||||
jid: BareJid,
|
||||
password: &str,
|
||||
rooms: Vec<BareJid>,
|
||||
nickname: ResourcePart,
|
||||
admins: Vec<BareJid>,
|
||||
) -> XmppClient {
|
||||
let config = AgentConfig {
|
||||
pub fn new(jid: BareJid, password: &str, nickname: ResourcePart, config: Config) -> XmppClient {
|
||||
let agent_config = AgentConfig {
|
||||
bookmarks_autojoin: false,
|
||||
..AgentConfig::default()
|
||||
};
|
||||
let agent = ClientBuilder::new(jid, password)
|
||||
.set_config(config)
|
||||
.set_config(agent_config)
|
||||
.set_client(ClientType::Bot, "xmpp-rs")
|
||||
.set_website("https://gitlab.com/xmpp-rs/xmpp-rs")
|
||||
.set_default_nick(&nickname)
|
||||
|
|
@ -60,9 +54,8 @@ impl XmppClient {
|
|||
XmppClient {
|
||||
is_online: false,
|
||||
agent,
|
||||
rooms,
|
||||
nickname,
|
||||
admins,
|
||||
config,
|
||||
recent_hooks: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
|
@ -73,7 +66,7 @@ impl XmppClient {
|
|||
Event::Online => {
|
||||
self.is_online = true;
|
||||
debug!("XMPP Online");
|
||||
for room in &self.rooms {
|
||||
for room in &self.config.rooms {
|
||||
self.agent
|
||||
.join_room(JoinRoomSettings {
|
||||
nick: Some(RoomNick::from_resource_ref(self.nickname.as_ref())),
|
||||
|
|
@ -83,7 +76,7 @@ impl XmppClient {
|
|||
}
|
||||
}
|
||||
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);
|
||||
continue;
|
||||
}
|
||||
|
|
@ -91,7 +84,7 @@ impl XmppClient {
|
|||
debug!("Received chat message from {}: {}", bare, message);
|
||||
|
||||
if message == "rejoin" {
|
||||
for room in &self.rooms {
|
||||
for room in &self.config.rooms {
|
||||
self.agent
|
||||
.join_room(JoinRoomSettings {
|
||||
nick: Some(RoomNick::from_resource_ref(self.nickname.as_ref())),
|
||||
|
|
@ -209,7 +202,7 @@ impl XmppClient {
|
|||
}
|
||||
if let Some(display) = format_hook(&wh) {
|
||||
debug!("Hook: {}", display);
|
||||
for room in &self.rooms {
|
||||
for room in &self.config.rooms {
|
||||
self.agent
|
||||
.send_raw_message(RawMessageSettings {
|
||||
recipient: Jid::from(room.clone()),
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ use xmpp::jid::{BareJid, ResourcePart};
|
|||
|
||||
use crate::error::Error;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
/// Accounts trusted for admin tasks
|
||||
#[serde(default = "Vec::new")]
|
||||
|
|
|
|||
|
|
@ -56,11 +56,10 @@ async fn main() -> Result<(), Error> {
|
|||
let (value_tx, value_rx) = mpsc::unbounded_channel::<Hook>();
|
||||
|
||||
let mut bot = XmppClient::new(
|
||||
config.jid,
|
||||
config.jid.clone(),
|
||||
config.password.as_str(),
|
||||
config.rooms,
|
||||
config.nickname,
|
||||
config.admins,
|
||||
config.nickname.clone(),
|
||||
config.clone(),
|
||||
);
|
||||
|
||||
let xmpp_handle = tokio::task::spawn(async move {
|
||||
|
|
|
|||
Loading…
Reference in a new issue