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)
158 when :remove, :delete, :rm, :del
159 "reaction #{topic} <trigger> => removes the reaction to expression <trigger>"
160 when :chance, :chances
161 "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 " +
162 "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, " +
163 "there is a chance that the trigger will not actually cause a reply. Otherwise, the chances express the relative frequency of the replies."
164 when :trigger, :triggers
165 "reaction triggers can have one of the format: single_word 'multiple words' \"multiple words \" /regular_expression/ !regular_expression!. " +
166 "If prefixed by 'act:' (e.g. act:/(order|command)s/) the bot will only respond if a CTCP ACTION matches the trigger"
167 when :reply, :replies
168 "reaction replies are simply messages that the bot will reply when a trigger is matched. " +
169 "Replies can be prefixed by 'act:' (e.g. act:goes shopping) to signify that the bot should act instead of saying the message. " +
170 "Replies can use the %%{key} syntax to access one of the following keys: " +
171 "who (the user that said the trigger), bot (the bot's own nick), " +
172 "target (the first word following the trigger), what (whatever follows target), " +
173 "stuff (everything that follows the trigger), match (the actual matched text)"
175 "reaction list [n]: lists the n-the page of programmed reactions (30 reactions are listed per page)"
177 "reaction topics: add, remove, delete, rm, del, triggers, replies, list"
182 return unless PrivMessage === m
183 debug "testing #{m} for reactions"
184 return if @reactions.empty?
185 wanted = @reactions.find { |react|
191 stuff = match.post_match.strip
192 target, what = stuff.split(/\s+/, 2)
194 :who => m.sourcenick,
200 subs = @subs.dup.merge extra
201 reply = wanted.pick_reply
202 debug "picked #{reply}"
204 args = reply.apply(subs)
208 def find_reaction(trigger)
209 @reactions.find { |react|
210 react.raw_trigger == trigger
214 def handle_add(m, params)
215 trigger = params[:trigger].to_s
216 reply = params[:reply].to_s
218 pct = params[:chance] || "1"
220 pct = (pct.to_f/100).clip(0,1)
222 pct = pct.to_f.clip(0,1)
225 reaction = find_reaction(trigger)
227 reaction = Reaction.new(trigger)
228 @reactions << reaction
229 m.reply "Ok, I'll start reacting to #{reaction.raw_trigger}"
231 reaction.add_reply(reply, pct, m.sourcenick, Time.now, m.channel)
232 m.reply "I'll react to #{reaction.raw_trigger} with #{reaction.raw_replies.last} (#{(reaction.replies.last.pct * 100).to_i}%)"
235 def handle_rm(m, params)
236 trigger = params[:trigger].to_s
237 debug trigger.inspect
238 found = find_reaction(trigger)
240 @reactions.delete(found)
241 m.reply "I won't react to #{found.raw_trigger} anymore"
243 m.reply "no reaction programmed for #{trigger}"
247 def handle_list(m, params)
249 m.reply "no reactions programmed"
254 pages = @reactions.length / per_page + 1
255 page = params[:page].to_i.clip(1, pages)
257 str = @reactions[(page-1)*per_page, per_page].join(", ")
259 m.reply "Programmed reactions (page #{page}/#{pages}): #{str}"
262 def handle_show(m, params)
264 m.reply "no reactions programmed"
268 trigger = params[:trigger].to_s
270 found = find_reaction(trigger)
273 m.reply "I'm not reacting to #{trigger}"
277 m.reply found.replies.join(", ")
282 plugin = ReactionPlugin.new
284 plugin.map plugin.add_syntax, :action => 'handle_add',
285 :requirements => { :trigger => /^(?:act:)?!.*?!/ }
286 plugin.map plugin.add_syntax, :action => 'handle_add',
287 :requirements => { :trigger => /^(?:act:)?\/.*?\// }
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.sub('*', ':'), :action => 'handle_add'
294 plugin.map 'reaction list [:page]', :action => 'handle_list',
295 :requirements => { :page => /^\d+$/ }
297 plugin.map 'reaction show *trigger', :action => 'handle_show'
299 plugin.map 'reaction del[ete] *trigger', :action => 'handle_rm'
300 plugin.map 'reaction delete *trigger', :action => 'handle_rm'
301 plugin.map 'reaction remove *trigger', :action => 'handle_rm'
302 plugin.map 'reaction rm *trigger', :action => 'handle_rm'