]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/topic.rb
applied patch to topic.rb by [dmp] for #100
[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
4 class TopicPlugin < Plugin
5   def initialize
6     super
7     @separator = "|" # default separator
8   end
9
10   def help(plugin, topic="")
11     case plugin
12     when "topic"
13       case topic
14       when "add"
15         return "topic add <text> => add <text> at the end the topic"
16       when "prepend"
17         return "topic prepend <text> => add <text> at the beginning of the topic"
18       when "addat"
19         return "topic addat <num> <text> => add <text> at position <num> of the topic"
20       when "del"
21         return "topic del <num> => remove section <num> from the topic"
22       when "separator"
23         return "topic sep(arator) [<text>] => get or set the topic section separator"
24       when "learn"
25         return "topic learn => remembers the topic for later"
26       when "replace"
27         return "topic replace <num> <text> => Replaces section <num> with <text>"
28       when "restore"
29         return "topic restore => resets the topic to the latest remembered one"
30       when "set"
31         return "topic set <text> => sets the topic to <text>"
32       else
33         return "topic add(at)|prepend|del|sep(arator)|learn|replace|restore|set: " + \
34                "manipulate the topic of the current channel; use topic <#channel> <command> " + \
35                "for private addressing"
36       end
37     end
38   end
39
40   def handletopic(m, param)
41     return unless m.kind_of?(PrivMessage)
42     if m.public?
43       ch = m.channel.downcase
44     else
45       ch = param[:channel].downcase
46     end
47     cmd = param[:command]
48     txt = param[:text].join(" ")
49     case cmd
50     when /^a(dd|ppend)$/
51       topicappend(m, ch, txt)
52     when 'prepend'
53       topicprepend(m, ch, txt)
54     when 'addat'
55       if txt =~ /\s*(-?\d+)\s+(.*)\s*/
56         num = $1.to_i - 1
57         num += 1 if num < 0
58         txt = $2
59         topicaddat(m, ch, num, txt)
60       end
61     when /^del(ete)?$/
62       if txt =~ /\s*(-?\d+)\s*/
63         num=$1.to_i - 1
64         num += 1 if num < 0
65         topicdel(m, ch, num)
66       end
67     when 'set'
68       topicset(m, ch, txt)
69     when /^sep(arator)?$/
70       topicsep(m, ch, txt)
71     when 'learn'
72       learntopic(m, ch)
73     when 'replace'
74       if txt =~ /\s*(-?\d+)\s+(.*)\s*/
75         num = $1.to_i - 1
76         num += 1 if num < 0
77         txt = $2
78         replacetopic(m, ch, num, txt)
79       end
80     when 'restore'
81       restoretopic(m, ch)
82     else
83       m.reply 'unknown command'
84     end
85   end
86
87   def topicsep(m, ch, txt)
88     if txt
89       sep = txt.strip
90       if sep != ""
91         setsep(ch, sep)
92       end
93     end
94     m.reply "Topic separator set to #{getsep(ch)}"
95   end
96
97   def setsep(ch, sep)
98     if @registry.has_key?(ch)
99       data = @registry[ch]
100     else
101       data = Hash.new
102     end
103     data[:separator] = sep
104     @registry[ch] = data
105   end
106
107   def getsep(ch)
108     if @registry.has_key?(ch)
109       if @registry[ch].has_key?(:separator)
110         return @registry[ch][:separator]
111       end
112     end
113     return @separator
114   end
115
116   def topicaddat(m, channel, num, txt)
117     sep = getsep(channel)
118     topic = @bot.channels[channel].topic.to_s
119     topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
120     topicarray.insert(num, txt)
121     newtopic = topicarray.join(" #{sep} ")
122     @bot.topic channel, newtopic
123   end
124
125   def topicappend(m, ch, txt)
126     topicaddat(m, ch, -1, txt)
127   end
128
129   def topicprepend(m, ch, txt)
130     topicaddat(m, ch, 0, txt)
131   end
132
133   def topicdel(m, channel, num)
134     sep = getsep(channel)
135     topic = @bot.channels[channel].topic.to_s
136     topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
137     topicarray.delete_at(num)
138     newtopic = topicarray.join(" #{sep} ")
139     @bot.topic channel, newtopic
140   end
141
142   def learntopic(m, channel)
143     return if !@bot.auth.allow?("learntopic", m.source, m.replyto)
144     topic = @bot.channels[channel].topic.to_s
145     if @registry.has_key?(channel)
146       data = @registry[channel]
147     else
148       data = Hash.new
149     end
150     data[:topic] = topic
151     @registry[channel] = data
152     m.okay
153   end
154
155   def replacetopic(m, channel, num, txt)
156     return if !@bot.auth.allow?("topic", m.source, m.replyto)
157     sep = getsep(channel)
158     topic = @bot.channels[channel].topic.to_s
159     topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
160     topicarray[num] = txt
161     newtopic = topicarray.join(" #{sep} ")
162     @bot.topic channel, newtopic
163   end
164   
165   def restoretopic(m, channel)
166     return if !@bot.auth.allow?("restoretopic", m.source, m.replyto)
167     if @registry.has_key?(channel) && @registry[channel].has_key?(:topic)
168       topic = @registry[channel][:topic]
169       @bot.topic channel, topic
170     else
171       m.reply "I don't remember any topic for this channel"
172     end
173   end
174
175   def topicset(m, channel, text)
176     return if !@bot.auth.allow?("topic", m.source, m.replyto)
177     @bot.topic channel, text
178   end
179
180 end
181 plugin = TopicPlugin.new
182 plugin.map 'topic :command *text', :action => 'handletopic', :public => true, :private => false
183 plugin.map 'topic :channel :command *text', :action => 'handletopic', :public => false, :private => true
184