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
81 82 83 |
# File 'lib/jabbot/macros.rb', line 81 def client bot.client end |
#close ⇒ Object Also known as: quit
Close the connection and exit the bot
86 87 88 |
# File 'lib/jabbot/macros.rb', line 86 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
52 53 54 |
# File 'lib/jabbot/macros.rb', line 52 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
62 63 64 |
# File 'lib/jabbot/macros.rb', line 62 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 29 30 31 |
# File 'lib/jabbot/macros.rb', line 26 def (pattern = nil, = {}, &blk) add_handler(:message, pattern, , &blk) # if :query => true, add this block for queries, too add_handler(:private, pattern, , &blk) if && [:query] 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"
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/jabbot/macros.rb', line 106 def post(msg, to=nil) if msg.is_a?(Hash) && msg.keys.size == 1 to = msg.values.first msg = msg.keys.first elsif to.kind_of?(Struct) if to.type == :query to = to.user else to = nil end 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
41 42 43 |
# File 'lib/jabbot/macros.rb', line 41 def query(pattern = nil, = {}, &blk) add_handler(:private, pattern, , &blk) end |
#run? ⇒ Boolean
Returns boolean wether to start the bot at_exit
121 122 123 |
# File 'lib/jabbot/macros.rb', line 121 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
74 75 76 |
# File 'lib/jabbot/macros.rb', line 74 def subject(pattern = nil, = {}, &blk) add_handler(:subject, pattern, , &blk) end |
#users ⇒ Object
Get array of all users in the channel
92 93 94 |
# File 'lib/jabbot/macros.rb', line 92 def users bot.users end |