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, :range
20 attr_reader :author, :date, :channel
36 def initialize(reaction, act, expr, pct, author, date, channel)
43 self.channel = channel
48 "#{act} #{reply} (#{pct} chance)",
49 @range ? "(#{@range})" : "",
50 "(#{author}, #{channel}, #{date})"
60 @raw_trigger = expr.dup
63 if rex.sub!(/^act:/,'')
67 if rex.sub!(%r@^([/!])(.*)\1$@, '\2')
68 @trigger << Regexp.new(rex)
70 @trigger << Regexp.new(/\b#{Regexp.escape(rex)}\b/u)
74 def add_reply(expr, *args)
75 @raw_replies << expr.dup
78 if rex.sub!(/^act:/,'')
81 @replies << Reply.new(self, act ? :act : :reply, rex, *args)
87 @replies[@raw_replies.index(expr)] rescue nil
92 pcts = @replies.map { |rep|
99 debug "percentages: #{pcts.inspect}"
102 @replies.each_with_index { |r, i|
104 r.range = last..(last+p)
107 debug "ranges: #{@replies.map { |r| r.range}.inspect}"
112 debug "#{pick} in #{@replies.map { |r| r.range}.inspect}"
114 return r if r.range and r.range === pick
120 return nil if @trigger.first and not message.action
121 return message.message.match(@trigger.last)
136 class ReactionPlugin < Plugin
138 ADD_SYNTAX = 'react to *trigger with *reply [at :chance chance]'
139 MOVE_SYNTAX = 'reaction move *source to *dest'
153 if @registry.has_key?(:reactions)
154 @reactions = @registry[:reactions]
155 raise unless @reactions
162 :underline => Underline,
165 :normal => NormalText,
173 @registry[:reactions] = @reactions
176 def help(plugin, topic="")
177 if plugin.to_sym == :react
178 return "react to <trigger> with <reply> [at <chance> chance] => " +
179 "create a new reaction to expression <trigger> to which the bot will reply <reply>, optionally at chance <chance>, " +
180 "seek help for reaction trigger, reaction reply and reaction chance for more details"
182 case (topic.to_sym rescue nil)
185 when :remove, :delete, :rm, :del
186 "reaction #{topic} <trigger> => removes the reaction to expression <trigger>"
187 when :chance, :chances
188 "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 " +
189 "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, " +
190 "there is a chance that the trigger will not actually cause a reply. Otherwise, the chances express the relative frequency of the replies."
191 when :trigger, :triggers
192 "reaction triggers can have one of the format: single_word 'multiple words' \"multiple words \" /regular_expression/ !regular_expression!. " +
193 "If prefixed by 'act:' (e.g. act:/(order|command)s/) the bot will only respond if a CTCP ACTION matches the trigger"
194 when :reply, :replies
195 "reaction replies are simply messages that the bot will reply when a trigger is matched. " +
196 "Replies can be prefixed by 'act:' (e.g. act:goes shopping) to signify that the bot should act instead of saying the message. " +
197 "Replies can use the %%{key} syntax to access one of the following keys: " +
198 "who (the user that said the trigger), bot (the bot's own nick), " +
199 "target (the first word following the trigger), what (whatever follows target), " +
200 "stuff (everything that follows the trigger), match (the actual matched text), " +
201 "match1, match2, ... (the i-th capture)"
203 "reaction list [n]: lists the n-the page of programmed reactions (30 reactions are listed per page)"
205 "reaction show <trigger>: list the programmed replies to trigger <trigger>"
207 "reaction topics: add, remove, delete, rm, del, triggers, replies, chance, list, show"
212 return unless PrivMessage === m
213 debug "testing #{m} for reactions"
214 return if @reactions.empty?
215 wanted = @reactions.find { |react|
221 stuff = match.post_match.strip
222 target, what = stuff.split(/\s+/, 2)
224 :who => m.sourcenick,
230 match.to_a.each_with_index { |d, i|
231 extra[:"match#{i}"] = d
233 subs = @subs.dup.merge extra
234 reply = wanted.pick_reply
235 debug "picked #{reply}"
237 args = reply.apply(subs)
241 def find_reaction(trigger)
242 @reactions.find { |react|
243 react.raw_trigger == trigger
247 def handle_add(m, params)
248 trigger = params[:trigger].to_s
249 reply = params[:reply].to_s
251 pct = params[:chance] || "1"
253 pct = (pct.to_f/100).clip(0,1)
255 pct = pct.to_f.clip(0,1)
258 reaction = find_reaction(trigger)
260 reaction = Reaction.new(trigger)
261 @reactions << reaction
262 m.reply "Ok, I'll start reacting to #{reaction.raw_trigger}"
264 found = reaction.find_reply(reply)
267 found.author = m.sourcenick
268 found.date = Time.now
269 found.channel = m.channel
271 found = reaction.add_reply(reply, pct, m.sourcenick, Time.now, m.channel)
273 m.reply "I'll react to #{reaction.raw_trigger} with #{reaction.raw_replies.last} (#{(reaction.replies.last.pct * 100).to_i}%)"
276 def handle_move(m, params)
277 source = params[:source].to_s
278 dest = params[:dest].to_s
279 found = find_reaction(source)
281 m.reply "I don't react to #{source}"
284 if find_reaction(dest)
285 m.reply "I already react to #{dest}, so I won't move #{source} to #{dest}"
289 m.reply "Ok, I'll react to #{found.raw_trigger} now"
292 def handle_rm(m, params)
293 trigger = params[:trigger].to_s
294 debug trigger.inspect
295 found = find_reaction(trigger)
297 @reactions.delete(found)
298 m.reply "I won't react to #{found.raw_trigger} anymore"
300 m.reply "no reaction programmed for #{trigger}"
304 def handle_list(m, params)
306 m.reply "no reactions programmed"
311 pages = @reactions.length / per_page + 1
312 page = params[:page].to_i.clip(1, pages)
314 str = @reactions[(page-1)*per_page, per_page].join(", ")
316 m.reply "Programmed reactions (page #{page}/#{pages}): #{str}"
319 def handle_show(m, params)
321 m.reply "no reactions programmed"
325 trigger = params[:trigger].to_s
327 found = find_reaction(trigger)
330 m.reply "I'm not reacting to #{trigger}"
334 m.reply found.replies.join(", ")
339 plugin = ReactionPlugin.new
341 plugin.map plugin.add_syntax, :action => 'handle_add',
342 :requirements => { :trigger => /^(?:act:)?!.*?!/ }
343 plugin.map plugin.add_syntax, :action => 'handle_add',
344 :requirements => { :trigger => /^(?:act:)?\/.*?\// }
345 plugin.map plugin.add_syntax, :action => 'handle_add',
346 :requirements => { :trigger => /^(?:act:)?".*?"/ }
347 plugin.map plugin.add_syntax, :action => 'handle_add',
348 :requirements => { :trigger => /^(?:act:)?'.*?'/ }
349 plugin.map plugin.add_syntax.sub('*', ':'), :action => 'handle_add'
351 plugin.map 'reaction list [:page]', :action => 'handle_list',
352 :requirements => { :page => /^\d+$/ }
354 plugin.map 'reaction show *trigger', :action => 'handle_show'
356 plugin.map plugin.move_syntax, :action => 'handle_move',
357 :requirements => { :source => /^(?:act:)?!.*?!/ }
358 plugin.map plugin.move_syntax, :action => 'handle_move',
359 :requirements => { :source => /^(?:act:)?\/.*?\// }
360 plugin.map plugin.move_syntax, :action => 'handle_move',
361 :requirements => { :source => /^(?:act:)?".*?"/ }
362 plugin.map plugin.move_syntax, :action => 'handle_move',
363 :requirements => { :source => /^(?:act:)?'.*?'/ }
364 plugin.map plugin.move_syntax.sub('*', ':'), :action => 'handle_move'
367 plugin.map 'reaction del[ete] *trigger', :action => 'handle_rm'
368 plugin.map 'reaction delete *trigger', :action => 'handle_rm'
369 plugin.map 'reaction remove *trigger', :action => 'handle_rm'
370 plugin.map 'reaction rm *trigger', :action => 'handle_rm'