author | Myhailo Danylenko <isbear@ukrpost.net> |
Thu, 21 Jan 2010 12:25:40 +0200 | |
changeset 34 | 6bfad7c963f4 |
parent 29 | e86ee1f4227d |
child 53 | 2dcfa81100e4 |
permissions | -rw-r--r-- |
23 | 1 |
--[[ Copyright 2009 Myhailo Danylenko |
2 |
||
3 |
This program is free software: you can redistribute it and/or modify |
|
4 |
it under the terms of the GNU General Public License as published by |
|
5 |
the Free Software Foundation, either version 2 of the License, or |
|
6 |
(at your option) any later version. |
|
7 |
||
8 |
This program is distributed in the hope that it will be useful, |
|
9 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 |
GNU General Public License for more details. |
|
12 |
||
13 |
You should have received a copy of the GNU General Public License |
|
14 |
along with this program. If not, see <http://www.gnu.org/licenses/>. ]] |
|
15 |
||
16 |
-- Note, this requires glib module, that is not released yet |
|
17 |
-- (and, probably, will not be released ever), thus, this |
|
18 |
-- code is provided only as example. |
|
21 | 19 |
|
20 |
require 'glib' |
|
21 |
local lm = require 'lm' |
|
22 |
||
23 |
g.log.handler ( |
|
24 |
function ( domain, lvl, mesg ) |
|
25 |
print ( mesg ) |
|
26 |
end ) |
|
27 |
||
28 |
sent = false |
|
29 |
||
29
e86ee1f4227d
Fix jid parsing in test
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
30 |
username, server = arg[1]:match ( '(.+)@(.+)' ) |
22
1a1bc121fe90
Test gets parameters from build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
21
diff
changeset
|
31 |
password = arg[2] |
1a1bc121fe90
Test gets parameters from build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
21
diff
changeset
|
32 |
recipient = arg[3] or arg[1] |
1a1bc121fe90
Test gets parameters from build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
21
diff
changeset
|
33 |
fingerprint = arg[4] or '01:23:45:67:89:ab:cd:ef:fe:dc:ba:98:76:54:32:10' |
21 | 34 |
|
35 |
--[[ |
|
36 |
-- What's goes on, pretty close to C api, |
|
37 |
-- but already some luxury... |
|
38 |
you_need_your_own_context = true |
|
39 |
||
40 |
if you_need_your_own_context then |
|
41 |
cont = g.main_context.new () |
|
42 |
conn = lm.connection.new ( server, cont:pointer () ) |
|
43 |
else |
|
44 |
cont = g.main_context.default () |
|
45 |
conn = lm.connection.new ( server ) |
|
46 |
end |
|
47 |
||
48 |
if lm.ssl.supported () then |
|
49 |
conn:port ( 5223 ) |
|
50 |
conn:ssl ( lm.ssl.new ( fingerprint, |
|
51 |
function (ssl, err) |
|
52 |
print ( 'SSL error occured: ' .. err ) |
|
53 |
print ( 'Fingerprint: ' .. ssl:fingerprint () ) |
|
54 |
return true |
|
55 |
end ) ) |
|
56 |
end |
|
57 |
||
58 |
print ( 'Opening connection... ' ) |
|
59 |
res = conn:open ( |
|
60 |
function ( connection, success ) |
|
61 |
if success then |
|
62 |
print ( "Authenticating..." ) |
|
63 |
res = connection:authenticate ( username, password, 'llm-test', |
|
64 |
function ( connection, success ) |
|
65 |
if success then |
|
66 |
print ( "Sending..." ) |
|
67 |
|
|
68 |
connection:send ( |
|
22
1a1bc121fe90
Test gets parameters from build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
21
diff
changeset
|
69 |
lm.message.new( recipient, 'message' ) |
21 | 70 |
:child ( 'body', 'Hello, this is just a test' ) ) |
71 |
||
72 |
connection:send ( |
|
22
1a1bc121fe90
Test gets parameters from build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
21
diff
changeset
|
73 |
lm.message.new( recipient, 'iq', 'get' ) |
21 | 74 |
:child( 'query', '' ):attribute ( 'xmlns', 'jabber:iq:version' ), |
75 |
function ( connection, message ) |
|
76 |
node = message:child ( 'query' ) |
|
77 |
print ( 'Got response:' ) |
|
78 |
print ( ' name: ', node:child( 'name' ):value () ) |
|
79 |
print ( ' version: ', node:child( 'version' ):value () ) |
|
80 |
print ( ' os: ', node:child( 'os' ):value () ) |
|
81 |
sent = true |
|
82 |
end ) |
|
83 |
else |
|
84 |
print ( "Authenticate failure" ) |
|
85 |
sent = true |
|
86 |
end |
|
87 |
end ) |
|
88 |
print ( 'Authenticate returns ', res ) |
|
89 |
else |
|
90 |
print ( "Open failure" ) |
|
91 |
sent = true |
|
92 |
end |
|
93 |
end ) |
|
94 |
print ( 'Open returns ', res ) |
|
95 |
||
96 |
--]] |
|
97 |
||
98 |
-- Shortened version with convenience lua functions: |
|
99 |
||
100 |
conn = lm.connect { |
|
101 |
server = server, |
|
102 |
port = 5223, |
|
103 |
ssl = { |
|
104 |
fingerprint = fingerprint, |
|
105 |
validate = false, |
|
106 |
}, |
|
107 |
username = username, |
|
108 |
password = password, |
|
109 |
resource = 'llm-test', |
|
110 |
onconnect = |
|
111 |
function ( connection ) |
|
112 |
print ( 'connected' ) |
|
113 |
||
114 |
connection:send ( |
|
22
1a1bc121fe90
Test gets parameters from build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
21
diff
changeset
|
115 |
lm.message.create { mtype = "message", to = recipient, |
21 | 116 |
body = { 'Hello, this is just a test' }, |
117 |
} ) |
|
118 |
print ( 'sent' ) |
|
119 |
||
120 |
connection:send ( |
|
22
1a1bc121fe90
Test gets parameters from build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
21
diff
changeset
|
121 |
lm.message.create { mtype = "iq-get", to = recipient, |
21 | 122 |
query = { xmlns = "jabber:iq:version" } |
123 |
}, |
|
124 |
function ( connection, message ) |
|
125 |
print ( 'received' ) |
|
126 |
print ( 'Got response:' ) |
|
127 |
print ( ' name: ', message:path( 'query', 'name' ):value () ) |
|
128 |
print ( ' version: ', message:path( 'query', 'version' ):value () ) |
|
129 |
print ( ' os: ', message:path( 'query', 'os' ):value () ) |
|
130 |
sent = true |
|
131 |
end ) |
|
132 |
print ( 'sent' ) |
|
133 |
end, |
|
134 |
} |
|
135 |
||
136 |
cont = g.main_context.default () |
|
137 |
print ( 'Entering main loop...' ) |
|
138 |
while not sent do |
|
139 |
cont:iteration ( true ) |
|
140 |
end |
|
141 |
||
142 |
print ( 'Closing connection... ' ) |
|
143 |
conn:close () |
|
144 |
print ( "Bye" ) |
|
145 |
||
146 |
-- vim: se ts=4: -- |