lm.lua
changeset 59 19cfaceda6bb
parent 46 d4484a8ed66b
child 66 a40beb82130c
equal deleted inserted replaced
58:24998d36f3e4 59:19cfaceda6bb
     1 --[[ Copyright 2009 Myhailo Danylenko
     1 --[[ Copyright 2009-2016 Myhailo Danylenko
     2 
     2 
     3 This program is free software: you can redistribute it and/or modify
     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
     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
     5 the Free Software Foundation, either version 2 of the License, or
     6 (at your option) any later version.
     6 (at your option) any later version.
    13 You should have received a copy of the GNU General Public License
    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/>. ]]
    14 along with this program.  If not, see <http://www.gnu.org/licenses/>. ]]
    15 
    15 
    16 local lm = require 'loudmouth'
    16 local lm = require 'loudmouth'
    17 
    17 
    18 -- argument is a table with keys,
    18 -- Argument is a table with keys:
    19 -- corresponding to method names.
    19 -- * server   - server name to connect to
       
    20 -- * port     - port to connect to
       
    21 -- * type     - proxy type
       
    22 -- * username - uername to authenticate on proxy
       
    23 -- * password - password to authenticate with
    20 function lm.proxy.create ( a )
    24 function lm.proxy.create ( a )
    21 	if type ( a ) ~= "table" then
    25 	if type ( a ) ~= "table" then
    22 		error "arguments should be in a table"
    26 		error "arguments should be in a table"
    23 	end
    27 	end
    24 	local p = lm.proxy.new ()
    28 	local p = lm.proxy.new ()
    38 		p:password ( a.password )
    42 		p:password ( a.password )
    39 	end
    43 	end
    40 	return p
    44 	return p
    41 end
    45 end
    42 
    46 
    43 -- argument is a table with two keys:
    47 -- Argument is a table with keys:
    44 -- callback and fingerprint
    48 -- * fingerprint - fingerprint string
       
    49 -- * callback    - ssl error callback
       
    50 -- * tls         - string - one of "on", "required", "off" (default)
       
    51 -- * ca_path     - path to trusted certificates
       
    52 -- * cipher_list - list of allowed ciphers to ues
    45 function lm.ssl.create ( a )
    53 function lm.ssl.create ( a )
    46 	if not lm.ssl.supported () then
    54 	if not lm.ssl.supported () then
    47 		-- XXX
    55 		-- XXX
    48 		-- error "ssl is not supported by your loudmouth library"
    56 		-- error "ssl is not supported by your loudmouth library"
    49 		return nil
    57 		return nil
    50 	end
    58 	end
    51 	local fp, cb, ut, rt
    59 	local fp, cb, ut, rt, ca, cl
    52 	local st = type ( a )
    60 	local st = type ( a )
    53 	if st == "table" then
    61 	if st == "table" then
    54 		fp = a.fingerprint
    62 		fp = a.fingerprint
    55 		cb = a.callback
    63 		cb = a.callback
    56 		ut = a.tls
    64 		ca = a.ca_path
    57 		rt = a.require_tls
    65 		cl = a.cipher_list
       
    66 		tl = a.tls
       
    67 		if tl ~= nil then
       
    68 			if tl == "on" then
       
    69 				ut = true
       
    70 				rt = false
       
    71 			elseif tl == "required" then
       
    72 				ut = true
       
    73 				rt = true
       
    74 			end
       
    75 		end
    58 	elseif st == "function" then
    76 	elseif st == "function" then
    59 		cb = a
    77 		cb = a
    60 	elseif st == "string" then
    78 	elseif st == "string" then
    61 		fp = a
    79 		fp = a
    62 	elseif st ~= "nil" then
    80 	elseif st ~= "nil" then
    74 			ssl = lm.ssl.new ( cb )
    92 			ssl = lm.ssl.new ( cb )
    75 		else
    93 		else
    76 			ssl = lm.ssl.new ()
    94 			ssl = lm.ssl.new ()
    77 		end
    95 		end
    78 	end
    96 	end
       
    97 	if ca ~= nil then
       
    98 		ssl:ca_path ( ca )
       
    99 	end
       
   100 	if cl ~= nil then
       
   101 		ssl:cipher_list ( cl )
       
   102 	end
    79 	if ut ~= nil then
   103 	if ut ~= nil then
    80 		ssl:tls ( ut, rt )
   104 		ssl:tls ( ut, rt )
    81 	end
   105 	end
    82 	return ssl
   106 	return ssl
    83 end
   107 end
    84 
   108 
    85 -- basically, it just provides a way
   109 -- Argument is a table with keys:
    86 -- to initialize many parameters at once.
   110 -- * server          - server name
    87 -- keys in a table correspond to methods
   111 -- * context         - glib main context
    88 -- of connection object, except for handlers,
   112 -- * port            - server port
    89 -- where format is {
   113 -- * jid             - jid to connect with
    90 --     "type/priority" = function/object,
   114 -- * keep_alive_rate - rate of keep alive packets
    91 --     ...
   115 -- * proxy           - lm.proxy object or table
    92 -- }
   116 -- * ssl             - lm.ssl object or table
    93 -- two extra keys - server and context.
   117 -- * ondisconnect    - disconnect callback
    94 -- ssl and proxy objects can either be objects
   118 -- * handlers        - table with { "type/priority" = function/object } mapping
    95 -- or tables, directly passed to corresponding
       
    96 -- create routine.
       
    97 function lm.connection.create ( a )
   119 function lm.connection.create ( a )
    98 	local at = type (a)
   120 	local at = type (a)
    99 	if at == "string" then
   121 	if at == "string" then
   100 		return lm.connection.new ( a )
   122 		return lm.connection.new ( a )
   101 	elseif at == "table" then
   123 	elseif at == "table" then
   203 	end
   225 	end
   204 	lm.message_node.parse ( message, r )
   226 	lm.message_node.parse ( message, r )
   205 	return r
   227 	return r
   206 end
   228 end
   207 
   229 
   208 -- the same table, as for lm.connection.create, but with few more fields:
   230 -- The same table, as for lm.connection.create, but with few more fields:
   209 -- ssl.validate - boolean
   231 -- * ssl.validate - boolean
   210 -- preopen      - callback to call after connection creation but before opening (to install log handler, for example)
   232 -- * preopen      - callback to call after connection creation but before opening (to install log handler, for example)
   211 -- onopen       - callback to call after connection is established, but before authentication
   233 -- * onopen       - callback to call after connection is established, but before authentication
   212 -- onconnect    - callback to call after connection will be established
   234 -- * onconnect    - callback to call after connection will be established
   213 -- username
   235 -- * username     - username to authenticate with
   214 -- password
   236 -- * password     - password to authenticate with
   215 -- resource
   237 -- * resource     - jabber resource to use
   216 function lm.connect ( a )
   238 function lm.connect ( a )
   217 	if type ( a ) ~= "table" then
   239 	if type ( a ) ~= "table" then
   218 		error "table expected as argument"
   240 		error "table expected as argument"
   219 	end
   241 	end
   220 	if a.ssl then
   242 	if a.ssl then