- def listen(m)
- return unless m.kind_of?(PrivMessage) && m.public?
- command = m.message.dup
- debug command
- if m.address? || command.gsub!(/^!/, "")
- case command
- when /^#{@addtopic}\s+(.*)\s*/
- txt=$1
- debug txt
- if @bot.auth.allow?("topic", m.source, m.replyto)
- topicappend(m, txt)
- end
- when /^#{@prependtopic}\s+(.*)\s*/
- txt=$1
- debug txt
- if @bot.auth.allow?("topic", m.source, m.replyto)
- topicaddat(m, 0, txt)
- end
- when /^#{@addtopicat}\s+(-?\d+)\s+(.*)\s*/
- num=$1.to_i - 1
- num += 1 if num < 0
- txt=$2
- debug txt
- if @bot.auth.allow?("topic", m.source, m.replyto)
- topicaddat(m, num, txt)
- end
- when /^#{@deltopic}\s+(-?\d+)\s*/
- num=$1.to_i - 1
- num += 1 if num < 0
- debug num
- if @bot.auth.allow?("topic", m.source, m.replyto)
- topicdel(m, num)
- end
+ def handletopic(m, param)
+ return unless m.kind_of?(PrivMessage)
+ if m.public?
+ ch = m.channel
+ else
+ ch = m.server.get_channel(param[:channel])
+ unless ch
+ m.reply("I am not in channel #{param[:channel]}")
+ return
+ end
+ end
+ cmd = param[:command]
+ txt = param[:text].to_s
+
+ case cmd
+ when /^a(dd|ppend)$/
+ topicappend(m, ch, txt)
+ when 'prepend'
+ topicprepend(m, ch, txt)
+ when 'addat'
+ if txt =~ /\s*(-?\d+)\s+(.*)\s*/
+ num = $1.to_i - 1
+ num += 1 if num < 0
+ txt = $2
+ topicaddat(m, ch, num, txt)
+ end
+ when /^del(ete)?$/
+ if txt =~ /\s*(-?\d+)\s*/
+ num=$1.to_i - 1
+ num += 1 if num < 0
+ topicdel(m, ch, num)
+ end
+ when 'set'
+ topicset(m, ch, txt)
+ when 'clear'
+ topicset(m, ch, '')
+ when /^sep(arator)?$/
+ topicsep(m, ch, txt)
+ when 'learn'
+ learntopic(m, ch)
+ when 'replace'
+ if txt =~ /\s*(-?\d+)\s+(.*)\s*/
+ num = $1.to_i - 1
+ num += 1 if num < 0
+ txt = $2
+ replacetopic(m, ch, num, txt)
+ end
+ when 'restore'
+ restoretopic(m, ch)
+ when 'undo'
+ undotopic(m, ch)
+ else
+ m.reply 'unknown command'
+ end
+ end
+
+ def topicsep(m, ch, txt)
+ return if !@bot.auth.allow?("topic::edit::separator", m.source, m.replyto)
+ if txt
+ sep = txt.strip
+ if sep != ""
+ setsep(ch, sep)
+ end
+ end
+ m.reply "Topic separator set to #{getsep(ch)}"
+ end
+
+ def setsep(ch, sep)
+ raise unless ch.class <= Irc::Channel
+ # TODO multiserver
+ k = ch.downcase
+
+ if @registry.has_key?(k)
+ data = @registry[k]
+ else
+ data = Hash.new
+ end
+
+ oldsep = getsep(ch)
+ topic = ch.topic.text
+ topicarray = topic.split(/\s+#{Regexp.escape(oldsep)}\s*/)
+
+ if sep != oldsep and topicarray.length > 0
+ newtopic = topicarray.join(" #{sep} ")
+ @bot.topic ch, newtopic if newtopic != topic
+ end
+
+ data[:separator] = sep
+ @registry[k] = data
+ end
+
+ def getsep(ch)
+ raise unless ch.class <= Irc::Channel
+ # TODO multiserver
+ k = ch.downcase
+
+ if @registry.has_key?(k)
+ if @registry[k].has_key?(:separator)
+ return @registry[k][:separator]