class Irc::NetmaskDb

Public Class Methods

new() click to toggle source

api wrapper for netmasks

# File lib/rbot/maskdb.rb, line 101
def initialize
  @tree = Tree.new
end

Public Instance Methods

add(user, *masks) click to toggle source
# File lib/rbot/maskdb.rb, line 126
def add(user, *masks)
  masks.each do |m|
    debug "adding user #{user} with mask #{m.fullform}"
    @tree.add([user, m], *mask2keys(m))
  end
end
cook_component(str) click to toggle source
# File lib/rbot/maskdb.rb, line 105
def cook_component(str)
  s = (str && !str.empty?) ? str : '*'
  l = s.index(/[\?\*]/)
  if l
    l2 = s.size - s.rindex(/[\?\*]/) - 1
    if l2 > l
      s = s.reverse
      l = l2
    end

    return (l > 0) ? s.slice(0 .. (l - 1)) : ''
  else
    return s
  end
end
find(iu) click to toggle source
# File lib/rbot/maskdb.rb, line 149
def find(iu)
  debug "find(#{iu.fullform})"
  iud = iu.downcased
  matches = @tree.find(iud.host, iud.user, iud.nick).uniq.map do |val|
    m = metric(iu, *val)
    m ? [val[0], m] : nil
  end.compact.sort { |a, b| a[1] <=> a[1] }
  debug "matches: " + (matches.map do |m|
    "#{m[0].username}: [#{m[1]}]"
  end.join(', '))
  return matches.empty? ? nil : matches[0][0]
end
mask2keys(m) click to toggle source
# File lib/rbot/maskdb.rb, line 121
def mask2keys(m)
  md = m.downcased
  [md.host, md.user, md.nick].map { |c| cook_component(c) }
end
metric(iu, bu, mask) click to toggle source
# File lib/rbot/maskdb.rb, line 140
def metric(iu, bu, mask)
  ret = nil
  if iu.matches? mask
    ret = iu.fullform.length - mask.fullform.length
    ret += 10 if bu.transient?
  end
  return ret
end
remove(user, mask) click to toggle source
# File lib/rbot/maskdb.rb, line 133
def remove(user, mask)
  debug "trying to remove user #{user} with mask #{mask}"
  @tree.remove(*mask2keys(mask)) do |val|
    val[0] == user and val[1].fullform == mask.fullform
  end
end