class Msg
Constants
- IRC_RESET
- PENDING_KEY
- PLUGIN_NAME
- STYLE_TITLE
Public Class Methods
Source
# File plugins/msg.rb, line 56 def self.channel_users_from(channel) return [] unless channel raw = if channel.respond_to?(:user_list) channel.user_list else channel.users end normalize_channel_users(raw) end
Source
# File plugins/msg.rb, line 23 def self.command_pattern /msg(?:\s+(.+))?$/i end
Source
# File plugins/msg.rb, line 226 def self.deliveries_on_join(network:, channel:, nick:, store: RabbotDb) pop_messages_for_target(network: network, channel: channel, target: nick, store: store).map do |entry| format_delivery( target: nick, from: entry["from"], message: entry["message"] ) end end
Source
# File plugins/msg.rb, line 161 def self.dispatch( args, from:, channel_users:, network:, channel:, store: RabbotDb ) if args.to_s.strip.empty? return { reply: format_usage_reply } end result = prepare_queue( args, from: from, channel_users: channel_users, network: network, channel: channel, store: store ) return { reply: format_error_reply(result[:error]) } if result[:error] { reply: result[:reply] } end
Source
# File plugins/msg.rb, line 67 def self.find_user_by_nick(users, nick) key = Normalize.user_key(nick) normalize_channel_users(users).find { |user| Normalize.user_key(user.nick) == key } end
Source
# File plugins/msg.rb, line 131 def self.format_channel_required_reply format_error_reply("Use !msg in a channel.") end
Source
# File plugins/msg.rb, line 152 def self.format_delivery(target:, from:, message:) [ IrcFormat.tag("MSG"), IrcFormat.menu_heading("#{Normalize.nick(target)} joined", style: STYLE_TITLE), message.to_s, IrcFormat.report_footer("from #{Normalize.nick(from)}") ].join("\n") end
Source
# File plugins/msg.rb, line 124 def self.format_error_reply(message) [ IrcFormat.tag("MSG"), message.to_s ].join("\n") end
Source
# File plugins/msg.rb, line 135 def self.format_present_reply(target:) [ IrcFormat.tag("MSG"), IrcFormat.menu_heading("#{Normalize.nick(target)} is here", style: STYLE_TITLE), "#{IrcFormat.nick(target)} is already in the channel — say it directly." ].join("\n") end
Source
# File plugins/msg.rb, line 143 def self.format_queued_reply(target:, from:, channel:) [ IrcFormat.tag("MSG"), IrcFormat.menu_heading("queued for #{Normalize.nick(target)}", style: STYLE_TITLE), "Message held until they join.", IrcFormat.report_footer(from, channel) ].join("\n") end
Source
# File plugins/msg.rb, line 116 def self.format_usage_reply [ IrcFormat.report_header(tag: "MSG", title: "leave a message", style: STYLE_TITLE), usage_message, IrcFormat.report_footer("delivered on join") ].join("\n") end
Source
# File plugins/msg.rb, line 76 def self.load_queue(network:, channel:, store: RabbotDb) store.get_plugin_json( plugin: PLUGIN_NAME, key: PENDING_KEY, network: network, channel: channel, default: {} ) end
Source
# File plugins/msg.rb, line 47 def self.normalize_channel_users(channel_users) case channel_users when Hash channel_users.keys else Array(channel_users) end end
Source
# File plugins/msg.rb, line 34 def self.parse_command(text) stripped = text.to_s.strip return { error: usage_message } if stripped.empty? target, message = stripped.split(/\s+/, 2) return { error: usage_message } if target.nil? || target.empty? message = message.to_s.strip return { error: "Message cannot be empty." } if message.empty? { target: Normalize.nick(target), message: message } end
Source
# File plugins/msg.rb, line 108 def self.pop_messages_for_target(network:, channel:, target:, store: RabbotDb) queue = load_queue(network: network, channel: channel, store: store) key = Normalize.user_key(target) entries = Array(queue.delete(key)) save_queue(network: network, channel: channel, queue: queue, store: store) entries end
Source
# File plugins/msg.rb, line 186 def self.prepare_queue( args, from:, channel_users:, network:, channel:, store: RabbotDb ) parsed = parse_command(args) return parsed if parsed[:error] target = parsed[:target] if Normalize.user_key(from) == Normalize.user_key(target) return { error: "You cannot leave a message for yourself." } end if user_in_channel?(channel_users, target) return { present: true, target: target, reply: format_present_reply(target: target) } end queue_message( network: network, channel: channel, from: from, target: target, message: parsed[:message], store: store ) { queued: true, target: target, reply: format_queued_reply(target: target, from: from, channel: channel) } end
Source
# File plugins/msg.rb, line 96 def self.queue_message(network:, channel:, from:, target:, message:, store: RabbotDb) queue = load_queue(network: network, channel: channel, store: store) key = Normalize.user_key(target) queue[key] ||= [] queue[key] << { "from" => Normalize.nick(from), "message" => message.to_s, "at" => RabbotDb.iso_time } save_queue(network: network, channel: channel, queue: queue, store: store) end
Source
# File plugins/msg.rb, line 86 def self.save_queue(network:, channel:, queue:, store: RabbotDb) store.set_plugin_json( plugin: PLUGIN_NAME, key: PENDING_KEY, network: network, channel: channel, value: queue ) end
Source
# File plugins/msg.rb, line 30 def self.usage_message "Usage: !msg <nick> <message> — e.g. !msg MeatAnt hey" end
Source
# File plugins/msg.rb, line 72 def self.user_in_channel?(users, nick) !find_user_by_nick(users, nick).nil? end
Public Instance Methods
Source
# File plugins/msg.rb, line 248 def execute(m, args = nil) unless m.channel themed_flood_safe_reply(m, self.class.format_channel_required_reply) return end result = self.class.dispatch( args, from: m.user.nick, channel_users: self.class.channel_users_from(m.channel), network: bot.config.server, channel: m.channel.name ) themed_flood_safe_reply(m, result[:reply]) end
Source
# File plugins/msg.rb, line 236 def on_join(m) return unless m.channel return if m.user.nick == bot.nick deliveries = self.class.deliveries_on_join( network: bot.config.server, channel: m.channel.name, nick: m.user.nick ) deliveries.each { |text| themed_flood_safe_reply(m, text) } end