Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

Commit

Permalink
Introduce Response.apportion and use it in Channel#respond_to_names
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanfb committed Feb 22, 2010
1 parent 6d77810 commit f55483d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/hector/channel.rb
Expand Up @@ -73,7 +73,8 @@ def respond_to_topic(session)
end

def respond_to_names(session)
session.respond_with(353, session.nickname, "=", name, :source => "hector.irc", :text => nicknames.join(" "))
names_responses = Response.apportion(nicknames, 353, session.nickname, "=", name, :source => "hector.irc")
names_responses.each {|nr| session.respond_with(nr)}
session.respond_with(366, session.nickname, name, :source => "hector.irc", :text => "End of /NAMES list.")
end

Expand Down
21 changes: 19 additions & 2 deletions lib/hector/response.rb
@@ -1,6 +1,7 @@
module Hector
class Response
attr_reader :command, :args, :text, :source
attr_reader :command, :args, :source
attr_accessor :text

def initialize(command, *args)
@command = command.to_s.upcase
Expand All @@ -12,12 +13,28 @@ def initialize(command, *args)
end

def to_s
@to_s ||= [].tap do |line|
[].tap do |line|
line.push(":#{source}") if source
line.push(command)
line.concat(args)
line.push(":#{text}") if text
end.join(" ")[0, 510] + "\r\n"
end

def self.apportion(args, *base_args)
[].tap do |responses|
base_response = Response.new(*base_args)
unprocessed_args = args.reverse
while unprocessed_args.length > 0
this_response_text = []
while (base_response.to_s.length + this_response_text.join(" ").length) < 512
this_response_text << unprocessed_args.pop
end
this_response = base_response.dup
this_response.text = this_response_text.join(" ")
responses << this_response
end
end
end
end
end

0 comments on commit f55483d

Please sign in to comment.