1- // hack to emulate `extern crate libstrophe`
2- mod libstrophe {
3- pub use crate :: * ;
4- }
1+ //! Port of https://github.com/strophe/libstrophe/blob/0.14.0/examples/bot.c
2+
3+ use libstrophe:: { Connection , ConnectionEvent , Context , HandlerResult , Stanza } ;
54
6- /// Port of the [bot.c](https://github.com/strophe/libstrophe/blob/0.12.2/examples/bot.c) code
7- #[ allow( dead_code) ]
85pub fn main ( ) {
96 env_logger:: init ( ) ;
107
118 let jid = "test@example.com" ;
129 let pass = "<password>" ;
1310
14- let version_handler = |_ctx : & libstrophe :: Context , conn : & mut libstrophe :: Connection , stanza : & libstrophe :: Stanza | {
11+ let version_handler = |_ctx : & Context , conn : & mut Connection , stanza : & Stanza | {
1512 eprintln ! ( "Received version request from {}" , stanza. from( ) . expect( "Empty from" ) ) ;
1613
1714 let mut reply = stanza. reply ( ) ;
1815 reply. set_stanza_type ( "result" ) . expect ( "Cannot set stanza type" ) ;
1916
20- let mut query = libstrophe :: Stanza :: new ( ) ;
17+ let mut query = Stanza :: new ( ) ;
2118 query. set_name ( "query" ) . expect ( "Cannot set stanza name" ) ;
2219 if let Some ( ns) = stanza. get_first_child ( ) . expect ( "No children" ) . ns ( ) {
2320 query. set_ns ( ns) . expect ( "Cannot set stanza ns" ) ;
2421 }
2522
26- let mut name = libstrophe :: Stanza :: new ( ) ;
23+ let mut name = Stanza :: new ( ) ;
2724 name. set_name ( "name" ) . expect ( "Cannot set stanza name" ) ;
2825
29- let mut text = libstrophe :: Stanza :: new ( ) ;
26+ let mut text = Stanza :: new ( ) ;
3027 text. set_text ( "libstrophe example bot" ) . expect ( "Cannot set stanza text" ) ;
3128 name. add_child ( text) . expect ( "Cannot add child" ) ;
3229
3330 query. add_child ( name) . expect ( "Cannot add child" ) ;
3431
35- let mut version = libstrophe :: Stanza :: new ( ) ;
32+ let mut version = Stanza :: new ( ) ;
3633 version. set_name ( "version" ) . expect ( "Cannot set stanza name" ) ;
3734
38- let mut text = libstrophe :: Stanza :: new ( ) ;
35+ let mut text = Stanza :: new ( ) ;
3936 text. set_text ( "1.0" ) . expect ( "Cannot set stanza text" ) ;
4037 version. add_child ( text) . expect ( "Cannot add child" ) ;
4138
@@ -44,31 +41,27 @@ pub fn main() {
4441 reply. add_child ( query) . expect ( "Cannot add child" ) ;
4542
4643 conn. send ( & reply) ;
47- libstrophe :: HandlerResult :: KeepHandler
44+ HandlerResult :: KeepHandler
4845 } ;
4946
50- let message_handler = |_ctx : & libstrophe :: Context , conn : & mut libstrophe :: Connection , stanza : & libstrophe :: Stanza | {
47+ let message_handler = |_ctx : & Context , conn : & mut Connection , stanza : & Stanza | {
5148 let body = match stanza. get_child_by_name ( "body" ) {
5249 Some ( body) => body,
53- None => return libstrophe :: HandlerResult :: KeepHandler ,
50+ None => return HandlerResult :: KeepHandler ,
5451 } ;
5552
5653 match stanza. stanza_type ( ) {
5754 Some ( typ) => {
5855 if typ == "error" {
59- return libstrophe :: HandlerResult :: KeepHandler ;
56+ return HandlerResult :: KeepHandler ;
6057 }
6158 }
62- None => return libstrophe :: HandlerResult :: KeepHandler ,
59+ None => return HandlerResult :: KeepHandler ,
6360 } ;
6461
6562 let intext = body. text ( ) . expect ( "Cannot get body" ) ;
6663
67- eprintln ! (
68- "Incoming message from {}: {}" ,
69- stanza. from( ) . expect( "Cannot get from" ) ,
70- intext
71- ) ;
64+ eprintln ! ( "Incoming message from {}: {intext}" , stanza. from( ) . expect( "Cannot get from" ) ) ;
7265
7366 let mut reply = stanza. reply ( ) ;
7467 if reply. stanza_type ( ) . is_none ( ) {
@@ -78,7 +71,7 @@ pub fn main() {
7871 let ( quit, replytext) = if intext == "quit" {
7972 ( true , "bye!" . to_owned ( ) )
8073 } else {
81- ( false , format ! ( "{} to you too!" , intext ) )
74+ ( false , format ! ( "{intext } to you too!" ) )
8275 } ;
8376 reply. set_body ( replytext) . expect ( "Cannot set body" ) ;
8477
@@ -88,26 +81,26 @@ pub fn main() {
8881 conn. disconnect ( ) ;
8982 }
9083
91- libstrophe :: HandlerResult :: KeepHandler
84+ HandlerResult :: KeepHandler
9285 } ;
9386
94- let conn_handler = move |ctx : & libstrophe :: Context , conn : & mut libstrophe :: Connection , evt : libstrophe :: ConnectionEvent | {
95- if let libstrophe :: ConnectionEvent :: Connect = evt {
87+ let conn_handler = move |ctx : & Context , conn : & mut Connection , evt : ConnectionEvent | {
88+ if let ConnectionEvent :: Connect = evt {
9689 eprintln ! ( "Connected" ) ;
9790 conn. handler_add ( version_handler, Some ( "jabber:iq:version" ) , Some ( "iq" ) , None ) ;
9891 conn. handler_add ( message_handler, None , Some ( "message" ) , None ) ;
99- let pres = libstrophe :: Stanza :: new_presence ( ) ;
92+ let pres = Stanza :: new_presence ( ) ;
10093 conn. send ( & pres) ;
10194 } else {
10295 eprintln ! ( "Disconnected" ) ;
10396 ctx. stop ( ) ;
10497 }
10598 } ;
10699
107- let mut conn = libstrophe :: Connection :: new ( libstrophe :: Context :: new_with_default_logger ( ) ) ;
100+ let mut conn = Connection :: new ( Context :: new_with_default_logger ( ) ) ;
108101 conn. set_jid ( jid) ;
109102 conn. set_pass ( pass) ;
110- let ctx = conn
103+ let mut ctx = conn
111104 . connect_client ( None , None , conn_handler)
112105 . expect ( "Cannot connect to XMPP server" ) ;
113106 ctx. run ( ) ;
0 commit comments