-
Notifications
You must be signed in to change notification settings - Fork 0
/
IrcConn.rb
executable file
·83 lines (74 loc) · 1.7 KB
/
IrcConn.rb
1
2
3
4
5
6
7
8
9
10
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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/ruby
require "socket"
require 'thread'
class IrcConn
attr_reader :address, :port, :nick, :user, :password
attr_writer :nick
#each IrcConn needs its own socket
def initialize(addy, p, nic, usr, pass=nil)
@reading = nil
@address = addy
@port = p
@nick = nic
@user = usr
@password = pass
#@sock = TCPSocket.open(@address, @port)
addr = Socket.getaddrinfo(@address, nil)
@sock = Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0)
#setup our timeout
@@TIMEOUT = 20
secs = Integer(@@TIMEOUT)
usecs = Integer((@@TIMEOUT-secs)*1_000_000)
optval = [secs, usecs].pack("l_2")
@sock.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval
@sock.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval
tryagain = true
while(tryagain)
begin
@sock.connect(Socket.pack_sockaddr_in(port, addr[0][3]))
tryagain = false
rescue
puts "Connection failed, trying in a few seconds..."
sleep(5)
tryagain = true
end
end
write "USER #{@user} . . :#{@nick}"
write "NICK #{@nick}"
if(pass)
write "PASS #{@password}"
end
@mutex = Mutex.new
end
#it needs a read function
def read
@mutex.synchronize {
if(!@reading)
@reading = 1
begin
str = @sock.gets
rescue
end
@reading = nil
return str
else
return nil
end
}
end
#it needs a write function
def write(line)
begin
@sock.puts line
rescue
end
puts ">> #{line}"
end
def close
#@sock.shutdown(2)
begin
@sock.close
rescue
end
end
end