| 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 |
@addtopic = /(?:topic(?:append|add)|(?:append|add)topic)/ |
|---|
| 8 |
@prependtopic = /(?:topicprepend|prependtopic)/ |
|---|
| 9 |
@addtopicat = /(?:(?:addtopic|topicadd)at)/ |
|---|
| 10 |
@deltopic = /(?:deltopic|topicdel)/ |
|---|
| 11 |
@separator = "|" # default separator |
|---|
| 12 |
end |
|---|
| 13 |
|
|---|
| 14 |
def help(plugin, topic="") |
|---|
| 15 |
case plugin |
|---|
| 16 |
when "topic" |
|---|
| 17 |
case topic |
|---|
| 18 |
when "add" |
|---|
| 19 |
return "#{topic} <text> => add <text> at the end the topic" |
|---|
| 20 |
when "prepend" |
|---|
| 21 |
return "#{topic} <text> => add <text> at the beginning of the topic" |
|---|
| 22 |
when "addat" |
|---|
| 23 |
return "#{topic} <num> <text> => add <text> at position <num> of the topic" |
|---|
| 24 |
when "del" |
|---|
| 25 |
return "#{topic} <num> => remove section <num> from the topic" |
|---|
| 26 |
when "set" |
|---|
| 27 |
return "settopic <text> => sets the topic to <text>" |
|---|
| 28 |
when "separator" |
|---|
| 29 |
return "separator [<text>] => get or set the topic section separator" |
|---|
| 30 |
when "learntopic" |
|---|
| 31 |
return "learntopic => remembers the topic for later" |
|---|
| 32 |
when "resumetopic" |
|---|
| 33 |
return "resumetopic => resets the topic to the latest remembered one" |
|---|
| 34 |
when "settopic" |
|---|
| 35 |
return "settopic <text> => sets the topic to <text>" |
|---|
| 36 |
else |
|---|
| 37 |
return "topic commands: addtopic, prependtopic, addtopicat, deltopic, learntopic, resumetopic, settopic" |
|---|
| 38 |
end |
|---|
| 39 |
when "learntopic" |
|---|
| 40 |
return "learntopic => remembers the topic for later" |
|---|
| 41 |
when "resumetopic" |
|---|
| 42 |
return "resumetopic => resets the topic to the latest remembered one" |
|---|
| 43 |
when "settopic" |
|---|
| 44 |
return "settopic <text> => sets the topic to <text>" |
|---|
| 45 |
end |
|---|
| 46 |
end |
|---|
| 47 |
|
|---|
| 48 |
def handletopic(m, param) |
|---|
| 49 |
return unless m.kind_of?(PrivMessage) && m.public? |
|---|
| 50 |
cmd = param[:command] |
|---|
| 51 |
txt = param[:text].join(" ") |
|---|
| 52 |
case cmd |
|---|
| 53 |
when /a(dd|ppend)/ |
|---|
| 54 |
topicappend(m, txt) |
|---|
| 55 |
when 'prepend' |
|---|
| 56 |
topicprepend(m, txt) |
|---|
| 57 |
when 'addat' |
|---|
| 58 |
if txt =~ /\s*(-?\d+)\s+(.*)\s*/ |
|---|
| 59 |
num = $1.to_i - 1 |
|---|
| 60 |
num += 1 if num < 0 |
|---|
| 61 |
txt = $2 |
|---|
| 62 |
topicaddat(m, num, txt) |
|---|
| 63 |
end |
|---|
| 64 |
when /del(ete)?/ |
|---|
| 65 |
if txt =~ /\s*(-?\d+)\s*/ |
|---|
| 66 |
num=$1.to_i - 1 |
|---|
| 67 |
num += 1 if num < 0 |
|---|
| 68 |
topicdel(m, num) |
|---|
| 69 |
end |
|---|
| 70 |
when /set/ |
|---|
| 71 |
do_topicset(m, txt) |
|---|
| 72 |
when /sep(arator)?/ |
|---|
| 73 |
do_topicsep(m, txt) |
|---|
| 74 |
else |
|---|
| 75 |
m.reply 'unknown command' |
|---|
| 76 |
end |
|---|
| 77 |
end |
|---|
| 78 |
|
|---|
| 79 |
def listen(m) |
|---|
| 80 |
return unless m.kind_of?(PrivMessage) && m.public? && m.address? && !m.action? |
|---|
| 81 |
command = m.message.dup |
|---|
| 82 |
if m.address? || command.gsub!(/^!/, "") |
|---|
| 83 |
case command |
|---|
| 84 |
when /^#{@addtopic}\s+(.*)\s*/ |
|---|
| 85 |
txt=$1 |
|---|
| 86 |
if @bot.auth.allow?("topic", m.source, m.replyto) |
|---|
| 87 |
topicappend(m, txt) |
|---|
| 88 |
end |
|---|
| 89 |
when /^#{@prependtopic}\s+(.*)\s*/ |
|---|
| 90 |
txt=$1 |
|---|
| 91 |
if @bot.auth.allow?("topic", m.source, m.replyto) |
|---|
| 92 |
topicprepend(m, txt) |
|---|
| 93 |
end |
|---|
| 94 |
when /^#{@addtopicat}\s+(-?\d+)\s+(.*)\s*/ |
|---|
| 95 |
num=$1.to_i - 1 |
|---|
| 96 |
num += 1 if num < 0 |
|---|
| 97 |
txt=$2 |
|---|
| 98 |
if @bot.auth.allow?("topic", m.source, m.replyto) |
|---|
| 99 |
topicaddat(m, num, txt) |
|---|
| 100 |
end |
|---|
| 101 |
when /^#{@deltopic}\s+(-?\d+)\s*/ |
|---|
| 102 |
num=$1.to_i - 1 |
|---|
| 103 |
num += 1 if num < 0 |
|---|
| 104 |
if @bot.auth.allow?("topic", m.source, m.replyto) |
|---|
| 105 |
topicdel(m, num) |
|---|
| 106 |
end |
|---|
| 107 |
end |
|---|
| 108 |
end |
|---|
| 109 |
end |
|---|
| 110 |
|
|---|
| 111 |
def topicsep(m, param) |
|---|
| 112 |
do_topicsep(m, param[:text].join(" ")) |
|---|
| 113 |
end |
|---|
| 114 |
|
|---|
| 115 |
def do_topicsep(m, txt) |
|---|
| 116 |
ch = m.channel.downcase |
|---|
| 117 |
if txt |
|---|
| 118 |
sep = txt.strip |
|---|
| 119 |
if sep != "" |
|---|
| 120 |
setsep(ch, sep) |
|---|
| 121 |
end |
|---|
| 122 |
end |
|---|
| 123 |
m.reply "Topic separator set to #{getsep(ch)}" |
|---|
| 124 |
end |
|---|
| 125 |
|
|---|
| 126 |
def setsep(ch, sep) |
|---|
| 127 |
if @registry.has_key?(ch) |
|---|
| 128 |
data = @registry[ch] |
|---|
| 129 |
else |
|---|
| 130 |
data = Hash.new |
|---|
| 131 |
end |
|---|
| 132 |
data[:separator] = sep |
|---|
| 133 |
@registry[ch] = data |
|---|
| 134 |
end |
|---|
| 135 |
|
|---|
| 136 |
def getsep(ch) |
|---|
| 137 |
if @registry.has_key?(ch) |
|---|
| 138 |
if @registry[ch].has_key?(:separator) |
|---|
| 139 |
return @registry[ch][:separator] |
|---|
| 140 |
end |
|---|
| 141 |
end |
|---|
| 142 |
return @separator |
|---|
| 143 |
end |
|---|
| 144 |
|
|---|
| 145 |
def topicaddat(m, num, txt) |
|---|
| 146 |
channel = m.channel.downcase |
|---|
| 147 |
sep = getsep(channel) |
|---|
| 148 |
topic = @bot.channels[m.channel].topic.to_s |
|---|
| 149 |
topicarray = topic.split(/\s+#{sep}\s*/) |
|---|
| 150 |
topicarray.insert(num, txt) |
|---|
| 151 |
newtopic = topicarray.join(" #{sep} ") |
|---|
| 152 |
@bot.topic channel, newtopic |
|---|
| 153 |
end |
|---|
| 154 |
|
|---|
| 155 |
def topicappend(m, txt) |
|---|
| 156 |
topicaddat(m, -1, txt) |
|---|
| 157 |
end |
|---|
| 158 |
|
|---|
| 159 |
def topicprepend(m, txt) |
|---|
| 160 |
topicaddat(m, 0, txt) |
|---|
| 161 |
end |
|---|
| 162 |
|
|---|
| 163 |
def topicdel(m, num) |
|---|
| 164 |
channel = m.channel.downcase |
|---|
| 165 |
sep = getsep(channel) |
|---|
| 166 |
topic = @bot.channels[m.channel].topic.to_s |
|---|
| 167 |
topicarray = topic.split(/\s+#{sep}\s*/) |
|---|
| 168 |
topicarray.delete_at(num) |
|---|
| 169 |
newtopic = topicarray.join(" #{sep} ") |
|---|
| 170 |
@bot.topic channel, newtopic |
|---|
| 171 |
end |
|---|
| 172 |
|
|---|
| 173 |
def learntopic(m, param) |
|---|
| 174 |
return if !@bot.auth.allow?("learntopic", m.source, m.replyto) |
|---|
| 175 |
channel = m.channel.downcase |
|---|
| 176 |
topic = @bot.channels[m.channel].topic.to_s |
|---|
| 177 |
if @registry.has_key?(channel) |
|---|
| 178 |
data = @registry[channel] |
|---|
| 179 |
else |
|---|
| 180 |
data = Hash.new |
|---|
| 181 |
end |
|---|
| 182 |
data[:topic] = topic |
|---|
| 183 |
@registry[channel] = data |
|---|
| 184 |
m.okay |
|---|
| 185 |
end |
|---|
| 186 |
|
|---|
| 187 |
def resumetopic(m, param) |
|---|
| 188 |
return if !@bot.auth.allow?("resumetopic", m.source, m.replyto) |
|---|
| 189 |
channel = m.channel.downcase |
|---|
| 190 |
if @registry.has_key?(channel) && @registry[channel].has_key?(:topic) |
|---|
| 191 |
topic = @registry[channel][:topic] |
|---|
| 192 |
@bot.topic channel, topic |
|---|
| 193 |
else |
|---|
| 194 |
@bot.say channel, "I don't remember any topic for this channel" |
|---|
| 195 |
end |
|---|
| 196 |
end |
|---|
| 197 |
|
|---|
| 198 |
def topicset(m, param) |
|---|
| 199 |
do_topicset(m, param[:text].join(" ")) |
|---|
| 200 |
end |
|---|
| 201 |
|
|---|
| 202 |
def do_topicset(m, text) |
|---|
| 203 |
return if !@bot.auth.allow?("topic", m.source, m.replyto) |
|---|
| 204 |
channel = m.channel.downcase |
|---|
| 205 |
@bot.topic channel, text |
|---|
| 206 |
end |
|---|
| 207 |
|
|---|
| 208 |
end |
|---|
| 209 |
plugin = TopicPlugin.new |
|---|
| 210 |
plugin.map 'topic :command *text', :action => 'handletopic' |
|---|
| 211 |
plugin.map 'settopic *text', :action => 'topicset' |
|---|
| 212 |
plugin.map 'topicset *text', :action => 'topicset' |
|---|
| 213 |
plugin.map 'topicsep *text', :action => 'topicsep' |
|---|
| 214 |
plugin.map 'resumetopic' |
|---|
| 215 |
plugin.map 'learntopic' |
|---|
| 216 |
|
|---|