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 } ;
5
4
6
- /// Port of the [bot.c](https://github.com/strophe/libstrophe/blob/0.12.2/examples/bot.c) code
7
- #[ allow( dead_code) ]
8
5
pub fn main ( ) {
9
6
env_logger:: init ( ) ;
10
7
11
8
12
9
let pass = "<password>" ;
13
10
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 | {
15
12
eprintln ! ( "Received version request from {}" , stanza. from( ) . expect( "Empty from" ) ) ;
16
13
17
14
let mut reply = stanza. reply ( ) ;
18
15
reply. set_stanza_type ( "result" ) . expect ( "Cannot set stanza type" ) ;
19
16
20
- let mut query = libstrophe :: Stanza :: new ( ) ;
17
+ let mut query = Stanza :: new ( ) ;
21
18
query. set_name ( "query" ) . expect ( "Cannot set stanza name" ) ;
22
19
if let Some ( ns) = stanza. get_first_child ( ) . expect ( "No children" ) . ns ( ) {
23
20
query. set_ns ( ns) . expect ( "Cannot set stanza ns" ) ;
24
21
}
25
22
26
- let mut name = libstrophe :: Stanza :: new ( ) ;
23
+ let mut name = Stanza :: new ( ) ;
27
24
name. set_name ( "name" ) . expect ( "Cannot set stanza name" ) ;
28
25
29
- let mut text = libstrophe :: Stanza :: new ( ) ;
26
+ let mut text = Stanza :: new ( ) ;
30
27
text. set_text ( "libstrophe example bot" ) . expect ( "Cannot set stanza text" ) ;
31
28
name. add_child ( text) . expect ( "Cannot add child" ) ;
32
29
33
30
query. add_child ( name) . expect ( "Cannot add child" ) ;
34
31
35
- let mut version = libstrophe :: Stanza :: new ( ) ;
32
+ let mut version = Stanza :: new ( ) ;
36
33
version. set_name ( "version" ) . expect ( "Cannot set stanza name" ) ;
37
34
38
- let mut text = libstrophe :: Stanza :: new ( ) ;
35
+ let mut text = Stanza :: new ( ) ;
39
36
text. set_text ( "1.0" ) . expect ( "Cannot set stanza text" ) ;
40
37
version. add_child ( text) . expect ( "Cannot add child" ) ;
41
38
@@ -44,31 +41,27 @@ pub fn main() {
44
41
reply. add_child ( query) . expect ( "Cannot add child" ) ;
45
42
46
43
conn. send ( & reply) ;
47
- libstrophe :: HandlerResult :: KeepHandler
44
+ HandlerResult :: KeepHandler
48
45
} ;
49
46
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 | {
51
48
let body = match stanza. get_child_by_name ( "body" ) {
52
49
Some ( body) => body,
53
- None => return libstrophe :: HandlerResult :: KeepHandler ,
50
+ None => return HandlerResult :: KeepHandler ,
54
51
} ;
55
52
56
53
match stanza. stanza_type ( ) {
57
54
Some ( typ) => {
58
55
if typ == "error" {
59
- return libstrophe :: HandlerResult :: KeepHandler ;
56
+ return HandlerResult :: KeepHandler ;
60
57
}
61
58
}
62
- None => return libstrophe :: HandlerResult :: KeepHandler ,
59
+ None => return HandlerResult :: KeepHandler ,
63
60
} ;
64
61
65
62
let intext = body. text ( ) . expect ( "Cannot get body" ) ;
66
63
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" ) ) ;
72
65
73
66
let mut reply = stanza. reply ( ) ;
74
67
if reply. stanza_type ( ) . is_none ( ) {
@@ -78,7 +71,7 @@ pub fn main() {
78
71
let ( quit, replytext) = if intext == "quit" {
79
72
( true , "bye!" . to_owned ( ) )
80
73
} else {
81
- ( false , format ! ( "{} to you too!" , intext ) )
74
+ ( false , format ! ( "{intext } to you too!" ) )
82
75
} ;
83
76
reply. set_body ( replytext) . expect ( "Cannot set body" ) ;
84
77
@@ -88,26 +81,26 @@ pub fn main() {
88
81
conn. disconnect ( ) ;
89
82
}
90
83
91
- libstrophe :: HandlerResult :: KeepHandler
84
+ HandlerResult :: KeepHandler
92
85
} ;
93
86
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 {
96
89
eprintln ! ( "Connected" ) ;
97
90
conn. handler_add ( version_handler, Some ( "jabber:iq:version" ) , Some ( "iq" ) , None ) ;
98
91
conn. handler_add ( message_handler, None , Some ( "message" ) , None ) ;
99
- let pres = libstrophe :: Stanza :: new_presence ( ) ;
92
+ let pres = Stanza :: new_presence ( ) ;
100
93
conn. send ( & pres) ;
101
94
} else {
102
95
eprintln ! ( "Disconnected" ) ;
103
96
ctx. stop ( ) ;
104
97
}
105
98
} ;
106
99
107
- let mut conn = libstrophe :: Connection :: new ( libstrophe :: Context :: new_with_default_logger ( ) ) ;
100
+ let mut conn = Connection :: new ( Context :: new_with_default_logger ( ) ) ;
108
101
conn. set_jid ( jid) ;
109
102
conn. set_pass ( pass) ;
110
- let ctx = conn
103
+ let mut ctx = conn
111
104
. connect_client ( None , None , conn_handler)
112
105
. expect ( "Cannot connect to XMPP server" ) ;
113
106
ctx. run ( ) ;
0 commit comments