]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/topic.rb
giuseppe.bilotta's new topic plugin, as well as a tiny patch to the quotes plugin.
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / topic.rb
1 # Author: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
2 # Add a bunch of topic manipulation features
3 # NOTE: topic separator is defined by the global symbol SEPARATOR
4
5 SEPARATOR=" | "
6
7 class TopicPlugin < Plugin
8   def initialize
9     super
10     @addtopic = /(?:topic(?:append|add)|(?:append|add)topic)/
11     @prependtopic = /(?:topicprepend|prependtopic)/
12     @addtopicat = /(?:(?:addtopic|topicadd)at)/
13     @deltopic = /(?:deltopic|topicdel)/
14   end
15
16   def help(plugin, topic="")
17     case plugin
18     when "topic"
19       case topic
20       when @addtopic
21         return "#{topic} <text> => add <text> at the end the topic"
22       when @prependtopic
23         return "#{topic} <text> => add <text> at the beginning of the topic"
24       when @addtopicat
25         return "#{topic} <num> <text> => add <text> at position <num> of the topic"
26       when @deltopic
27         return "#{topic} <num> => remove section <num> from the topic"
28       when "learntopic"
29         return "learntopic => remembers the topic for later"
30       when "resumetopic"
31         return "resumetopic => resets the topic to the latest remembered one"
32       when "settopic"
33         return "settopic <text> => sets the topic to <text>"
34       else
35         return "topic commands: addtopic, prependtopic, addtopicat, deltopic, learntopic, resumetopic, settopic"
36       end
37     when "learntopic"
38       return "learntopic => remembers the topic for later"
39     when "resumetopic"
40       return "resumetopic => resets the topic to the latest remembered one"
41     when "settopic"
42       return "settopic <text> => sets the topic to <text>"
43     end
44   end
45
46   def listen(m)
47     return unless m.kind_of?(PrivMessage) && m.public?
48     command = m.message.dup
49     debug command
50     if m.address? || command.gsub!(/^!/, "")
51       case command
52       when /^#{@addtopic}\s+(.*)\s*/
53         txt=$1
54         debug txt
55         if @bot.auth.allow?("topic", m.source, m.replyto)
56           topicappend(m, txt)
57         end
58       when /^#{@prependtopic}\s+(.*)\s*/
59         txt=$1
60         debug txt
61         if @bot.auth.allow?("topic", m.source, m.replyto)
62           topicaddat(m, 0, txt)
63         end
64       when /^#{@addtopicat}\s+(-?\d+)\s+(.*)\s*/
65         num=$1.to_i - 1
66         num += 1 if num < 0
67         txt=$2
68         debug txt
69         if @bot.auth.allow?("topic", m.source, m.replyto)
70           topicaddat(m, num, txt)
71         end
72       when /^#{@deltopic}\s+(-?\d+)\s*/
73         num=$1.to_i - 1
74         num += 1 if num < 0
75         debug num
76         if @bot.auth.allow?("topic", m.source, m.replyto)
77           topicdel(m, num)
78         end
79       end
80     end
81   end
82
83   def topicaddat(m, num, txt)
84     channel = m.channel.downcase
85     topic = @bot.channels[m.channel].topic.to_s
86     topicarray = topic.split(SEPARATOR)
87     topicarray.insert(num, txt)
88     newtopic = topicarray.join(SEPARATOR)
89     @bot.topic channel, newtopic
90   end
91
92   def topicappend(m, txt)
93     channel = m.channel.downcase
94     topic = @bot.channels[m.channel].topic.to_s
95     topicarray = topic.split(SEPARATOR)
96     topicarray << txt
97     newtopic = topicarray.join(SEPARATOR)
98     @bot.topic channel, newtopic
99   end
100
101   def topicdel(m, num)
102     channel = m.channel.downcase
103     topic = @bot.channels[m.channel].topic.to_s
104     topicarray = topic.split(SEPARATOR)
105     topicarray.delete_at(num)
106     newtopic = topicarray.join(SEPARATOR)
107     @bot.topic channel, newtopic
108   end
109
110   def learntopic(m, param)
111     return if !@bot.auth.allow?("learntopic", m.source, m.replyto)
112     channel = m.channel.downcase
113     debug channel
114     topic = @bot.channels[m.channel].topic.to_s
115     @registry[channel] = topic
116     @bot.say channel, "Ok"
117   end
118
119   def resumetopic(m, param)
120     return if !@bot.auth.allow?("resumetopic", m.source, m.replyto)
121     channel = m.channel.downcase
122     debug "Channel: #{channel}"
123     if @registry.has_key?(channel)
124       topic = @registry[channel]
125       debug "Channel: #{channel}, topic: #{topic}"
126       @bot.topic channel, topic
127     else
128       @bot.say channel, "Non ricordo nulla"
129     end
130   end
131
132   def settopic(m, param)
133     return if !@bot.auth.allow?("topic", m.source, m.replyto)
134     channel = m.channel.downcase
135     debug "Channel: #{channel}"
136     @bot.topic channel, param[:text].to_s
137   end
138
139 end
140 plugin = TopicPlugin.new
141 plugin.register 'topic'
142 plugin.map 'settopic *text', :action => 'settopic'
143 plugin.map 'resumetopic'
144 plugin.map 'learntopic'
145