class RabbotUser
Constants
- DEFAULT_ALTERNATIVE_NICKS
- DEFAULT_USERS
- IRC_CTRL
- IRC_RESET
- STYLE_LEVEL
- STYLE_NICK
- STYLE_TEXT
Public Class Methods
Source
# File plugins/rabbot_user.rb, line 188 def self.add_user(network:, channel:, nick:, level:, actor_nick:, is_op:) actor_level = lookup_actor_level(network: network, channel: channel, nick: actor_nick, is_op: is_op) unless can_assign_level?(actor_level: actor_level, new_level: level, is_op: is_op) return insufficient_level_message(level) end if RabbotDb.registered?(network: network, channel: channel, nick: nick) return "#{nick} is already registered." end RabbotDb.register_user(network: network, channel: channel, nick: nick, level: level) "#{nick} registered as #{level}." end
Source
# File plugins/rabbot_user.rb, line 83 def self.already_joined?(bot, channel_name) bot.channels.any? { |channel| channel.name.to_s.casecmp?(channel_name.to_s) } end
Source
# File plugins/rabbot_user.rb, line 157 def self.can_assign_level?(actor_level:, new_level:, is_op:) return true if is_op RabbotDb.level_rank(actor_level) > RabbotDb.level_rank(new_level) end
Source
# File plugins/rabbot_user.rb, line 163 def self.can_manage_user?(actor_level:, target_level:, is_op:) return true if is_op RabbotDb.level_rank(actor_level) > RabbotDb.level_rank(target_level) end
Source
# File plugins/rabbot_user.rb, line 173 def self.cannot_manage_message(nick) "You cannot manage #{nick}." end
Source
# File plugins/rabbot_user.rb, line 139 def self.command_allowed?(user_is_channel_op, user_is_registered) user_is_channel_op || user_is_registered end
Source
# File plugins/rabbot_user.rb, line 76 def self.configured_channel?(bot, channel_name) name = channel_name.to_s.strip return false if name.empty? Array(bot.config.channels).any? { |configured| configured.to_s.casecmp?(name) } end
Source
# File plugins/rabbot_user.rb, line 143 def self.denied_message "Only registered users and channel ops can manage the user list." end
Source
# File plugins/rabbot_user.rb, line 177 def self.format_user_entry(user) "#{STYLE_NICK}#{user.nick}#{IRC_RESET}#{STYLE_TEXT} (#{STYLE_LEVEL}#{user.level}#{IRC_RESET}#{STYLE_TEXT})#{IRC_RESET}" end
Source
# File plugins/rabbot_user.rb, line 181 def self.format_user_list(users) return "#{STYLE_TEXT}No registered users.#{IRC_RESET}" if users.empty? formatted = users.map { |user| format_user_entry(user) }.join("#{STYLE_TEXT}, #{IRC_RESET}") "#{STYLE_TEXT}registered users: #{IRC_RESET}#{formatted}#{IRC_RESET}" end
Source
# File plugins/rabbot_user.rb, line 169 def self.insufficient_level_message(level) "You cannot assign the #{level} level." end
Source
# File plugins/rabbot_user.rb, line 135 def self.invalid_level_message "Unknown level. Valid levels: #{RabbotDb::LEVELS.join(", ")}" end
Source
# File plugins/rabbot_user.rb, line 29 def self.irc_color(fg, bg = nil) IrcFormat.color(fg, bg) end
Source
# File plugins/rabbot_user.rb, line 65 def self.join_configured_channels(bot) joined = bot.channels.map { |channel| channel.name.to_s.downcase } Array(bot.config.channels).each do |channel_name| name = channel_name.to_s.strip next if name.empty? next if joined.include?(name.downcase) bot.join(name) end end
Source
# File plugins/rabbot_user.rb, line 87 def self.join_invited_channel(bot, channel_name) name = channel_name.to_s.strip return false if name.empty? return false unless configured_channel?(bot, name) return false if already_joined?(bot, name) bot.join(name) true end
Source
# File plugins/rabbot_user.rb, line 147 def self.lookup_actor_level(network:, channel:, nick:, is_op:) return RabbotDb::LEVEL_OWNER if is_op registered = RabbotDb.registered_channel_user(network: network, channel: channel, nick: nick) return registered.level if registered user = RabbotDb.channel_user(network: network, channel: channel, nick: nick) user&.level || RabbotDb::DEFAULT_LEVEL end
Source
# File plugins/rabbot_user.rb, line 235 def self.manage_command(network:, channel:, args:, actor_nick:, is_op:) parsed = parse_command(args) return parsed[:error] if parsed[:error] case parsed[:action] when :add add_user( network: network, channel: channel, nick: parsed[:nick], level: parsed[:level], actor_nick: actor_nick, is_op: is_op ) when :rem remove_user( network: network, channel: channel, nick: parsed[:nick], actor_nick: actor_nick, is_op: is_op ) when :set set_user_level( network: network, channel: channel, nick: parsed[:nick], level: parsed[:level], actor_nick: actor_nick, is_op: is_op ) when :list format_user_list(RabbotDb.registered_users(network: network, channel: channel)) end end
Source
# File plugins/rabbot_user.rb, line 97 def self.normalize_nick(nick) Normalize.nick(nick) end
Source
# File plugins/rabbot_user.rb, line 101 def self.parse_command(args) text = args.to_s.strip return { action: :list } if text.casecmp?("list") match = text.match(/\A(add|rem|set)\s+(\S+)(?:\s+(\S+))?\z/i) return { error: usage_message } unless match action = match[1].downcase.to_sym nick = normalize_nick(match[2]) return { error: usage_message } if nick.empty? if action == :rem && match[3] return { error: usage_message } end if action == :add level = match[3] ? RabbotDb.normalize_level(match[3]) : RabbotDb::DEFAULT_LEVEL return { error: invalid_level_message } if level.nil? return { action: action, nick: nick, level: level } end if action == :set return { error: usage_message } unless match[3] level = RabbotDb.normalize_level(match[3]) return { error: invalid_level_message } if level.nil? return { action: action, nick: nick, level: level } end { action: action, nick: nick } end
Source
# File plugins/rabbot_user.rb, line 202 def self.remove_user(network:, channel:, nick:, actor_nick:, is_op:) stored = RabbotDb.registered_channel_user(network: network, channel: channel, nick: nick) unless stored return "#{nick} is not registered." end actor_level = lookup_actor_level(network: network, channel: channel, nick: actor_nick, is_op: is_op) unless can_manage_user?(actor_level: actor_level, target_level: stored.level, is_op: is_op) return cannot_manage_message(stored.nick) end RabbotDb.unregister_user(network: network, channel: channel, nick: stored.nick) "Removed #{stored.nick} from the registered user list." end
Source
# File plugins/rabbot_user.rb, line 49 def self.seed_channel_defaults(network:, channel:, default_users: DEFAULT_USERS) RabbotDb.ensure_default_registered_users!( network: network, channel: channel, default_users: default_users ) end
Source
# File plugins/rabbot_user.rb, line 57 def self.seed_configured_channels(network:, channels:, default_users: DEFAULT_USERS) Array(channels).each do |channel| next if channel.to_s.strip.empty? seed_channel_defaults(network: network, channel: channel, default_users: default_users) end end
Source
# File plugins/rabbot_user.rb, line 217 def self.set_user_level(network:, channel:, nick:, level:, actor_nick:, is_op:) stored = RabbotDb.registered_channel_user(network: network, channel: channel, nick: nick) unless stored return "#{nick} is not registered." end actor_level = lookup_actor_level(network: network, channel: channel, nick: actor_nick, is_op: is_op) unless can_manage_user?(actor_level: actor_level, target_level: stored.level, is_op: is_op) return cannot_manage_message(stored.nick) end unless can_assign_level?(actor_level: actor_level, new_level: level, is_op: is_op) return insufficient_level_message(level) end RabbotDb.set_registered_level(network: network, channel: channel, nick: stored.nick, level: level) "Set #{stored.nick} to #{level}." end
Source
# File plugins/rabbot_user.rb, line 45 def self.storage_key(network, channel) "#{network}/#{channel.to_s.downcase}" end
Source
# File plugins/rabbot_user.rb, line 40 def self.usage_message levels = RabbotDb::LEVELS.join("|") "Usage: !rabbot user <add|rem|set|list> [nick] [#{levels}]" end
Public Instance Methods
Source
# File plugins/rabbot_user.rb, line 286 def execute(m, args) network = bot.config.server channel = m.channel.name self.class.seed_channel_defaults(network: network, channel: channel) is_op = m.channel.opped?(m.user) is_registered = RabbotDb.registered?(network: network, channel: channel, nick: m.user.nick) unless self.class.command_allowed?(is_op, is_registered) themed_flood_safe_reply(m, self.class.denied_message) return end parsed = self.class.parse_command(args) if parsed[:error] themed_flood_safe_reply(m, parsed[:error]) return end actor_nick = m.user.nick case parsed[:action] when :list themed_flood_safe_reply(m, self.class.format_user_list(RabbotDb.registered_users(network: network, channel: channel))) when :add themed_flood_safe_reply(m, self.class.add_user( network: network, channel: channel, nick: parsed[:nick], level: parsed[:level], actor_nick: actor_nick, is_op: is_op )) when :rem themed_flood_safe_reply(m, self.class.remove_user( network: network, channel: channel, nick: parsed[:nick], actor_nick: actor_nick, is_op: is_op )) when :set themed_flood_safe_reply(m, self.class.set_user_level( network: network, channel: channel, nick: parsed[:nick], level: parsed[:level], actor_nick: actor_nick, is_op: is_op )) end end
Source
# File plugins/rabbot_user.rb, line 279 def on_bot_join(m) return unless m.user.nick == bot.nick return unless m.channel self.class.seed_channel_defaults(network: bot.config.server, channel: m.channel.name) end
Source
# File plugins/rabbot_user.rb, line 271 def on_connect(_m) self.class.seed_configured_channels( network: bot.config.server, channels: bot.config.channels ) self.class.join_configured_channels(bot) end