Module: JsChat
- Includes:
- EM::Protocols::LineText2
- Defined in:
- lib/jschat/init.rb,
lib/jschat/init.rb,
lib/jschat/client.rb,
lib/jschat/errors.rb,
lib/jschat/server.rb,
lib/jschat/http/jschat.rb,
lib/jschat/flood_protection.rb
Defined Under Namespace
Modules: Auth, Errors, FloodProtection, Server, Storage
Classes: Bridge, ConnectionError, Error, Protocol, Room, User
Constant Summary
collapse
- STATELESS_TIMEOUT =
60
- LASTLOG_DEFAULT =
100
Class Method Summary
collapse
Instance Method Summary
collapse
-
#change(change, options = {}) ⇒ Object
-
#current_stateless_client ⇒ Object
-
#disconnect_lagged_users ⇒ Object
-
#disconnect_user(user) ⇒ Object
-
#get_remote_ip ⇒ Object
-
#identify(name, ip, session_length, options = {}) ⇒ Object
-
#join(room_name, options = {}) ⇒ Object
-
#lagged?(time) ⇒ Boolean
-
#lastlog(room, options = {}) ⇒ Object
-
#list(list, options = {}) ⇒ Object
-
#load_stateless_user ⇒ Object
-
#log(level, message) ⇒ Object
-
#name_taken?(name) ⇒ Boolean
-
#names(room_name, options = {}) ⇒ Object
-
#new_cookie ⇒ Object
-
#part(room_name, options = {}) ⇒ Object
-
#ping(message, options = {}) ⇒ Object
-
#post_init ⇒ Object
-
#print_call_stack(from = 0, to = 10) ⇒ Object
-
#private_message(message, options) ⇒ Object
-
#quit(message, options = {}) ⇒ Object
-
#receive_line(data) ⇒ Object
-
#register_stateless_client ⇒ Object
-
#register_stateless_user ⇒ Object
-
#room_message(message, options) ⇒ Object
-
#search(query, options = {}) ⇒ Object
-
#send_message(message, options) ⇒ Object
-
#send_response(data) ⇒ Object
-
#since(room, options = {}) ⇒ Object
-
#times(message, options = {}) ⇒ Object
-
#unbind ⇒ Object
-
#users_with_names ⇒ Object
User initially has a nil name.
-
#valid_stateless_user? ⇒ Boolean
Class Method Details
.init ⇒ Object
43
44
45
46
|
# File 'lib/jschat/http/jschat.rb', line 43
def self.init
configure_authenticators
JsChat.init_storage
end
|
Instance Method Details
#change(change, options = {}) ⇒ Object
467
468
469
470
471
472
473
474
475
476
|
# File 'lib/jschat/server.rb', line 467
def change(change, options = {})
if change == 'user'
field, value = @user.send :change, options[change]
{ 'display' => 'notice', 'notice' => "Your #{field} has been changed to: #{value}" }
else
Error.new(:invalid_request, 'Invalid change request')
end
rescue JsChat::Errors::InvalidName => exception
exception
end
|
#current_stateless_client ⇒ Object
404
405
406
|
# File 'lib/jschat/server.rb', line 404
def current_stateless_client
@@stateless_cookies.find { |c| c[:cookie] == @stateless_cookie }
end
|
#disconnect_lagged_users ⇒ Object
425
426
427
428
429
430
431
|
# File 'lib/jschat/server.rb', line 425
def disconnect_lagged_users
@@stateless_cookies.delete_if do |cookie|
if cookie[:user].session_expired?
lagged?(cookie[:user].last_poll) ? disconnect_user(cookie[:user]) && true : false
end
end
end
|
#disconnect_user(user) ⇒ Object
443
444
445
446
447
448
449
450
|
# File 'lib/jschat/server.rb', line 443
def disconnect_user(user)
log :info, "Removing a connection"
Room.find(user).each do |room|
room.quit_notice user
end
@@users.delete_if { |u| u == user }
end
|
#get_remote_ip ⇒ Object
506
507
508
|
# File 'lib/jschat/server.rb', line 506
def get_remote_ip
Socket.unpack_sockaddr_in(get_peername)[1]
end
|
#identify(name, ip, session_length, options = {}) ⇒ Object
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
# File 'lib/jschat/server.rb', line 262
def identify(name, ip, session_length, options = {})
if @user and @user.identified
Error.new :already_identified, 'You have already identified'
elsif name_taken? name
Error.new :name_taken, 'Name already taken'
else
@user.name = name
@user.ip = ip
@user.session_length = session_length
@user.update_session_expiration
register_stateless_user if @stateless
{ 'display' => 'identified', 'identified' => @user }
end
rescue JsChat::Errors::InvalidName => exception
exception
end
|
#join(room_name, options = {}) ⇒ Object
364
365
366
367
368
369
370
371
|
# File 'lib/jschat/server.rb', line 364
def join(room_name, options = {})
if Room.valid_name? room_name
room = Room.find_or_create(room_name)
room.join @user
else
Error.new(:invalid_room, 'Invalid room name')
end
end
|
#lagged?(time) ⇒ Boolean
433
434
435
|
# File 'lib/jschat/server.rb', line 433
def lagged?(time)
Time.now.utc - time > STATELESS_TIMEOUT
end
|
#lastlog(room, options = {}) ⇒ Object
279
280
281
282
283
284
285
286
|
# File 'lib/jschat/server.rb', line 279
def lastlog(room, options = {})
room = Room.find room
if room and room.users.include? @user
room.lastlog
else
Error.new(:not_in_room, "Please join this room first")
end
end
|
#list(list, options = {}) ⇒ Object
478
479
480
481
482
483
484
485
|
# File 'lib/jschat/server.rb', line 478
def list(list, options = {})
case list
when 'rooms'
@user.rooms.collect { |room| room.name }
else
Error.new(:invalid_request, 'Invalid list command')
end
end
|
#load_stateless_user ⇒ Object
416
417
418
419
420
421
422
423
|
# File 'lib/jschat/server.rb', line 416
def load_stateless_user
if client = current_stateless_client
@user = client[:user]
@stateless = true
else
raise JsChat::Errors::InvalidCookie.new(:invalid_cookie, 'Invalid cookie')
end
end
|
#log(level, message) ⇒ Object
458
459
460
461
462
463
464
465
|
# File 'lib/jschat/server.rb', line 458
def log(level, message)
if Object.const_defined? :ServerConfig and ServerConfig['logger']
if @user
message = "#{@user.name} (#{@user.ip}): #{message}"
end
ServerConfig['logger'].send level, message
end
end
|
#name_taken?(name) ⇒ Boolean
257
258
259
|
# File 'lib/jschat/server.rb', line 257
def name_taken?(name)
users_with_names.find { |user| user.name.downcase == name.downcase }
end
|
#names(room_name, options = {}) ⇒ Object
382
383
384
385
386
387
388
389
|
# File 'lib/jschat/server.rb', line 382
def names(room_name, options = {})
room = Room.find(room_name)
if room
{ 'display' => 'names', 'names' => room.users, 'room' => room.name }
else
Error.new(:room_not_available, 'No such room')
end
end
|
#new_cookie ⇒ Object
391
392
393
394
|
# File 'lib/jschat/server.rb', line 391
def new_cookie
chars = ("a".."z").to_a + ("1".."9").to_a
Array.new(8, '').collect { chars[rand(chars.size)] }.join
end
|
#part(room_name, options = {}) ⇒ Object
373
374
375
376
377
378
379
380
|
# File 'lib/jschat/server.rb', line 373
def part(room_name, options = {})
room = @user.rooms.find { |r| r.name == room_name }
if room
room.part @user
else
Error.new(:not_in_room, "You are not in that room")
end
end
|
#ping(message, options = {}) ⇒ Object
316
317
318
319
320
321
322
323
324
325
|
# File 'lib/jschat/server.rb', line 316
def ping(message, options = {})
if @user and @user.last_poll and Time.now.utc > @user.last_poll
time = Time.now.utc
@user.update_session_expiration
{ 'pong' => time }
else
Error.new(:ping_out, 'Your connection has been lost')
end
end
|
#post_init ⇒ Object
452
453
454
455
456
|
# File 'lib/jschat/server.rb', line 452
def post_init
@@users ||= []
@@stateless_cookies ||= []
@user = User.new(self)
end
|
#print_call_stack(from = 0, to = 10) ⇒ Object
574
575
576
577
578
579
|
# File 'lib/jschat/server.rb', line 574
def print_call_stack(from = 0, to = 10)
puts "Stack:"
(from..to).each do |index|
puts "\t#{caller[index]}"
end
end
|
#private_message(message, options) ⇒ Object
342
343
344
345
346
347
348
349
350
351
352
|
# File 'lib/jschat/server.rb', line 342
def private_message(message, options)
user = users_with_names.find { |u| u.name.downcase == options['to'].downcase }
if user
now = Time.now.utc
user.private_message({ 'message' => message, 'user' => @user.name })
@user.private_message({ 'message' => message, 'user' => @user.name })
else
Error.new(:not_online, 'User not online')
end
end
|
#quit(message, options = {}) ⇒ Object
327
328
329
330
331
|
# File 'lib/jschat/server.rb', line 327
def quit(message, options = {})
if @user
disconnect_user @user
end
end
|
#receive_line(data) ⇒ Object
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
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
|
# File 'lib/jschat/server.rb', line 510
def receive_line(data)
response = ''
disconnect_lagged_users
if data and data.size > ServerConfig['max_message_length']
raise JsChat::Errors::MessageTooLong.new(:message_too_long, 'Message too long')
end
data.chomp.split("\n").each do |line|
input = JSON.parse line
@user.seen!
if input.has_key?('cookie')
@stateless_cookie = input['cookie']
load_stateless_user
end
if input.has_key? 'protocol'
if input['protocol'] == 'stateless'
@stateless = true
response << send_response(register_stateless_client)
end
elsif input.has_key? 'identify'
input['ip'] ||= get_remote_ip
response << send_response(identify(input['identify'], input['ip'], input['session_length']))
else
%w{search lastlog change send join names part since ping list quit times}.each do |command|
if @user.name.nil?
response << send_response(Error.new(:identity_required, 'Identify first'))
return response
end
if input.has_key? command
if command == 'send'
@user.last_activity = Time.now.utc
message_result = send('send_message', input[command], input)
response << message_result if message_result.kind_of? String
else
result = send_response(send(command, input[command], input))
response << result if result.kind_of? String
end
end
end
end
end
response
rescue JsChat::Errors::StillFlooding
""
rescue JsChat::Errors::Flooding => exception
send_response exception
rescue JsChat::Errors::MessageTooLong => exception
send_response exception
rescue JsChat::Errors::InvalidCookie => exception
send_response exception
rescue Exception => exception
puts "Data that raised exception: #{exception}"
p data
print_call_stack
end
|
#register_stateless_client ⇒ Object
396
397
398
399
400
401
402
|
# File 'lib/jschat/server.rb', line 396
def register_stateless_client
@stateless_cookie = new_cookie
user = User.new(self)
@@stateless_cookies << { :cookie => @stateless_cookie, :user => user }
@@users << user
{ 'cookie' => @stateless_cookie }
end
|
#register_stateless_user ⇒ Object
408
409
410
|
# File 'lib/jschat/server.rb', line 408
def register_stateless_user
current_stateless_client[:user] = @user
end
|
#room_message(message, options) ⇒ Object
333
334
335
336
337
338
339
340
|
# File 'lib/jschat/server.rb', line 333
def room_message(message, options)
room = Room.find options['to']
if room and room.users.include? @user
room.send_message({ 'message' => message, 'user' => @user.name })
else
send_response Error.new(:not_in_room, "Please join this room first")
end
end
|
#search(query, options = {}) ⇒ Object
288
289
290
291
292
293
294
295
|
# File 'lib/jschat/server.rb', line 288
def search(query, options = {})
room = Room.find options['room']
if room and room.users.include? @user
room.search query
else
Error.new(:not_in_room, "Please join this room first")
end
end
|
#send_message(message, options) ⇒ Object
354
355
356
357
358
359
360
361
362
|
# File 'lib/jschat/server.rb', line 354
def send_message(message, options)
if options['to'].nil?
send_response Error.new(:to_required, 'Please specify who to send the message to or join a channel')
elsif options['to'][0].chr == '#'
room_message message, options
else
private_message message, options
end
end
|
#send_response(data) ⇒ Object
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
|
# File 'lib/jschat/server.rb', line 487
def send_response(data)
response = ''
case data
when String
response = data
when Error
response = data.to_json + "\n"
log :error, data.message
else
response = data.to_json + "\n"
log :info, response.strip
end
send_data response
end
|
#since(room, options = {}) ⇒ Object
297
298
299
300
301
302
303
304
305
306
|
# File 'lib/jschat/server.rb', line 297
def since(room, options = {})
room = Room.find room
if room and room.users.include? @user
response = room.lastlog(@user.last_poll)
@user.last_poll = Time.now.utc
response
else
Error.new(:not_in_room, "Please join this room first")
end
end
|
#times(message, options = {}) ⇒ Object
308
309
310
311
312
313
314
|
# File 'lib/jschat/server.rb', line 308
def times(message, options = {})
times = {}
@user.rooms.each do |room|
times[room.name] = room.last_update_time
end
times
end
|
#unbind ⇒ Object
437
438
439
440
441
|
# File 'lib/jschat/server.rb', line 437
def unbind
return if @stateless
disconnect_user(@user)
@user = nil
end
|
#users_with_names ⇒ Object
User initially has a nil name
253
254
255
|
# File 'lib/jschat/server.rb', line 253
def users_with_names
@@users.find_all { |u| u.name }
end
|
#valid_stateless_user? ⇒ Boolean
412
413
414
|
# File 'lib/jschat/server.rb', line 412
def valid_stateless_user?
current_stateless_client
end
|