require 'cinch' # A server implementation for IRC using cinch. class Sv_IRC < Vrobot4::Server::Server # The server type name. def self.type() "IRC" end Vrobot4::Server.add_server_type self attr_reader :bot # The Cinch::Bot instance. # (see Vrobot4::Server::Server#initialize) def initialize info super this = self @bot = Cinch::Bot.new do configure do |cfg| cfg.server = info["server"] cfg.nick = info["nick"] || "vrobot4" cfg.port = info["port"] if info.key? "port" cfg.password = info["pass"] if info.key? "pass" cfg.modes = info["modes"] if info.key? "modes" cfg.channels = info["channels"] if info.key? "channels" cfg.realname = "vrobot4" cfg.user = "vrobot4" cfg.message_split_start = "… " cfg.message_split_end = " …" end on :message do |evt| return unless evt.channel m = Vrobot4::Server::Message.new \ msg: evt.message, user: User.new(evt.user, evt.channel), chan: Channel.new(evt.channel), serv: this, reply: -> (text) {evt.reply text}, reply_b: -> (text) {evt.reply text} this.handle_text_cmd m end end @bot.loggers.level = :warn @id = info["server"].downcase.sum end # (see Vrobot4::Server::Server#connect) def connect @bot.start end # (see Vrobot4::Server::Server#flags) def flags ?L end protected # (see Vrobot4::Server::Server#load_permissions) def load_permissions pinf @mprm = {chan: {}, role: {}, glob: {}} pinf.each do |pr| mod = Vrobot4::Module.get_module_type(pr["module"])[:type] if pr.key? "channel" @mprm[:chan][mod] = ChannelPerms.new unless @mprm[:chan].key? mod @mprm[:chan][mod][pr["channel"].downcase] = true elsif pr.key? "roles" @mprm[:role][mod] = pr["roles"] elsif pr.key? "enable" @mprm[:glob][mod] = pr["enable"] end end if pinf end # Helper for channel permissions. class ChannelPerms def initialize() @cprm = {} end # @param chan [Sv_IRC::Channel] the channel for this permission # @return [Boolean] true if the channel is enabled, false otherwise. def [](chan) @cprm[chan.name] end # Sets a channel's permission on/off. # @param name [String] the channel for this permission # @param set [Boolean] if this permission should be set # @return [void] def []=(name, set) @cprm[name] = set end end private_constant :ChannelPerms # An IRC user. class User < Vrobot4::Server::User attr_reader :real # The Cinch::User instance. # @param user [Cinch::User] the irc user # @param chan [Cinch::Channel] the irc channel this object was made in def initialize user, chan @real = user @name = user.nick @id = user.nick.downcase.sum if user.oper? then @roles = "Oohv" elsif chan.opped? user then @roles = "ohv" elsif chan.half_opped? user then @roles = "hv" elsif chan.voiced? user then @roles = "v" else @roles = "" end end end # An IRC channel. class Channel < Vrobot4::Server::Channel attr_reader :real # The Cinch::Channel instance. # @param chan [Cinch::Channel] the irc channel def initialize chan @real = chan @name = chan.name.downcase @id = chan.name.downcase.sum end end end ## EOF