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:/,'')
83 @replies << Reply.new(self, act ? :act : :reply, rex, *args)
89 @replies[@raw_replies.index(expr)] rescue nil
94 pcts = @replies.map { |rep|
101 debug "percentages: #{pcts.inspect}"
104 @replies.each_with_index { |r, i|
106 r.range = last..(last+p)
109 debug "ranges: #{@replies.map { |r| r.range}.inspect}"
114 debug "#{pick} in #{@replies.map { |r| r.range}.inspect}"
116 return r if r.range and r.range === pick
122 return nil if @trigger.first and not message.action
123 return message.message.match(@trigger.last)
138 class ReactionPlugin < Plugin
140 ADD_SYNTAX = 'react to *trigger with *reply [at :chance chance]'
141 MOVE_SYNTAX = 'reaction move *source to *dest'
155 if @registry.has_key?(:reactions)
156 @reactions = @registry[:reactions]
157 raise unless @reactions
164 :underline => Underline,
167 :normal => NormalText,
175 @registry[:reactions] = @reactions
178 def help(plugin, topic="")
179 if plugin.to_sym == :react
180 return "react to <trigger> with <reply> [at <chance> chance] => " +
181 "create a new reaction to expression <trigger> to which the bot will reply <reply>, optionally at chance <chance>, " +
182 "seek help for reaction trigger, reaction reply and reaction chance for more details"
184 case (topic.to_sym rescue nil)
187 when :remove, :delete, :rm, :del
188 "reaction #{topic} <trigger> => removes the reaction to expression <trigger>"
189 when :chance, :chances
190 "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 " +
191 "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, " +
192 "there is a chance that the trigger will not actually cause a reply. Otherwise, the chances express the relative frequency of the replies."
193 when :trigger, :triggers
194 "reaction triggers can have one of the format: single_word 'multiple words' \"multiple words \" /regular_expression/ !regular_expression!. " +
195 "If prefixed by 'act:' (e.g. act:/(order|command)s/) the bot will only respond if a CTCP ACTION matches the trigger"
196 when :reply, :replies
197 "reaction replies are simply messages that the bot will reply when a trigger is matched. " +
198 "Replies can be prefixed by 'act:' (e.g. act:goes shopping) to signify that the bot should act instead of saying the message. " +
199 "Replies can use the %%{key} syntax to access one of the following keys: " +
200 "who (the user that said the trigger), bot (the bot's own nick), " +
201 "target (the first word following the trigger), what (whatever follows target), " +
202 "stuff (everything that follows the trigger), match (the actual matched text), " +
203 "match1, match2, ... (the i-th capture)"
205 "reaction list [n]: lists the n-the page of programmed reactions (30 reactions are listed per page)"
207 "reaction show <trigger>: list the programmed replies to trigger <trigger>"
209 "reaction topics: add, remove, delete, rm, del, triggers, replies, chance, list, show"
214 return unless PrivMessage === m
215 debug "testing #{m} for reactions"
216 return if @reactions.empty?
217 candidates = @reactions.map { |react|
219 blob ? [blob, react] : nil
221 return if candidates.empty?
222 match, wanted = candidates.sort { |m1, m2|
223 m1.first[0].length <=> m2.first[0].length
226 stuff = match.post_match.strip
227 target, what = stuff.split(/\s+/, 2)
229 :who => m.sourcenick,
235 match.to_a.each_with_index { |d, i|
236 extra[:"match#{i}"] = d
238 subs = @subs.dup.merge extra
239 reply = wanted.pick_reply
240 debug "picked #{reply}"
242 args = reply.apply(subs)
246 def find_reaction(trigger)
247 @reactions.find { |react|
248 react.raw_trigger.downcase == trigger.downcase
252 def handle_add(m, params)
253 trigger = params[:trigger].to_s
254 reply = params[:reply].to_s
256 pct = params[:chance] || "1"
258 pct = (pct.to_f/100).clip(0,1)
260 pct = pct.to_f.clip(0,1)
263 reaction = find_reaction(trigger)
265 reaction = Reaction.new(trigger)
266 @reactions << reaction
267 m.reply "Ok, I'll start reacting to #{reaction.raw_trigger}"
269 found = reaction.find_reply(reply)
272 found.author = m.sourcenick
273 found.date = Time.now
274 found.channel = m.channel
276 found = reaction.add_reply(reply, pct, m.sourcenick, Time.now, m.channel)
278 m.reply "I'll react to #{reaction.raw_trigger} with #{reaction.raw_replies.last} (#{(reaction.replies.last.pct * 100).to_i}%)"
281 def handle_move(m, params)
282 source = params[:source].to_s
283 dest = params[:dest].to_s
284 found = find_reaction(source)
286 m.reply "I don't react to #{source}"
289 if find_reaction(dest)
290 m.reply "I already react to #{dest}, so I won't move #{source} to #{dest}"
294 m.reply "Ok, I'll react to #{found.raw_trigger} now"
297 def handle_rm(m, params)
298 trigger = params[:trigger].to_s
299 debug trigger.inspect
300 found = find_reaction(trigger)
302 @reactions.delete(found)
303 m.reply "I won't react to #{found.raw_trigger} anymore"
305 m.reply "no reaction programmed for #{trigger}"
309 def handle_list(m, params)
311 m.reply "no reactions programmed"
316 pages = @reactions.length / per_page + 1
317 page = params[:page].to_i.clip(1, pages)
319 str = @reactions[(page-1)*per_page, per_page].join(", ")
321 m.reply "Programmed reactions (page #{page}/#{pages}): #{str}"
324 def handle_show(m, params)
326 m.reply "no reactions programmed"
330 trigger = params[:trigger].to_s
332 found = find_reaction(trigger)
335 m.reply "I'm not reacting to #{trigger}"
339 m.reply found.replies.join(", ")
344 plugin = ReactionPlugin.new
346 plugin.map plugin.add_syntax, :action => 'handle_add',
347 :requirements => { :trigger => /^(?:act:)?!.*?!/ }
348 plugin.map plugin.add_syntax, :action => 'handle_add',
349 :requirements => { :trigger => /^(?:act:)?\/.*?\// }
350 plugin.map plugin.add_syntax, :action => 'handle_add',
351 :requirements => { :trigger => /^(?:act:)?".*?"/ }
352 plugin.map plugin.add_syntax, :action => 'handle_add',
353 :requirements => { :trigger => /^(?:act:)?'.*?'/ }
354 plugin.map plugin.add_syntax.sub('*', ':'), :action => 'handle_add'
356 plugin.map 'reaction list [:page]', :action => 'handle_list',
357 :requirements => { :page => /^\d+$/ }
359 plugin.map 'reaction show *trigger', :action => 'handle_show'
361 plugin.map plugin.move_syntax, :action => 'handle_move',
362 :requirements => { :source => /^(?:act:)?!.*?!/ }
363 plugin.map plugin.move_syntax, :action => 'handle_move',
364 :requirements => { :source => /^(?:act:)?\/.*?\// }
365 plugin.map plugin.move_syntax, :action => 'handle_move',
366 :requirements => { :source => /^(?:act:)?".*?"/ }
367 plugin.map plugin.move_syntax, :action => 'handle_move',
368 :requirements => { :source => /^(?:act:)?'.*?'/ }
369 plugin.map plugin.move_syntax.sub('*', ':'), :action => 'handle_move'
372 plugin.map 'reaction del[ete] *trigger', :action => 'handle_rm'
373 plugin.map 'reaction delete *trigger', :action => 'handle_rm'
374 plugin.map 'reaction remove *trigger', :action => 'handle_rm'
375 plugin.map 'reaction rm *trigger', :action => 'handle_rm'