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'
148 if @registry.has_key?(:reactions)
149 @reactions = @registry[:reactions]
150 raise unless @reactions
157 :underline => Underline,
160 :normal => NormalText,
168 @registry[:reactions] = @reactions
171 def help(plugin, topic="")
172 if plugin.to_sym == :react
173 return "react to <trigger> with <reply> [at <chance> chance] => " +
174 "create a new reaction to expression <trigger> to which the bot will reply <reply>, optionally at chance <chance>, " +
175 "seek help for reaction trigger, reaction reply and reaction chance for more details"
177 case (topic.to_sym rescue nil)
180 when :remove, :delete, :rm, :del
181 "reaction #{topic} <trigger> => removes the reaction to expression <trigger>"
182 when :chance, :chances
183 "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 " +
184 "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, " +
185 "there is a chance that the trigger will not actually cause a reply. Otherwise, the chances express the relative frequency of the replies."
186 when :trigger, :triggers
187 "reaction triggers can have one of the format: single_word 'multiple words' \"multiple words \" /regular_expression/ !regular_expression!. " +
188 "If prefixed by 'act:' (e.g. act:/(order|command)s/) the bot will only respond if a CTCP ACTION matches the trigger"
189 when :reply, :replies
190 "reaction replies are simply messages that the bot will reply when a trigger is matched. " +
191 "Replies can be prefixed by 'act:' (e.g. act:goes shopping) to signify that the bot should act instead of saying the message. " +
192 "Replies can use the %%{key} syntax to access one of the following keys: " +
193 "who (the user that said the trigger), bot (the bot's own nick), " +
194 "target (the first word following the trigger), what (whatever follows target), " +
195 "stuff (everything that follows the trigger), match (the actual matched text)"
197 "reaction list [n]: lists the n-the page of programmed reactions (30 reactions are listed per page)"
199 "reaction show <trigger>: list the programmed replies to trigger <trigger>"
201 "reaction topics: add, remove, delete, rm, del, triggers, replies, chance, list, show"
206 return unless PrivMessage === m
207 debug "testing #{m} for reactions"
208 return if @reactions.empty?
209 wanted = @reactions.find { |react|
215 stuff = match.post_match.strip
216 target, what = stuff.split(/\s+/, 2)
218 :who => m.sourcenick,
224 subs = @subs.dup.merge extra
225 reply = wanted.pick_reply
226 debug "picked #{reply}"
228 args = reply.apply(subs)
232 def find_reaction(trigger)
233 @reactions.find { |react|
234 react.raw_trigger == trigger
238 def handle_add(m, params)
239 trigger = params[:trigger].to_s
240 reply = params[:reply].to_s
242 pct = params[:chance] || "1"
244 pct = (pct.to_f/100).clip(0,1)
246 pct = pct.to_f.clip(0,1)
249 reaction = find_reaction(trigger)
251 reaction = Reaction.new(trigger)
252 @reactions << reaction
253 m.reply "Ok, I'll start reacting to #{reaction.raw_trigger}"
255 found = reaction.find_reply(reply)
258 found.author = m.sourcenick
259 found.date = Time.now
260 found.channel = m.channel
262 found = reaction.add_reply(reply, pct, m.sourcenick, Time.now, m.channel)
264 m.reply "I'll react to #{reaction.raw_trigger} with #{reaction.raw_replies.last} (#{(reaction.replies.last.pct * 100).to_i}%)"
267 def handle_rm(m, params)
268 trigger = params[:trigger].to_s
269 debug trigger.inspect
270 found = find_reaction(trigger)
272 @reactions.delete(found)
273 m.reply "I won't react to #{found.raw_trigger} anymore"
275 m.reply "no reaction programmed for #{trigger}"
279 def handle_list(m, params)
281 m.reply "no reactions programmed"
286 pages = @reactions.length / per_page + 1
287 page = params[:page].to_i.clip(1, pages)
289 str = @reactions[(page-1)*per_page, per_page].join(", ")
291 m.reply "Programmed reactions (page #{page}/#{pages}): #{str}"
294 def handle_show(m, params)
296 m.reply "no reactions programmed"
300 trigger = params[:trigger].to_s
302 found = find_reaction(trigger)
305 m.reply "I'm not reacting to #{trigger}"
309 m.reply found.replies.join(", ")
314 plugin = ReactionPlugin.new
316 plugin.map plugin.add_syntax, :action => 'handle_add',
317 :requirements => { :trigger => /^(?:act:)?!.*?!/ }
318 plugin.map plugin.add_syntax, :action => 'handle_add',
319 :requirements => { :trigger => /^(?:act:)?\/.*?\// }
320 plugin.map plugin.add_syntax, :action => 'handle_add',
321 :requirements => { :trigger => /^(?:act:)?".*?"/ }
322 plugin.map plugin.add_syntax, :action => 'handle_add',
323 :requirements => { :trigger => /^(?:act:)?'.*?'/ }
324 plugin.map plugin.add_syntax.sub('*', ':'), :action => 'handle_add'
326 plugin.map 'reaction list [:page]', :action => 'handle_list',
327 :requirements => { :page => /^\d+$/ }
329 plugin.map 'reaction show *trigger', :action => 'handle_show'
331 plugin.map 'reaction del[ete] *trigger', :action => 'handle_rm'
332 plugin.map 'reaction delete *trigger', :action => 'handle_rm'
333 plugin.map 'reaction remove *trigger', :action => 'handle_rm'
334 plugin.map 'reaction rm *trigger', :action => 'handle_rm'