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 @trigger << Regexp.new(/\b#{Regexp.escape(rex)}\b/ui)
75 def add_reply(expr, *args)
76 @raw_replies << expr.dup
79 if rex.sub!(/^act:/,'')
82 @replies << Reply.new(self, act ? :act : :reply, rex, *args)
88 @replies[@raw_replies.index(expr)] rescue nil
93 pcts = @replies.map { |rep|
100 debug "percentages: #{pcts.inspect}"
103 @replies.each_with_index { |r, i|
105 r.range = last..(last+p)
108 debug "ranges: #{@replies.map { |r| r.range}.inspect}"
113 debug "#{pick} in #{@replies.map { |r| r.range}.inspect}"
115 return r if r.range and r.range === pick
121 return nil if @trigger.first and not message.action
122 return message.message.match(@trigger.last)
137 class ReactionPlugin < Plugin
139 ADD_SYNTAX = 'react to *trigger with *reply [at :chance chance]'
140 MOVE_SYNTAX = 'reaction move *source to *dest'
154 if @registry.has_key?(:reactions)
155 @reactions = @registry[:reactions]
156 raise unless @reactions
163 :underline => Underline,
166 :normal => NormalText,
174 @registry[:reactions] = @reactions
177 def help(plugin, topic="")
178 if plugin.to_sym == :react
179 return "react to <trigger> with <reply> [at <chance> chance] => " +
180 "create a new reaction to expression <trigger> to which the bot will reply <reply>, optionally at chance <chance>, " +
181 "seek help for reaction trigger, reaction reply and reaction chance for more details"
183 case (topic.to_sym rescue nil)
186 when :remove, :delete, :rm, :del
187 "reaction #{topic} <trigger> => removes the reaction to expression <trigger>"
188 when :chance, :chances
189 "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 " +
190 "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, " +
191 "there is a chance that the trigger will not actually cause a reply. Otherwise, the chances express the relative frequency of the replies."
192 when :trigger, :triggers
193 "reaction triggers can have one of the format: single_word 'multiple words' \"multiple words \" /regular_expression/ !regular_expression!. " +
194 "If prefixed by 'act:' (e.g. act:/(order|command)s/) the bot will only respond if a CTCP ACTION matches the trigger"
195 when :reply, :replies
196 "reaction replies are simply messages that the bot will reply when a trigger is matched. " +
197 "Replies can be prefixed by 'act:' (e.g. act:goes shopping) to signify that the bot should act instead of saying the message. " +
198 "Replies can use the %%{key} syntax to access one of the following keys: " +
199 "who (the user that said the trigger), bot (the bot's own nick), " +
200 "target (the first word following the trigger), what (whatever follows target), " +
201 "stuff (everything that follows the trigger), match (the actual matched text), " +
202 "match1, match2, ... (the i-th capture)"
204 "reaction list [n]: lists the n-the page of programmed reactions (30 reactions are listed per page)"
206 "reaction show <trigger>: list the programmed replies to trigger <trigger>"
208 "reaction topics: add, remove, delete, rm, del, triggers, replies, chance, list, show"
213 return unless PrivMessage === m
214 debug "testing #{m} for reactions"
215 return if @reactions.empty?
216 candidates = @reactions.map { |react|
218 blob ? [blob, react] : nil
220 return if candidates.empty?
221 match, wanted = candidates.sort { |m1, m2|
222 m1.first[0].length <=> m2.first[0].length
225 stuff = match.post_match.strip
226 target, what = stuff.split(/\s+/, 2)
228 :who => m.sourcenick,
234 match.to_a.each_with_index { |d, i|
235 extra[:"match#{i}"] = d
237 subs = @subs.dup.merge extra
238 reply = wanted.pick_reply
239 debug "picked #{reply}"
241 args = reply.apply(subs)
245 def find_reaction(trigger)
246 @reactions.find { |react|
247 react.raw_trigger.downcase == trigger.downcase
251 def handle_add(m, params)
252 trigger = params[:trigger].to_s
253 reply = params[:reply].to_s
255 pct = params[:chance] || "1"
257 pct = (pct.to_f/100).clip(0,1)
259 pct = pct.to_f.clip(0,1)
262 reaction = find_reaction(trigger)
264 reaction = Reaction.new(trigger)
265 @reactions << reaction
266 m.reply "Ok, I'll start reacting to #{reaction.raw_trigger}"
268 found = reaction.find_reply(reply)
271 found.author = m.sourcenick
272 found.date = Time.now
273 found.channel = m.channel
275 found = reaction.add_reply(reply, pct, m.sourcenick, Time.now, m.channel)
277 m.reply "I'll react to #{reaction.raw_trigger} with #{reaction.raw_replies.last} (#{(reaction.replies.last.pct * 100).to_i}%)"
280 def handle_move(m, params)
281 source = params[:source].to_s
282 dest = params[:dest].to_s
283 found = find_reaction(source)
285 m.reply "I don't react to #{source}"
288 if find_reaction(dest)
289 m.reply "I already react to #{dest}, so I won't move #{source} to #{dest}"
293 m.reply "Ok, I'll react to #{found.raw_trigger} now"
296 def handle_rm(m, params)
297 trigger = params[:trigger].to_s
298 debug trigger.inspect
299 found = find_reaction(trigger)
301 @reactions.delete(found)
302 m.reply "I won't react to #{found.raw_trigger} anymore"
304 m.reply "no reaction programmed for #{trigger}"
308 def handle_list(m, params)
310 m.reply "no reactions programmed"
315 pages = @reactions.length / per_page + 1
316 page = params[:page].to_i.clip(1, pages)
318 str = @reactions[(page-1)*per_page, per_page].join(", ")
320 m.reply "Programmed reactions (page #{page}/#{pages}): #{str}"
323 def handle_show(m, params)
325 m.reply "no reactions programmed"
329 trigger = params[:trigger].to_s
331 found = find_reaction(trigger)
334 m.reply "I'm not reacting to #{trigger}"
338 m.reply found.replies.join(", ")
343 plugin = ReactionPlugin.new
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, :action => 'handle_add',
350 :requirements => { :trigger => /^(?:act:)?".*?"/ }
351 plugin.map plugin.add_syntax, :action => 'handle_add',
352 :requirements => { :trigger => /^(?:act:)?'.*?'/ }
353 plugin.map plugin.add_syntax.sub('*', ':'), :action => 'handle_add'
355 plugin.map 'reaction list [:page]', :action => 'handle_list',
356 :requirements => { :page => /^\d+$/ }
358 plugin.map 'reaction show *trigger', :action => 'handle_show'
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, :action => 'handle_move',
365 :requirements => { :source => /^(?:act:)?".*?"/ }
366 plugin.map plugin.move_syntax, :action => 'handle_move',
367 :requirements => { :source => /^(?:act:)?'.*?'/ }
368 plugin.map plugin.move_syntax.sub('*', ':'), :action => 'handle_move'
371 plugin.map 'reaction del[ete] *trigger', :action => 'handle_rm'
372 plugin.map 'reaction delete *trigger', :action => 'handle_rm'
373 plugin.map 'reaction remove *trigger', :action => 'handle_rm'
374 plugin.map 'reaction rm *trigger', :action => 'handle_rm'