summaryrefslogtreecommitdiffstats
path: root/modules/Topic.rb
diff options
context:
space:
mode:
Diffstat (limited to 'modules/Topic.rb')
-rw-r--r--modules/Topic.rb76
1 files changed, 76 insertions, 0 deletions
diff --git a/modules/Topic.rb b/modules/Topic.rb
new file mode 100644
index 0000000..14bd9c4
--- /dev/null
+++ b/modules/Topic.rb
@@ -0,0 +1,76 @@
+# -*- 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)
+ elsif /\A!topic\s+d(?:el(?:ete)?)?\s+(\d+)\Z/ =~ message.body
+ del_topic(muc, message, $~[1].to_i)
+ end
+ end
+
+ def commands
+ {
+ '!topic [s[how]]' => 'show topics (with IDs)',
+ '!topic a[dd] <topic>' => 'add a topic to the topic line',
+ '!topic d[el[ete]] <ID>' => 'remove topic <ID>'
+ }
+ 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)
+ topic.each_with_index.map { |t, i| "[#{i}] #{t}" }.join("\n")
+ end
+
+ def show_topic(muc, message)
+ topic = current_topic muc
+ if topic.empty?
+ muc.send(Jabber::Message.new(muc.room, "No topic set."))
+ return
+ end
+
+ muc.send(Jabber::Message.new(muc.room, "Current topics:\n" + topic_list(topic)))
+ end
+
+ def add_topic(muc, message, text)
+ topic = current_topic muc
+ topic.unshift text
+ set_topic(muc, topic)
+
+ muc.send(Jabber::Message.new(muc.room, "New topics:\n" + topic_list(topic)))
+ end
+
+ def del_topic(muc, message, id)
+ topic = current_topic muc
+ begin
+ topic.delete_at id
+ rescue
+ end
+ set_topic(muc, topic)
+
+ muc.send(Jabber::Message.new(muc.room, "New topics:\n" + topic_list(topic)))
+ end
+ end
+ end
+end