summaryrefslogtreecommitdiff
path: root/lib/rbot
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2007-02-11 09:23:12 +0000
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2007-02-11 09:23:12 +0000
commit4179ece26a79077775c7ec35a77445fc917f2788 (patch)
treeef978248b9fb4f8227e9b1a172b9d7ff73ee33fb /lib/rbot
parent10025272f5d85daa27b73dcf2c90c33fa3f216fc (diff)
Start work on preferring size to length: it's the same for arrays, and will be more correct in most cases when proper support for multibyte strings will be implemented (via package or because of ruby 2)
Diffstat (limited to 'lib/rbot')
-rw-r--r--lib/rbot/ircbot.rb28
-rw-r--r--lib/rbot/ircsocket.rb44
2 files changed, 37 insertions, 35 deletions
diff --git a/lib/rbot/ircbot.rb b/lib/rbot/ircbot.rb
index a54fd7cd..ed435fb4 100644
--- a/lib/rbot/ircbot.rb
+++ b/lib/rbot/ircbot.rb
@@ -373,7 +373,7 @@ class IrcBot
end
def STDOUT.write(str=nil)
log str, 2
- return str.to_s.length
+ return str.to_s.size
end
def STDERR.write(str=nil)
if str.to_s.match(/:\d+: warning:/)
@@ -381,7 +381,7 @@ class IrcBot
else
error str, 2
end
- return str.to_s.length
+ return str.to_s.size
end
end
@@ -794,7 +794,7 @@ class IrcBot
lines = Array.new
message.each_line { |line|
line.chomp!
- next unless(line.length > 0)
+ next unless(line.size > 0)
lines << line
}
else
@@ -809,7 +809,7 @@ class IrcBot
# The maximum raw message length we can send is therefore 512 - 2 - 2
# minus the length of our hostmask.
- max_len = 508 - myself.fullform.length
+ max_len = 508 - myself.fullform.size
# On servers that support IDENTIFY-MSG, we have to subtract 1, because messages
# will have a + or - prepended
@@ -822,7 +822,7 @@ class IrcBot
fixed = "#{type} #{where} :"
# And this is what's left
- left = max_len - fixed.length
+ left = max_len - fixed.size
case opts[:overlong]
when :split
@@ -830,8 +830,8 @@ class IrcBot
split_at = opts[:split_at]
when :truncate
truncate = opts[:truncate_text]
- truncate = @default_send_options[:truncate_text] if truncate.length > left
- truncate = "" if truncate.length > left
+ truncate = @default_send_options[:truncate_text] if truncate.size > left
+ truncate = "" if truncate.size > left
else
raise "Unknown :overlong option #{opts[:overlong]} while sending #{original_message.inspect}"
end
@@ -845,21 +845,21 @@ class IrcBot
begin
if max_lines > 0 and cmd_lines == max_lines - 1
truncate = opts[:truncate_text]
- truncate = @default_send_options[:truncate_text] if truncate.length > left
- truncate = "" if truncate.length > left
+ truncate = @default_send_options[:truncate_text] if truncate.size > left
+ truncate = "" if truncate.size > left
maxed = true
end
- if(left >= msg.length) and not maxed
+ if(left >= msg.size) and not maxed
sendq "#{fixed}#{msg}", chan, ring
log_sent(type, where, msg)
cmd_lines += 1
break
end
if truncate
- line.replace msg.slice(0, left-truncate.length)
+ line.replace msg.slice(0, left-truncate.size)
# line.sub!(/\s+\S*$/, truncate)
line << truncate
- raise "PROGRAMMER ERROR! #{line.inspect} of length #{line.length} > #{left}" if line.length > left
+ raise "PROGRAMMER ERROR! #{line.inspect} of size #{line.size} > #{left}" if line.size > left
sendq "#{fixed}#{line}", chan, ring
log_sent(type, where, line)
return
@@ -867,13 +867,13 @@ class IrcBot
line.replace msg.slice!(0, left)
lastspace = line.rindex(opts[:split_at])
if(lastspace)
- msg.replace line.slice!(lastspace, line.length) + msg
+ msg.replace line.slice!(lastspace, line.size) + msg
msg.gsub!(/^#{opts[:split_at]}/, "") if opts[:purge_split]
end
sendq "#{fixed}#{line}", chan, ring
log_sent(type, where, line)
cmd_lines += 1
- end while(msg.length > 0)
+ end while(msg.size > 0)
}
end
diff --git a/lib/rbot/ircsocket.rb b/lib/rbot/ircsocket.rb
index e70528ea..973ae9b8 100644
--- a/lib/rbot/ircsocket.rb
+++ b/lib/rbot/ircsocket.rb
@@ -3,9 +3,9 @@ class ::String
# by the IRCd
def irc_send_penalty
# According to eggrdop, the initial penalty is
- penalty = 1 + self.length/100
+ penalty = 1 + self.size/100
# on everything but UnderNET where it's
- # penalty = 2 + self.length/120
+ # penalty = 2 + self.size/120
cmd, pars = self.split($;,2)
debug "cmd: #{cmd}, pars: #{pars.inspect}"
@@ -14,29 +14,29 @@ class ::String
chan, nick, msg = pars.split
chan = chan.split(',')
nick = nick.split(',')
- penalty += nick.length
- penalty *= chan.length
+ penalty += nick.size
+ penalty *= chan.size
when :MODE
chan, modes, argument = pars.split
extra = 0
if modes
extra = 1
if argument
- extra += modes.split(/\+|-/).length
+ extra += modes.split(/\+|-/).size
else
- extra += 3 * modes.split(/\+|-/).length
+ extra += 3 * modes.split(/\+|-/).size
end
end
if argument
- extra += 2 * argument.split.length
+ extra += 2 * argument.split.size
end
- penalty += extra * chan.split.length
+ penalty += extra * chan.split.size
when :TOPIC
penalty += 1
- penalty += 2 unless pars.split.length < 2
+ penalty += 2 unless pars.split.size < 2
when :PRIVMSG, :NOTICE
dests = pars.split($;,2).first
- penalty += dests.split(',').length
+ penalty += dests.split(',').size
when :WHO
# I'm too lazy to implement this one correctly
penalty += 5
@@ -85,12 +85,13 @@ module Irc
end
def length
- length = 0
+ len = 0
@storage.each {|c|
- length += c[1].length
+ len += c[1].size
}
- return length
+ return len
end
+ alias :size :length
def empty?
@storage.empty?
@@ -113,7 +114,7 @@ module Irc
return nil
end
save_idx = @last_idx
- @last_idx = (@last_idx + 1) % @storage.length
+ @last_idx = (@last_idx + 1) % @storage.size
mess = @storage[@last_idx][1].first
@last_idx = save_idx
return mess
@@ -124,7 +125,7 @@ module Irc
warning "trying to access empty ring"
return nil
end
- @last_idx = (@last_idx + 1) % @storage.length
+ @last_idx = (@last_idx + 1) % @storage.size
mess = @storage[@last_idx][1].shift
@storage.delete(@storage[@last_idx]) if @storage[@last_idx][1] == []
return mess
@@ -180,10 +181,11 @@ module Irc
def length
len = 0
@rings.each { |r|
- len += r.length
+ len += r.size
}
len
end
+ alias :size :length
def next
if empty?
@@ -195,8 +197,8 @@ module Irc
mess = @rings[0].first
else
save_ring = @last_ring
- (@rings.length - 1).times {
- @last_ring = (@last_ring % (@rings.length - 1)) + 1
+ (@rings.size - 1).times {
+ @last_ring = (@last_ring % (@rings.size - 1)) + 1
if !@rings[@last_ring].empty?
mess = @rings[@last_ring].next
break
@@ -217,8 +219,8 @@ module Irc
if !@rings[0].empty?
return @rings[0].shift
end
- (@rings.length - 1).times {
- @last_ring = (@last_ring % (@rings.length - 1)) + 1
+ (@rings.size - 1).times {
+ @last_ring = (@last_ring % (@rings.size - 1)) + 1
if !@rings[@last_ring].empty?
return @rings[@last_ring].shift
end
@@ -420,7 +422,7 @@ module Irc
return
end
@flood_send = now if @flood_send < now
- debug "can send #{@sendq_burst - @burst} lines, there are #{@sendq.length} to send"
+ debug "can send #{@sendq_burst - @burst} lines, there are #{@sendq.size} to send"
while !@sendq.empty? and @burst < @sendq_burst and @flood_send - now < MAX_IRC_SEND_PENALTY
debug "sending message (#{@flood_send - now} < #{MAX_IRC_SEND_PENALTY})"
puts_critical(@sendq.shift, true)