lm.lua
changeset 18 6effa1929af7
parent 17 ab4470465a0c
child 23 13f03e604c8a
equal deleted inserted replaced
17:ab4470465a0c 18:6effa1929af7
   151 	else
   151 	else
   152 		error "at least server name parameter required"
   152 		error "at least server name parameter required"
   153 	end
   153 	end
   154 end
   154 end
   155 
   155 
   156 --[=[
       
   157 -- recursively fills a node, see lm.message.create
       
   158 function lm.message_node.fill ( n, a )
       
   159 	for name, value in pairs ( a ) do
       
   160 		if type ( value ) == "table" then
       
   161 			if type ( value[1] ) == "table" then
       
   162 				for index, instance in ipairs ( value ) do
       
   163 					lm.message_node.fill ( n:child ( name, "" ), instance )
       
   164 				end
       
   165 			else
       
   166 				lm.message_node.fill ( n:child ( name, "" ), value )
       
   167 			end
       
   168 		elseif name == 1 then
       
   169 			n:value ( value )
       
   170 		else
       
   171 			n:attribute ( name, value )
       
   172 		end
       
   173 	end
       
   174 end
       
   175 --[[
       
   176 -- recursively fills a message
       
   177 lm.message.create { mtype = 'iq-result', to = 'foo@bar.xyz',
       
   178 	command = { xmlns = 'http://jabber.org/protocol/commands', node = 'http://jabber.org/protocol/rc#set-status', status = 'executing', sessionid = 'set-status:aaa3',
       
   179 		x = { xmlns = 'jabber:x:data', type = 'form',
       
   180 			title = { "Change Status" },
       
   181 			instructions = { "Choose the status and status message" },
       
   182 			field = {{ type = 'hidden', var = 'FORM_TYPE',
       
   183 				value = { "http://jabber.org/protocol/rc" },
       
   184 			},{ type = 'list-single', label = 'Status', var = 'status',
       
   185 				required = { },
       
   186 				value = { "online" },
       
   187 				option = {{ label = 'Chat',
       
   188 					value = { "chat" },
       
   189 				},{ label = 'Online',
       
   190 					value = { "online" },
       
   191 				},{ label = 'Away',
       
   192 					value = { "away" },
       
   193 				},{ label = 'Extended Away',
       
   194 					value = { "xa" },
       
   195 				},{ label = 'Do Not Disturb',
       
   196 					value = { "dnd" },
       
   197 				},{ label = 'Invisible',
       
   198 					value = { "invisible" },
       
   199 				},{ label = 'Offline',
       
   200 					value = { "offline" },
       
   201 				}},
       
   202 			},{ type = 'text-single', label = 'Priority', var = 'status-priority',
       
   203 				value = { "5" },
       
   204 			},{ type = 'text-multi', label = 'Message', var = 'status-message' }},
       
   205 		},
       
   206 	},
       
   207 }
       
   208 --]]
       
   209 function lm.message.create ( a )
       
   210 	if type ( a ) ~= "table" then
       
   211 		error "table expected as argument"
       
   212 	end
       
   213 	if not a.mtype then
       
   214 		error "you must specify message type"
       
   215 	end
       
   216 	local mtype, subtype = a.mtype:match ( "(.-)%-(.+)" )
       
   217 	local m
       
   218 	if not mtype then
       
   219 		m = lm.message.new ( a.to, a.mtype )
       
   220 	else
       
   221 		m = lm.message.new ( a.to, mtype, subtype )
       
   222 	end
       
   223 	a.to = nil
       
   224 	a.mtype = nil
       
   225 	lm.message_node.fill ( m, a )
       
   226 	return m
       
   227 end
       
   228 --]=]
       
   229 
       
   230 -- TODO: multiple nodes with same name
   156 -- TODO: multiple nodes with same name
   231 function lm.message_node.parse ( node, r )
   157 function lm.message_node.parse ( node, r )
   232 	local n = node:children ()
   158 	local n = node:child ()
   233 	while n do
   159 	while n do
   234 		local name = n:name ()
   160 		local name = n:name ()
   235 		r[name] = { }
   161 		r[name] = { }
   236 		local value = n:value ()
   162 		local value = n:value ()
   237 		if value then
   163 		if value then