# IRC backend. module Backend_IRC require 'cinch' # Helper for channel permissions. class ChannelPerms def initialize() @cprm = {} end # @param chan [Backend_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 # 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 = Vrobot4.hash_str user.nick.downcase 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 = Vrobot4.hash_str chan.name.downcase end end # An IRC server. class Server < Vrobot4::Server::Server # The server type name. def self.type() "IRC" end # (see Vrobot4::Server::Server#initialize) def initialize info, bot super @id = Vrobot4.hash_str info["server"].downcase for mod in info["modules"] do load_mod mod end 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: {}} return unless pinf 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][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 end end # A bot implementation for IRC using cinch. class Bot < Vrobot4::Robots::Bot # The bot type name. def self.type() "IRC" end Vrobot4::Robots.add_bot_type self attr_accessor :serv # @return [Vrobot4::Server::Server] the server # (see Vrobot4::Robots::Bot#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.serv, reply: -> (text) {evt.reply text}, reply_b: -> (text) {evt.reply text} this.serv.handle_text_cmd m end end @bot.loggers.level = :warn @serv = Server.new info, self end # (see Vrobot4::Robots::Bot#connect) def connect @bot.start end end end ## EOF