This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
lain/modules/Topic.rb

88 lines
2.2 KiB
Ruby
Raw Permalink Normal View History

2013-01-02 02:16:57 +01:00
# -*- coding: utf-8 -*-
require 'xmpp4r/message'
require_relative '../module_base'
module Lain
module Modules
class Topic < Base
def on_message(muc, message)
return unless /\A!topic\b/ =~ message.body
if /\A!topic\s*(?:\ss(?:how)?.*)?\Z/ =~ message.body
show_topic(muc, message)
elsif /\A!topic\s+a(?:dd)?\s+(.+)\Z/ =~ message.body
add_topic(muc, message, $~[1].strip)
2013-01-02 05:06:04 +01:00
elsif /\A!topic\s+d(?:el(?:ete)?)?\s+([[:xdigit:]]+)\Z/ =~ message.body
del_topic(muc, message, $~[1].to_i(16))
2013-01-02 02:16:57 +01:00
end
end
def commands
{
2013-01-02 05:06:04 +01:00
'!topic [s[how]]' => 'show topics (with indices)',
'!topic a[dd] <topic>' => 'add a topic',
'!topic d[el[ete]] <index>' => 'remove topic <index>'
2013-01-02 02:16:57 +01:00
}
end
private
def current_topic(muc)
topic = muc.subject
return [] if topic.nil?
topic.split('|').map { |s| s.strip }
end
def set_topic(muc, topic)
muc.subject = topic.join(" | ")
end
def topic_list(topic)
2013-01-02 05:06:04 +01:00
topic.each_with_index.map { |t, i| "[#{'%02x' % i}] #{t}" }.join("\n")
2013-01-02 02:16:57 +01:00
end
def show_topic(muc, message)
topic = current_topic muc
if topic.empty?
2013-01-02 05:06:04 +01:00
muc.say "\nTOPIC EMPTY"
2013-01-02 02:16:57 +01:00
return
end
2013-01-02 05:06:04 +01:00
muc.say("\nBEGIN TOPIC LIST\n" + topic_list(topic) + "\nEND TOPIC LIST")
2013-01-02 02:16:57 +01:00
end
def add_topic(muc, message, text)
topic = current_topic muc
2013-01-02 05:06:04 +01:00
if text.empty?
pre = "\nERROR: No topic given."
else
topic.unshift text
set_topic(muc, topic)
pre = "\nSUCCESS"
end
2013-01-02 02:16:57 +01:00
2013-01-02 05:06:04 +01:00
muc.say(pre + "\n\nBEGIN TOPIC LIST\n" + topic_list(topic) + "\nEND TOPIC LIST")
2013-01-02 02:16:57 +01:00
end
2013-01-02 05:06:04 +01:00
def del_topic(muc, message, index)
2013-01-02 02:16:57 +01:00
topic = current_topic muc
begin
2013-01-02 05:06:04 +01:00
deleted = topic.delete_at index
2013-01-02 02:16:57 +01:00
rescue
2013-01-02 05:06:04 +01:00
deleted = nil
end
if deleted.nil?
pre = "\nERROR: Invalid index [#{'%02x' % index}]."
else
set_topic(muc, topic)
pre = "\nSUCCESS"
2013-01-02 02:16:57 +01:00
end
2013-01-02 05:06:04 +01:00
muc.say(pre + "\n\nBEGIN TOPIC LIST\n" + topic_list(topic) + "\nEND TOPIC LIST")
2013-01-02 02:16:57 +01:00
end
end
end
end