]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/reaction.rb
reaction plugin: only react to unreplied messages
[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, :reply
16   attr_reader :raw_trigger, :raw_reply
17   attr_accessor :author, :date, :channel
18
19   def trigger=(expr)
20     @raw_trigger = expr.dup
21     act = false
22     rex = expr.dup
23     if rex.sub!(/^act:/,'')
24       act = true
25     end
26     @trigger = [act]
27     if rex.sub!(%r@^([/!])(.*)\1$@, '\2')
28       debug rex
29       @trigger << Regexp.new(rex)
30     else
31       @trigger << Regexp.new(/\b#{Regexp.escape(rex)}\b/u)
32     end
33   end
34
35   def reply=(expr)
36     @raw_reply = expr.dup
37     act = false
38     rex = expr.dup
39     if rex.sub!(/^act:/,'')
40       act = true
41     end
42     @reply = [act ? :act : :reply]
43     @reply << rex
44   end
45
46   def ===(message)
47     return nil if @trigger.first and not message.action
48     return message.message.match(@trigger.last)
49   end
50
51   def initialize(trig, msg , auth, dt, chan)
52     self.trigger=trig
53     self.reply=msg
54     self.author=auth.to_s
55     self.date=dt
56     self.channel=chan.to_s
57   end
58
59   def to_s
60     "trigger #{raw_trigger} (#{author}, #{channel}, #{date})"
61   end
62
63 end
64
65 class ReactionPlugin < Plugin
66
67   ADD_SYNTAX = 'react to *trigger with *reply'
68
69   def add_syntax
70     return ADD_SYNTAX
71   end
72
73   attr :reactions
74
75   def initialize
76     super
77     if @registry.has_key?(:reactions)
78       @reactions = @registry[:reactions]
79       raise unless @reactions
80     else
81       @reactions = []
82     end
83
84     @subs = {
85       :bold => Bold,
86       :underline => Underline,
87       :reverse => Reverse,
88       :italic => Italic,
89       :normal => NormalText,
90       :color => Color,
91       :colour => Color,
92       :bot => @bot.myself,
93     }.merge ColorCode
94   end
95
96   def save
97     @registry[:reactions] = @reactions
98   end
99
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"
103     end
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)"
117     when :list
118       "reaction list [n]: lists the n-the page of programmed reactions (30 reactions are listed per page)"
119     else
120       "reaction topics: add, remove, delete, rm, del, triggers, replies, list"
121     end
122   end
123
124   def unreplied(m)
125     return unless PrivMessage === m
126     debug "testing #{m} for reactions"
127     return if @reactions.empty?
128     wanted = @reactions.find { |react|
129       react === m
130     }
131     return unless wanted
132     match = wanted === m
133     matched = match[0]
134     stuff = match.post_match.strip
135     target, what = stuff.split(/\s+/, 2)
136     extra = {
137       :who => m.sourcenick,
138       :match => matched,
139       :target => target,
140       :what => what,
141       :stuff => stuff
142     }
143     subs = @subs.dup.merge extra
144     args = [wanted.reply.first]
145     args << wanted.reply.last % extra
146     m.__send__(*args)
147   end
148
149   def find_reaction(trigger)
150     @reactions.find { |react|
151       react.raw_trigger == trigger
152     }
153   end
154
155   def handle_add(m, params)
156     trigger = params[:trigger].to_s
157     reply = params[:reply].to_s
158     if find_reaction(trigger)
159       m.reply "there's already a reaction to #{trigger}"
160       return
161     end
162
163     reaction = Reaction.new(trigger, reply, m.sourcenick, Time.now, m.channel)
164     @reactions << reaction
165     m.reply "added reaction to #{reaction.trigger.last} with #{reaction.reply.last}"
166   end
167
168   def handle_rm(m, params)
169     trigger = params[:trigger].to_s
170     debug trigger.inspect
171     found = find_reaction(trigger)
172     if found
173       @reactions.delete(found)
174       m.reply "removed reaction to #{found.trigger.last} with #{found.reply.last}"
175     else
176       m.reply "no reaction programmed for #{trigger}"
177     end
178   end
179
180   def handle_list(m, params)
181     if @reactions.empty?
182       m.reply "no reactions programmed"
183       return
184     end
185
186     per_page = 30
187     pages = @reactions.length / per_page + 1
188     page = params[:page].to_i.clip(1, pages)
189
190     str = @reactions[(page-1)*per_page, per_page].join(", ")
191
192     m.reply "Programmed reactions (page #{page}/#{pages}): #{str}"
193   end
194
195 end
196
197 plugin = ReactionPlugin.new
198
199 plugin.map plugin.add_syntax, :action => 'handle_add',
200   :requirements => { :trigger => /^(?:act:)?!.*?!/ }
201 plugin.map plugin.add_syntax, :action => 'handle_add',
202   :requirements => { :trigger => /^(?:act:)?\/.*?\// }
203 plugin.map plugin.add_syntax, :action => 'handle_add',
204   :requirements => { :trigger => /^(?:act:)?".*?"/ }
205 plugin.map plugin.add_syntax, :action => 'handle_add',
206   :requirements => { :trigger => /^(?:act:)?'.*?'/ }
207 plugin.map plugin.add_syntax.sub('*', ':'), :action => 'handle_add'
208
209 plugin.map 'reaction list [:page]', :action => 'handle_list',
210   :requirements => { :page => /^\d+$/ }
211
212 plugin.map 'reaction del[ete] *trigger', :action => 'handle_rm'
213 plugin.map 'reaction delete *trigger', :action => 'handle_rm'
214 plugin.map 'reaction remove *trigger', :action => 'handle_rm'
215 plugin.map 'reaction rm *trigger', :action => 'handle_rm'