Skip to content
Snippets Groups Projects
Select Git revision
  • 328bb1a72c97a97e346938e5471330f87cb7da16
  • main default protected
  • release/1.1
  • encrypt_comments
  • mnemonic_dewif
  • authors_rules
  • 0.14
  • rtd
  • 1.2.1 protected
  • 1.2.0 protected
  • 1.1.1 protected
  • 1.1.0 protected
  • 1.0.0 protected
  • 1.0.0rc1 protected
  • 1.0.0rc0 protected
  • 1.0.0-rc protected
  • 0.62.0 protected
  • 0.61.0 protected
  • 0.60.1 protected
  • 0.58.1 protected
  • 0.60.0 protected
  • 0.58.0 protected
  • 0.57.0 protected
  • 0.56.0 protected
  • 0.55.1 protected
  • 0.55.0 protected
  • 0.54.3 protected
  • 0.54.2 protected
28 results

pyproject.toml

Blame
  • xmpp_bot.rs 2.31 KiB
    use crate::*;
    
    #[derive(Clone)]
    #[allow(clippy::clippy::upper_case_acronyms)]
    pub struct XMPPBot {
        agent: XMPPAgent,
        muc_jid: BareJid,
    }
    
    impl XMPPBot {
        pub fn new<'a>(
            jid: &'a str,
            password: &'a str,
            muc_jid: &'a str,
        ) -> (Box<dyn Future<Item = (), Error = ()> + 'a>, Self) {
            let muc_jid: BareJid = match BareJid::from_str(muc_jid) {
                Ok(jid) => jid,
                Err(err) => panic!("MUC Jid invalid: {:?}", err),
            };
    
            let (agent, stream) = ClientBuilder::new(jid, password)
                .set_client(ClientType::Bot, "gitbot")
                .set_website("https://git.duniter.org/tools/gitbot")
                .set_default_nick("gitbot")
                .build()
                .unwrap();
    
            let handler = {
                let mut agent = agent.clone();
                let muc_jid = muc_jid.clone();
                let jid = jid;
                stream
                    .map_err(|e| {
                        log::error!("XMPP ERROR: {}", e);
                    })
                    .for_each(move |evt: Event| {
                        match evt {
                            Event::Online => {
                                info!("XMPP client now online at {}", jid);
                                agent.join_room(
                                    muc_jid.clone(),
                                    None,
                                    None,
                                    "en",
                                    "Your friendly hook bot.",
                                );
                            }
                            Event::Disconnected => {
                                info!("XMPP client disconnected");
                            }
                            Event::RoomJoined(jid) => {
                                info!("Entered MUC {}", jid);
                            }
                            Event::RoomLeft(jid) => {
                                info!("Left MUC {}", jid);
                            }
                            _ => (),
                        }
                        Ok(())
                    })
                    .map(|_| ())
            };
    
            (Box::new(handler), XMPPBot { agent, muc_jid })
        }
    
        pub fn send_room_text(&mut self, text: String) {
            self.agent.send_message(
                Jid::Bare(self.muc_jid.clone()),
                MessageType::Groupchat,
                "en",
                &text,
            );
        }
    }