Module: Lijab::HooksHandler

Defined in:
lib/lijab/hooks.rb

Class Method Summary collapse

Class Method Details

.handle_connectObject



67
68
69
# File 'lib/lijab/hooks.rb', line 67

def handle_connect
   @on_connect.each { |b| b.call }
end

.handle_disconnectObject



71
72
73
# File 'lib/lijab/hooks.rb', line 71

def handle_disconnect
   @on_disconnect.each { |b| b.call }
end

.handle_message(msg) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/lijab/hooks.rb', line 24

def handle_message(msg)
   return unless msg.body && !msg.body.empty?

   @on_incoming_message.each do |b|
      b.call(Main.contacts[msg.from.strip], msg.body)
   end
end

.handle_post_send_message(contact, msg) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/lijab/hooks.rb', line 57

def handle_post_send_message(contact, msg)
   return if !msg.body || msg.body.empty?

   @on_post_send_message.each do |block|
      args = [contact, msg.body]
      args.push(msg) if block.arity == 3
      block.call(*args)
   end
end

.handle_pre_send_message(contact, msg) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lijab/hooks.rb', line 38

def handle_pre_send_message(contact, msg)
   return msg if @on_pre_send_message.empty? || !msg.body || msg.body.empty?

   @on_pre_send_message.inject(msg) do |ret_msg, block|
      args = [contact, ret_msg.body]
      args.push(msg) if block.arity == 3

      m = block.call(*args)
      break if !m 

      if m.is_a?(Jabber::Message)
         m
      else
         ret_msg.body = m.to_s
         ret_msg
      end
   end
end

.handle_presence(roster_item, old_p, new_p) ⇒ Object



32
33
34
35
36
# File 'lib/lijab/hooks.rb', line 32

def handle_presence(roster_item, old_p, new_p)
   @on_presence.each do |b|
      b.call(Main.contacts[roster_item.jid.strip], old_p, new_p)
   end
end

.initObject



14
15
16
17
18
19
20
21
22
# File 'lib/lijab/hooks.rb', line 14

def init
   @on_connect, @on_disconnect, @on_incoming_message, @on_presence = [], [], [], []
   @on_pre_send_message, @on_post_send_message = [], []

   Dir[File.join(Config.dirs[:hooks], '**', '*.rb')].each { |f| load f }

   Main.client.add_message_callback(&method(:handle_message))
   Main.contacts.roster.add_presence_callback(&method(:handle_presence))
end

.on_connectObject

.on_disconnectObject

.on_incoming_messageObject

.on_post_send_messageObject

.on_pre_send_messageObject

.on_presenceObject