-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc_client.rb
494 lines (437 loc) · 13.8 KB
/
irc_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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
class IRCClient
include NetUtils
attr_reader :nick, :user, :realname, :channels, :state
def initialize(sock, serv)
@serv = serv
@socket = sock
@channels = []
@peername = peer()
@welcomed = false
@nick_tries = 0
@state = {}
carp "initializing connection from #{@peername}"
end
def host
return @peername
end
def userprefix
return @usermsg
end
def closed?
return @socket.nil? || @socket.closed?
end
def ready
#check for nick and pass
return (!@pass.nil? && !@nick.nil?) ? true : false
end
def peer
begin
sockaddr = @socket.getpeername
begin
return Socket.getnameinfo(sockaddr, Socket::NI_NAMEREQD).first
rescue
return Socket.getnameinfo(sockaddr).first
end
rescue
return @socket.peeraddr[2]
end
end
def handle_pass(s)
carp "pass = #{s}"
@pass = s
end
def handle_nick(s)
carp "nick => #{s}"
if @serv.user_store[s].nil?
userlist = {}
if @nick.nil?
handle_newconnect(s)
else
userlist[s] = self if self.nick != s
@serv.user_store.delete(@nick)
@nick = s
end
@serv.user_store << self
#send the info to the world
#get unique users.
@channels.each {|c|
channel_store[c].each_user {|u|
userlist[u.nick] = u
}
}
userlist.values.each {|user|
user.reply :nick, s
}
@usermsg = ":#{@nick}!~#{@user}@#{@peername}"
else
#check if we are just nicking ourselves.
unless @serv.user_store[s] == self
#verify the connectivity of earlier guy
unless @serv.user_store[s].closed?
reply :numeric, ERR_NICKNAMEINUSE,"* #{s} ","Nickname is already in use."
@nick_tries += 1
if @nick_tries > $config['nick-tries']
carp "kicking spurious user #{s} after #{@nick_tries} tries"
handle_abort
end
return
else
@serv.user_store[s].handle_abort
@serv.user_store[s] = self
end
end
end
@nick_tries = 0
end
def handle_user(user, mode, unused, realname)
@user = user
@mode = mode
@realname = realname
@usermsg = ":#{@nick}!~#{@user}@#{@peername}"
send_welcome if !@nick.nil?
end
def mode
return @mode
end
def handle_newconnect(nick)
@alive = true
@nick = nick
@host = $config['hostname']
@ver = $config['version']
@starttime = $config['starttime']
send_welcome if !@user.nil?
end
def send_welcome
if !@welcomed
repl_welcome
repl_yourhost
repl_created
repl_myinfo
repl_motd
repl_mode
@welcomed = true
end
end
def repl_welcome
client = "#{@nick}!#{@user}@#{@peername}"
reply :numeric, RPL_WELCOME, @nick, "Welcome to this IRC server #{client}"
end
def repl_yourhost
reply :numeric, RPL_YOURHOST, @nick, "Your host is #{@host}, running version #{@ver}"
end
def repl_created
reply :numeric, RPL_CREATED, @nick, "This server was created #{@starttime}"
end
def repl_myinfo
reply :numeric, RPL_MYINFO, @nick, "#{@host} #{@ver} #{@serv.usermodes} #{@serv.channelmodes}"
end
def repl_bounce(sever, port)
reply :numeric, RPL_BOUNCE ,"Try server #{server}, port #{port}"
end
def repl_ison(*args)
#XXX TODO
reply :numeric, RPL_ISON, args.to_s
end
def repl_away(nick, msg)
reply :numeric, RPL_AWAY, nick, msg
end
def repl_unaway()
reply :numeric, RPL_UNAWAY, @nick,"You are no longer marked as being away"
end
def repl_nowaway()
reply :numeric, RPL_NOWAWAY, @nick,"You have been marked as being away"
end
def repl_motd()
reply :numeric, RPL_MOTDSTART,'', "- Message of the Day"
reply :numeric, RPL_MOTD,'', "- Do the dance see the source"
reply :numeric, RPL_ENDOFMOTD,'', "- End of /MOTD command."
end
def repl_mode()
end
def send_nonick(nick)
reply :numeric, ERR_NOSUCHNICK, nick, "No such nick/channel"
end
def send_nochannel(channel)
reply :numeric, ERR_NOSUCHCHANNEL, channel, "That channel doesn't exist"
end
def send_notonchannel(channel)
reply :numeric, ERR_NOTONCHANNEL, channel, "Not a member of that channel"
end
def send_topic(channel)
if @serv.channel_store[channel]
reply :numeric, RPL_TOPIC,channel, "#{@serv.channel_store[channel].topic}"
else
send_notonchannel channel
end
end
def names(channel)
return @serv.channel_store[channel].nicks
end
def send_nameslist(channel)
c = @serv.channel_store[channel]
if c.nil?
carp "names failed :#{c}"
return
end
names = []
c.each_user {|user|
names << c.mode(user) + user.nick if user.nick
}
reply :numeric, RPL_NAMREPLY,"= #{c.name}","#{names.join(' ')}"
reply :numeric, RPL_ENDOFNAMES,"#{c.name} ","End of /NAMES list."
end
def send_ping()
reply :ping, "#{$config['hostname']}"
end
def handle_join(channels)
channels.split(/,/).each {|ch|
c = ch.strip
if c !~ CHANNEL
send_nochannel(c)
carp "no such channel:#{c}"
return
end
channel = @serv.channel_store.add(c)
if channel.join(self)
send_topic(c)
send_nameslist(c)
@channels << c
else
carp "already joined #{c}"
end
}
end
def handle_ping(pingmsg, rest)
reply :pong, pingmsg
end
def handle_pong(srv)
carp "got pong: #{srv}"
end
def handle_privmsg(target, msg)
case target.strip
when CHANNEL
channel= @serv.channel_store[target]
if !channel.nil?
channel.privatemsg(msg, self)
else
send_nonick(target)
end
else
user = @serv.user_store[target]
if !user.nil?
if !user.state[:away].nil?
repl_away(user.nick,user.state[:away])
end
user.reply :privmsg, self.userprefix, user.nick, msg
else
send_nonick(target)
end
end
end
def handle_notice(target, msg)
case target.strip
when CHANNEL
channel= @serv.channel_store[target]
if !channel.nil?
channel.notice(msg, self)
else
send_nonick(target)
end
else
user = @ser.user_store[target]
if !user.nil?
user.reply :notice, self.userprefix, user.nick, msg
else
send_nonick(target)
end
end
end
def handle_part(channel, msg)
if @serv.channel_store.channels.include? channel
if @serv.channel_store[channel].part(self, msg)
@channels.delete(channel)
else
send_notonchannel channel
end
else
send_nochannel channel
end
end
def handle_quit(msg)
#do this to avoid double quit due to 2 threads.
return if !@alive
@alive = false
@channels.each do |channel|
@serv.channel_store[channel].quit(self, msg)
end
@serv.user_store.delete(self.nick)
carp "#{self.nick} #{msg}"
@socket.close if !@socket.closed?
end
def handle_topic(channel, topic)
carp "handle topic for #{channel}:#{topic}"
if topic.nil? or topic =~ /^ *$/
send_topic(channel)
else
begin
@serv.channel_store[channel].topic(topic,self)
rescue Exception => e
carp e
end
end
end
def handle_away(msg)
carp "handle away :#{msg}"
if msg.nil? or msg =~ /^ *$/
@state.delete(:away)
repl_unaway
else
@state[:away] = msg
repl_nowaway
end
end
def handle_list(channel)
reply :numeric, RPL_LISTSTART
case channel.strip
when /^#/
channel.split(/,/).each {|cname|
c = @serv.channel_store[cname.strip]
reply :numeric, RPL_LIST, c.name, c.topic if c
}
else
#older opera client sends LIST <1000
#we wont obey the boolean after list, but allow the listing
#nonetheless
@serv.channel_store.each_channel {|c|
reply :numeric, RPL_LIST, c.name, c.topic
}
end
reply :numeric, RPL_LISTEND
end
def handle_whois(target,nicks)
#ignore target for now.
return reply(:numeric, RPL_NONICKNAMEGIVEN, "", "No nickname given") if nicks.strip.length == 0
nicks.split(/,/).each {|nick|
nick.strip!
user = @serv.user_store[nick]
if user
reply :numeric, RPL_WHOISUSER, "#{user.nick} #{user.user} #{user.host} *", "#{user.realname}"
reply :numeric, RPL_WHOISCHANNELS, user.nick, "#{user.channels.join(' ')}"
repl_away user.nick, user.state[:away] if !user.state[:away].nil?
reply :numeric, RPL_ENDOFWHOIS, user.nick, "End of /WHOIS list"
else
return send_nonick(nick)
end
}
end
def handle_names(channels, server)
channels.split(/,/).each {|ch| send_nameslist(ch.strip) }
end
def handle_who(mask, rest)
channel = @serv.channel_store[mask]
hopcount = 0
if channel.nil?
#match against all users
@serv.user_store.each_user {|user|
reply :numeric, RPL_WHOREPLY ,
"#{user.channels[0]} #{user.userprefix} #{user.host} #{$config['hostname']} #{user.nick} H" ,
"#{hopcount} #{user.realname}" if File.fnmatch?(mask, "#{user.host}.#{user.realname}.#{user.nick}")
}
reply :numeric, RPL_ENDOFWHO, mask, "End of /WHO list."
else
#get all users in the channel
channel.each_user {|user|
reply :numeric, RPL_WHOREPLY ,
"#{mask} #{user.userprefix} #{user.host} #{$config['hostname']} #{user.nick} H" ,
"#{hopcount} #{user.realname}"
}
reply :numeric, RPL_ENDOFWHO, mask, "End of /WHO list."
end
end
def handle_mode(target, rest)
#TODO: dummy
reply :mode, target, rest
end
def handle_userhost(nicks)
info = []
nicks.split(/,/).each {|nick|
user = @serv.user_store[nick]
info << user.nick + '=-' + user.nick + '@' + user.peer
}
reply :numeric, RPL_USERHOST,"", info.join(' ')
end
def handle_reload(password)
end
def handle_abort()
handle_quit('aborted..')
end
def handle_version()
reply :numeric, RPL_VERSION,"#{$config['version']} Ruby IRCD", ""
end
def handle_eval(s)
reply :raw, eval(s)
end
def handle_unknown(s)
carp "unknown:>#{s}<"
reply :numeric, ERR_UNKNOWNCOMMAND,s, "Unknown command"
end
def handle_connect
reply :raw, "NOTICE AUTH :#{$config['version']} initialized, welcome."
end
def reply(method, *args)
case method
when :raw
arg = *args
raw arg
when :ping
host = *args
raw "PING :#{host}"
when :pong
msg = *args
# according to rfc 2812 the PONG must be of
#PONG csd.bu.edu tolsun.oulu.fi
# PONG message from csd.bu.edu to tolsun.oulu.fi
# ie no host at the begining
raw "PONG #{@host} #{@peername} :#{msg}"
when :join
user,channel = args
raw "#{user} JOIN :#{channel}"
when :part
user,channel,msg = args
raw "#{user} PART #{channel} :#{msg}"
when :quit
user,msg = args
raw "#{user} QUIT :#{msg}"
when :privmsg
usermsg, channel, msg = args
raw "#{usermsg} PRIVMSG #{channel} :#{msg}"
when :notice
usermsg, channel, msg = args
raw "#{usermsg} NOTICE #{channel} :#{msg}"
when :topic
usermsg, channel, msg = args
raw "#{usermsg} TOPIC #{channel} :#{msg}"
when :nick
nick = *args
raw "#{@usermsg} NICK :#{nick}"
when :mode
nick, rest = args
raw "#{@usermsg} MODE #{nick} :#{rest}"
when :numeric
numeric,msg,detail = args
server = $config['hostname']
raw ":#{server} #{'%03d'%numeric} #{@nick} #{msg} :#{detail}"
end
end
def raw(arg, abrt=false)
begin
carp "--> #{arg}"
@socket.print arg.chomp + "\n" if !arg.nil?
rescue Exception => e
carp "<#{self.userprefix}>#{e.message}"
#puts e.backtrace.join("\n")
handle_abort()
raise e if abrt
end
end
end