]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/reaction.rb
c1c3074e0cb44f7dfbe158d0dfa4a8e67ce0453e
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / reaction.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Reaction plugin
5 #
6 # Author:: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
7 # Copyright:: (C) 2007 Giuseppe Bilotta
8 # License:: GPLv2
9 #
10 # Build one-liner replies/reactions to expressions/actions in channel
11 #
12 # Very alpha stage, so beware of sudden reaction syntax changes
13
14 class ::Reaction
15   attr_reader :trigger, :replies
16   attr_reader :raw_trigger, :raw_replies
17
18   class ::Reply
19     attr_accessor :act, :reply, :pct, :range
20     attr_accessor :author, :date, :channel
21     def initialize(act, expr, pct, author, date, channel, range=nil)
22       @act = act
23       @reply = expr
24       @pct = pct
25       @author = author.to_s
26       @date = date
27       @channel = channel.to_s
28       @range = range
29     end
30
31     def to_s
32       [
33         "#{act} #{reply} (#{pct} chance)",
34         @range ? "(#{@range})" : "",
35         "(#{author}, #{channel}, #{date})"
36       ].join(" ")
37     end
38
39     def apply(subs={})
40       [act, reply % subs]
41     end
42   end
43
44   def trigger=(expr)
45     @raw_trigger = expr.dup
46     act = false
47     rex = expr.dup
48     if rex.sub!(/^act:/,'')
49       act = true
50     end
51     @trigger = [act]
52     if rex.sub!(%r@^([/!])(.*)\1$@, '\2')
53       @trigger << Regexp.new(rex)
54     else
55       @trigger << Regexp.new(/\b#{Regexp.escape(rex)}\b/u)
56     end
57   end
58
59   def add_reply(expr, *args)
60     @raw_replies << expr.dup
61     act = false
62     rex = expr.dup
63     if rex.sub!(/^act:/,'')
64       act = true
65     end
66     @replies << Reply.new(act ? :act : :reply, rex, *args)
67     make_ranges
68   end
69
70   def make_ranges
71     totals = 0
72     pcts = @replies.map { |rep|
73       totals += rep.pct
74       rep.pct
75     }
76     pcts.map! { |p|
77       p/totals
78     } if totals > 1
79     debug "percentages: #{pcts.inspect}"
80
81     last = 0
82     @replies.each_with_index { |r, i|
83       p = pcts[i]
84       r.range = last..(last+p)
85       last+=p
86     }
87     debug "ranges: #{@replies.map { |r| r.range}.inspect}"
88   end
89
90   def pick_reply
91     pick = rand()
92     debug "#{pick} in #{@replies.map { |r| r.range}.inspect}"
93     @replies.each { |r|
94       return r if r.range and r.range === pick
95     }
96     return nil
97   end
98
99   def ===(message)
100     return nil if @trigger.first and not message.action
101     return message.message.match(@trigger.last)
102   end
103
104   def initialize(trig)
105     self.trigger=trig
106     @raw_replies = []
107     @replies = []
108   end
109
110   def to_s
111     raw_trigger
112   end
113
114 end
115
116 class ReactionPlugin < Plugin
117
118   ADD_SYNTAX = 'react to *trigger with *reply at :chance chance'
119
120   def add_syntax
121     return ADD_SYNTAX
122   end
123
124   attr :reactions
125
126   def initialize
127     super
128     if @registry.has_key?(:reactions)
129       @reactions = @registry[:reactions]
130       raise unless @reactions
131     else
132       @reactions = []
133     end
134
135     @subs = {
136       :bold => Bold,
137       :underline => Underline,
138       :reverse => Reverse,
139       :italic => Italic,
140       :normal => NormalText,
141       :color => Color,
142       :colour => Color,
143       :bot => @bot.myself,
144     }.merge ColorCode
145   end
146
147   def save
148     @registry[:reactions] = @reactions
149   end
150
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"
156     end
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)"
174     when :list
175       "reaction list [n]: lists the n-the page of programmed reactions (30 reactions are listed per page)"
176     else
177       "reaction topics: add, remove, delete, rm, del, triggers, replies, list"
178     end
179   end
180
181   def unreplied(m)
182     return unless PrivMessage === m
183     debug "testing #{m} for reactions"
184     return if @reactions.empty?
185     wanted = @reactions.find { |react|
186       react === m
187     }
188     return unless wanted
189     match = wanted === m
190     matched = match[0]
191     stuff = match.post_match.strip
192     target, what = stuff.split(/\s+/, 2)
193     extra = {
194       :who => m.sourcenick,
195       :match => matched,
196       :target => target,
197       :what => what,
198       :stuff => stuff
199     }
200     subs = @subs.dup.merge extra
201     reply = wanted.pick_reply
202     debug "picked #{reply}"
203     return unless reply
204     args = reply.apply(subs)
205     m.__send__(*args)
206   end
207
208   def find_reaction(trigger)
209     @reactions.find { |react|
210       react.raw_trigger == trigger
211     }
212   end
213
214   def handle_add(m, params)
215     trigger = params[:trigger].to_s
216     reply = params[:reply].to_s
217
218     pct = params[:chance] || "1"
219     if pct.sub(/%$/,'')
220       pct = (pct.to_f/100).clip(0,1)
221     else
222       pct = pct.to_f.clip(0,1)
223     end
224
225     reaction = find_reaction(trigger)
226     if not reaction
227       reaction = Reaction.new(trigger)
228       @reactions << reaction
229       m.reply "Ok, I'll start reacting to #{reaction.raw_trigger}"
230     end
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}%)"
233   end
234
235   def handle_rm(m, params)
236     trigger = params[:trigger].to_s
237     debug trigger.inspect
238     found = find_reaction(trigger)
239     if found
240       @reactions.delete(found)
241       m.reply "I won't react to #{found.raw_trigger} anymore"
242     else
243       m.reply "no reaction programmed for #{trigger}"
244     end
245   end
246
247   def handle_list(m, params)
248     if @reactions.empty?
249       m.reply "no reactions programmed"
250       return
251     end
252
253     per_page = 30
254     pages = @reactions.length / per_page + 1
255     page = params[:page].to_i.clip(1, pages)
256
257     str = @reactions[(page-1)*per_page, per_page].join(", ")
258
259     m.reply "Programmed reactions (page #{page}/#{pages}): #{str}"
260   end
261
262   def handle_show(m, params)
263     if @reactions.empty?
264       m.reply "no reactions programmed"
265       return
266     end
267
268     trigger = params[:trigger].to_s
269
270     found = find_reaction(trigger)
271
272     unless found
273       m.reply "I'm not reacting to #{trigger}"
274       return
275     end
276
277     m.reply found.replies.join(", ")
278   end
279
280 end
281
282 plugin = ReactionPlugin.new
283
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'
293
294 plugin.map 'reaction list [:page]', :action => 'handle_list',
295   :requirements => { :page => /^\d+$/ }
296
297 plugin.map 'reaction show *trigger', :action => 'handle_show'
298
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'