echo_bot.rs

  1extern crate futures;
  2extern crate tokio_core;
  3extern crate tokio_xmpp;
  4extern crate jid;
  5extern crate xml;
  6
  7use std::str::FromStr;
  8use tokio_core::reactor::Core;
  9use futures::{Future, Stream, Sink, future};
 10use tokio_xmpp::{Client, ClientEvent};
 11use tokio_xmpp::xmpp_codec::Packet;
 12
 13fn main() {
 14    let mut core = Core::new().unwrap();
 15    let client = Client::new("astrobot@example.org", "", &core.handle()).unwrap();
 16    // let client = TcpClient::connect(
 17    //     jid.clone(),
 18    //     &addr,
 19    //     &core.handle()
 20    // ).map_err(|e| format!("{}", e)
 21    // ).and_then(|stream| {
 22    //     if stream.can_starttls() {
 23    //         stream.starttls()
 24    //     } else {
 25    //         panic!("No STARTTLS")
 26    //     }
 27    // }).and_then(|stream| {
 28    //     let username = jid.node.as_ref().unwrap().to_owned();
 29    //     stream.auth(username, password).expect("auth")
 30    // }).and_then(|stream| {
 31    //     stream.bind()
 32    // }).and_then(|stream| {
 33    //     println!("Bound to {}", stream.jid);
 34
 35    //     let presence = xml::Element::new("presence".to_owned(), None, vec![]);
 36    //     stream.send(Packet::Stanza(presence))
 37    //         .map_err(|e| format!("{}", e))
 38    // }).and_then(|stream| {
 39    //     let main_loop = |stream| {
 40    //         stream.into_future()
 41    //             .and_then(|(event, stream)| {
 42    //                 stream.send(Packet::Stanza(unreachable!()))
 43    //             }).and_then(main_loop)
 44    //     };
 45    //     main_loop(stream)
 46    // }).and_then(|(event, stream)| {
 47    //     let (mut sink, stream) = stream.split();
 48    //     stream.for_each(move |event| {
 49    //         match event {
 50    //             Packet::Stanza(ref message)
 51    //                 if message.name == "message" => {
 52    //                     let ty = message.get_attribute("type", None);
 53    //                     let body = message.get_child("body", Some("jabber:client"))
 54    //                         .map(|body_el| body_el.content_str());
 55    //                     match ty {
 56    //                         None | Some("normal") | Some("chat")
 57    //                             if body.is_some() => {
 58    //                                 let from = message.get_attribute("from", None).unwrap();
 59    //                                 println!("Got message from {}: {:?}", from, body);
 60    //                                 let reply = make_reply(from, body.unwrap());
 61    //                                 sink.send(Packet::Stanza(reply))
 62    //                                     .and_then(|_| Ok(()))
 63    //                             },
 64    //                         _ => future::ok(()),
 65    //                     }
 66    //                 },
 67    //             _ => future::ok(()),
 68    //         }
 69    //     }).map_err(|e| format!("{}", e))
 70    // });
 71
 72    let done = client.for_each(|event| {
 73        match event {
 74            ClientEvent::Online => {
 75                println!("Online!");
 76            },
 77            ClientEvent::Stanza(stanza) => {
 78            },
 79            _ => {
 80                println!("Event: {:?}", event);
 81            },
 82        }
 83        
 84        Ok(())
 85    });
 86    
 87    match core.run(done) {
 88        Ok(_) => (),
 89        Err(e) => {
 90            println!("Fatal: {}", e);
 91            ()
 92        }
 93    }
 94}
 95
 96fn make_reply(to: &str, body: String) -> xml::Element {
 97    let mut message = xml::Element::new(
 98        "message".to_owned(),
 99        None,
100        vec![("type".to_owned(), None, "chat".to_owned()),
101             ("to".to_owned(), None, to.to_owned())]
102    );
103    message.tag(xml::Element::new("body".to_owned(), None, vec![]))
104        .text(body);
105    message
106}