1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# -*- 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+([[:xdigit:]]+)\Z/ =~ message.body
del_topic(muc, message, $~[1].to_i(16))
end
end
def commands
{
'!topic [s[how]]' => 'show topics (with indices)',
'!topic a[dd] <topic>' => 'add a topic',
'!topic d[el[ete]] <index>' => 'remove topic <index>'
}
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| "[#{'%02x' % i}] #{t}" }.join("\n")
end
def show_topic(muc, message)
topic = current_topic muc
if topic.empty?
muc.say "\nTOPIC EMPTY"
return
end
muc.say("\nBEGIN TOPIC LIST\n" + topic_list(topic) + "\nEND TOPIC LIST")
end
def add_topic(muc, message, text)
topic = current_topic muc
if text.empty?
pre = "\nERROR: No topic given."
else
topic.unshift text
set_topic(muc, topic)
pre = "\nSUCCESS"
end
muc.say(pre + "\n\nBEGIN TOPIC LIST\n" + topic_list(topic) + "\nEND TOPIC LIST")
end
def del_topic(muc, message, index)
topic = current_topic muc
begin
deleted = topic.delete_at index
rescue
deleted = nil
end
if deleted.nil?
pre = "\nERROR: Invalid index [#{'%02x' % index}]."
else
set_topic(muc, topic)
pre = "\nSUCCESS"
end
muc.say(pre + "\n\nBEGIN TOPIC LIST\n" + topic_list(topic) + "\nEND TOPIC LIST")
end
end
end
end
|