Permalink
Browse files

Introduce Response.apportion and use it in Channel#respond_to_names

  • Loading branch information...
1 parent 6d77810 commit f55483db1fbb1931d6ca8dfb8e69f1e05270eef1 @ryanfb ryanfb committed Feb 22, 2010
Showing with 21 additions and 3 deletions.
  1. +2 −1 lib/hector/channel.rb
  2. +19 −2 lib/hector/response.rb
View
@@ -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
View
@@ -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
@@ -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.