author | Myhailo Danylenko <isbear@ukrpost.net> |
Wed, 28 Nov 2012 01:55:12 +0200 | |
changeset 53 | 2dcfa81100e4 |
parent 29 | e86ee1f4227d |
child 57 | 37ed3c7ac1b6 |
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 |
|
53 | 20 |
local g = require 'glib' |
21 | 21 |
local lm = require 'lm' |
22 |
||
23 |
g.log.handler ( |
|
24 |
function ( domain, lvl, mesg ) |
|
25 |
print ( mesg ) |
|
26 |
end ) |
|
27 |
||
53 | 28 |
local sent = false |
21 | 29 |
|
53 | 30 |
local username, server = arg[1]:match ( '(.+)@(.+)' ) |
31 |
local password = arg[2] |
|
32 |
local recipient = arg[3] or arg[1] |
|
33 |
local fingerprint = arg[4] or '01:23:45:67:89:ab:cd:ef:fe:dc:ba:98:76:54:32:10' |
|
34 |
||
35 |
print ( ("Connectng to %s with username %s\nWill contact %s."):format ( server, username, recipient ) ) |
|
21 | 36 |
|
37 |
--[[ |
|
38 |
-- What's goes on, pretty close to C api, |
|
39 |
-- but already some luxury... |
|
40 |
you_need_your_own_context = true |
|
41 |
||
42 |
if you_need_your_own_context then |
|
43 |
cont = g.main_context.new () |
|
44 |
conn = lm.connection.new ( server, cont:pointer () ) |
|
45 |
else |
|
46 |
cont = g.main_context.default () |
|
47 |
conn = lm.connection.new ( server ) |
|
48 |
end |
|
49 |
||
50 |
if lm.ssl.supported () then |
|
51 |
conn:port ( 5223 ) |
|
52 |
conn:ssl ( lm.ssl.new ( fingerprint, |
|
53 |
function (ssl, err) |
|
54 |
print ( 'SSL error occured: ' .. err ) |
|
55 |
print ( 'Fingerprint: ' .. ssl:fingerprint () ) |
|
56 |
return true |
|
57 |
end ) ) |
|
58 |
end |
|
59 |
||
60 |
print ( 'Opening connection... ' ) |
|
61 |
res = conn:open ( |
|
62 |
function ( connection, success ) |
|
63 |
if success then |
|
64 |
print ( "Authenticating..." ) |
|
65 |
res = connection:authenticate ( username, password, 'llm-test', |
|
66 |
function ( connection, success ) |
|
67 |
if success then |
|
68 |
print ( "Sending..." ) |
|
69 |
|
|
70 |
connection:send ( |
|
22
1a1bc121fe90
Test gets parameters from build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
21
diff
changeset
|
71 |
lm.message.new( recipient, 'message' ) |
21 | 72 |
:child ( 'body', 'Hello, this is just a test' ) ) |
73 |
||
74 |
connection:send ( |
|
22
1a1bc121fe90
Test gets parameters from build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
21
diff
changeset
|
75 |
lm.message.new( recipient, 'iq', 'get' ) |
21 | 76 |
:child( 'query', '' ):attribute ( 'xmlns', 'jabber:iq:version' ), |
77 |
function ( connection, message ) |
|
78 |
node = message:child ( 'query' ) |
|
79 |
print ( 'Got response:' ) |
|
80 |
print ( ' name: ', node:child( 'name' ):value () ) |
|
81 |
print ( ' version: ', node:child( 'version' ):value () ) |
|
82 |
print ( ' os: ', node:child( 'os' ):value () ) |
|
83 |
sent = true |
|
84 |
end ) |
|
85 |
else |
|
86 |
print ( "Authenticate failure" ) |
|
87 |
sent = true |
|
88 |
end |
|
89 |
end ) |
|
90 |
print ( 'Authenticate returns ', res ) |
|
91 |
else |
|
92 |
print ( "Open failure" ) |
|
93 |
sent = true |
|
94 |
end |
|
95 |
end ) |
|
96 |
print ( 'Open returns ', res ) |
|
97 |
||
98 |
--]] |
|
99 |
||
100 |
-- Shortened version with convenience lua functions: |
|
101 |
||
53 | 102 |
local conn = lm.connect { |
21 | 103 |
server = server, |
53 | 104 |
port = 5222, |
21 | 105 |
ssl = { |
106 |
fingerprint = fingerprint, |
|
107 |
validate = false, |
|
108 |
}, |
|
109 |
username = username, |
|
110 |
password = password, |
|
111 |
resource = 'llm-test', |
|
112 |
onconnect = |
|
113 |
function ( connection ) |
|
114 |
print ( 'connected' ) |
|
115 |
||
116 |
connection:send ( |
|
22
1a1bc121fe90
Test gets parameters from build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
21
diff
changeset
|
117 |
lm.message.create { mtype = "message", to = recipient, |
21 | 118 |
body = { 'Hello, this is just a test' }, |
119 |
} ) |
|
120 |
print ( 'sent' ) |
|
121 |
||
122 |
connection:send ( |
|
22
1a1bc121fe90
Test gets parameters from build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
21
diff
changeset
|
123 |
lm.message.create { mtype = "iq-get", to = recipient, |
21 | 124 |
query = { xmlns = "jabber:iq:version" } |
125 |
}, |
|
126 |
function ( connection, message ) |
|
127 |
print ( 'received' ) |
|
128 |
print ( 'Got response:' ) |
|
129 |
print ( ' name: ', message:path( 'query', 'name' ):value () ) |
|
130 |
print ( ' version: ', message:path( 'query', 'version' ):value () ) |
|
131 |
print ( ' os: ', message:path( 'query', 'os' ):value () ) |
|
132 |
sent = true |
|
133 |
end ) |
|
134 |
print ( 'sent' ) |
|
135 |
end, |
|
136 |
} |
|
137 |
||
53 | 138 |
local cont = g.main_context.default () |
21 | 139 |
print ( 'Entering main loop...' ) |
140 |
while not sent do |
|
141 |
cont:iteration ( true ) |
|
142 |
end |
|
143 |
||
144 |
print ( 'Closing connection... ' ) |
|
145 |
conn:close () |
|
146 |
print ( "Bye" ) |
|
147 |
||
148 |
-- vim: se ts=4: -- |