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
71 if @registry.has_key?(:reactions)
72 @reactions = @registry[:reactions]
73 raise unless @reactions
80 :underline => Underline,
83 :normal => NormalText,
91 @registry[:reactions] = @reactions
94 def help(plugin, topic="")
95 case (topic.to_sym rescue nil)
97 "reaction add <trigger> <reply> => create a new reaction to expression <trigger> to which the bot will reply <reply>"
98 when :remove, :delete, :rm, :del
99 "reaction #{topic} <trigger> => removes the reaction to expression <trigger>"
100 when :trigger, :triggers
101 "reaction triggers can have one of the format: single_word /regular_expression/ !regular_expression!. " +
102 "If prefixed by 'act:' (e.g. act:/(order|command)s/) the bot will only respond if a CTCP ACTION matches the trigger"
103 when :reply, :replies
104 "reaction replies are simply messages that the bot will reply when a trigger is matched. " +
105 "Replies can be prefixed by 'act:' (e.g. act:goes shopping) to signify that the bot should act instead of saying the message. " +
106 "Replies can use the %%{key} syntax to access one of the following keys: " +
107 "who (the user that said the trigger), bot (the bot's own nick), " +
108 "target (the first word following the trigger), what (whatever follows target), " +
109 "stuff (everything that follows the trigger), match (the actual matched text)"
111 "lists all the programmed reactions"
113 "reaction topics: add, remove, delete, rm, del, triggers, replies, list"
118 return unless PrivMessage === m
119 debug "testing #{m} for reactions"
120 return if @reactions.empty?
121 wanted = @reactions.find { |react|
127 stuff = match.post_match.strip
128 target, what = stuff.split(/\s+/, 2)
130 :who => m.sourcenick,
136 subs = @subs.dup.merge extra
137 args = [wanted.reply.first]
138 args << wanted.reply.last % extra
142 def handle_add(m, params)
143 reaction = Reaction.new(params[:trigger].to_s, params[:reply].to_s, m.sourcenick, Time.now, m.channel)
144 @reactions << reaction
145 m.reply "added reaction to #{reaction.trigger.last} with #{reaction.reply.last}"
148 def handle_rm(m, params)
149 trigger = params[:trigger].to_s
150 debug trigger.inspect
151 found = @reactions.find { |react|
152 react.raw_trigger == trigger
155 @reactions.delete(found)
156 m.reply "removed reaction to #{found.trigger.last} with #{found.reply.last}"
158 m.reply "no reaction programmed for #{trigger}"
162 def handle_list(m, params)
164 m.reply "no reactions programmed"
169 pages = @reactions.length / per_page + 1
170 page = params[:page].to_i.clip(1, pages)
172 str = @reactions[(page-1)*per_page, per_page].join(", ")
174 m.reply "Programmed reactions (page #{page}/#{pages}): #{str}"
179 plugin = ReactionPlugin.new
181 plugin.map 'reaction add *trigger *reply', :action => 'handle_add',
182 :requirements => { :trigger => /^(?:act:)?!.*?!/ }
183 plugin.map 'reaction add *trigger *reply', :action => 'handle_add',
184 :requirements => { :trigger => /^(?:act:)?\/.*?\// }
185 plugin.map 'reaction add :trigger *reply', :action => 'handle_add'
187 plugin.map 'reaction list [:page]', :action => 'handle_list',
188 :requirements => { :page => /^\d+$/ }
190 plugin.map 'reaction del[ete] *trigger', :action => 'handle_rm'
191 plugin.map 'reaction delete *trigger', :action => 'handle_rm'
192 plugin.map 'reaction remove *trigger', :action => 'handle_rm'
193 plugin.map 'reaction rm *trigger', :action => 'handle_rm'