Class: EventMachine::IRC::ConnectedClient

Inherits:
Object
  • Object
show all
Defined in:
lib/eventmachine/irc/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ ConnectedClient

Returns a new instance of ConnectedClient.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/eventmachine/irc/server.rb', line 54

def initialize(server)
  @server = server
  @channels = Array.new
  @nick = nil
  @user = nil
  @pass = nil
  @last_ping = Time.now
  @last_pong = Time.now
  @state = {}
  @welcomed = false
  @nick_tries = 0
end

Instance Attribute Details

#channelsObject (readonly)

Returns the value of attribute channels.



52
53
54
# File 'lib/eventmachine/irc/server.rb', line 52

def channels
  @channels
end

#nickObject (readonly)

Returns the value of attribute nick.



52
53
54
# File 'lib/eventmachine/irc/server.rb', line 52

def nick
  @nick
end

#realnameObject (readonly)

Returns the value of attribute realname.



52
53
54
# File 'lib/eventmachine/irc/server.rb', line 52

def realname
  @realname
end

#stateObject (readonly)

Returns the value of attribute state.



52
53
54
# File 'lib/eventmachine/irc/server.rb', line 52

def state
  @state
end

#userObject (readonly)

Returns the value of attribute user.



52
53
54
# File 'lib/eventmachine/irc/server.rb', line 52

def user
  @user
end

Instance Method Details

#handle_abortObject



446
447
448
# File 'lib/eventmachine/irc/server.rb', line 446

def handle_abort()
	handle_quit('aborted..')
end

#handle_away(msg) ⇒ Object



356
357
358
359
360
361
362
363
364
365
# File 'lib/eventmachine/irc/server.rb', line 356

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

#handle_connectObject



463
464
465
# File 'lib/eventmachine/irc/server.rb', line 463

def handle_connect
	reply :raw, "NOTICE AUTH :#{Server.config['version']} initialized, welcome."
end

#handle_eval(s) ⇒ Object



454
455
456
# File 'lib/eventmachine/irc/server.rb', line 454

def handle_eval(s)
	reply :raw, eval(s)
end

#handle_join(channels) ⇒ Object



81
82
83
# File 'lib/eventmachine/irc/server.rb', line 81

def handle_join(channel)
  @channels << channel
end

#handle_list(channel) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/eventmachine/irc/server.rb', line 367

def handle_list(channel)
	reply :numeric, LISTSTART
	case channel.strip
	when /^#/
		channel.split(/,/).each {|cname|
			c = Server.channel_store[cname.strip]
			reply :numeric, 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
		Server.channel_store.each_channel {|c|
			reply :numeric, LIST, c.name, c.topic
		}
	end
	reply :numeric, LISTEND
end

#handle_mode(target, rest) ⇒ Object



429
430
431
432
# File 'lib/eventmachine/irc/server.rb', line 429

def handle_mode(target, rest)
	#TODO: dummy
	reply :mode, target, rest
end

#handle_names(channels, server) ⇒ Object



403
404
405
# File 'lib/eventmachine/irc/server.rb', line 403

def handle_names(channels, server)
	channels.split(/,/).each {|ch| send_nameslist(ch.strip) }
end

#handle_newconnect(nick) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/eventmachine/irc/server.rb', line 137

def handle_newconnect(nick)
	@alive = true
	@nick = nick
	@host = Server.config['hostname']
	@ver = Server.config['version']
	@starttime = Server.config['starttime']
	send_welcome if !@user.nil?
end

#handle_nick(nick) ⇒ Object



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
# File 'lib/eventmachine/irc/server.rb', line 85

def handle_nick(nick)
carp "nick => #{nick}"
if Server.user_store[nick].nil?
	userlist = {}
	if @nick.nil?
		handle_newconnect(nick)
	else
		userlist[nick] = self if self.nick != nick
		Server.user_store.delete(@nick)
		@nick = nick
	end

	Server.user_store << self

	#send the info to the world
	#get unique users.
	@channels.each { |c|
		Server.channel_store[c].each_user { |u|
			userlist[u.nick] = u
		}
	}
	userlist.values.each {|user|
		user.reply :nick, nick
	}
	@usermsg = ":#{@nick}!~#{@user}@#{@peername}"
else
	#check if we are just nicking ourselves.
	unless Server.user_store[nick] == self
		#verify the connectivity of earlier guy
	reply :numeric, ERR_NICKNAMEINUSE, "* #{nick} ", "Nickname is already in use."
	@nick_tries += 1
	if @nick_tries > $config['nick-tries']
		carp "kicking spurious user #{nick} after #{@nick_tries} tries"
		handle_abort
	end
	end
end
@nick_tries = 0
end

#handle_notice(target, msg) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/eventmachine/irc/server.rb', line 300

def handle_notice(target, msg)
	case target.strip
	when CHANNEL
		channel= Server.channel_store[target]
		if !channel.nil?
			channel.notice(msg, self)
		else
			send_nonick(target)
		end
	else
		user = Server.user_store[target]
		if !user.nil?
			user.reply :notice, self.userprefix, user.nick, msg
		else
			send_nonick(target)
		end
	end
end

#handle_part(channel, msg) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
# File 'lib/eventmachine/irc/server.rb', line 319

def handle_part(channel, msg)
	if Server.channel_store.channels.include? channel
		if Server.channel_store[channel].part(self, msg)
			@channels.delete(channel)
		else
			send_notonchannel channel
		end
	else
		send_nochannel channel
	end
end

#handle_pass(pass) ⇒ Object



146
147
148
# File 'lib/eventmachine/irc/server.rb', line 146

def handle_pass(pass)
  @pass = pass
end

#handle_ping(pingmsg, rest) ⇒ Object



270
271
272
# File 'lib/eventmachine/irc/server.rb', line 270

def handle_ping(pingmsg, rest)
	reply :pong, pingmsg
end

#handle_pong(srv) ⇒ Object



274
275
276
# File 'lib/eventmachine/irc/server.rb', line 274

def handle_pong(srv)
	carp "got pong: #{srv}"
end

#handle_privmsg(target, msg) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/eventmachine/irc/server.rb', line 278

def handle_privmsg(target, msg)
	case target.strip
	when CHANNEL
		channel= Server.channel_store[target]
		if !channel.nil?
			channel.privatemsg(msg, self)
		else
			send_nonick(target)
		end
	else
		user = Server.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

#handle_quit(msg) ⇒ Object



331
332
333
334
335
336
337
338
339
340
341
# File 'lib/eventmachine/irc/server.rb', line 331

def handle_quit(msg)
	#do this to avoid double quit due to 2 threads.
	return if !@alive
	@alive = false
	@channels.each do |channel|
		Server.channel_store[channel].quit(self, msg)
	end
	Server.user_store.delete(self.nick)
	carp "#{self.nick} #{msg}"
   @server.close_connection
end

#handle_reload(password) ⇒ Object



443
444
# File 'lib/eventmachine/irc/server.rb', line 443

def handle_reload(password)
end

#handle_topic(channel, topic) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/eventmachine/irc/server.rb', line 343

def handle_topic(channel, topic)
	carp "handle topic for #{channel}:#{topic}"
	if topic.nil? or topic =~ /^ *$/
		send_topic(channel)
	else
		begin
			Server.channel_store[channel].topic(topic,self)
		rescue Exception => e
			carp e
		end
	end
end

#handle_unknown(s) ⇒ Object



458
459
460
461
# File 'lib/eventmachine/irc/server.rb', line 458

def handle_unknown(s)
	carp "unknown:>#{s}<"
	reply :numeric, ERR_UNKNOWNCOMMAND,s, "Unknown command"
end

#handle_user(user, mode, unused, realname) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/eventmachine/irc/server.rb', line 125

def handle_user(user, mode, unused, realname)
  @user = user
  @mode = mode
  @realname = realname
  @usermsg = ":#{@nick}!~#{@user}@#{@peername}"
  send_welcome if !@nick.nil?
end

#handle_userhost(nicks) ⇒ Object



434
435
436
437
438
439
440
441
# File 'lib/eventmachine/irc/server.rb', line 434

def handle_userhost(nicks)
	info = []
	nicks.split(/,/).each {|nick|
		user = Server.user_store[nick]
		info << user.nick + '=-' + user.nick + '@' + user.peer
	}
	reply :numeric, USERHOST,"", info.join(' ')
end

#handle_versionObject



450
451
452
# File 'lib/eventmachine/irc/server.rb', line 450

def handle_version()
	reply :numeric, VERSION,"#{Server.config['version']} Ruby IRCD", ""
end

#handle_who(mask, rest) ⇒ Object



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/eventmachine/irc/server.rb', line 407

def handle_who(mask, rest)
	channel = Server.channel_store[mask]
	hopcount = 0
	if channel.nil?
		#match against all users
		Server.user_store.each_user {|user|
			reply :numeric, WHOREPLY ,
			"#{user.channels[0]} #{user.userprefix} #{user.host} #{Server.config['hostname']} #{user.nick} H" , 
			"#{hopcount} #{user.realname}" if File.fnmatch?(mask, "#{user.host}.#{user.realname}.#{user.nick}")
		}
		reply :numeric, ENDOFWHO, mask, "End of /WHO list."
	else
		#get all users in the channel
		channel.each_user {|user|
			reply :numeric, WHOREPLY ,
			"#{mask} #{user.userprefix} #{user.host} #{Server.config['hostname']} #{user.nick} H" , 
			"#{hopcount} #{user.realname}"
		}
		reply :numeric, ENDOFWHO, mask, "End of /WHO list."
	end
end

#handle_whois(target, nicks) ⇒ Object



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/eventmachine/irc/server.rb', line 386

def handle_whois(target,nicks)
	#ignore target for now.
	return reply(:numeric, NONICKNAMEGIVEN, "", "No nickname given") if nicks.strip.length == 0
	nicks.split(/,/).each {|nick|
		nick.strip!
		user = Server.user_store[nick]
		if user
			reply :numeric, WHOISUSER, "#{user.nick} #{user.user} #{user.host} *", "#{user.realname}"
			reply :numeric, WHOISCHANNELS, user.nick, "#{user.channels.join(' ')}"
			repl_away user.nick, user.state[:away] if !user.state[:away].nil?
			reply :numeric, ENDOFWHOIS, user.nick, "End of /WHOIS list"
		else
			return send_nonick(nick) 
		end
	}
end

#hostObject



67
68
69
70
# File 'lib/eventmachine/irc/server.rb', line 67

def host
  # TODO: figure out how to do this with event machine
  return @peername
end

#modeObject



133
134
135
# File 'lib/eventmachine/irc/server.rb', line 133

def mode
  return @mode
end

#names(channel) ⇒ Object



229
230
231
# File 'lib/eventmachine/irc/server.rb', line 229

def names(channel)
	return Server.channel_store[channel].nicks
end

#raw(arg, abrt = false) ⇒ Object



513
514
515
516
517
518
519
520
521
522
523
# File 'lib/eventmachine/irc/server.rb', line 513

def raw(arg, abrt=false)
	begin
		carp "--> #{arg}"
		@server.send_data(arg.chomp + "\n") if !arg.nil?
	rescue Exception => e
		carp "<#{self.userprefix}>#{e.message}"
		#carp e.backtrace.join("\n")
		handle_abort()
		raise e if abrt
	end
end

#readyObject



77
78
79
# File 'lib/eventmachine/irc/server.rb', line 77

def ready
  return (!@pass.nil? && !@nick.nil?)
end

#receive_data(data) ⇒ Object



525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/eventmachine/irc/server.rb', line 525

def receive_data(data)
carp "<-- '#{data.strip}'"
s = if data =~ PREFIX
	$1
else
	data
end
case s
when /^[ ]*$/
	return
when /^PASS +(.+)$/i
	handle_pass($1.strip)
when /^NICK +(.+)$/i
	handle_nick($1.strip) #done
when /^USER +([^ ]+) +([0-9]+) +([^ ]+) +:(.*)$/i
	handle_user($1, $2, $3, $4) #done
when /^USER +([^ ]+) +([0-9]+) +([^ ]+) +:*(.*)$/i
	#opera does this.
	handle_user($1, $2, $3, $4) #done
when /^USER ([^ ]+) +[^:]*:(.*)/i
	#chatzilla does this.
	handle_user($1, '', '', $3) #done
when /^JOIN +(.+)$/i
	handle_join($1) #done
when /^PING +([^ ]+) *(.*)$/i
	handle_ping($1, $2) #done
when /^PONG +:(.+)$/i , /^PONG +(.+)$/i
	handle_pong($1)
when /^PRIVMSG +([^ ]+) +:(.*)$/i
	handle_privmsg($1, $2) #done
when /^NOTICE +([^ ]+) +(.*)$/i
	handle_notice($1, $2) #done
when /^PART :+([^ ]+) *(.*)$/i  
	#some clients require this.
	handle_part($1, $2) #done
when /^PART +([^ ]+) *(.*)$/i
	handle_part($1, $2) #done
when /^QUIT :(.*)$/i
	handle_quit($1) #done
when /^QUIT *(.*)$/i
	handle_quit($1) #done
when /^TOPIC +([^ ]+) *:*(.*)$/i
	handle_topic($1, $2) #done
when /^AWAY +:(.*)$/i
	handle_away($1)
when /^AWAY +(.*)$/i #for opera
	handle_away($1)
when /^:*([^ ])* *AWAY *$/i
	handle_away(nil)
  when /^AWAY\s*$/i
    handle_away(nil)
when /^LIST *(.*)$/i
	handle_list($1)
when /^WHOIS +([^ ]+) +(.+)$/i
	handle_whois($1,$2)
when /^WHOIS +([^ ]+)$/i
	handle_whois(nil,$1)
when /^WHO +([^ ]+) *(.*)$/i
	handle_who($1, $2)
when /^NAMES +([^ ]+) *(.*)$/i
	handle_names($1, $2)
when /^MODE +([^ ]+) *(.*)$/i
	handle_mode($1, $2)
when /^USERHOST +:(.+)$/i
	#besirc does this (not accourding to RFC 2812)
	handle_userhost($1)
when /^USERHOST +(.+)$/i
	handle_userhost($1)
when /^RELOAD +(.+)$/i
	handle_reload($1)
when /^VERSION *$/i
	handle_version()
when /^EVAL (.*)$/i
	#strictly for debug
	handle_eval($1)
else
	handle_unknown(s)
end
end

#repl_away(nick, msg) ⇒ Object



188
189
190
# File 'lib/eventmachine/irc/server.rb', line 188

def repl_away(nick, msg)
	reply :numeric, AWAY, nick, msg
end

#repl_bounce(sever, port) ⇒ Object



179
180
181
# File 'lib/eventmachine/irc/server.rb', line 179

def repl_bounce(sever, port)
	reply :numeric, BOUNCE ,"Try server #{server}, port #{port}"
end

#repl_createdObject



171
172
173
# File 'lib/eventmachine/irc/server.rb', line 171

def repl_created
	reply :numeric, CREATED, @nick, "This server was created #{@starttime}"
end

#repl_isonObject



183
184
185
186
# File 'lib/eventmachine/irc/server.rb', line 183

def repl_ison()
	#XXX TODO
	reply :numeric, ISON,"notimpl"
end

#repl_modeObject



206
207
# File 'lib/eventmachine/irc/server.rb', line 206

def repl_mode()
end

#repl_motdObject



200
201
202
203
204
# File 'lib/eventmachine/irc/server.rb', line 200

def repl_motd()
	reply :numeric, MOTDSTART,'', "- Message of the Day"
	reply :numeric, MOTD,'',      "- Do the dance see the source"
	reply :numeric, ENDOFMOTD,'', "- End of /MOTD command."
end

#repl_myinfoObject



175
176
177
# File 'lib/eventmachine/irc/server.rb', line 175

def repl_myinfo
	reply :numeric, MYINFO, @nick, "#{@host} #{@ver} #{@server.usermodes} #{@server.channelmodes}"
end

#repl_nowawayObject



196
197
198
# File 'lib/eventmachine/irc/server.rb', line 196

def repl_nowaway()
	reply :numeric, NOWAWAY, @nick,"You have been marked as being away"
end

#repl_unawayObject



192
193
194
# File 'lib/eventmachine/irc/server.rb', line 192

def repl_unaway()
	reply :numeric, UNAWAY, @nick,"You are no longer marked as being away"
end

#repl_welcomeObject



162
163
164
165
# File 'lib/eventmachine/irc/server.rb', line 162

def repl_welcome
	client = "#{@nick}!#{@user}@#{@peername}"
	reply :numeric, WELCOME, @nick, "Welcome to this IRC server #{client}"
end

#repl_yourhostObject



167
168
169
# File 'lib/eventmachine/irc/server.rb', line 167

def repl_yourhost
	reply :numeric, YOURHOST, @nick, "Your host is #{@host}, running version #{@ver}"
end

#reply(method, *args) ⇒ Object



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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/eventmachine/irc/server.rb', line 467

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 = Server.config['hostname']
		raw ":#{server} #{'%03d'%numeric} #{@nick} #{msg} :#{detail}"
	end
end

#send_nameslist(channel) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/eventmachine/irc/server.rb', line 233

def send_nameslist(channel)
	c =  Server.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, NAMREPLY,"= #{c.name}","#{names.join(' ')}"
	reply :numeric, ENDOFNAMES,"#{c.name} ","End of /NAMES list."
end

#send_nochannel(channel) ⇒ Object



221
222
223
# File 'lib/eventmachine/irc/server.rb', line 221

def send_nochannel(channel)
	reply :numeric, ERR_NOSUCHCHANNEL, channel, "That channel doesn't exist"
end

#send_nonick(nick) ⇒ Object



217
218
219
# File 'lib/eventmachine/irc/server.rb', line 217

def send_nonick(nick)
	reply :numeric, ERR_NOSUCHNICK, nick, "No such nick/channel"
end

#send_notonchannel(channel) ⇒ Object



225
226
227
# File 'lib/eventmachine/irc/server.rb', line 225

def send_notonchannel(channel)
	reply :numeric, ERR_NOTONCHANNEL, channel, "Not a member of that channel"
end

#send_pingObject



247
248
249
# File 'lib/eventmachine/irc/server.rb', line 247

def send_ping()
	reply :ping, "#{Server.config['hostname']}"
end

#send_topic(channel) ⇒ Object



209
210
211
212
213
214
215
# File 'lib/eventmachine/irc/server.rb', line 209

def send_topic(channel)
	if Server.channel_store[channel]
		reply :numeric, TOPIC,channel, "#{Server.channel_store[channel].topic}" 
	else
		send_notonchannel channel
	end
end

#send_welcomeObject



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/eventmachine/irc/server.rb', line 150

def send_welcome
	if !@welcomed
		repl_welcome
		repl_yourhost
		repl_created
		repl_myinfo
		repl_motd
		repl_mode
		@welcomed = true
	end
end

#userprefixObject



72
73
74
75
# File 'lib/eventmachine/irc/server.rb', line 72

def userprefix
  # Where is this defined?
  return @usermsg
end