lm.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Wed, 16 Mar 2016 01:53:56 +0200
changeset 66 a40beb82130c
parent 59 19cfaceda6bb
permissions -rw-r--r--
node: Use new loudmouth feature - public attribute list (v0.9.7)
59
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
     1
--[[ Copyright 2009-2016 Myhailo Danylenko
23
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 18
diff changeset
     2
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 18
diff changeset
     3
This program is free software: you can redistribute it and/or modify
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 18
diff changeset
     4
it under the terms of the GNU General Public License as published by
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 18
diff changeset
     5
the Free Software Foundation, either version 2 of the License, or
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 18
diff changeset
     6
(at your option) any later version.
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 18
diff changeset
     7
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 18
diff changeset
     8
This program is distributed in the hope that it will be useful,
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 18
diff changeset
     9
but WITHOUT ANY WARRANTY; without even the implied warranty of
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 18
diff changeset
    10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 18
diff changeset
    11
GNU General Public License for more details.
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 18
diff changeset
    12
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 18
diff changeset
    13
You should have received a copy of the GNU General Public License
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 18
diff changeset
    14
along with this program.  If not, see <http://www.gnu.org/licenses/>. ]]
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    15
16
09b375e9ce32 Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents: 12
diff changeset
    16
local lm = require 'loudmouth'
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    17
59
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    18
-- Argument is a table with keys:
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    19
-- * server   - server name to connect to
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    20
-- * port     - port to connect to
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    21
-- * type     - proxy type
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    22
-- * username - uername to authenticate on proxy
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    23
-- * password - password to authenticate with
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    24
function lm.proxy.create ( a )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    25
	if type ( a ) ~= "table" then
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    26
		error "arguments should be in a table"
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    27
	end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    28
	local p = lm.proxy.new ()
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    29
	if a.server then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    30
		p:server ( a.server )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    31
	end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    32
	if a.port then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    33
		p:port ( a.port )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    34
	end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    35
	if a['type'] then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    36
		p:type ( a['type'] )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    37
	end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    38
	if a.username then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    39
		p:username ( a.username )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    40
	end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    41
	if a.password then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    42
		p:password ( a.password )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    43
	end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    44
	return p
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    45
end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    46
59
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    47
-- Argument is a table with keys:
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    48
-- * fingerprint - fingerprint string
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    49
-- * callback    - ssl error callback
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    50
-- * tls         - string - one of "on", "required", "off" (default)
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    51
-- * ca_path     - path to trusted certificates
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    52
-- * cipher_list - list of allowed ciphers to ues
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    53
function lm.ssl.create ( a )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    54
	if not lm.ssl.supported () then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    55
		-- XXX
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    56
		-- error "ssl is not supported by your loudmouth library"
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    57
		return nil
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    58
	end
59
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    59
	local fp, cb, ut, rt, ca, cl
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    60
	local st = type ( a )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    61
	if st == "table" then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    62
		fp = a.fingerprint
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    63
		cb = a.callback
59
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    64
		ca = a.ca_path
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    65
		cl = a.cipher_list
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    66
		tl = a.tls
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    67
		if tl ~= nil then
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    68
			if tl == "on" then
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    69
				ut = true
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    70
				rt = false
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    71
			elseif tl == "required" then
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    72
				ut = true
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    73
				rt = true
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    74
			end
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    75
		end
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    76
	elseif st == "function" then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    77
		cb = a
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    78
	elseif st == "string" then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    79
		fp = a
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    80
	elseif st ~= "nil" then
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    81
		error "unexpected type of argument"
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    82
	end
42
61d0ab29f17f Add ssl:tls method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 36
diff changeset
    83
	local ssl
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    84
	if fp then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    85
		if cb then
42
61d0ab29f17f Add ssl:tls method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 36
diff changeset
    86
			ssl = lm.ssl.new ( fp, cb )
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    87
		else
42
61d0ab29f17f Add ssl:tls method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 36
diff changeset
    88
			ssl = lm.ssl.new ( fp )
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    89
		end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    90
	else
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    91
		if cb then
42
61d0ab29f17f Add ssl:tls method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 36
diff changeset
    92
			ssl = lm.ssl.new ( cb )
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    93
		else
42
61d0ab29f17f Add ssl:tls method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 36
diff changeset
    94
			ssl = lm.ssl.new ()
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    95
		end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    96
	end
59
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    97
	if ca ~= nil then
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    98
		ssl:ca_path ( ca )
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
    99
	end
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
   100
	if cl ~= nil then
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
   101
		ssl:cipher_list ( cl )
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
   102
	end
42
61d0ab29f17f Add ssl:tls method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 36
diff changeset
   103
	if ut ~= nil then
61d0ab29f17f Add ssl:tls method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 36
diff changeset
   104
		ssl:tls ( ut, rt )
61d0ab29f17f Add ssl:tls method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 36
diff changeset
   105
	end
61d0ab29f17f Add ssl:tls method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 36
diff changeset
   106
	return ssl
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   107
end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   108
59
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
   109
-- Argument is a table with keys:
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
   110
-- * server          - server name
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
   111
-- * context         - glib main context
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
   112
-- * port            - server port
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
   113
-- * jid             - jid to connect with
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
   114
-- * keep_alive_rate - rate of keep alive packets
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
   115
-- * proxy           - lm.proxy object or table
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
   116
-- * ssl             - lm.ssl object or table
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
   117
-- * ondisconnect    - disconnect callback
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
   118
-- * handlers        - table with { "type/priority" = function/object } mapping
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   119
function lm.connection.create ( a )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   120
	local at = type (a)
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   121
	if at == "string" then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   122
		return lm.connection.new ( a )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   123
	elseif at == "table" then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   124
		local server = a.server
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   125
		if not server then
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   126
			error "server name parameter required"
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   127
		end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   128
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   129
		-- create connection object
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   130
		local c
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   131
		if a.context then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   132
			c = lm.connection.new ( server, a.context )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   133
		else
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   134
			c = lm.connection.new ( server )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   135
		end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   136
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   137
		-- connection parameters
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   138
		if a.port then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   139
			c:port ( a.port )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   140
		end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   141
		if a.jid then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   142
			c:jid ( a.jid )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   143
		end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   144
		if a.keep_alive_rate then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   145
			c:keep_alive_rate ( a.keep_alive_rate )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   146
		end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   147
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   148
		-- proxy
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   149
		if a.proxy then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   150
			local pt = type ( a.proxy )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   151
			if pt == "userdata" then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   152
				c:proxy ( a.proxy )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   153
			else
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   154
				local proxy = lm.proxy.create ( a.proxy )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   155
				c:proxy ( proxy )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   156
			end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   157
		end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   158
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   159
		-- ssl
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   160
		if a.ssl then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   161
			local st = type ( a.ssl )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   162
			if st == "userdata" then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   163
				c:ssl ( a.ssl )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   164
			else
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   165
				local ssl = lm.ssl.create ( a.ssl )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   166
				c:ssl ( ssl )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   167
			end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   168
		end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   169
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   170
		-- disconnect callback
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   171
		if a.ondisconnect then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   172
			c:ondisconnect ( a.ondisconnect )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   173
		end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   174
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   175
		-- message handlers
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   176
		if a.handlers then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   177
			if type ( a.handlers ) ~= "table" then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   178
				error ( "handlers parameter expected to be a table " ..
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   179
				        "of the form { \"type/priority\" = function/object }" )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   180
			end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   181
			for mhtype, handler in pairs ( a.handlers ) do
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   182
				local mtype, prio = mhtype:match ( "(.-)/(%d+)" )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   183
				if not mtype then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   184
					mtype = mhtype
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   185
					prio = 0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   186
				else
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   187
					prio = tonumber ( prio )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   188
				end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   189
				c:handler ( handler, mtype, prio )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   190
			end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   191
		end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   192
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   193
		return c
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   194
	else
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   195
		error "at least server name parameter required"
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   196
	end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   197
end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   198
66
a40beb82130c node: Use new loudmouth feature - public attribute list (v0.9.7)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 59
diff changeset
   199
function lm.message_node.parse ( node )
a40beb82130c node: Use new loudmouth feature - public attribute list (v0.9.7)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 59
diff changeset
   200
	-- attributes
a40beb82130c node: Use new loudmouth feature - public attribute list (v0.9.7)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 59
diff changeset
   201
	local r = node:attributes ()
a40beb82130c node: Use new loudmouth feature - public attribute list (v0.9.7)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 59
diff changeset
   202
	-- node value (text)
a40beb82130c node: Use new loudmouth feature - public attribute list (v0.9.7)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 59
diff changeset
   203
	local value = n:value ()
a40beb82130c node: Use new loudmouth feature - public attribute list (v0.9.7)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 59
diff changeset
   204
	if value then
a40beb82130c node: Use new loudmouth feature - public attribute list (v0.9.7)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 59
diff changeset
   205
		s[1] = value
a40beb82130c node: Use new loudmouth feature - public attribute list (v0.9.7)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 59
diff changeset
   206
	end
a40beb82130c node: Use new loudmouth feature - public attribute list (v0.9.7)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 59
diff changeset
   207
	-- children nodes
18
6effa1929af7 Children node method removed
Myhailo Danylenko <isbear@ukrpost.net>
parents: 17
diff changeset
   208
	local n = node:child ()
66
a40beb82130c node: Use new loudmouth feature - public attribute list (v0.9.7)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 59
diff changeset
   209
	for n in node:children () do
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   210
		local name = n:name ()
66
a40beb82130c node: Use new loudmouth feature - public attribute list (v0.9.7)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 59
diff changeset
   211
		if type ( r[name] ) ~= 'table' then
a40beb82130c node: Use new loudmouth feature - public attribute list (v0.9.7)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 59
diff changeset
   212
			r[name] = { }
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   213
		end
66
a40beb82130c node: Use new loudmouth feature - public attribute list (v0.9.7)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 59
diff changeset
   214
		table.insert ( r[name], lm.message_node.parse ( n ) )
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   215
	end
66
a40beb82130c node: Use new loudmouth feature - public attribute list (v0.9.7)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 59
diff changeset
   216
	return r
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   217
end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   218
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   219
function lm.message.parse ( message )
66
a40beb82130c node: Use new loudmouth feature - public attribute list (v0.9.7)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 59
diff changeset
   220
	local r = lm.message_node.parse ( message )
a40beb82130c node: Use new loudmouth feature - public attribute list (v0.9.7)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 59
diff changeset
   221
	-- message type we treat in a special way
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   222
	local mtype, subtype = message:type ()
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   223
	if subtype then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   224
		mtype = mtype .. '-' .. subtype
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   225
	end
66
a40beb82130c node: Use new loudmouth feature - public attribute list (v0.9.7)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 59
diff changeset
   226
	r['mtype'] = mtype
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   227
	return r
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   228
end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   229
59
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
   230
-- The same table, as for lm.connection.create, but with few more fields:
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
   231
-- * ssl.validate - boolean
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
   232
-- * preopen      - callback to call after connection creation but before opening (to install log handler, for example)
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
   233
-- * onopen       - callback to call after connection is established, but before authentication
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
   234
-- * onconnect    - callback to call after connection will be established
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
   235
-- * username     - username to authenticate with
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
   236
-- * password     - password to authenticate with
19cfaceda6bb ssl: Add ssl:ca_path and ssl:cipher_list methods (v0.9.4)
Myhailo Danylenko <isbear@ukrpost.net>
parents: 46
diff changeset
   237
-- * resource     - jabber resource to use
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   238
function lm.connect ( a )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   239
	if type ( a ) ~= "table" then
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   240
		error "table expected as argument"
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   241
	end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   242
	if a.ssl then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   243
		if a.ssl.validate and not a.ssl.callback then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   244
			a.ssl.callback =
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   245
				function ( obj, status )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   246
					return false
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   247
				end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   248
		end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   249
	end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   250
	local c = lm.connection.create ( a )
36
41df6f2a7aa8 Add preconnect hook to lm.connect
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   251
	if type ( a.preopen ) == "function" then
41df6f2a7aa8 Add preconnect hook to lm.connect
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   252
		a.preopen ( c ) -- XXX
41df6f2a7aa8 Add preconnect hook to lm.connect
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   253
	end
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   254
	c:open (
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   255
		function ( obj, status )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   256
			if status then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   257
				if type ( a.onopen ) == "function" then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   258
					a.onopen ( obj )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   259
				end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   260
				c:authenticate ( a.username, a.password, a.resource,
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   261
					function ( obj, status )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   262
						if type ( a.onconnect ) == "function" then
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   263
							a.onconnect ( obj )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   264
						end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   265
					end )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   266
			end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   267
		end )
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   268
	return c
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   269
end
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   270
16
09b375e9ce32 Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents: 12
diff changeset
   271
return lm
09b375e9ce32 Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents: 12
diff changeset
   272
36
41df6f2a7aa8 Add preconnect hook to lm.connect
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   273
-- vim: se ts=4 sw=4: --