Class: Contact

Inherits:
Hash show all
Defined in:
lib/rubymta/contact.rb

Instance Method Summary collapse

Constructor Details

#initialize(remote_ip) ⇒ Contact

Returns a new instance of Contact.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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

Instance Method Details

#allowObject



95
96
97
98
99
# File 'lib/rubymta/contact.rb', line 95

def allow
  self[:violations] = 0
  modify
  nil
end

#inhibited?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/rubymta/contact.rb', line 70

def inhibited?
  @inhibit
end

#modifyObject



40
41
42
43
44
45
46
47
48
49
50
51
# 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

#prohibitObject

set this one to prohibited



90
91
92
93
# File 'lib/rubymta/contact.rb', line 90

def prohibit
  self[:violations] = MaxFailedMsgsPerPeriod+1
  modify
end

#prohibited?Boolean

Returns:

  • (Boolean)


84
85
86
87
# File 'lib/rubymta/contact.rb', line 84

def prohibited?
  # Returns true or false
  self[:violations] > MaxFailedMsgsPerPeriod
end

#removeObject



30
31
32
33
34
35
36
37
38
# 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

#violationObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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?Boolean

Returns:

  • (Boolean)


74
75
76
77
# File 'lib/rubymta/contact.rb', line 74

def violations?
  # Returns the current count
  self[:violations]
end

#warning?Boolean

Returns:

  • (Boolean)


79
80
81
82
# File 'lib/rubymta/contact.rb', line 79

def warning?
  # Returns true or false
  self[:violations] >= MaxFailedMsgsPerPeriod
end