def self.dispatch_command(args_text, network:, nick:, channel: nil, lookup: nil, login: nil)
parsed = RedRoosterAuth.parse_command(args_text)
if parsed[:action] == :error
return { error: parsed[:error] || RedRoosterAuth.usage_message }
end
lookup ||= ->(location, **opts) { RedRoosterApi.lookup_menu(location, **opts) }
login ||= lambda do |**opts|
RedRoosterAuth.login_account(**opts, fetch: method(:default_fetch))
end
case parsed[:action]
when :help
{ reply: RedRoosterAuth.format_help_reply }
when :login
if !RedRoosterAuth.private_message?(channel)
return { error: RedRoosterAuth.login_channel_denied_message }
end
result = login.call(network: network, nick: nick, token: parsed[:token])
return { error: result[:error] } unless result[:success]
{ reply: RedRoosterAuth.format_login_reply(result) }
when :logout
RedRoosterAuth.clear_account(network: network, nick: nick, channel: channel)
{ reply: RedRoosterAuth.format_logout_reply }
when :whoami
account = RedRoosterAuth.load_account(network: network, nick: nick, channel: channel)
{ reply: RedRoosterAuth.format_whoami_reply(account) }
when :menu
account = RedRoosterAuth.load_account(network: network, nick: nick, channel: channel)
{
reply: lookup.call(
parsed[:location],
fetch: method(:default_fetch),
account: account
)
}
else
{ error: RedRoosterAuth.usage_message }
end
end