isbear@23: --[[ Copyright 2009 Myhailo Danylenko isbear@23: isbear@23: This program is free software: you can redistribute it and/or modify isbear@23: it under the terms of the GNU General Public License as published by isbear@23: the Free Software Foundation, either version 2 of the License, or isbear@23: (at your option) any later version. isbear@23: isbear@23: This program is distributed in the hope that it will be useful, isbear@23: but WITHOUT ANY WARRANTY; without even the implied warranty of isbear@23: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the isbear@23: GNU General Public License for more details. isbear@23: isbear@23: You should have received a copy of the GNU General Public License isbear@23: along with this program. If not, see . ]] isbear@23: isbear@23: -- Note, this requires glib module, that is not released yet isbear@23: -- (and, probably, will not be released ever), thus, this isbear@23: -- code is provided only as example. isbear@21: isbear@53: local g = require 'glib' isbear@21: local lm = require 'lm' isbear@21: isbear@21: g.log.handler ( isbear@21: function ( domain, lvl, mesg ) isbear@21: print ( mesg ) isbear@21: end ) isbear@21: isbear@53: local sent = false isbear@21: isbear@53: local username, server = arg[1]:match ( '(.+)@(.+)' ) isbear@53: local password = arg[2] isbear@53: local recipient = arg[3] or arg[1] isbear@57: local fingerprint = arg[4] isbear@57: isbear@57: if not fingerprint then isbear@57: local ssl_fp_format = lm.ssl.supported () isbear@57: if ssl_fp_format then isbear@57: if ssl_fp_format == 'MD5' then isbear@57: fingerprint = '01:23:45:67:89:ab:cd:ef:fe:dc:ba:98:76:54:32:10' isbear@57: else isbear@57: fingerprint = 'SHA256:0123456789abcdeffedcba98765432100123456789acbdeffedcba9876543210' isbear@57: end isbear@57: end isbear@57: end isbear@53: isbear@53: print ( ("Connectng to %s with username %s\nWill contact %s."):format ( server, username, recipient ) ) isbear@21: isbear@21: --[[ isbear@21: -- What's goes on, pretty close to C api, isbear@21: -- but already some luxury... isbear@21: you_need_your_own_context = true isbear@21: isbear@21: if you_need_your_own_context then isbear@21: cont = g.main_context.new () isbear@21: conn = lm.connection.new ( server, cont:pointer () ) isbear@21: else isbear@21: cont = g.main_context.default () isbear@21: conn = lm.connection.new ( server ) isbear@21: end isbear@21: isbear@21: if lm.ssl.supported () then isbear@21: conn:port ( 5223 ) isbear@21: conn:ssl ( lm.ssl.new ( fingerprint, isbear@21: function (ssl, err) isbear@21: print ( 'SSL error occured: ' .. err ) isbear@21: print ( 'Fingerprint: ' .. ssl:fingerprint () ) isbear@21: return true isbear@21: end ) ) isbear@21: end isbear@21: isbear@21: print ( 'Opening connection... ' ) isbear@21: res = conn:open ( isbear@21: function ( connection, success ) isbear@21: if success then isbear@21: print ( "Authenticating..." ) isbear@21: res = connection:authenticate ( username, password, 'llm-test', isbear@21: function ( connection, success ) isbear@21: if success then isbear@21: print ( "Sending..." ) isbear@21: isbear@21: connection:send ( isbear@22: lm.message.new( recipient, 'message' ) isbear@21: :child ( 'body', 'Hello, this is just a test' ) ) isbear@21: isbear@21: connection:send ( isbear@22: lm.message.new( recipient, 'iq', 'get' ) isbear@21: :child( 'query', '' ):attribute ( 'xmlns', 'jabber:iq:version' ), isbear@21: function ( connection, message ) isbear@21: node = message:child ( 'query' ) isbear@21: print ( 'Got response:' ) isbear@21: print ( ' name: ', node:child( 'name' ):value () ) isbear@21: print ( ' version: ', node:child( 'version' ):value () ) isbear@21: print ( ' os: ', node:child( 'os' ):value () ) isbear@21: sent = true isbear@21: end ) isbear@21: else isbear@21: print ( "Authenticate failure" ) isbear@21: sent = true isbear@21: end isbear@21: end ) isbear@21: print ( 'Authenticate returns ', res ) isbear@21: else isbear@21: print ( "Open failure" ) isbear@21: sent = true isbear@21: end isbear@21: end ) isbear@21: print ( 'Open returns ', res ) isbear@21: isbear@21: --]] isbear@21: isbear@21: -- Shortened version with convenience lua functions: isbear@21: isbear@53: local conn = lm.connect { isbear@21: server = server, isbear@53: port = 5222, isbear@21: ssl = { isbear@21: fingerprint = fingerprint, isbear@21: validate = false, isbear@21: }, isbear@21: username = username, isbear@21: password = password, isbear@21: resource = 'llm-test', isbear@21: onconnect = isbear@21: function ( connection ) isbear@21: print ( 'connected' ) isbear@21: isbear@21: connection:send ( isbear@22: lm.message.create { mtype = "message", to = recipient, isbear@21: body = { 'Hello, this is just a test' }, isbear@21: } ) isbear@21: print ( 'sent' ) isbear@21: isbear@21: connection:send ( isbear@22: lm.message.create { mtype = "iq-get", to = recipient, isbear@21: query = { xmlns = "jabber:iq:version" } isbear@21: }, isbear@21: function ( connection, message ) isbear@21: print ( 'received' ) isbear@21: print ( 'Got response:' ) isbear@21: print ( ' name: ', message:path( 'query', 'name' ):value () ) isbear@21: print ( ' version: ', message:path( 'query', 'version' ):value () ) isbear@21: print ( ' os: ', message:path( 'query', 'os' ):value () ) isbear@21: sent = true isbear@21: end ) isbear@21: print ( 'sent' ) isbear@21: end, isbear@21: } isbear@21: isbear@53: local cont = g.main_context.default () isbear@21: print ( 'Entering main loop...' ) isbear@21: while not sent do isbear@21: cont:iteration ( true ) isbear@21: end isbear@21: isbear@21: print ( 'Closing connection... ' ) isbear@21: conn:close () isbear@21: print ( "Bye" ) isbear@21: isbear@57: -- vim: se ts=4 sw=4: --