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 # Order by longest matching text first,
224 # and by number of captures second
225 longer = m1.first[0].length <=> m2.first[0].length
226 longer == 0 ? m1.first.length <=> m2.first.length : longer
229 before = match.pre_match.strip
230 after = match.post_match.strip
231 target, what = after.split(/\s+/, 2)
233 :who => m.sourcenick,
240 match.to_a.each_with_index { |d, i|
241 extra[:"match#{i}"] = d
243 subs = @subs.dup.merge extra
244 reply = wanted.pick_reply
245 debug "picked #{reply}"
247 args = reply.apply(subs)
251 def find_reaction(trigger)
252 @reactions.find { |react|
253 react.raw_trigger.downcase == trigger.downcase
257 def handle_add(m, params)
258 trigger = params[:trigger].to_s
259 reply = params[:reply].to_s
261 pct = params[:chance] || "1"
263 pct = (pct.to_f/100).clip(0,1)
265 pct = pct.to_f.clip(0,1)
268 reaction = find_reaction(trigger)
270 reaction = Reaction.new(trigger)
271 @reactions << reaction
272 m.reply "Ok, I'll start reacting to #{reaction.raw_trigger}"
274 found = reaction.find_reply(reply)
277 found.author = m.sourcenick
278 found.date = Time.now
279 found.channel = m.channel
281 found = reaction.add_reply(reply, pct, m.sourcenick, Time.now, m.channel)
283 m.reply "I'll react to #{reaction.raw_trigger} with #{reaction.raw_replies.last} (#{(reaction.replies.last.pct * 100).to_i}%)"
286 def handle_move(m, params)
287 source = params[:source].to_s
288 dest = params[:dest].to_s
289 found = find_reaction(source)
291 m.reply "I don't react to #{source}"
294 if find_reaction(dest)
295 m.reply "I already react to #{dest}, so I won't move #{source} to #{dest}"
299 m.reply "Ok, I'll react to #{found.raw_trigger} now"
302 def handle_rm(m, params)
303 trigger = params[:trigger].to_s
304 debug trigger.inspect
305 found = find_reaction(trigger)
307 @reactions.delete(found)
308 m.reply "I won't react to #{found.raw_trigger} anymore"
310 m.reply "no reaction programmed for #{trigger}"
314 def handle_list(m, params)
316 m.reply "no reactions programmed"
321 pages = @reactions.length / per_page + 1
322 page = params[:page].to_i.clip(1, pages)
324 str = @reactions[(page-1)*per_page, per_page].join(", ")
326 m.reply "Programmed reactions (page #{page}/#{pages}): #{str}"
329 def handle_show(m, params)
331 m.reply "no reactions programmed"
335 trigger = params[:trigger].to_s
337 found = find_reaction(trigger)
340 m.reply "I'm not reacting to #{trigger}"
344 m.reply found.replies.join(", ")
349 plugin = ReactionPlugin.new
351 plugin.map plugin.add_syntax, :action => 'handle_add',
352 :requirements => { :trigger => /^(?:act:)?!.*?!/ }
353 plugin.map plugin.add_syntax, :action => 'handle_add',
354 :requirements => { :trigger => /^(?:act:)?\/.*?\// }
355 plugin.map plugin.add_syntax, :action => 'handle_add',
356 :requirements => { :trigger => /^(?:act:)?".*?"/ }
357 plugin.map plugin.add_syntax, :action => 'handle_add',
358 :requirements => { :trigger => /^(?:act:)?'.*?'/ }
359 plugin.map plugin.add_syntax.sub('*', ':'), :action => 'handle_add'
361 plugin.map 'reaction list [:page]', :action => 'handle_list',
362 :requirements => { :page => /^\d+$/ }
364 plugin.map 'reaction show *trigger', :action => 'handle_show'
366 plugin.map plugin.move_syntax, :action => 'handle_move',
367 :requirements => { :source => /^(?:act:)?!.*?!/ }
368 plugin.map plugin.move_syntax, :action => 'handle_move',
369 :requirements => { :source => /^(?:act:)?\/.*?\// }
370 plugin.map plugin.move_syntax, :action => 'handle_move',
371 :requirements => { :source => /^(?:act:)?".*?"/ }
372 plugin.map plugin.move_syntax, :action => 'handle_move',
373 :requirements => { :source => /^(?:act:)?'.*?'/ }
374 plugin.map plugin.move_syntax.sub('*', ':'), :action => 'handle_move'
377 plugin.map 'reaction del[ete] *trigger', :action => 'handle_rm'
378 plugin.map 'reaction delete *trigger', :action => 'handle_rm'
379 plugin.map 'reaction remove *trigger', :action => 'handle_rm'
380 plugin.map 'reaction rm *trigger', :action => 'handle_rm'