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