]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/reaction.rb
reaction plugin: update help
[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 :add
159       help(:react)
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)"
176     when :list
177       "reaction list [n]: lists the n-the page of programmed reactions (30 reactions are listed per page)"
178     when :show
179       "reaction show <trigger>: list the programmed replies to trigger <trigger>"
180     else
181       "reaction topics: add, remove, delete, rm, del, triggers, replies, chance, list, show"
182     end
183   end
184
185   def unreplied(m)
186     return unless PrivMessage === m
187     debug "testing #{m} for reactions"
188     return if @reactions.empty?
189     wanted = @reactions.find { |react|
190       react === m
191     }
192     return unless wanted
193     match = wanted === m
194     matched = match[0]
195     stuff = match.post_match.strip
196     target, what = stuff.split(/\s+/, 2)
197     extra = {
198       :who => m.sourcenick,
199       :match => matched,
200       :target => target,
201       :what => what,
202       :stuff => stuff
203     }
204     subs = @subs.dup.merge extra
205     reply = wanted.pick_reply
206     debug "picked #{reply}"
207     return unless reply
208     args = reply.apply(subs)
209     m.__send__(*args)
210   end
211
212   def find_reaction(trigger)
213     @reactions.find { |react|
214       react.raw_trigger == trigger
215     }
216   end
217
218   def handle_add(m, params)
219     trigger = params[:trigger].to_s
220     reply = params[:reply].to_s
221
222     pct = params[:chance] || "1"
223     if pct.sub!(/%$/,'')
224       pct = (pct.to_f/100).clip(0,1)
225     else
226       pct = pct.to_f.clip(0,1)
227     end
228
229     reaction = find_reaction(trigger)
230     if not reaction
231       reaction = Reaction.new(trigger)
232       @reactions << reaction
233       m.reply "Ok, I'll start reacting to #{reaction.raw_trigger}"
234     end
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}%)"
237   end
238
239   def handle_rm(m, params)
240     trigger = params[:trigger].to_s
241     debug trigger.inspect
242     found = find_reaction(trigger)
243     if found
244       @reactions.delete(found)
245       m.reply "I won't react to #{found.raw_trigger} anymore"
246     else
247       m.reply "no reaction programmed for #{trigger}"
248     end
249   end
250
251   def handle_list(m, params)
252     if @reactions.empty?
253       m.reply "no reactions programmed"
254       return
255     end
256
257     per_page = 30
258     pages = @reactions.length / per_page + 1
259     page = params[:page].to_i.clip(1, pages)
260
261     str = @reactions[(page-1)*per_page, per_page].join(", ")
262
263     m.reply "Programmed reactions (page #{page}/#{pages}): #{str}"
264   end
265
266   def handle_show(m, params)
267     if @reactions.empty?
268       m.reply "no reactions programmed"
269       return
270     end
271
272     trigger = params[:trigger].to_s
273
274     found = find_reaction(trigger)
275
276     unless found
277       m.reply "I'm not reacting to #{trigger}"
278       return
279     end
280
281     m.reply found.replies.join(", ")
282   end
283
284 end
285
286 plugin = ReactionPlugin.new
287
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'
297
298 plugin.map 'reaction list [:page]', :action => 'handle_list',
299   :requirements => { :page => /^\d+$/ }
300
301 plugin.map 'reaction show *trigger', :action => 'handle_show'
302
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'