Module: Jabbot::Macros
- Defined in:
- lib/jabbot/macros.rb
Overview
Defines the DSL used for bots.
Class Method Summary collapse
Instance Method Summary collapse
-
#client ⇒ Object
Returns the Jabber::Client instance used You may execute low-level functions on this object if needed.
-
#close ⇒ Object
(also: #quit)
Close the connection and exit the bot.
-
#config ⇒ Object
Returns the current config hash.
-
#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.
-
#join(options = {}, &blk) ⇒ Object
Add join handler Block gets executed when new user joins.
-
#leave(options = {}, &blk) ⇒ Object
Add leave handler Block gets executed when user leaves the channel.
-
#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.
-
#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.
-
#query(pattern = nil, options = {}, &blk) ⇒ Object
(also: #private_message)
Add query handler Only private messages are matched against this handler.
-
#run? ⇒ Boolean
Returns boolean wether to start the bot at_exit.
-
#subject(pattern = nil, options = {}, &blk) ⇒ Object
(also: #topic)
Add subject/topic handler Block gets executed when topic gets changed.
-
#users ⇒ Object
Get array of all users in the channel.
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
#client ⇒ Object
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 |
#close ⇒ Object 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 |
#config ⇒ Object
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( = {}, &blk) add_handler(:join, /\Ajoin\Z/, , &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( = {}, &blk) add_handler(:leave, /\Aleave\Z/, , &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 (pattern = nil, = {}, &blk) add_handler(:message, pattern, , &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.(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, = {}, &blk) add_handler(:private, pattern, , &blk) end |
#run? ⇒ Boolean
Returns boolean wether to start the bot at_exit
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, = {}, &blk) add_handler(:subject, pattern, , &blk) end |
#users ⇒ Object
Get array of all users in the channel
89 90 91 |
# File 'lib/jabbot/macros.rb', line 89 def users bot.users end |