vrobot4/source/server.rb

200 lines
6.6 KiB
Ruby
Raw Normal View History

2017-08-11 19:40:43 -07:00
# Module for vrobot4 server interfaces.
module Vrobot4::Server
2019-02-23 20:46:40 -08:00
require "./modules/base.rb"
2017-08-11 19:40:43 -07:00
# Generic user information. May be extended.
class User
attr_reader :name # @return [String] plaintext name of the user
attr_reader :roles # @return [String] list of user's roles
2017-08-15 13:48:43 -07:00
attr_reader :id # @return [Integer] unique identifier for the user
end
2017-08-11 19:40:43 -07:00
# Generic channel information. May be extended.
class Channel
attr_reader :name # @return [String] plaintext name of the channel
2017-08-15 13:48:43 -07:00
attr_reader :id # @return [Integer] unique identifier for the channel
end
2017-08-11 19:40:43 -07:00
# Generic event information. May not be extended.
class Message
attr_reader :msg # @return [String] plaintext of message
attr_reader :user # @return [User] user that triggered this message
attr_reader :chan # @return [Channel] channel this message was sent to
attr_reader :serv # @return [Server] server this message was sent to
2019-02-24 03:00:41 -08:00
attr_reader :bot # @return [Vrobot4::Robots::Bot] bot this was sent to
2017-08-11 19:40:43 -07:00
# @param info [Hash] A hash containing message info. Keys may be omitted.
# [:msg] Plaintext of message.
# [:user] User that triggered this message.
# [:chan] Channel that this message was sent to.
# [:serv] Server this message was sent to.
2019-02-24 03:00:41 -08:00
# [:bot] Bot this message was sent to.
2017-08-11 19:40:43 -07:00
# [:reply] Method that sends a message to the specified channel.
# [:reply_b] Method that sends a large message to the specified channel.
2017-08-08 21:00:28 -07:00
def initialize(**info)
@msg = info[:msg]
@user = info[:user]
@chan = info[:chan]
@serv = info[:serv]
2019-02-24 03:00:41 -08:00
@bot = info[:bot]
@reply = info[:reply]
@reply_b = info[:reply_b]
end
2017-08-15 13:48:43 -07:00
# Sends a message to the originating channel.
2019-02-23 20:46:40 -08:00
# @param args [Array<String>] the text to send
# @return [void]
def reply *args
2019-02-23 16:18:39 -08:00
@reply.call args.join(?\s)
end
2017-08-15 13:48:43 -07:00
# Sends a large message to the originating channel.
2019-02-23 20:46:40 -08:00
# @param args [Array<String>] the text to send
# @return [void]
2017-08-08 04:33:30 -07:00
def reply_b *args
2019-02-23 16:18:39 -08:00
@reply_b.call args.join(?\s)
end
end
2017-08-11 19:40:43 -07:00
# Generic server interface.
class Server
attr_reader :mprm # @return [Hash] module permissions for this server
2019-02-23 16:18:39 -08:00
attr_reader :id # @return [Integer] unique identifier for the server
2019-02-23 22:43:50 -08:00
attr_reader :bot # @return [Vrobot4::Robots::Bot] the bot we belong to
2017-08-11 19:40:43 -07:00
# @param info [Hash] arbitrary extra information for this server
2019-02-23 22:43:50 -08:00
# @param bot [Vrobot4::Robots::Bot] the bot we belong to
def initialize info, bot
@bot = bot
2017-08-08 21:03:18 -07:00
@info = info
2017-08-09 00:52:34 -07:00
@modules = [Mod_Base.new(nil)]
2019-02-24 03:00:41 -08:00
load_permissions info[:permissions]
end
2017-08-11 19:40:43 -07:00
# Loads and initializes a module into the load list.
# @param mod [Vrobot4::Module::Module]
2019-02-23 20:46:40 -08:00
# @return [void]
def load_mod mod
mt = Vrobot4::Module.get_module_type(mod)
2017-08-11 18:50:43 -07:00
if mt[:server] and mt[:server] != self.class.type or
mt[:servflags] and mt[:servflags] !~ flags then
2019-02-23 22:43:50 -08:00
raise ArgumentError, "Module #{mod} is not valid for this server"
end
2019-02-24 03:00:41 -08:00
@modules << mt[:type].new(@info[mod.to_sym])
end
2017-08-11 19:40:43 -07:00
# Drops a module from the load list.
# @param mod [Vrobot4::Module::Module]
2019-02-23 20:46:40 -08:00
# @return [void]
def drop_mod mod
mt = Vrobot4::Module.get_module_type(mod)
@modules.each_index do |i|
@modules.delete_at i if @modules[i].is_a? mt[:type]
end
end
2017-08-11 19:40:43 -07:00
# Yields for every module loaded in the server.
2019-02-23 20:46:40 -08:00
# @yieldparam mod [Vrobot4::Module::Module] an individual module
# @return [void]
def each_mod
2019-02-23 20:46:40 -08:00
@modules.each do |mod|
yield mod
end
end
2017-08-11 19:40:43 -07:00
# (see Vrobot4::Module::Module#on_message)
2019-02-23 20:46:40 -08:00
# This function passes information to all modules.
2017-08-11 15:16:16 -07:00
def on_message m
@modules.each do |mod|
break if mod.class.usable_in? m and mod.on_message m
end
end
2017-08-11 19:40:43 -07:00
# (see Vrobot4::Module::Module#on_command)
2019-02-23 20:46:40 -08:00
# This function passes information to all modules.
2017-08-11 15:16:16 -07:00
def on_command m, cnam, argv
@modules.each do |mod|
break if mod.class.usable_in? m and mod.on_command m, cnam, argv
end
end
2017-08-11 19:40:43 -07:00
# Connect to the server.
2019-02-23 20:46:40 -08:00
# @abstract
# @return [void]
2017-08-11 19:40:43 -07:00
def connect
raise NotImplementedError, "Server#connect not implemented"
end
# Flags for this server.
2019-02-23 20:46:40 -08:00
# [A] Server has audio capabilities and inherits from
# {Vrobot4::Server::AudioServer}.
# [L] Server cannot support large text dumps
# (code should assume <=4 lines, 240 characters per as maximum)
2019-02-23 20:46:40 -08:00
# @return [String] all available flags
2017-08-08 04:32:45 -07:00
def flags
""
end
2019-02-23 20:46:40 -08:00
# Checks if the message starts with +.+ and splits the command from the
# arguments, then emits an {#on_command} event. Otherwise, it will emit
# an {#on_message} event.
2017-08-13 03:48:00 -07:00
# @param m [Vrobot4::Server::Message] the message to parse and emit
2019-02-23 20:46:40 -08:00
# @return [void]
2017-08-13 03:48:00 -07:00
def handle_text_cmd m
2019-02-23 16:18:39 -08:00
if m.msg.start_with? ?.
arg = m.msg.split(?\s, 2)
on_command m, arg[0][1 .. -1], arg[1] || ""
2017-08-13 03:48:00 -07:00
else
on_message m
end
end
protected
2019-02-23 20:46:40 -08:00
2017-08-11 19:40:43 -07:00
# Implementation defined permission loader.
# Loads information into +@mprm+.
2019-02-23 20:46:40 -08:00
# @abstract
# @param pinf [Hash] arbitrary permissions information
# @return [void]
def load_permissions pinf
2017-08-11 19:40:43 -07:00
raise NotImplementedError, "Server#load_permissions not implemented"
end
end
2017-08-11 19:40:43 -07:00
# Basis for an audio-enabled server interface.
2017-08-08 21:03:18 -07:00
class AudioServer < Server
2017-08-11 19:40:43 -07:00
# (see Vrobot4::Server::Server#flags)
2017-08-08 21:03:18 -07:00
def flags
2019-02-23 16:18:39 -08:00
?A
2017-08-08 21:03:18 -07:00
end
2019-02-23 20:46:40 -08:00
# Quit using voice channels.
# @abstract
# @param m [Vrobot4::Server::Message] message for context
# @return [void]
def voice_quit m
raise NotImplementedError, "AudioServer#voice_quit not implemented"
end
2019-02-23 20:46:40 -08:00
# Plays an audio file by name into the current voice channel.
# @abstract
# @param m [Vrobot4::Server::Message] message for context
# @param fname [String] file name
# @return [void]
def voice_play m, fname
raise NotImplementedError, "AudioServer#voice_play not implemented"
end
2019-02-23 20:46:40 -08:00
# Plays an audio I/O stream into the current voice channel.
# @abstract
# @param m [Vrobot4::Server::Message] message for context
# @param io [IO] io stream
# @return [void]
def voice_play_io m, io
raise NotImplementedError, "AudioServer#voice_play_io not implemented"
end
end
end
## EOF