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