Skip to content
Snippets Groups Projects
Select Git revision
  • f1207b80ce5f05a44f84818dbbc5738caf914040
  • master default protected
  • dev
  • appimage
  • fix_gitlab
  • fixappveyor
  • gitlab
  • fix_ci
  • fix_dbus_error
  • fix_ci_osx
  • sakia020
  • fix_travis#1105
  • feature/backend
  • check_uniq_node_by_endpoints
  • qt5.7
  • feature/agent_architecture
  • translations
  • pyqt5.6
  • qtwebengine
  • pyinstaller
  • landscape
  • 0.53.2
  • 0.53.1
  • 0.53.0
  • 0.52.0
  • 0.51.1
  • 0.51.0
  • 0.50.5
  • 0.50.4
  • 0.50.3
  • 0.50.2
  • 0.50.1
  • 0.50.0
  • 0.33.0rc7
  • 0.33.0rc6
  • 0.33.0rc5
  • 0.33.0rc4
  • 0.33.0rc3
  • 0.33.0rc2
  • 0.33.0rc1
  • 0.32.10post1
41 results

account.py

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,
            );
        }
    }