Module: Jabbot::Macros

Defined in:
lib/jabbot/macros.rb

Overview

Defines the DSL used for bots.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



4
5
6
# File 'lib/jabbot/macros.rb', line 4

def self.included(mod)
  @@bot = nil
end

Instance Method Details

#clientObject

Returns the Jabber::Client instance used You may execute low-level functions on this object if needed



78
79
80
# File 'lib/jabbot/macros.rb', line 78

def client
  bot.client
end

#closeObject Also known as: quit

Close the connection and exit the bot



83
84
85
# File 'lib/jabbot/macros.rb', line 83

def close
  bot.close
end

#configObject

Returns the current config hash



16
17
18
# File 'lib/jabbot/macros.rb', line 16

def config
  bot.config
end

#configure(&blk) ⇒ Object

Configure the bot The block gets passed an instance of OpenStruct used as the config See lib/jabbot/config.rb for possible options



11
12
13
# File 'lib/jabbot/macros.rb', line 11

def configure(&blk)
  bot.configure(&blk)
end

#join(options = {}, &blk) ⇒ Object

Add join handler Block gets executed when new user joins

options - Hash defining users to react on joins

{ :from => ['user1', 'user2', ...] }

blk - The block to execute on successfull match



49
50
51
# File 'lib/jabbot/macros.rb', line 49

def join(options = {}, &blk)
  add_handler(:join, /\Ajoin\Z/, options, &blk)
end

#leave(options = {}, &blk) ⇒ Object

Add leave handler Block gets executed when user leaves the channel

options - Hash defining users to react on leaves

{ :from => ['user1', 'user2', ...] }

blk - The block to execute on successfull match



59
60
61
# File 'lib/jabbot/macros.rb', line 59

def leave(options = {}, &blk)
  add_handler(:leave, /\Aleave\Z/, options, &blk)
end

#message(pattern = nil, options = {}, &blk) ⇒ Object

Add message handler pattern - can be a String containing :matches

or a Regexp with matching groups

options - Hash defining users to receive messages from

{ :from => ['user1', 'user2', ...] }

blk - The block to execute on successfull match



26
27
28
# File 'lib/jabbot/macros.rb', line 26

def message(pattern = nil, options = {}, &blk)
  add_handler(:message, pattern, options, &blk)
end

#post(msg, to = nil) ⇒ Object

Post message back to the channel msg - Message to send, can be a String to be send

or a Hash: { msg => user }

to - User to send the message to,

left blank if syntax-sugar variant is used

Syntax-sugar variant:

post "msg" => "user1"

is the same as

post "msg", "user1"


103
104
105
106
107
108
109
# File 'lib/jabbot/macros.rb', line 103

def post(msg, to=nil)
  if msg.is_a?(Hash) && msg.keys.size == 1
    to = msg.values.first
    msg = msg.keys.first
  end
  bot.send_message(msg, to)
end

#query(pattern = nil, options = {}, &blk) ⇒ Object Also known as: private_message

Add query handler Only private messages are matched against this handler

pattern - can be a String containing :matches

or a Regexp with matching groups

options - Hash defining users to receive messages from

{ :from => ['user1', 'user2', ...] }

blk - The block to execute on successfull match



38
39
40
# File 'lib/jabbot/macros.rb', line 38

def query(pattern = nil, options = {}, &blk)
  add_handler(:private, pattern, options, &blk)
end

#run?Boolean

Returns boolean wether to start the bot at_exit

Returns:

  • (Boolean)


112
113
114
# File 'lib/jabbot/macros.rb', line 112

def run?
  !@@bot.nil?
end

#subject(pattern = nil, options = {}, &blk) ⇒ Object Also known as: topic

Add subject/topic handler Block gets executed when topic gets changed

pattern - can be a String containing :matches

or a Regexp with matching groups

options - Hash defining users

{ :from => ['user1', 'user2', ...] }

blk - The block to execute on successfull match



71
72
73
# File 'lib/jabbot/macros.rb', line 71

def subject(pattern = nil, options = {}, &blk)
  add_handler(:subject, pattern, options, &blk)
end

#usersObject

Get array of all users in the channel



89
90
91
# File 'lib/jabbot/macros.rb', line 89

def users
  bot.users
end