4 # :title: Reaction plugin
6 # Author:: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
7 # Copyright:: (C) 2007 Giuseppe Bilotta
10 # Build one-liner replies/reactions to expressions/actions in channel
12 # Very alpha stage, so beware of sudden reaction syntax changes
15 attr_reader :trigger, :replies
16 attr_reader :raw_trigger, :raw_replies
19 attr_reader :act, :reply, :pct
21 attr_reader :author, :date, :channel
37 def initialize(reaction, act, expr, pct, author, date, channel)
44 self.channel = channel
49 "#{act} #{reply} (#{pct} chance)",
50 @range ? "(#{@range})" : "",
51 "(#{author}, #{channel}, #{date})"
61 @raw_trigger = expr.dup
64 if rex.sub!(/^act:/,'')
68 if rex.sub!(%r@^([/!])(.*)\1$@, '\2')
69 @trigger << Regexp.new(rex, true)
71 rex.sub!(/^(["'])(.*)\1$/, '\2')
72 @trigger << Regexp.new(/\b#{Regexp.escape(rex)}(?:\b|$)/ui)
76 def add_reply(expr, *args)
77 @raw_replies << expr.dup
80 if rex.sub!(/^act:/,'')
82 elsif rex.sub!(/^(?:cmd|command):/,'')
85 @replies << Reply.new(self, act, rex, *args)
91 @replies.delete_at(num-1)
93 return @raw_replies.delete_at(num-1)
97 @replies[@raw_replies.index(expr)] rescue nil
102 pcts = @replies.map { |rep|
109 debug "percentages: #{pcts.inspect}"
112 @replies.each_with_index { |r, i|
114 r.range = last..(last+p)
117 debug "ranges: #{@replies.map { |r| r.range}.inspect}"
122 debug "#{pick} in #{@replies.map { |r| r.range}.inspect}"
124 return r if r.range and r.range === pick
130 return nil if @trigger.first and not message.action
131 return message.message.match(@trigger.last)
146 class ReactionPlugin < Plugin
148 ADD_SYNTAX = 'react to *trigger with *reply [at :chance chance]'
149 MOVE_SYNTAX = 'reaction move *source to *dest'
150 # We'd like to use backreferences for the trigger syntax
151 # but we can't because it will be merged with the Plugin#map()
153 TRIGGER_SYNTAX = /^(?:act:)?(?:!.*?!|\/.*?\/|".*?"|'.*?'|\S+)/
164 return TRIGGER_SYNTAX
171 if @registry.has_key?(:reactions)
172 @reactions = @registry[:reactions]
173 raise unless @reactions
180 :underline => Underline,
183 :normal => NormalText,
191 @registry[:reactions] = @reactions
194 def help(plugin, topic="")
195 if plugin.to_sym == :react
196 return "react to <trigger> with <reply> [at <chance> chance] => " +
197 "create a new reaction to expression <trigger> to which the bot will reply <reply>, optionally at chance <chance>, " +
198 "seek help for reaction trigger, reaction reply and reaction chance for more details"
200 case (topic.to_sym rescue nil)
203 when :remove, :delete, :rm, :del
204 "reaction #{topic} <trigger> [<n>] => removes reactions to expression <trigger>. If <n> (a positive integer) is specified, only remove the n-th reaction, otherwise remove the trigger completely"
205 when :chance, :chances
206 "reaction chances are expressed either in terms of percentage (like 30%) or in terms of floating point numbers (like 0.3), and are clipped to be " +
207 "between 0 and 1 (i.e. 0% and 100%). A reaction can have multiple replies, each with a different chance; if the total of the chances is less than one, " +
208 "there is a chance that the trigger will not actually cause a reply. Otherwise, the chances express the relative frequency of the replies."
209 when :trigger, :triggers
210 "reaction triggers can have one of the format: single_word 'multiple words' \"multiple words \" /regular_expression/ !regular_expression!. " +
211 "If prefixed by 'act:' (e.g. act:/(order|command)s/) the bot will only respond if a CTCP ACTION matches the trigger"
212 when :reply, :replies
213 "reaction replies are simply messages that the bot will reply when a trigger is matched. " +
214 "Replies can be prefixed by 'act:' (e.g. act:goes shopping) to signify that the bot should act instead of saying the message. " +
215 "Replies can be prefixed by 'cmd:' or 'command:' (e.g. cmd:lart %{who}) to issue a command to the bot. " +
216 "Replies can use the %{key} syntax to access one of the following keys: " +
217 "who (the user that said the trigger), bot (the bot's own nick), " +
218 "target (the first word following the trigger), what (whatever follows target), " +
219 "before (everything that precedes the trigger), after, (everything that follows the trigger), " +
220 "match (the actual matched text), match1, match2, ... (the i-th capture)"
222 "reaction list [n]: lists the n-the page of programmed reactions (30 reactions are listed per page)"
224 "reaction show <trigger>: list the programmed replies to trigger <trigger>"
226 "reaction topics: add, remove, delete, rm, del, triggers, replies, chance, list, show"
231 return unless PrivMessage === m
232 debug "testing #{m} for reactions"
233 return if @reactions.empty?
234 candidates = @reactions.map { |react|
236 blob ? [blob, react] : nil
238 return if candidates.empty?
239 match, wanted = candidates.sort { |m1, m2|
240 # Order by longest matching text first,
241 # and by number of captures second
242 longer = m1.first[0].length <=> m2.first[0].length
243 longer == 0 ? m1.first.length <=> m2.first.length : longer
246 before = match.pre_match.strip
247 after = match.post_match.strip
248 target, what = after.split(/\s+/, 2)
250 :who => m.sourcenick,
257 match.to_a.each_with_index { |d, i|
258 extra[:"match#{i}"] = d
260 subs = @subs.dup.merge extra
261 reply = wanted.pick_reply
262 debug "picked #{reply}"
264 args = reply.apply(subs)
267 # Pass the new message back to the bot.
268 # FIXME Maybe we should do it the alias way, only calling
269 # @bot.plugins.privmsg() ?
270 fake_message(@bot.nick+": "+args[1], :from => m)
271 rescue RecurseTooDeep => e
279 def find_reaction(trigger)
280 @reactions.find { |react|
281 react.raw_trigger.downcase == trigger.downcase
285 def handle_add(m, params)
286 trigger = params[:trigger].to_s
287 reply = params[:reply].to_s
289 pct = params[:chance] || "1"
291 pct = (pct.to_f/100).clip(0,1)
293 pct = pct.to_f.clip(0,1)
296 reaction = find_reaction(trigger)
298 reaction = Reaction.new(trigger)
299 @reactions << reaction
300 m.reply "Ok, I'll start reacting to #{reaction.raw_trigger}"
302 found = reaction.find_reply(reply)
305 found.author = m.sourcenick
306 found.date = Time.now
307 found.channel = m.channel
309 found = reaction.add_reply(reply, pct, m.sourcenick, Time.now, m.channel)
311 m.reply "I'll react to #{reaction.raw_trigger} with #{reaction.raw_replies.last} (#{(reaction.replies.last.pct * 100).to_i}%)"
314 def handle_move(m, params)
315 source = params[:source].to_s
316 dest = params[:dest].to_s
317 found = find_reaction(source)
319 m.reply "I don't react to #{source}"
322 if find_reaction(dest)
323 m.reply "I already react to #{dest}, so I won't move #{source} to #{dest}"
327 m.reply "Ok, I'll react to #{found.raw_trigger} now"
330 def handle_rm(m, params)
331 trigger = params[:trigger].to_s
334 debug trigger.inspect
335 found = find_reaction(trigger)
339 if n < 1 or n > found.replies.length
340 m.reply "Please specify an index between 1 and #{found.replies.length}"
343 purged = found.rm_reply(n)
344 if found.replies.length == 0
345 @reactions.delete(found)
348 purged = " with #{purged}"
351 @reactions.delete(found)
353 m.reply "I won't react to #{found.raw_trigger}#{purged} anymore"
355 m.reply "no reaction programmed for #{trigger}"
359 def handle_list(m, params)
361 m.reply "no reactions programmed"
366 pages = @reactions.length / per_page + 1
367 page = params[:page].to_i.clip(1, pages)
369 str = @reactions[(page-1)*per_page, per_page].join(", ")
371 m.reply "Programmed reactions (page #{page}/#{pages}): #{str}"
374 def handle_show(m, params)
376 m.reply "no reactions programmed"
380 trigger = params[:trigger].to_s
382 found = find_reaction(trigger)
385 m.reply "I'm not reacting to #{trigger}"
389 m.reply found.replies.join(", ")
394 plugin = ReactionPlugin.new
396 plugin.map plugin.add_syntax, :action => 'handle_add',
397 :requirements => { :trigger => plugin.trigger_syntax }
399 plugin.map 'reaction list [:page]', :action => 'handle_list',
400 :requirements => { :page => /^\d+$/ }
402 plugin.map 'reaction show *trigger', :action => 'handle_show'
404 plugin.map plugin.move_syntax, :action => 'handle_move',
406 :source => plugin.trigger_syntax,
407 :dest => plugin.trigger_syntax
410 plugin.map 'reaction del[ete] *trigger [:n]', :action => 'handle_rm', :auth_path => 'del!',
411 :requirements => { :trigger => plugin.trigger_syntax, :n => /^\d+$/ }
412 plugin.map 'reaction remove *trigger [:n]', :action => 'handle_rm', :auth_path => 'del!',
413 :requirements => { :trigger => plugin.trigger_syntax, :n => /^\d+$/ }
414 plugin.map 'reaction rm *trigger [:n]', :action => 'handle_rm', :auth_path => 'del!',
415 :requirements => { :trigger => plugin.trigger_syntax, :n => /^\d+$/ }