module ChanOps
Constants
- ACTIONS
- NICK_ACTIONS
- TAG
Public Instance Methods
Source
# File lib/chan_ops.rb, line 97 def admin_allowed?(network:, channel:, nick:, is_op:) RabbotDb.user_allowed_at_level?( network: network, channel: channel, nick: nick, is_op: is_op, minimum: RabbotDb::LEVEL_ADMIN ) end
Source
# File lib/chan_ops.rb, line 207 def build_dispatch_result(parsed) detail = dispatch_detail(parsed) parsed.merge(private_reply: ChanReport.success( action: parsed[:action], channel: parsed[:channel], detail: detail )) end
Source
# File lib/chan_ops.rb, line 62 def build_parsed(channel:, action:, rest:) result = { channel: channel, action: action } case action when :topic text = rest.to_s.strip return { error: :missing_text } if text.empty? result[:text] = text when :mode parts = rest.to_s.strip.split(/\s+/, 2) modes = parts[0].to_s.strip return { error: :missing_modes } if modes.empty? result[:modes] = modes result[:mode_args] = parts[1].to_s.strip.split(/\s+/).reject(&:empty?) when :part message = rest.to_s.strip result[:part_message] = message unless message.empty? when *NICK_ACTIONS target, reason = rest.to_s.strip.split(/\s+/, 2) return { error: :missing_target } if target.to_s.strip.empty? result[:target] = Normalize.nick(target) result[:reason] = reason.to_s.strip unless reason.to_s.strip.empty? when :unban mask = rest.to_s.strip return { error: :missing_target } if mask.empty? result[:ban_mask] = mask end result end
Source
# File lib/chan_ops.rb, line 139 def channel_gate_error "You must share that channel with the bot." end
Source
# File lib/chan_ops.rb, line 119 def channel_lookup(channels) channels.map { |channel| [channel.to_s.downcase, channel.to_s] }.to_h end
Source
# File lib/chan_ops.rb, line 27 def denied_message "Only channel ops and registered admins can control channels." end
Source
# File lib/chan_ops.rb, line 156 def dispatch_command(args_text, user_channels:, bot_channels:, network:, nick:, is_op: false) text = args_text.to_s.strip return { error: usage_message } if text.empty? parsed = parse_command(text) return { error: error_message(parsed[:error]) } if parsed[:error] authorize!( parsed, network: network, nick: nick, is_op: is_op, user_channels: user_channels, bot_channels: bot_channels ) end
Source
# File lib/chan_ops.rb, line 216 def dispatch_detail(parsed) case parsed[:action] when :topic then parsed[:text] when :mode then [parsed[:modes], *Array(parsed[:mode_args])].join(" ") when :part then parsed[:part_message] when :unban then parsed[:ban_mask] else parsed[:target] end end
Source
# File lib/chan_ops.rb, line 143 def error_message(code) case code when :usage then usage_message when :unknown_action then unknown_action_message when :missing_target then "A nick or mask is required for that action." when :missing_text then "Topic text cannot be empty." when :missing_modes then "Mode flags are required." when :denied then denied_message when :channel then channel_gate_error else code.to_s end end
Source
# File lib/chan_ops.rb, line 35 def parse_command(text) stripped = text.to_s.strip return { error: :usage } if stripped.empty? if (match = stripped.match(/\Ajoin\s+(#\S+)\z/i)) return { action: :join, channel: Normalize.channel(match[1]) } end if (match = stripped.match(/\Apart\s+(#\S+)(?:\s+(.*))?\z/mi)) message = match[2].to_s.strip result = { action: :part, channel: Normalize.channel(match[1]) } result[:part_message] = message unless message.empty? return result end match = stripped.match(/\A(#\S+)\s+(\w+)(?:\s+(.*))?\z/mi) return { error: :usage } unless match channel = Normalize.channel(match[1]) action = match[2].to_s.downcase.to_sym rest = match[3].to_s return { error: :unknown_action } unless ACTIONS.include?(action) build_parsed(channel: channel, action: action, rest: rest) end
Source
# File lib/chan_ops.rb, line 123 def resolve_explicit_channel(channel:, user_channels:, bot_channels:) target_key = channel.to_s.downcase user_by_key = channel_lookup(user_channels) bot_keys = bot_channels.map { |name| name.to_s.downcase } unless user_by_key.key?(target_key) return { error: :channel } end unless bot_keys.include?(target_key) return { error: :channel } end { channel: user_by_key[target_key] } end
Source
# File lib/chan_ops.rb, line 31 def unknown_action_message "Unknown action. Use: #{(ACTIONS + [:join]).join(', ')}." end
Source
# File lib/chan_ops.rb, line 22 def usage_message actions = (ACTIONS + [:join]).join("|") "Usage: /msg <bot> !chan <#channel> <#{actions}> [args] — or !chan join|part <#channel> [msg]" end