examples/pep.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Fri, 27 Mar 2009 01:46:53 +0200
changeset 64 bf7521ed96eb
parent 59 4660c4f10ef1
child 66 542f61e113cb
permissions -rw-r--r--
Rewrite of ibb in object style


-- PERSONAL EVENTING PROTOCOL (XEP-0163)

-- library

require 'lm'
local iq = require 'iq'

-- public

pep = {
	handlers = {},
}

function pep.publish ( conn, node, item, success, fail )
--	local bjid = conn:jid():gsub ( '/.*', '' )
--	item.id = 'current'
	iq.send ( conn, nil, 'set',
		{
			pubsub = { xmlns = 'http://jabber.org/protocol/pubsub',
				publish = { node = node,
					item = item,
				},
			},
--[[
			configure = {
				x = {
					field = {{ type = "hidden", var = 'FORM_TYPE',
						value = { 'http://jabber.org/protocol/pubsub#node_config' },
					},{ var = "pubsub#access_model",
						value = { 'presence' },
					}},
				},
			},
--]]
		}, success, fail )
end

-- private

-- XXX in fact, it is not a pep handler, it is pubsub handler.
--     should it go there?
local pep_incoming_message_handler = lm.message_handler.new (
	function ( conn, mess )
		local e = mess:child ( 'event' )
		if e and e:attribute ( 'xmlns' ) == 'http://jabber.org/protocol/pubsub#event' then
			local is = e:child ( 'items' )
			if is then
				local from = mess:attribute ( 'from' )
				local node = is:attribute ( 'node' )
				local item = is:child ( 'item' )
				if item then
					local handled = true -- XXX should we do this? well, if it becomes general pubsub handler - then no.
					local n       = item:children ()
					while n do
						local xmlns = n:attribute ( 'xmlns' )
						if pep.handlers[xmlns] then
							if not pep.handlers[xmlns] ( from, node, n ) then
								handled = false
							end
						else
							handled = false
						end
						n = n:next ()
					end
					return handled
				end
			end
		end
		return false
	end )

-- mcabber

local pep_handler_registered = false

hooks_d['hook-post-connect'].pep =
	function ( args )
		lm.connection.bless( main.connection () ):handler ( pep_incoming_message_handler, 'message', 'normal' )
		pep_handler_registered = true
		hooks_d['hook-post-connect'].pep = nil
		hooks_d['hook-quit'].pep =
			function ( args )
				if pep_handler_registered then
					lm.connection.bless( main.connection () ):handler ( pep_incoming_message_handler, 'message' )
					pep_handler_registered = false
				end
			end
	end

-- vim: se ts=4: --