Class: Irc::NetmaskDb

Inherits:
Object show all
Defined in:
lib/rbot/maskdb.rb

Defined Under Namespace

Classes: Tree

Instance Method Summary collapse

Constructor Details

#initializeNetmaskDb

api wrapper for netmasks



101
102
103
# File 'lib/rbot/maskdb.rb', line 101

def initialize
  @tree = Tree.new
end

Instance Method Details

#add(user, *masks) ⇒ Object



126
127
128
129
130
131
# 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) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# 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) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
# 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) ⇒ Object



121
122
123
124
# 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) ⇒ Object



140
141
142
143
144
145
146
147
# 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) ⇒ Object



133
134
135
136
137
138
# 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