Class: Relayer::IRCClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ IRCClient

Returns a new instance of IRCClient.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/relayer/client.rb', line 11

def initialize(options = {})
  @ssl = options[:ssl]
  
  @hostname = options[:hostname]
  @port = options[:port]
  
  @ping_reply = options[:ping_reply]
  @version_reply = options[:version_reply]
  
  @version_string = options[:version_string]
  
  @nick = options[:nick]
  @nick_pass = options[:pass]
  @ident = options[:ident]
  @real_name = options[:real_name]
  
  @first_channels = options[:channels] || []
  default_options!
  
  @users = {}
  @channels = {}
  
  @protocol = IRCProtocol.new(self)
  @events = IRCEvents.new(@protocol, self)

  @mutex = Mutex.new
  
  default_handlers!
end

Instance Attribute Details

#eventsObject

Returns the value of attribute events.



7
8
9
# File 'lib/relayer/client.rb', line 7

def events
  @events
end

#nickObject

Returns the value of attribute nick.



8
9
10
# File 'lib/relayer/client.rb', line 8

def nick
  @nick
end

#protocolObject

Returns the value of attribute protocol.



9
10
11
# File 'lib/relayer/client.rb', line 9

def protocol
  @protocol
end

Instance Method Details

#channel(channel) ⇒ Object



119
120
121
122
# File 'lib/relayer/client.rb', line 119

def channel(channel)
  channel = channel.downcase
  @channels[channel] ||= IRC::Channel.new(channel) if IRC::Channel.is_channel?(channel)
end

#default_handlers!Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/relayer/client.rb', line 50

def default_handlers!
  @events.ping do |irc, event|
    @protocol.pong event[:token]
  end
  
  @events.connected do
    unless @nick_pass.nil?
      @protocol.message('NickServ', "identify #{@nick_pass}")
    end

    @first_channels.each do |channel|
      @protocol.join(channel)
    end
  end
  
  @events.ctcp do |irc, event|
    @protocol.ctcp_reply :version, event[:actor], version_string if event[:query] == :version
  end
end

#default_options!Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/relayer/client.rb', line 70

def default_options!
  if @ssl.nil?
    @ssl = false
  end
  
  unless @port
    @port = 6697 if @ssl
    @port ||= 6667
  end
  
  @nick ||= "Relayer"
  @ident ||= "relayer"
  @real_name ||= "Relayer"
  
  @hostname ||= 'irc.freenode.net'
end

#process_raw(line) ⇒ Object



102
103
104
# File 'lib/relayer/client.rb', line 102

def process_raw(line)
  @protocol.process_line(line)
end

#readableObject



92
93
94
# File 'lib/relayer/client.rb', line 92

def readable
  @socket.read
end

#registerObject



45
46
47
48
# File 'lib/relayer/client.rb', line 45

def register
  @protocol.user(@ident, @real_name)
  @protocol.nick(@nick)
end

#send_raw(line) ⇒ Object



106
107
108
109
110
# File 'lib/relayer/client.rb', line 106

def send_raw(line)
  @mutex.synchronize do
    @socket.send line
  end
end

#startObject



87
88
89
90
# File 'lib/relayer/client.rb', line 87

def start
  @socket = IRCSocket.new(self, @hostname, @port, @ssl)
  register
end

#user(hostmask) ⇒ Object



112
113
114
115
116
117
# File 'lib/relayer/client.rb', line 112

def user(hostmask)
  parts = IRC::User.parse_hostmask(hostmask)
  
  nickname = parts[:nick].downcase
  @users[nickname] ||= IRC::User.new(hostmask) if IRC::User.is_user?(hostmask)
end

#version_stringObject



41
42
43
# File 'lib/relayer/client.rb', line 41

def version_string
  @version_string || "Relayer IRC Library v#{Relayer::VERSION}"
end

#writableObject



96
97
98
99
100
# File 'lib/relayer/client.rb', line 96

def writable
  @mutex.synchronize do
    @socket.write
  end
end