examples/pep.lua
changeset 66 542f61e113cb
parent 64 bf7521ed96eb
equal deleted inserted replaced
65:f1be8dbb209c 66:542f61e113cb
     1 
     1 
     2 -- PERSONAL EVENTING PROTOCOL (XEP-0163)
     2 -- PERSONAL EVENTING PROTOCOL (XEP-0163)
     3 
     3 
     4 -- library
     4 -- library
     5 
     5 
     6 require 'lm'
     6 local lm = require 'lm'
     7 local iq = require 'iq'
     7 local iq = require 'iq'
     8 
     8 
     9 -- public
     9 --
    10 
    10 
    11 pep = {
    11 local F = { }
    12 	handlers = {},
       
    13 }
       
    14 
    12 
    15 function pep.publish ( conn, node, item, success, fail )
    13 function F.publish ( conn, node, item, success, fail )
    16 --	local bjid = conn:jid():gsub ( '/.*', '' )
    14 --	local bjid = conn:jid():gsub ( '/.*', '' )
    17 --	item.id = 'current'
    15 --	item.id = 'current'
    18 	iq.send ( conn, nil, 'set',
    16 	iq.send ( conn, nil, 'set',
    19 		{
    17 		{
    20 			pubsub = { xmlns = 'http://jabber.org/protocol/pubsub',
    18 			pubsub = { xmlns = 'http://jabber.org/protocol/pubsub',
    34 			},
    32 			},
    35 --]]
    33 --]]
    36 		}, success, fail )
    34 		}, success, fail )
    37 end
    35 end
    38 
    36 
    39 -- private
    37 return F
    40 
       
    41 -- XXX in fact, it is not a pep handler, it is pubsub handler.
       
    42 --     should it go there?
       
    43 local pep_incoming_message_handler = lm.message_handler.new (
       
    44 	function ( conn, mess )
       
    45 		local e = mess:child ( 'event' )
       
    46 		if e and e:attribute ( 'xmlns' ) == 'http://jabber.org/protocol/pubsub#event' then
       
    47 			local is = e:child ( 'items' )
       
    48 			if is then
       
    49 				local from = mess:attribute ( 'from' )
       
    50 				local node = is:attribute ( 'node' )
       
    51 				local item = is:child ( 'item' )
       
    52 				if item then
       
    53 					local handled = true -- XXX should we do this? well, if it becomes general pubsub handler - then no.
       
    54 					local n       = item:children ()
       
    55 					while n do
       
    56 						local xmlns = n:attribute ( 'xmlns' )
       
    57 						if pep.handlers[xmlns] then
       
    58 							if not pep.handlers[xmlns] ( from, node, n ) then
       
    59 								handled = false
       
    60 							end
       
    61 						else
       
    62 							handled = false
       
    63 						end
       
    64 						n = n:next ()
       
    65 					end
       
    66 					return handled
       
    67 				end
       
    68 			end
       
    69 		end
       
    70 		return false
       
    71 	end )
       
    72 
       
    73 -- mcabber
       
    74 
       
    75 local pep_handler_registered = false
       
    76 
       
    77 hooks_d['hook-post-connect'].pep =
       
    78 	function ( args )
       
    79 		lm.connection.bless( main.connection () ):handler ( pep_incoming_message_handler, 'message', 'normal' )
       
    80 		pep_handler_registered = true
       
    81 		hooks_d['hook-post-connect'].pep = nil
       
    82 		hooks_d['hook-quit'].pep =
       
    83 			function ( args )
       
    84 				if pep_handler_registered then
       
    85 					lm.connection.bless( main.connection () ):handler ( pep_incoming_message_handler, 'message' )
       
    86 					pep_handler_registered = false
       
    87 				end
       
    88 			end
       
    89 	end
       
    90 
    38 
    91 -- vim: se ts=4: --
    39 -- vim: se ts=4: --