author | Myhailo Danylenko <isbear@ukrpost.net> |
Tue, 19 May 2009 01:16:55 +0300 | |
changeset 22 | 1a1bc121fe90 |
parent 21 | a9b92b65961e |
child 23 | 13f03e604c8a |
permissions | -rw-r--r-- |
21 | 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 |
||
22
1a1bc121fe90
Test gets parameters from build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
21
diff
changeset
|
12 |
server, username = arg[1]:match ( '(.+)@(.-)' ) |
1a1bc121fe90
Test gets parameters from build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
21
diff
changeset
|
13 |
password = arg[2] |
1a1bc121fe90
Test gets parameters from build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
21
diff
changeset
|
14 |
recipient = arg[3] or arg[1] |
1a1bc121fe90
Test gets parameters from build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
21
diff
changeset
|
15 |
fingerprint = arg[4] or '01:23:45:67:89:ab:cd:ef:fe:dc:ba:98:76:54:32:10' |
21 | 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 ( |
|
22
1a1bc121fe90
Test gets parameters from build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
21
diff
changeset
|
51 |
lm.message.new( recipient, 'message' ) |
21 | 52 |
:child ( 'body', 'Hello, this is just a test' ) ) |
53 |
||
54 |
connection:send ( |
|
22
1a1bc121fe90
Test gets parameters from build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
21
diff
changeset
|
55 |
lm.message.new( recipient, 'iq', 'get' ) |
21 | 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 ( |
|
22
1a1bc121fe90
Test gets parameters from build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
21
diff
changeset
|
97 |
lm.message.create { mtype = "message", to = recipient, |
21 | 98 |
body = { 'Hello, this is just a test' }, |
99 |
} ) |
|
100 |
print ( 'sent' ) |
|
101 |
||
102 |
connection:send ( |
|
22
1a1bc121fe90
Test gets parameters from build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
21
diff
changeset
|
103 |
lm.message.create { mtype = "iq-get", to = recipient, |
21 | 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: -- |