summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/rbot/ircbot.rb8
-rw-r--r--lib/rbot/ircsocket.rb21
2 files changed, 20 insertions, 9 deletions
diff --git a/lib/rbot/ircbot.rb b/lib/rbot/ircbot.rb
index fb33b7f5..d513cb71 100644
--- a/lib/rbot/ircbot.rb
+++ b/lib/rbot/ircbot.rb
@@ -407,8 +407,12 @@ class IrcBot
# relevant say() or notice() methods. This one should be used for IRCd
# extensions you want to use in modules.
def sendmsg(type, where, message)
- # limit it 440 chars + CRLF.. so we have to split long lines
- left = 440 - type.length - where.length - 3
+ # limit it according to the byterate, splitting the message
+ # taking into consideration the actual message length
+ # and all the extra stuff
+ # TODO allow something to do for commands that produce too many messages
+ # TODO example: math 10**10000
+ left = @socket.bytes_per - type.length - where.length - 4
begin
if(left >= message.length)
sendq("#{type} #{where} :#{message}")
diff --git a/lib/rbot/ircsocket.rb b/lib/rbot/ircsocket.rb
index e9fc884f..2a1549a2 100644
--- a/lib/rbot/ircsocket.rb
+++ b/lib/rbot/ircsocket.rb
@@ -22,6 +22,10 @@ module Irc
# accumulator for the throttle
attr_reader :throttle_bytes
+ # byterate components
+ attr_reader :bytes_per
+ attr_reader :seconds_per
+
# delay between lines sent
attr_reader :sendq_delay
@@ -179,9 +183,12 @@ module Irc
end
now = Time.new
if @throttle_bytes > 0
- @throttle_bytes -= (now - @last_throttle)*@bytes_per/@seconds_per
- @throttle_bytes = 0 if @throttle_bytes < 0
- @last_throttle = now
+ delta = ((now - @last_throttle)*@bytes_per/@seconds_per).floor
+ if delta > 0
+ @throttle_bytes -= delta
+ @throttle_bytes = 0 if @throttle_bytes < 0
+ @last_throttle = now
+ end
end
if (now >= (@last_send + @sendq_delay))
# reset burst counter after @sendq_delay has passed
@@ -200,9 +207,8 @@ module Irc
if @throttle_bytes == 0 or mess.length+@throttle_bytes < @bytes_per
puts_critical(@sendq.shift)
else
- debug "(flood protection: breaking at message of length #{mess.length})"
- debug "(Throttle bytes: #{@throttle_bytes})"
- debug "(Byterate: #{byterate})"
+ debug "(flood protection: throttling message of length #{mess.length})"
+ debug "(byterate: #{byterate}, throttle bytes: #{@throttle_bytes})"
break
end
end
@@ -250,7 +256,8 @@ module Irc
@last_send = Time.new
@lines_sent += 1
@burst += 1
- @throttle_bytes += message.length
+ @throttle_bytes += message.length + 1
+ @last_throttle = Time.new
end
end