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, :reply
16 attr_reader :raw_trigger, :raw_reply
17 attr_accessor :author, :date, :channel
20 @raw_trigger = expr.dup
23 if rex.sub!(/^act:/,'')
27 if rex.sub!(%r@^([/!])(.*)\1$@, '\2')
29 @trigger << Regexp.new(rex)
31 @trigger << Regexp.new(/\b#{Regexp.escape(rex)}\b/u)
39 if rex.sub!(/^act:/,'')
42 @reply = [act ? :act : :reply]
47 return nil if @trigger.first and not message.action
48 return message.message.match(@trigger.last)
51 def initialize(trig, msg , auth, dt, chan)
56 self.channel=chan.to_s
60 "trigger #{raw_trigger} (#{author}, #{channel}, #{date})"
65 class ReactionPlugin < Plugin
67 ADD_SYNTAX = 'react to *trigger with *reply'
77 if @registry.has_key?(:reactions)
78 @reactions = @registry[:reactions]
79 raise unless @reactions
86 :underline => Underline,
89 :normal => NormalText,
97 @registry[:reactions] = @reactions
100 def help(plugin, topic="")
101 if plugin.to_sym == :react
102 return "react to <trigger> with <reply> => create a new reaction to expression <trigger> to which the bot will reply <reply>, seek help for reaction trigger and reaction reply for more details"
104 case (topic.to_sym rescue nil)
105 when :remove, :delete, :rm, :del
106 "reaction #{topic} <trigger> => removes the reaction to expression <trigger>"
107 when :trigger, :triggers
108 "reaction triggers can have one of the format: single_word 'multiple words' \"multiple words \" /regular_expression/ !regular_expression!. " +
109 "If prefixed by 'act:' (e.g. act:/(order|command)s/) the bot will only respond if a CTCP ACTION matches the trigger"
110 when :reply, :replies
111 "reaction replies are simply messages that the bot will reply when a trigger is matched. " +
112 "Replies can be prefixed by 'act:' (e.g. act:goes shopping) to signify that the bot should act instead of saying the message. " +
113 "Replies can use the %%{key} syntax to access one of the following keys: " +
114 "who (the user that said the trigger), bot (the bot's own nick), " +
115 "target (the first word following the trigger), what (whatever follows target), " +
116 "stuff (everything that follows the trigger), match (the actual matched text)"
118 "reaction list [n]: lists the n-the page of programmed reactions (30 reactions are listed per page)"
120 "reaction topics: add, remove, delete, rm, del, triggers, replies, list"
125 return unless PrivMessage === m
126 debug "testing #{m} for reactions"
127 return if @reactions.empty?
128 wanted = @reactions.find { |react|
134 stuff = match.post_match.strip
135 target, what = stuff.split(/\s+/, 2)
137 :who => m.sourcenick,
143 subs = @subs.dup.merge extra
144 args = [wanted.reply.first]
145 args << wanted.reply.last % extra
149 def handle_add(m, params)
150 reaction = Reaction.new(params[:trigger].to_s, params[:reply].to_s, m.sourcenick, Time.now, m.channel)
151 @reactions << reaction
152 m.reply "added reaction to #{reaction.trigger.last} with #{reaction.reply.last}"
155 def handle_rm(m, params)
156 trigger = params[:trigger].to_s
157 debug trigger.inspect
158 found = @reactions.find { |react|
159 react.raw_trigger == trigger
162 @reactions.delete(found)
163 m.reply "removed reaction to #{found.trigger.last} with #{found.reply.last}"
165 m.reply "no reaction programmed for #{trigger}"
169 def handle_list(m, params)
171 m.reply "no reactions programmed"
176 pages = @reactions.length / per_page + 1
177 page = params[:page].to_i.clip(1, pages)
179 str = @reactions[(page-1)*per_page, per_page].join(", ")
181 m.reply "Programmed reactions (page #{page}/#{pages}): #{str}"
186 plugin = ReactionPlugin.new
188 plugin.map plugin.add_syntax, :action => 'handle_add',
189 :requirements => { :trigger => /^(?:act:)?!.*?!/ }
190 plugin.map plugin.add_syntax, :action => 'handle_add',
191 :requirements => { :trigger => /^(?:act:)?\/.*?\// }
192 plugin.map plugin.add_syntax, :action => 'handle_add',
193 :requirements => { :trigger => /^(?:act:)?".*?"/ }
194 plugin.map plugin.add_syntax, :action => 'handle_add',
195 :requirements => { :trigger => /^(?:act:)?'.*?'/ }
196 plugin.map plugin.add_syntax.sub('*', ':'), :action => 'handle_add'
198 plugin.map 'reaction list [:page]', :action => 'handle_list',
199 :requirements => { :page => /^\d+$/ }
201 plugin.map 'reaction del[ete] *trigger', :action => 'handle_rm'
202 plugin.map 'reaction delete *trigger', :action => 'handle_rm'
203 plugin.map 'reaction remove *trigger', :action => 'handle_rm'
204 plugin.map 'reaction rm *trigger', :action => 'handle_rm'