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
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)
71 @trigger << Regexp.new(/\b#{Regexp.escape(rex)}\b/u)
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'
149 if @registry.has_key?(:reactions)
150 @reactions = @registry[:reactions]
151 raise unless @reactions
158 :underline => Underline,
161 :normal => NormalText,
169 @registry[:reactions] = @reactions
172 def help(plugin, topic="")
173 if plugin.to_sym == :react
174 return "react to <trigger> with <reply> [at <chance> chance] => " +
175 "create a new reaction to expression <trigger> to which the bot will reply <reply>, optionally at chance <chance>, " +
176 "seek help for reaction trigger, reaction reply and reaction chance for more details"
178 case (topic.to_sym rescue nil)
181 when :remove, :delete, :rm, :del
182 "reaction #{topic} <trigger> => removes the reaction to expression <trigger>"
183 when :chance, :chances
184 "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 " +
185 "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, " +
186 "there is a chance that the trigger will not actually cause a reply. Otherwise, the chances express the relative frequency of the replies."
187 when :trigger, :triggers
188 "reaction triggers can have one of the format: single_word 'multiple words' \"multiple words \" /regular_expression/ !regular_expression!. " +
189 "If prefixed by 'act:' (e.g. act:/(order|command)s/) the bot will only respond if a CTCP ACTION matches the trigger"
190 when :reply, :replies
191 "reaction replies are simply messages that the bot will reply when a trigger is matched. " +
192 "Replies can be prefixed by 'act:' (e.g. act:goes shopping) to signify that the bot should act instead of saying the message. " +
193 "Replies can use the %%{key} syntax to access one of the following keys: " +
194 "who (the user that said the trigger), bot (the bot's own nick), " +
195 "target (the first word following the trigger), what (whatever follows target), " +
196 "stuff (everything that follows the trigger), match (the actual matched text)"
198 "reaction list [n]: lists the n-the page of programmed reactions (30 reactions are listed per page)"
200 "reaction show <trigger>: list the programmed replies to trigger <trigger>"
202 "reaction topics: add, remove, delete, rm, del, triggers, replies, chance, list, show"
207 return unless PrivMessage === m
208 debug "testing #{m} for reactions"
209 return if @reactions.empty?
210 wanted = @reactions.find { |react|
216 stuff = match.post_match.strip
217 target, what = stuff.split(/\s+/, 2)
219 :who => m.sourcenick,
225 subs = @subs.dup.merge extra
226 reply = wanted.pick_reply
227 debug "picked #{reply}"
229 args = reply.apply(subs)
233 def find_reaction(trigger)
234 @reactions.find { |react|
235 react.raw_trigger == trigger
239 def handle_add(m, params)
240 trigger = params[:trigger].to_s
241 reply = params[:reply].to_s
243 pct = params[:chance] || "1"
245 pct = (pct.to_f/100).clip(0,1)
247 pct = pct.to_f.clip(0,1)
250 reaction = find_reaction(trigger)
252 reaction = Reaction.new(trigger)
253 @reactions << reaction
254 m.reply "Ok, I'll start reacting to #{reaction.raw_trigger}"
256 found = reaction.find_reply(reply)
259 found.author = m.sourcenick
260 found.date = Time.now
261 found.channel = m.channel
263 found = reaction.add_reply(reply, pct, m.sourcenick, Time.now, m.channel)
265 m.reply "I'll react to #{reaction.raw_trigger} with #{reaction.raw_replies.last} (#{(reaction.replies.last.pct * 100).to_i}%)"
268 def handle_rm(m, params)
269 trigger = params[:trigger].to_s
270 debug trigger.inspect
271 found = find_reaction(trigger)
273 @reactions.delete(found)
274 m.reply "I won't react to #{found.raw_trigger} anymore"
276 m.reply "no reaction programmed for #{trigger}"
280 def handle_list(m, params)
282 m.reply "no reactions programmed"
287 pages = @reactions.length / per_page + 1
288 page = params[:page].to_i.clip(1, pages)
290 str = @reactions[(page-1)*per_page, per_page].join(", ")
292 m.reply "Programmed reactions (page #{page}/#{pages}): #{str}"
295 def handle_show(m, params)
297 m.reply "no reactions programmed"
301 trigger = params[:trigger].to_s
303 found = find_reaction(trigger)
306 m.reply "I'm not reacting to #{trigger}"
310 m.reply found.replies.join(", ")
315 plugin = ReactionPlugin.new
317 plugin.map plugin.add_syntax, :action => 'handle_add',
318 :requirements => { :trigger => /^(?:act:)?!.*?!/ }
319 plugin.map plugin.add_syntax, :action => 'handle_add',
320 :requirements => { :trigger => /^(?:act:)?\/.*?\// }
321 plugin.map plugin.add_syntax, :action => 'handle_add',
322 :requirements => { :trigger => /^(?:act:)?".*?"/ }
323 plugin.map plugin.add_syntax, :action => 'handle_add',
324 :requirements => { :trigger => /^(?:act:)?'.*?'/ }
325 plugin.map plugin.add_syntax.sub('*', ':'), :action => 'handle_add'
327 plugin.map 'reaction list [:page]', :action => 'handle_list',
328 :requirements => { :page => /^\d+$/ }
330 plugin.map 'reaction show *trigger', :action => 'handle_show'
332 plugin.map 'reaction del[ete] *trigger', :action => 'handle_rm'
333 plugin.map 'reaction delete *trigger', :action => 'handle_rm'
334 plugin.map 'reaction remove *trigger', :action => 'handle_rm'
335 plugin.map 'reaction rm *trigger', :action => 'handle_rm'