forked from sbfaulkner/net-irc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple-client.rb
156 lines (119 loc) · 4.63 KB
/
simple-client.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/ruby
$:.push(File.join(File.dirname(__FILE__),'../lib'))
require 'net/irc'
module Ansi
RESET = 0
BOLD = 1
DIM = 2
UNDERSCORE = 4
BLINK = 5
REVERSE = 7
HIDDEN = 8
BLACK = 0
RED = 1
GREEN = 2
YELLOW = 3
BLUE = 4
MAGENTA = 5
CYAN = 6
WHITE = 7
def esc(*attrs)
"\033[#{attrs.join(';')}m"
end
def fg(colour)
30 + colour
end
def bg(colour)
40 + colour
end
def highlight(text, *attrs)
"#{esc(*attrs)}#{text}#{esc(RESET)}"
end
end
include Ansi
Net::IRC.logger.level = Logger::DEBUG
Net::IRC.logger.datetime_format = "%Y/%m/%d %H:%M:%S"
Thread.abort_on_exception = true
Net::IRC.start 'unwwwired', 'S. Brent Faulkner', 'irc.freenode.net' do |irc|
Thread.new do
irc.each do |message|
case message
# TODO: required = VERSION, PING, CLIENTINFO, ACTION
# TODO: handle internally... probably true for most CTCP requests
when Net::IRC::CTCPVersion
irc.ctcp_version(message.source, "net-irc simple-client", Net::IRC::VERSION, PLATFORM, "http://www.github.com/sbfaulkner/net-irc")
when Net::IRC::CTCP
puts highlight("Unhandled CTCP REQUEST: #{message.class} (#{message.code})", BOLD, fg(RED))
when Net::IRC::Join
puts "#{highlight(message.prefix.nickname, BOLD, fg(YELLOW))} joined #{highlight(message.channels.first, BOLD, fg(GREEN))}."
when Net::IRC::Part
if message.text && ! message.text.empty?
puts "#{highlight(message.prefix.nickname, BOLD, fg(YELLOW))} has left #{highlight(message.channels.first, BOLD, fg(GREEN))} (#{message.text})."
else
puts "#{highlight(message.prefix.nickname, BOLD, fg(YELLOW))} has left #{highlight(message.channels.first, BOLD, fg(GREEN))}."
end
when Net::IRC::Quit
if message.text && ! message.text.empty?
puts "#{highlight(message.prefix.nickname, BOLD, fg(YELLOW))} has quit (#{message.text})."
else
puts "#{highlight(message.prefix.nickname, BOLD, fg(YELLOW))} has quit."
end
when Net::IRC::Notice
puts highlight(message.text, fg(CYAN))
when Net::IRC::Privmsg
puts "#{highlight(message.prefix.nickname, BOLD, fg(YELLOW))} #{highlight(message.target, BOLD, fg(GREEN))}: #{highlight(message.text, BOLD)}"
when Net::IRC::ErrNicknameinuse
irc.nick message.nickname.sub(/\d*$/) { |n| n.to_i + 1 }
when Net::IRC::Error
puts highlight("Unhandled ERROR: #{message.class} (#{message.command})", BOLD, fg(RED))
when Net::IRC::RplWelcome, Net::IRC::RplYourhost, Net::IRC::RplCreated
puts message.text
when Net::IRC::RplLuserclient, Net::IRC::RplLuserme, Net::IRC::RplLocalusers, Net::IRC::RplGlobalusers, Net::IRC::RplStatsconn
puts highlight(message.text, fg(BLUE))
when Net::IRC::RplLuserop, Net::IRC::RplLuserchannels
puts highlight("#{message.count} #{message.text}", fg(BLUE))
when Net::IRC::RplIsupport
# TODO: handle internally... parse into capabilities collection
when Net::IRC::RplMyinfo
when Net::IRC::RplMotdstart
when Net::IRC::RplTopic
# TODO: handle internally
puts "#{highlight(message.channel, BOLD, fg(GREEN))}: #{message.text}"
when Net::IRC::RplTopicwhotime
# TODO: handle internally
puts "#{highlight(message.channel, BOLD, fg(GREEN))}: #{message.nickname} #{message.time.strftime("%Y/%m/%d %H:%M:%S")}"
when Net::IRC::RplNamreply
# TODO: handle internally
puts "#{highlight(message.channel, BOLD, fg(GREEN))}: #{message.names.join(', ')}"
when Net::IRC::RplEndofnames
# TODO: handle internally
when Net::IRC::RplMotd
puts message.text.sub(/^- /,'')
when Net::IRC::RplEndofmotd
puts ""
when Net::IRC::Reply
puts highlight("Unhandled REPLY: #{message.class} (#{message.command})", BOLD, fg(RED))
when Net::IRC::Message
puts highlight("Unhandled MESSAGE: #{message.class} (#{message.command})", BOLD, fg(RED))
else
raise IOError, "unknown class #{message.class}"
end
end
end
while line = STDIN.gets
scanner = StringScanner.new(line.chomp)
if command = scanner.scan(/\/([[:alpha:]]+)\s*/) && scanner[1]
case command.upcase
when 'JOIN'
# TODO: validate arguments... support for password... etc.
irc.join scanner.rest
when 'QUIT'
break
else
puts highlight("Unknown COMMAND: #{command}", BOLD, fg(RED))
end
else
# TODO: send privmsg to channel
end
end
end