Class: Relayer::IRCProtocol

Inherits:
Object
  • Object
show all
Defined in:
lib/relayer/protocol.rb

Constant Summary collapse

IRC_LINE_REGEX =
/^(:([^ ]+) )?([A-Z0-9]+) ([^:]+)? ?(:(.*))?$/

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ IRCProtocol

Returns a new instance of IRCProtocol.



10
11
12
13
# File 'lib/relayer/protocol.rb', line 10

def initialize(client)
  @client = client
  @registration_commands = []
end

Instance Method Details

#ctcp(label, to) ⇒ Object



88
89
90
91
92
# 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) ⇒ Object



94
95
96
97
98
99
# 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 = {}) ⇒ Object



15
16
17
# File 'lib/relayer/protocol.rb', line 15

def dispatch(event, args = {})
  @client.events.dispatch(event, args)
end

#join(channel) ⇒ Object



109
110
111
112
# File 'lib/relayer/protocol.rb', line 109

def join(channel)
  channel = "\##{channel}" unless channel[0] == '#'
  send_command :join, channel
end

#message(channel, message) ⇒ Object



101
102
103
# File 'lib/relayer/protocol.rb', line 101

def message(channel, message)
  send_command :privmsg, channel, message
end

#nick(nickname) ⇒ Object



105
106
107
# File 'lib/relayer/protocol.rb', line 105

def nick(nickname)
  send_command :nick, nickname
end

#pong(token = false) ⇒ Object



84
85
86
# File 'lib/relayer/protocol.rb', line 84

def pong(token = false)
  send_command :pong, token
end

#process_line(line) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/relayer/protocol.rb', line 70

def registered?
  (("001".."004").to_a - @registration_commands).empty?
end

#send_command(command, *args) ⇒ Object



74
75
76
77
78
79
80
81
82
# 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) ⇒ Object



114
115
116
# File 'lib/relayer/protocol.rb', line 114

def user(ident, real_name)
  send_command :user, ident, '0', '*', real_name
end