Ticket #93: topic.rb

File topic.rb, 4.0 kB (added by giuseppe.bilotta, 3 years ago)

Topic manipulation plugin

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