forked from jaspervdj/IRCFileNotify
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ircsocket.rb
68 lines (54 loc) · 1.25 KB
/
ircsocket.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
require "socket"
class IRCSocket < TCPSocket
attr_accessor :channel, :nick, :verbose
def initialize(server,port,channel,nick,verbose=false)
super server,port
@nick,@channel = nick,channel
@verbose = verbose
connect
end
# override puts and gets
# if verbose they will print to the terminal
def gets
result = super
$stdout.puts result if verbose
result
end
def puts(*args)
# if args is nil, were not supposed to crash
# we make sure we don't print when it is
# and let super take care of the error
$stdout.puts args.first if verbose && ! args.first.nil?
super
end
def connect
self.puts "NICK #{nick}"
self.puts "USER #{nick.downcase} ignored ignored :#{nick}"
setup_reply_ping_thread
end
def say(text)
self.puts "PRIVMSG #{channel} :#{text}"
end
def join
self.puts "JOIN #{channel}"
end
def leave
self.puts "PART #{channel}"
end
def close
leave
self.puts "QUIT"
super
@pingthread.join
end
private
def setup_reply_ping_thread
@pingthread = Thread.new(self) do |socket|
until socket.eof?
if socket.gets =~ /PING :(.*)$/
socket.puts "PONG :#{$1}"
end
end
end
end
end