|
1 |
|
2 require 'glib' |
|
3 local lm = require 'lm' |
|
4 |
|
5 g.log.handler ( |
|
6 function ( domain, lvl, mesg ) |
|
7 print ( mesg ) |
|
8 end ) |
|
9 |
|
10 sent = false |
|
11 |
|
12 server = 'jabber.kiev.ua' |
|
13 username = arg[1] |
|
14 password = arg[2] |
|
15 fingerprint = 'D5:F9:7B:6F:90:CC:E9:3C:36:AE:5E:3A:47:E4:C5:65' |
|
16 |
|
17 --[[ |
|
18 -- What's goes on, pretty close to C api, |
|
19 -- but already some luxury... |
|
20 you_need_your_own_context = true |
|
21 |
|
22 if you_need_your_own_context then |
|
23 cont = g.main_context.new () |
|
24 conn = lm.connection.new ( server, cont:pointer () ) |
|
25 else |
|
26 cont = g.main_context.default () |
|
27 conn = lm.connection.new ( server ) |
|
28 end |
|
29 |
|
30 if lm.ssl.supported () then |
|
31 conn:port ( 5223 ) |
|
32 conn:ssl ( lm.ssl.new ( fingerprint, |
|
33 function (ssl, err) |
|
34 print ( 'SSL error occured: ' .. err ) |
|
35 print ( 'Fingerprint: ' .. ssl:fingerprint () ) |
|
36 return true |
|
37 end ) ) |
|
38 end |
|
39 |
|
40 print ( 'Opening connection... ' ) |
|
41 res = conn:open ( |
|
42 function ( connection, success ) |
|
43 if success then |
|
44 print ( "Authenticating..." ) |
|
45 res = connection:authenticate ( username, password, 'llm-test', |
|
46 function ( connection, success ) |
|
47 if success then |
|
48 print ( "Sending..." ) |
|
49 |
|
50 connection:send ( |
|
51 lm.message.new( 'isbear@jabber.kiev.ua', 'message' ) |
|
52 :child ( 'body', 'Hello, this is just a test' ) ) |
|
53 |
|
54 connection:send ( |
|
55 lm.message.new( 'isbear@jabber.kiev.ua/lair', 'iq', 'get' ) |
|
56 :child( 'query', '' ):attribute ( 'xmlns', 'jabber:iq:version' ), |
|
57 function ( connection, message ) |
|
58 node = message:child ( 'query' ) |
|
59 print ( 'Got response:' ) |
|
60 print ( ' name: ', node:child( 'name' ):value () ) |
|
61 print ( ' version: ', node:child( 'version' ):value () ) |
|
62 print ( ' os: ', node:child( 'os' ):value () ) |
|
63 sent = true |
|
64 end ) |
|
65 else |
|
66 print ( "Authenticate failure" ) |
|
67 sent = true |
|
68 end |
|
69 end ) |
|
70 print ( 'Authenticate returns ', res ) |
|
71 else |
|
72 print ( "Open failure" ) |
|
73 sent = true |
|
74 end |
|
75 end ) |
|
76 print ( 'Open returns ', res ) |
|
77 |
|
78 --]] |
|
79 |
|
80 -- Shortened version with convenience lua functions: |
|
81 |
|
82 conn = lm.connect { |
|
83 server = server, |
|
84 port = 5223, |
|
85 ssl = { |
|
86 fingerprint = fingerprint, |
|
87 validate = false, |
|
88 }, |
|
89 username = username, |
|
90 password = password, |
|
91 resource = 'llm-test', |
|
92 onconnect = |
|
93 function ( connection ) |
|
94 print ( 'connected' ) |
|
95 |
|
96 connection:send ( |
|
97 lm.message.create { mtype = "message", to = 'isbear@jabber.kiev.ua', |
|
98 body = { 'Hello, this is just a test' }, |
|
99 } ) |
|
100 print ( 'sent' ) |
|
101 |
|
102 connection:send ( |
|
103 lm.message.create { mtype = "iq-get", to = "isbear@jabber.kiev.ua/lair", |
|
104 query = { xmlns = "jabber:iq:version" } |
|
105 }, |
|
106 function ( connection, message ) |
|
107 print ( 'received' ) |
|
108 print ( 'Got response:' ) |
|
109 print ( ' name: ', message:path( 'query', 'name' ):value () ) |
|
110 print ( ' version: ', message:path( 'query', 'version' ):value () ) |
|
111 print ( ' os: ', message:path( 'query', 'os' ):value () ) |
|
112 sent = true |
|
113 end ) |
|
114 print ( 'sent' ) |
|
115 end, |
|
116 } |
|
117 |
|
118 cont = g.main_context.default () |
|
119 print ( 'Entering main loop...' ) |
|
120 while not sent do |
|
121 cont:iteration ( true ) |
|
122 end |
|
123 |
|
124 print ( 'Closing connection... ' ) |
|
125 conn:close () |
|
126 print ( "Bye" ) |
|
127 |
|
128 -- vim: se ts=4: -- |