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