Adjust strings.

This commit is contained in:
Matthias Schiffer 2013-01-02 05:06:04 +01:00
parent 58649c9843
commit 4b3975fc0a
6 changed files with 47 additions and 24 deletions

17
bot.rb
View file

@ -11,26 +11,31 @@ module Lain
class Bot class Bot
def initialize def initialize
$stderr.puts "Lain #{Version} starting..." $stderr.puts "Lain #{Version}"
@modules = {} @modules = {}
$stderr.puts 'Loading modules...' $stderr.print 'Loading modules...'
Config::Modules.each { |mod, cfg| Config::Modules.each { |mod, cfg|
$stderr.print " #{mod}"
require_relative "modules/#{mod}" require_relative "modules/#{mod}"
@modules[mod] = Modules.const_get(mod).new(self, cfg) @modules[mod] = Modules.const_get(mod).new(self, cfg)
} }
$stderr.puts '.'
@commands = @modules.values.reduce({}) { |c, mod| c.merge mod.commands }.to_a.sort @commands = @modules.values.reduce({}) { |c, mod| c.merge mod.commands }.to_a.sort
$stderr.puts 'Connecting...' $stderr.print 'Connecting... '
@cl = Jabber::Client.new(Jabber::JID.new(Config::JID)) @cl = Jabber::Client.new(Jabber::JID.new(Config::JID))
@cl.connect @cl.connect
@cl.auth(Config::Password) @cl.auth(Config::Password)
@cl.send(Jabber::Presence.new) @cl.send(Jabber::Presence.new)
$stderr.puts 'connection established.'
@mucs = {} @mucs = {}
Config::Rooms.each { |r| Config::Rooms.each { |r|
@ -48,7 +53,7 @@ module Lain
end end
} }
$stderr.puts "Joining room `#{r}'..." $stderr.print "Trying to access room `#{r}'... "
muc.join(r) muc.join(r)
@ -56,9 +61,11 @@ module Lain
muc.configure muc.configure
rescue rescue
end end
$stderr.puts "access granted."
} }
$stderr.puts 'Startup finished.' $stderr.puts 'Initialization finished.'
end end
def commands def commands

View file

@ -9,7 +9,12 @@ module Lain
def on_message(muc, message) def on_message(muc, message)
return unless /\A!credits\b/ =~ message.body return unless /\A!credits\b/ =~ message.body
muc.send(Jabber::Message.new(muc.room, "\nLain (レイン) #{Version}\n\n -- coded by NeoRaider")) muc.say <<END
I am Lain () #{Version}.
-- coded by NeoRaider
END
end end
def commands def commands

View file

@ -8,7 +8,7 @@ module Lain
def on_message(muc, message) def on_message(muc, message)
return unless /\A!ddate\b/ =~ message.body return unless /\A!ddate\b/ =~ message.body
muc.send(Jabber::Message.new(muc.room, IO.popen("ddate").read.chomp)) muc.say("DISCORDIAN DATE " + IO.popen("ddate").read.chomp)
end end
def commands def commands

View file

@ -8,7 +8,7 @@ module Lain
def on_message(muc, message) def on_message(muc, message)
return unless /\A!fortune\b/ =~ message.body return unless /\A!fortune\b/ =~ message.body
muc.send(Jabber::Message.new(muc.room, "\n" + IO.popen(@config['command']).read.chomp)) muc.say("\nBEGIN FORTUNE COOKIE\n" + IO.popen(@config['command']).read.chomp + "\nEND FORTUNE COOKIE")
end end
def commands def commands

View file

@ -8,7 +8,7 @@ module Lain
def on_message(muc, message) def on_message(muc, message)
return unless /\A!help\b/ =~ message.body return unless /\A!help\b/ =~ message.body
muc.send(Jabber::Message.new(muc.room, "Commands:" + @lain.commands.reduce('') { |s, cmd| "#{s}\n#{cmd[0]}: #{cmd[1]}" })) muc.say("\nBEGIN COMMAND LIST" + @lain.commands.reduce('') { |s, cmd| s + "\n#{cmd[0]}: #{cmd[1]}" } + "\nEND COMMAND LIST")
end end
def commands def commands

View file

@ -13,16 +13,16 @@ module Lain
show_topic(muc, message) show_topic(muc, message)
elsif /\A!topic\s+a(?:dd)?\s+(.+)\Z/ =~ message.body elsif /\A!topic\s+a(?:dd)?\s+(.+)\Z/ =~ message.body
add_topic(muc, message, $~[1].strip) add_topic(muc, message, $~[1].strip)
elsif /\A!topic\s+d(?:el(?:ete)?)?\s+(\d+)\Z/ =~ message.body elsif /\A!topic\s+d(?:el(?:ete)?)?\s+([[:xdigit:]]+)\Z/ =~ message.body
del_topic(muc, message, $~[1].to_i) del_topic(muc, message, $~[1].to_i(16))
end end
end end
def commands def commands
{ {
'!topic [s[how]]' => 'show topics (with IDs)', '!topic [s[how]]' => 'show topics (with indices)',
'!topic a[dd] <topic>' => 'add a topic to the topic line', '!topic a[dd] <topic>' => 'add a topic',
'!topic d[el[ete]] <ID>' => 'remove topic <ID>' '!topic d[el[ete]] <index>' => 'remove topic <index>'
} }
end end
@ -40,36 +40,47 @@ module Lain
end end
def topic_list(topic) def topic_list(topic)
topic.each_with_index.map { |t, i| "[#{i}] #{t}" }.join("\n") topic.each_with_index.map { |t, i| "[#{'%02x' % i}] #{t}" }.join("\n")
end end
def show_topic(muc, message) def show_topic(muc, message)
topic = current_topic muc topic = current_topic muc
if topic.empty? if topic.empty?
muc.send(Jabber::Message.new(muc.room, "No topic set.")) muc.say "\nTOPIC EMPTY"
return return
end end
muc.send(Jabber::Message.new(muc.room, "Current topics:\n" + topic_list(topic))) muc.say("\nBEGIN TOPIC LIST\n" + topic_list(topic) + "\nEND TOPIC LIST")
end end
def add_topic(muc, message, text) def add_topic(muc, message, text)
topic = current_topic muc topic = current_topic muc
if text.empty?
pre = "\nERROR: No topic given."
else
topic.unshift text topic.unshift text
set_topic(muc, topic) set_topic(muc, topic)
pre = "\nSUCCESS"
muc.send(Jabber::Message.new(muc.room, "New topics:\n" + topic_list(topic)))
end end
def del_topic(muc, message, id) 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 topic = current_topic muc
begin begin
topic.delete_at id deleted = topic.delete_at index
rescue rescue
deleted = nil
end end
if deleted.nil?
pre = "\nERROR: Invalid index [#{'%02x' % index}]."
else
set_topic(muc, topic) set_topic(muc, topic)
pre = "\nSUCCESS"
end
muc.send(Jabber::Message.new(muc.room, "New topics:\n" + topic_list(topic))) muc.say(pre + "\n\nBEGIN TOPIC LIST\n" + topic_list(topic) + "\nEND TOPIC LIST")
end end
end end
end end