Class: Net::TOC::Client

Inherits:
Object
  • Object
show all
Includes:
Net::TOC
Defined in:
lib/aim/net_toc.rb

Overview

A high-level interface to TOC. It supports asynchronous message handling through the use of threads, and maintains a list of buddies.

Constant Summary

Constants included from Net::TOC

Debug, ErrorCode

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Net::TOC

#format_message, #format_screen_name, new

Constructor Details

#initialize(screen_name, password, &optional_block) ⇒ Client

You must initialize the client with your screen name and password. If a block is given, Client#listen will be invoked with the block after initialization.



433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/aim/net_toc.rb', line 433

def initialize(screen_name, password, &optional_block) # :yields: message, buddy, auto_response, client
  @conn = Connection.new(screen_name)
  @screen_name = format_screen_name(screen_name)
  @password = password
  @callbacks = {}
  @buddy_list = BuddyList.new(@conn)
  add_callback(:config, :config2) { |v| @buddy_list.decode_toc v }
  add_callback(:update_buddy, :update_buddy2) { |v| update_buddy v }
  on_error do | error |
    $stderr.puts "Error: #{error}"
  end
  listen(&optional_block) if block_given?
end

Instance Attribute Details

#buddy_listObject (readonly)

Returns the value of attribute buddy_list.



429
430
431
# File 'lib/aim/net_toc.rb', line 429

def buddy_list
  @buddy_list
end

#screen_nameObject (readonly)

Returns the value of attribute screen_name.



429
430
431
# File 'lib/aim/net_toc.rb', line 429

def screen_name
  @screen_name
end

Instance Method Details

#capabilitiesObject

Returns a list of this client’s capabilities. Not yet implemented.



591
592
593
# File 'lib/aim/net_toc.rb', line 591

def capabilities
  [] # TODO
end

#clear_callbacks!Object



573
574
575
# File 'lib/aim/net_toc.rb', line 573

def clear_callbacks!
  @callbacks = { }
end

#come_backObject

Sets your status to available.



563
564
565
# File 'lib/aim/net_toc.rb', line 563

def come_back
  @conn.toc_set_away
end

#connect(server = "toc.oscar.aol.com", port = 9898, oscar_server = "login.oscar.aol.com", oscar_port = 5190) ⇒ Object

Connects to the server and starts an event-handling thread.



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/aim/net_toc.rb', line 448

def connect(server="toc.oscar.aol.com", port=9898, oscar_server="login.oscar.aol.com", oscar_port=5190)
  @conn.open(server, port)
  code = 7696 * @screen_name[0] * @password[0]
  @conn.toc2_signon(oscar_server, oscar_port, @screen_name, roasted_pass, "english", "\"TIC:toc.rb\"", 160, code)

  @conn.recv do |msg, val|
    if msg == :sign_on
      @conn.toc_add_buddy(@screen_name)
      @conn.toc_init_done
      capabilities.each do |capability|
        @conn.toc_set_caps(capability)
      end
    end
  end
  @thread.kill unless @thread.nil? # ha
  @thread = Thread.new { loop { event_loop } }
end

#disconnectObject

Disconnects and kills the event-handling thread. You may still add callbacks while disconnected.



467
468
469
470
471
# File 'lib/aim/net_toc.rb', line 467

def disconnect
  @thread.kill unless @thread.nil?
  @thread = nil
  @conn.close
end

#go_away(away_message) ⇒ Object

Sets your status to away and away_message as your away message.



558
559
560
# File 'lib/aim/net_toc.rb', line 558

def go_away(away_message)
  @conn.toc_set_away "\"#{away_message.gsub("\"","\\\"")}\""
end

#idle_time=(seconds) ⇒ Object

Sets your idle time in seconds. You only need to set this once; afterwards, the server will keep track itself. Set to 0 to stop being idle.



569
570
571
# File 'lib/aim/net_toc.rb', line 569

def idle_time=(seconds)
  @conn.toc_set_idle seconds
end

#join_chat(room_name) ⇒ Object

remi



512
513
514
# File 'lib/aim/net_toc.rb', line 512

def join_chat room_name
  @conn.toc_chat_join 4, room_name if room_name
end

#keep_track_of_rooms_joinedObject

remi



496
497
498
499
500
501
502
503
504
# File 'lib/aim/net_toc.rb', line 496

def keep_track_of_rooms_joined
  @keeping_track_of_rooms_joined = true
  add_callback(:chat_join) do |val|
    room_id, room_name = *val.split(":")
    puts "joined chat room #{ room_name } [#{ room_id }]"
    @rooms ||= { }
    @rooms[room_id] = room_name # not an object for now, just strings!
  end
end

#keeping_track_of_rooms_joined?Boolean

Returns:

  • (Boolean)


505
506
507
# File 'lib/aim/net_toc.rb', line 505

def keeping_track_of_rooms_joined?
  @keeping_track_of_rooms_joined
end

#listen(*args) ⇒ Object

Connects to the server and forwards received IMs to the given block. See Client#connect for the arguments.



474
475
476
477
478
479
480
# File 'lib/aim/net_toc.rb', line 474

def listen(*args) # :yields: message, buddy, auto_response, client
  on_im do | message, buddy, auto_response |
    yield message, buddy, auto_response, self
  end
  connect(*args)
  wait
end

#on_chatObject

remi Pass a block to be called every time an IM is received. This will replace any previous on_im handler.

Raises:

  • (ArgumentException)


527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'lib/aim/net_toc.rb', line 527

def on_chat
  raise ArgumentException, "on_chat requires a block argument" unless block_given?
  keep_track_of_rooms_joined unless keeping_track_of_rooms_joined?
  add_callback(:chat_in) do |val|
    puts "chat_in val => #{ val.inspect }"
    room_id, screen_name, auto, *message = *val.split(":")
    message = message.join(":")
    message = message.gsub('<br>',"\n") # ... before getting rid of html
    message = message.chomp.gsub(/<[^>]+>/,"") # get rid of html
    message = message.gsub("\n",'<br />') # ... turn newlines back into br's
    buddy = @buddy_list.buddy_named(screen_name)
    room = @rooms[room_id] || room_id
    auto_response = auto == "T"
    yield message, buddy, room, auto_response
  end
end

#on_errorObject

Pass a block to be called every time an error occurs. This will replace any previous on_error handler, including the default exception-raising behavior.

Raises:

  • (ArgumentException)


546
547
548
549
550
551
552
553
554
555
# File 'lib/aim/net_toc.rb', line 546

def on_error
  raise ArgumentException, "on_error requires a block argument" unless block_given?
  add_callback(:error) do |val|
    code, param = *val.split(":")
    error = ErrorCode[code.to_i]
    error = "An unknown error occurred." if error.nil?
    error.gsub!("<param>", param) unless param.nil?
    yield error
  end
end

#on_imObject

Pass a block to be called every time an IM is received. This will replace any previous on_im handler.

Raises:

  • (ArgumentException)


483
484
485
486
487
488
489
490
491
492
# File 'lib/aim/net_toc.rb', line 483

def on_im
  raise ArgumentException, "on_im requires a block argument" unless block_given?
  add_callback(:im_in, :im_in2) do |val|
    screen_name, auto, f2, *message = *val.split(":")
    message = message.join(":")
    buddy = @buddy_list.buddy_named(screen_name)
    auto_response = auto == "T"
    yield message, buddy, auto_response
  end
end

#send_chat(room_name, message) ⇒ Object

remi



517
518
519
520
521
522
523
# File 'lib/aim/net_toc.rb', line 517

def send_chat room_name, message
  room = @rooms.find {|id,name| name == room_name } # end up with nil or [ '1234', 'the_name' ]
  room_id = room.first || room_name
  puts "i wanna send #{ message } to room with name #{ room_name } and therefore, id #{ room_id }"
  message = "\"" + format_message(message) + "\""
  @conn.toc_chat_send room_id, message
end

#wait(limit = nil) ⇒ Object

Waits for the event-handling thread for limit seconds, or indefinitely if no argument is given. Use this to prevent your program from exiting prematurely. For example, the following script will exit right after connecting:

client = Net::TOC.new("screenname", "p455w0rd")
client.connect

To prevent this, use wait:

client = Net::TOC.new("screenname", "p455w0rd")
client.connect
client.wait

Now the program will wait until the client has disconnected before exiting.



586
587
588
# File 'lib/aim/net_toc.rb', line 586

def wait(limit=nil)
  @thread.join limit
end