Module: Robut::Plugin

Included in:
Alias, Calc, Echo, GoogleImages, Help, Later, Lunch, Meme, Pick, Ping, Quips, Say, Sayings, Stock, TWSS, Weather
Defined in:
lib/robut/plugin.rb

Overview

Robut plugins implement a simple interface to listen for messages and optionally respond to them. All plugins include the Robut::Plugin module.

Defined Under Namespace

Modules: ClassMethods Classes: Alias, Calc, Echo, GoogleImages, Help, Later, Lunch, Meme, Pick, Ping, Quips, Say, Sayings, Stock, TWSS, Weather

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.pluginsObject

A list of all available plugin classes. When you require a new plugin class, you should add it to this list if you want it to respond to messages.


68
69
70
# File 'lib/robut/plugin.rb', line 68

def plugins
  @plugins
end

Instance Attribute Details

#connectionObject

A reference to the connection attached to this instance of the plugin. This is mostly used to communicate back to the server.


75
76
77
# File 'lib/robut/plugin.rb', line 75

def connection
  @connection
end

#private_senderObject

If we are handling a private message, holds a reference to the sender of the message. nil if the message was sent to the entire room.


80
81
82
# File 'lib/robut/plugin.rb', line 80

def private_sender
  @private_sender
end

#reply_toObject

Returns the value of attribute reply_to


82
83
84
# File 'lib/robut/plugin.rb', line 82

def reply_to
  @reply_to
end

Class Method Details

.included(klass) ⇒ Object

:nodoc:


85
86
87
# File 'lib/robut/plugin.rb', line 85

def self.included(klass)
  klass.send(:extend, ClassMethods)
end

Instance Method Details

#at_nickObject

#nick with the @-symbol prepended


135
136
137
# File 'lib/robut/plugin.rb', line 135

def at_nick
  "@#{nick}"
end

#fake_message(time, sender_nick, msg) ⇒ Object


169
170
171
172
173
# File 'lib/robut/plugin.rb', line 169

def fake_message(time, sender_nick, msg)
  # TODO: ensure this connection is threadsafe
  plugins = Robut::Plugin.plugins.map { |p| p.new(reply_to, private_sender) }
  reply_to.handle_message(plugins, time, sender_nick, msg)
end

#handle(time, sender_nick, message) ⇒ Object

Do whatever you need to do to handle this message. If you want to stop the plugin execution chain, return true from this method. Plugins are handled in the order that they appear in Robut::Plugin.plugins


148
149
150
151
152
153
154
# File 'lib/robut/plugin.rb', line 148

def handle(time, sender_nick, message)
  if matchers.empty?
    raise NotImplementedError, "Implement me in #{self.class.name}!"
  else
    find_match(message)
  end
end

#initialize(reply_to, private_sender = nil) ⇒ Object

Creates a new instance of this plugin to reply to a particular object over that object's connection


91
92
93
94
95
# File 'lib/robut/plugin.rb', line 91

def initialize(reply_to, private_sender = nil)
  self.reply_to = reply_to
  self.connection = reply_to.connection
  self.private_sender = private_sender
end

#nickObject

The bot's nickname, for @-replies.


130
131
132
# File 'lib/robut/plugin.rb', line 130

def nick
  connection.config.mention_name || connection.config.nick.gsub(/[^0-9a-z]/i, '')
end

#reply(message, to = nil) ⇒ Object

Send message back to the HipChat server. If to == :room, replies to the room. If to == nil, responds in the manner the original message was sent. Otherwise, PMs the message to to.


100
101
102
103
104
105
106
# File 'lib/robut/plugin.rb', line 100

def reply(message, to = nil)
  if to == :room
    reply_to.reply(message, nil)
  else
    reply_to.reply(message, to || private_sender)
  end
end

#sent_to_me?(message) ⇒ Boolean

Was message sent to Robut as an @reply?


140
141
142
# File 'lib/robut/plugin.rb', line 140

def sent_to_me?(message)
  message =~ /(^|\s)@#{nick}(\s|$)/i
end

#storeObject

Accessor for the store instance


176
177
178
# File 'lib/robut/plugin.rb', line 176

def store
  connection.store
end

#usageObject

Returns a list of messages describing the commands this plugin handles.


158
159
160
161
162
163
164
165
166
167
# File 'lib/robut/plugin.rb', line 158

def usage
  matchers.map do |regexp, options, action, description|
    next unless description
    if options[:sent_to_me]
      at_nick + " " + description
    else
      description
    end
  end.compact
end

#without_nick(message) ⇒ Object

Removes the first word in message if it is a reference to the bot's nick Given “@robut do this thing”, Returns “do this thing”


120
121
122
123
124
125
126
127
# File 'lib/robut/plugin.rb', line 120

def without_nick(message)
  possible_nick, command = message.split(' ', 2)
  if possible_nick.casecmp(at_nick) == 0
    command
  else
    message
  end
end

#words(message, command = nil) ⇒ Object

An ordered list of all words in the message with any reference to the bot's nick stripped out. If command is passed in, it is also stripped out. This is useful to separate the 'parameters' from the 'commands' in a message.


112
113
114
115
116
# File 'lib/robut/plugin.rb', line 112

def words(message, command = nil)
  reply = at_nick.downcase
  command = command.downcase if command
  message.split.reject {|word| word.downcase == reply || word.downcase == command }
end