class Relayer::IRCProtocol

Constants

IRC_LINE_REGEX

Public Class Methods

new(client) click to toggle source
# File lib/relayer/protocol.rb, line 10
def initialize(client)
  @client = client
  @registration_commands = []
end

Public Instance Methods

ctcp(label, to) click to toggle source
# File lib/relayer/protocol.rb, line 88
def ctcp(label, to)
  extended_arg = "\001#{label.to_s.upcase}\001"
  
  send_command :privmsg, to, extended_arg
end
ctcp_reply(label, to, data) click to toggle source
# File lib/relayer/protocol.rb, line 94
def ctcp_reply(label, to, data)
  data = " #{data}" if data
  extended_arg = "\001#{label.to_s.upcase}#{data}\001"
  
  send_command :notice, to, extended_arg
end
dispatch(event, args = {}) click to toggle source
# File lib/relayer/protocol.rb, line 15
def dispatch(event, args = {})
  @client.events.dispatch(event, args)
end
join(channel) click to toggle source
# File lib/relayer/protocol.rb, line 109
def join(channel)
  channel = "\##{channel}" unless channel[0] == '#'
  send_command :join, channel
end
message(channel, message) click to toggle source
# File lib/relayer/protocol.rb, line 101
def message(channel, message)
  send_command :privmsg, channel, message
end
nick(nickname) click to toggle source
# File lib/relayer/protocol.rb, line 105
def nick(nickname)
  send_command :nick, nickname
end
pong(token = false) click to toggle source
# File lib/relayer/protocol.rb, line 84
def pong(token = false)
  send_command :pong, token
end
process_line(line) click to toggle source
# File lib/relayer/protocol.rb, line 19
def process_line(line)
  match = IRC_LINE_REGEX.match(line)
  raise IRCProtocolException if match.nil?
  
  raw, actor_tmp, actor, command, args, ext_tmp, extended_arg = match.to_a
  
  if Relayer::IRC::User.is_user? actor
    actor = @client.user(actor)
  else
    actor = nil #todo: implement server actors
  end
  
  args = args.split unless args.nil?
  command = command.downcase.to_sym unless command.nil?
  
  if actor.nil?
    # match server-only commands like 'PING'
    case command
      when :ping
        dispatch :ping, :token => extended_arg
    end
    
    if ("001".."004").include? command.to_s
      @registration_commands.push command.to_s
      
      dispatch :connected if registered?
    end
  else
    case command
      when :privmsg
        to = args[0]
        
        message = extended_arg
        
        is_ctcp = message.start_with?("\001") and message.end_with?("\001")
        message = message[1..-2] if is_ctcp
        
        if is_ctcp
          query_args = message.split
          query = query_args.shift.downcase.to_sym
          
          dispatch :ctcp, :actor => actor, :channel => @client.channel(to), :query => query, :args => query_args
        elsif IRC::Channel.is_channel?(to)
          dispatch :channel_msg, :actor => actor, :channel => @client.channel(to), :message => extended_arg
        elsif to.downcase == @nick.downcase
          dispatch :private_msg, :actor => actor, :message => extended_arg
        end
    end
  end
end
registered?() click to toggle source
# File lib/relayer/protocol.rb, line 70
def registered?
  (("001".."004").to_a - @registration_commands).empty?
end
send_command(command, *args) click to toggle source
# File lib/relayer/protocol.rb, line 74
def send_command(command, *args)
  if args.last.include?(' ')
    arg = args.pop
    arg.insert(0, ':')
    args.push(arg)
  end
  
  @client.send_raw "#{command.to_s.upcase} #{args.join ' '}"
end
user(ident, real_name) click to toggle source
# File lib/relayer/protocol.rb, line 114
def user(ident, real_name)
  send_command :user, ident, '0', '*', real_name
end