]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/reaction.rb
reaction plugin: one-liner reply/reaction to in-channel messages/actions. Initial...
[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   attr :reactions
68
69   def initialize
70     super
71     if @registry.has_key?(:reactions)
72       @reactions = @registry[:reactions]
73       raise unless @reactions
74     else
75       @reactions = []
76     end
77
78     @subs = {
79       :bold => Bold,
80       :underline => Underline,
81       :reverse => Reverse,
82       :italic => Italic,
83       :normal => NormalText,
84       :color => Color,
85       :colour => Color,
86       :bot => @bot.myself,
87     }.merge ColorCode
88   end
89
90   def save
91     @registry[:reactions] = @reactions
92   end
93
94   def help(plugin, topic="")
95     case (topic.to_sym rescue nil)
96     when :add
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)"
110     when :list
111       "lists all the programmed reactions"
112     else
113       "reaction topics: add, remove, delete, rm, del, triggers, replies, list"
114     end
115   end
116
117   def listen(m)
118     return unless PrivMessage === m
119     debug "testing #{m} for reactions"
120     return if @reactions.empty?
121     wanted = @reactions.find { |react|
122       react === m
123     }
124     return unless wanted
125     match = wanted === m
126     matched = match[0]
127     stuff = match.post_match.strip
128     target, what = stuff.split(/\s+/, 2)
129     extra = {
130       :who => m.sourcenick,
131       :match => matched,
132       :target => target,
133       :what => what,
134       :stuff => stuff
135     }
136     subs = @subs.dup.merge extra
137     args = [wanted.reply.first]
138     args << wanted.reply.last % extra
139     m.__send__(*args)
140   end
141
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}"
146   end
147
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
153     }
154     if found
155       @reactions.delete(found)
156       m.reply "removed reaction to #{found.trigger.last} with #{found.reply.last}"
157     else
158       m.reply "no reaction programmed for #{trigger}"
159     end
160   end
161
162   def handle_list(m, params)
163     if @reactions.empty?
164       m.reply "no reactions programmed"
165       return
166     end
167
168     per_page = 30
169     pages = @reactions.length / per_page + 1
170     page = params[:page].to_i.clip(1, pages)
171
172     str = @reactions[(page-1)*per_page, per_page].join(", ")
173
174     m.reply "Programmed reactions (page #{page}/#{pages}): #{str}"
175   end
176
177 end
178
179 plugin = ReactionPlugin.new
180
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'
186
187 plugin.map 'reaction list [:page]', :action => 'handle_list',
188   :requirements => { :page => /^\d+$/ }
189
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'