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_accessor :act, :reply, :pct, :range
20 attr_accessor :author, :date, :channel
21 def initialize(act, expr, pct, author, date, channel, range=nil)
27 @channel = channel.to_s
33 "#{act} #{reply} (#{pct} chance)",
34 @range ? "(#{@range})" : "",
35 "(#{author}, #{channel}, #{date})"
45 @raw_trigger = expr.dup
48 if rex.sub!(/^act:/,'')
52 if rex.sub!(%r@^([/!])(.*)\1$@, '\2')
53 @trigger << Regexp.new(rex)
55 @trigger << Regexp.new(/\b#{Regexp.escape(rex)}\b/u)
59 def add_reply(expr, *args)
60 @raw_replies << expr.dup
63 if rex.sub!(/^act:/,'')
66 @replies << Reply.new(act ? :act : :reply, rex, *args)
72 pcts = @replies.map { |rep|
79 debug "percentages: #{pcts.inspect}"
82 @replies.each_with_index { |r, i|
84 r.range = last..(last+p)
87 debug "ranges: #{@replies.map { |r| r.range}.inspect}"
92 debug "#{pick} in #{@replies.map { |r| r.range}.inspect}"
94 return r if r.range and r.range === pick
100 return nil if @trigger.first and not message.action
101 return message.message.match(@trigger.last)
116 class ReactionPlugin < Plugin
118 ADD_SYNTAX = 'react to *trigger with *reply at :chance chance'
128 if @registry.has_key?(:reactions)
129 @reactions = @registry[:reactions]
130 raise unless @reactions
137 :underline => Underline,
140 :normal => NormalText,
148 @registry[:reactions] = @reactions
151 def help(plugin, topic="")
152 if plugin.to_sym == :react
153 return "react to <trigger> with <reply> [at <chance> chance] => " +
154 "create a new reaction to expression <trigger> to which the bot will reply <reply>, optionally at chance <chance>, " +
155 "seek help for reaction trigger, reaction reply and reaction chance for more details"
157 case (topic.to_sym rescue nil)
160 when :remove, :delete, :rm, :del
161 "reaction #{topic} <trigger> => removes the reaction to expression <trigger>"
162 when :chance, :chances
163 "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 " +
164 "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, " +
165 "there is a chance that the trigger will not actually cause a reply. Otherwise, the chances express the relative frequency of the replies."
166 when :trigger, :triggers
167 "reaction triggers can have one of the format: single_word 'multiple words' \"multiple words \" /regular_expression/ !regular_expression!. " +
168 "If prefixed by 'act:' (e.g. act:/(order|command)s/) the bot will only respond if a CTCP ACTION matches the trigger"
169 when :reply, :replies
170 "reaction replies are simply messages that the bot will reply when a trigger is matched. " +
171 "Replies can be prefixed by 'act:' (e.g. act:goes shopping) to signify that the bot should act instead of saying the message. " +
172 "Replies can use the %%{key} syntax to access one of the following keys: " +
173 "who (the user that said the trigger), bot (the bot's own nick), " +
174 "target (the first word following the trigger), what (whatever follows target), " +
175 "stuff (everything that follows the trigger), match (the actual matched text)"
177 "reaction list [n]: lists the n-the page of programmed reactions (30 reactions are listed per page)"
179 "reaction show <trigger>: list the programmed replies to trigger <trigger>"
181 "reaction topics: add, remove, delete, rm, del, triggers, replies, chance, list, show"
186 return unless PrivMessage === m
187 debug "testing #{m} for reactions"
188 return if @reactions.empty?
189 wanted = @reactions.find { |react|
195 stuff = match.post_match.strip
196 target, what = stuff.split(/\s+/, 2)
198 :who => m.sourcenick,
204 subs = @subs.dup.merge extra
205 reply = wanted.pick_reply
206 debug "picked #{reply}"
208 args = reply.apply(subs)
212 def find_reaction(trigger)
213 @reactions.find { |react|
214 react.raw_trigger == trigger
218 def handle_add(m, params)
219 trigger = params[:trigger].to_s
220 reply = params[:reply].to_s
222 pct = params[:chance] || "1"
224 pct = (pct.to_f/100).clip(0,1)
226 pct = pct.to_f.clip(0,1)
229 reaction = find_reaction(trigger)
231 reaction = Reaction.new(trigger)
232 @reactions << reaction
233 m.reply "Ok, I'll start reacting to #{reaction.raw_trigger}"
235 reaction.add_reply(reply, pct, m.sourcenick, Time.now, m.channel)
236 m.reply "I'll react to #{reaction.raw_trigger} with #{reaction.raw_replies.last} (#{(reaction.replies.last.pct * 100).to_i}%)"
239 def handle_rm(m, params)
240 trigger = params[:trigger].to_s
241 debug trigger.inspect
242 found = find_reaction(trigger)
244 @reactions.delete(found)
245 m.reply "I won't react to #{found.raw_trigger} anymore"
247 m.reply "no reaction programmed for #{trigger}"
251 def handle_list(m, params)
253 m.reply "no reactions programmed"
258 pages = @reactions.length / per_page + 1
259 page = params[:page].to_i.clip(1, pages)
261 str = @reactions[(page-1)*per_page, per_page].join(", ")
263 m.reply "Programmed reactions (page #{page}/#{pages}): #{str}"
266 def handle_show(m, params)
268 m.reply "no reactions programmed"
272 trigger = params[:trigger].to_s
274 found = find_reaction(trigger)
277 m.reply "I'm not reacting to #{trigger}"
281 m.reply found.replies.join(", ")
286 plugin = ReactionPlugin.new
288 plugin.map plugin.add_syntax, :action => 'handle_add',
289 :requirements => { :trigger => /^(?:act:)?!.*?!/ }
290 plugin.map plugin.add_syntax, :action => 'handle_add',
291 :requirements => { :trigger => /^(?:act:)?\/.*?\// }
292 plugin.map plugin.add_syntax, :action => 'handle_add',
293 :requirements => { :trigger => /^(?:act:)?".*?"/ }
294 plugin.map plugin.add_syntax, :action => 'handle_add',
295 :requirements => { :trigger => /^(?:act:)?'.*?'/ }
296 plugin.map plugin.add_syntax.sub('*', ':'), :action => 'handle_add'
298 plugin.map 'reaction list [:page]', :action => 'handle_list',
299 :requirements => { :page => /^\d+$/ }
301 plugin.map 'reaction show *trigger', :action => 'handle_show'
303 plugin.map 'reaction del[ete] *trigger', :action => 'handle_rm'
304 plugin.map 'reaction delete *trigger', :action => 'handle_rm'
305 plugin.map 'reaction remove *trigger', :action => 'handle_rm'
306 plugin.map 'reaction rm *trigger', :action => 'handle_rm'