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

Class Method Details

.configure_authenticatorsObject



37
38
39
40
41
# File 'lib/jschat/http/jschat.rb', line 37

def self.configure_authenticators
  if ServerConfig['twitter']
    JsChat::Auth::Twitter.load
  end
end

.initObject



43
44
45
46
# File 'lib/jschat/http/jschat.rb', line 43

def self.init
  configure_authenticators
  JsChat.init_storage
end

.init_storageObject



10
11
12
13
14
15
16
17
18
# File 'lib/jschat/init.rb', line 10

def self.init_storage
  if JsChat::Storage::MongoDriver.available?
    JsChat::Storage.enabled = true
    JsChat::Storage.driver = JsChat::Storage::MongoDriver
  else
    JsChat::Storage.enabled = false
    JsChat::Storage.driver = JsChat::Storage::NullDriver
  end
end

Instance Method Details

#change(change, options = {}) ⇒ Object



468
469
470
471
472
473
474
475
476
477
# File 'lib/jschat/server.rb', line 468

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_clientObject



405
406
407
# File 'lib/jschat/server.rb', line 405

def current_stateless_client
  @@stateless_cookies.find { |c| c[:cookie] == @stateless_cookie }
end

#disconnect_lagged_usersObject



426
427
428
429
430
431
432
# File 'lib/jschat/server.rb', line 426

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



444
445
446
447
448
449
450
451
# File 'lib/jschat/server.rb', line 444

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_ipObject



507
508
509
# File 'lib/jschat/server.rb', line 507

def get_remote_ip
  Socket.unpack_sockaddr_in(get_peername)[1]
end

#identify(name, ip, session_length, options = {}) ⇒ Object

“identify”:“alex”



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/jschat/server.rb', line 263

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



365
366
367
368
369
370
371
372
# File 'lib/jschat/server.rb', line 365

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

Returns:

  • (Boolean)


434
435
436
# File 'lib/jschat/server.rb', line 434

def lagged?(time)
  Time.now.utc - time > STATELESS_TIMEOUT
end

#lastlog(room, options = {}) ⇒ Object



280
281
282
283
284
285
286
287
# File 'lib/jschat/server.rb', line 280

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



479
480
481
482
483
484
485
486
# File 'lib/jschat/server.rb', line 479

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_userObject



417
418
419
420
421
422
423
424
# File 'lib/jschat/server.rb', line 417

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



459
460
461
462
463
464
465
466
# File 'lib/jschat/server.rb', line 459

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

Returns:

  • (Boolean)


258
259
260
# File 'lib/jschat/server.rb', line 258

def name_taken?(name)
  users_with_names.find { |user| user.name.downcase == name.downcase }
end

#names(room_name, options = {}) ⇒ Object



383
384
385
386
387
388
389
390
# File 'lib/jschat/server.rb', line 383

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


392
393
394
395
# File 'lib/jschat/server.rb', line 392

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



374
375
376
377
378
379
380
381
# File 'lib/jschat/server.rb', line 374

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



317
318
319
320
321
322
323
324
325
326
# File 'lib/jschat/server.rb', line 317

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
    # TODO: HANDLE PING OUTS
    Error.new(:ping_out, 'Your connection has been lost')
  end
end

#post_initObject



453
454
455
456
457
# File 'lib/jschat/server.rb', line 453

def post_init
  @@users ||= []
  @@stateless_cookies ||= []
  @user = User.new(self)
end


575
576
577
578
579
580
# File 'lib/jschat/server.rb', line 575

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



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

def private_message(message, options)
  user = users_with_names.find { |u| u.name.downcase == options['to'].downcase }
  if user
    # Return the message to the user, and send it to the other person too
    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



328
329
330
331
332
# File 'lib/jschat/server.rb', line 328

def quit(message, options = {})
  if @user
    disconnect_user @user
  end
end

#receive_line(data) ⇒ Object



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
573
# File 'lib/jschat/server.rb', line 511

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|
    # Receive the identify request
    input = JSON.parse line 

    @user.seen!

    # Unbind when a stateless connection doesn't match the cookie
    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_clientObject



397
398
399
400
401
402
403
# File 'lib/jschat/server.rb', line 397

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_userObject



409
410
411
# File 'lib/jschat/server.rb', line 409

def register_stateless_user
  current_stateless_client[:user] = @user
end

#room_message(message, options) ⇒ Object



334
335
336
337
338
339
340
341
# File 'lib/jschat/server.rb', line 334

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



289
290
291
292
293
294
295
296
# File 'lib/jschat/server.rb', line 289

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



355
356
357
358
359
360
361
362
363
# File 'lib/jschat/server.rb', line 355

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



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/jschat/server.rb', line 488

def send_response(data)
  response = ''
  case data
    when String
      response = data
    when Error
      response = data.to_json + "\n"
      log :error, data.message
    else
      # Other objects should be safe for to_json
      response = data.to_json + "\n"
      log :info, response.strip
  end
  
  send_data response
end

#since(room, options = {}) ⇒ Object



298
299
300
301
302
303
304
305
306
307
# File 'lib/jschat/server.rb', line 298

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



309
310
311
312
313
314
315
# File 'lib/jschat/server.rb', line 309

def times(message, options = {})
  times = {}
  @user.rooms.each do |room|
    times[room.name] = room.last_update_time
  end
  times
end

#unbindObject



438
439
440
441
442
# File 'lib/jschat/server.rb', line 438

def unbind
  return if @stateless
  disconnect_user(@user)
  @user = nil
end

#users_with_namesObject

User initially has a nil name



254
255
256
# File 'lib/jschat/server.rb', line 254

def users_with_names
  @@users.find_all { |u| u.name }
end

#valid_stateless_user?Boolean

Returns:

  • (Boolean)


413
414
415
# File 'lib/jschat/server.rb', line 413

def valid_stateless_user?
  current_stateless_client 
end