Class: Net::TOC::BuddyList

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

Overview

Manages groups and buddies. Don’t create one yourself - get one using Client#buddy_list.

Constant Summary

Constants included from Net::TOC

Debug, ErrorCode

Instance Method Summary collapse

Methods included from Net::TOC

#format_message, #format_screen_name, new

Constructor Details

#initialize(conn) ⇒ BuddyList

:nodoc:



342
343
344
345
346
347
# File 'lib/aim/net_toc.rb', line 342

def initialize(conn) # :nodoc:
  @conn = conn
  @buddies = {}
  @groups = {}
  @group_order = []
end

Instance Method Details

#add_buddy(group, buddy_name, sync = :sync) ⇒ Object

Adds the buddy named buddy_name to the group named group. If this group does not exist, it is created. Setting sync to :dont_sync will prevent this change from being sent to the server.



381
382
383
384
385
# File 'lib/aim/net_toc.rb', line 381

def add_buddy(group, buddy_name, sync=:sync)
  add_group(group, sync) if @groups[group].nil?
  @groups[group] << buddy_named(buddy_name)
  @conn.toc2_new_buddies("{g:#{group}\nb:#{format_screen_name(buddy_name)}\n}") if sync == :sync
end

#add_group(group_name, sync = :sync) ⇒ Object

Adds a new group named group_name. Setting sync to :dont_sync will prevent this change from being sent to the server.



371
372
373
374
375
376
377
# File 'lib/aim/net_toc.rb', line 371

def add_group(group_name, sync=:sync)
  if @groups[group_name].nil?
    @groups[group_name] = []
    @group_order << group_name
    @conn.toc2_new_group group_name if sync == :sync
  end
end

#buddy_named(name) ⇒ Object

Returns the buddy named name. If the buddy does not exist, it is created. name is not case- or whitespace-sensitive.



398
399
400
401
402
403
404
405
406
# File 'lib/aim/net_toc.rb', line 398

def buddy_named(name)
  formatted_name = format_screen_name(name)
  buddy = @buddies[formatted_name]
  if buddy.nil?
    buddy = Buddy.new(name, @conn)
    @buddies[formatted_name] = buddy
  end
  buddy
end

#decode_toc(val) ⇒ Object

Decodes the buddy list from raw CONFIG data.



409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/aim/net_toc.rb', line 409

def decode_toc(val) # :nodoc:
  current_group = nil
  val.each_line do | line |
    letter, name = *line.split(":")
    name = name.chomp
    case letter
    when "g"
      add_group(name, :dont_sync)
      current_group = name
    when "b"
      add_buddy(current_group, name, :dont_sync)
    end
  end
end

#each_groupObject

Calls the passed block once for each group, passing the group name and the list of buddies as parameters.



362
363
364
365
366
367
# File 'lib/aim/net_toc.rb', line 362

def each_group
  @group_order.each do | group |
    buddies = @groups[group]
    yield group, buddies
  end
end

#remove_buddy(group, buddy_name, sync = :sync) ⇒ Object

Removes the buddy named buddy_name from the group named group. Setting sync to :dont_sync will prevent this change from being sent to the server.



389
390
391
392
393
394
395
# File 'lib/aim/net_toc.rb', line 389

def remove_buddy(group, buddy_name, sync=:sync)
  unless @groups[group].nil?
    buddy = buddy_named(buddy_name)
    @groups[group].reject! { | b | b == buddy }
    @conn.toc2_remove_buddy(format_screen_name(buddy_name), group) if sync == :sync
  end
end

#to_sObject

Constructs a printable string representation of the buddy list.



350
351
352
353
354
355
356
357
358
359
# File 'lib/aim/net_toc.rb', line 350

def to_s
  s = ""
  each_group do | group, buddies |
    s << "== #{group} ==\n"
    buddies.each do | buddy |
      s << " * #{buddy}\n"
    end
  end
  s
end