class Contact

Public Class Methods

new(remote_ip) click to toggle source
# File lib/rubymta/contact.rb, line 3
def initialize(remote_ip)
  # Reset timed-out records
  S3DB[:contacts].where(Sequel.lit("expires_at<'#{Time.now.strftime("%Y-%m-%d %H:%M")}'")).update(:violations=>0)

  # See if it's already there
  rs = S3DB[:contacts].where(:remote_ip=>remote_ip).first
  if rs.nil?
    # No, add one
    self[:id] = nil
    self[:remote_ip] = remote_ip
    self[:hits] = self[:locks] = self[:violations] = 0
    self[:created_at] = Time.now
    id = S3DB[:contacts].insert(self)
    self[:id] = id
  else
    # Yes, copy the data
    rs.each { |k,v| self[k] = v }
  end
  # Set up the data-set for use later
  @ds = S3DB[:contacts].select(:id, :remote_ip, :hits, :locks, :violations, :expires_at, :created_at, :updated_at).where(:id=>self[:id])

  # count the hit and set the flag to count only one violation per invocation
  self[:hits] += 1
  @inhibit = false
  modify
end

Public Instance Methods

allow() click to toggle source
# File lib/rubymta/contact.rb, line 95
def allow
  self[:violations] = 0
  modify
  nil
end
inhibited?() click to toggle source
# File lib/rubymta/contact.rb, line 70
def inhibited?
  @inhibit
end
modify() click to toggle source
# File lib/rubymta/contact.rb, line 40
def modify
  # Modify resets the 'expires_at' value after any change to
  # then record, but it's only significant when 'violations'
  # is at or above ProhibitedSeconds
  expires_at = Time.now + ProhibitedSeconds
  if !self[:id].nil?
    self[:expires_at] = expires_at
    self[:updated_at] = Time.now
    @ds.update(self)
  end
  expires_at
end
prohibit() click to toggle source

set this one to prohibited

# File lib/rubymta/contact.rb, line 90
def prohibit
  self[:violations] = MaxFailedMsgsPerPeriod+1
  modify
end
prohibited?() click to toggle source
# File lib/rubymta/contact.rb, line 84
def prohibited?
  # Returns true or false
  self[:violations] > MaxFailedMsgsPerPeriod
end
remove() click to toggle source
# File lib/rubymta/contact.rb, line 30
def remove
  # Remove is rarely used, if at all, because the contacts
  # table keeps a history of IPs that have connected
  if !self[:id].nil?
    @ds.delete
    self[:id] = nil
  end
  nil
end
violation() click to toggle source
# File lib/rubymta/contact.rb, line 53
def violation
  # Count the violation and reset the 'expires_at' time -- Also
  # counts the times this IP has been locked out -- only count
  # one violation and one lock per instantiation
  if !inhibited?
    self[:violations]+=1
    @inhibit = true
    if prohibited?
      self[:locks] += 1
      self[:expires_at] = Time.now + ProhibitedSeconds
      modify
      raise Quit # force quit: some senders try to keep going
    end
    modify
  end
end
violations?() click to toggle source
# File lib/rubymta/contact.rb, line 74
def violations?
  # Returns the current count
  self[:violations]
end
warning?() click to toggle source
# File lib/rubymta/contact.rb, line 79
def warning?
  # Returns true or false
  self[:violations] >= MaxFailedMsgsPerPeriod
end