Sha256: a1afabf7e9c920cbef2e0d547e099f740b15d668308ff00286f19ba04c1e6e75

Contents?: true

Size: 1.72 KB

Versions: 5

Compression:

Stored size: 1.72 KB

Contents

require 'resolv'
module Antispam
  module Blacklists
    class Httpbl
      def self.check(ip, key)
        threat = 0
        begin
          return get_old_result(ip) if get_old_result(ip)
          check = ip.split('.').reverse.join('.')
          host = key + '.' + check + ".dnsbl.httpbl.org"
          address = Resolv::getaddress(host)
          z,days,threat,iptype = address.split('.')
          Rails.logger.info "Spam located: #{iptype} type at #{threat} threat. (#{ip} - #{address})"
          # Create or update
          if (threat.to_i > 30)
            Rails.logger.info "Spamcheck: Very high, over 30!"
          end
        rescue Exception => e
          case e
          when Resolv::ResolvError #Not spam! This blacklist gives an error when there's no spam threat.
            Rails.logger.info "Spamcheck: OK! Resolve error means the httpbl does not consider this spam."
          when Interrupt #Something broke while trying to check blacklist.
            Rails.logger.info "Spamcheck: Interrupt when trying to resolve http blacklist. Possible timeout?"
          else # Time Out
            Rails.logger.info "Spamcheck: There was an error, possibly a time out, when checking this IP."
            Rails.logger.info e.to_s
          end
        end
        update_old_result(ip, threat)
        return threat
      end
      def self.get_old_result(ip)
        result = Antispam::Ip.find_by(address: ip, provider: 'httpbl')
        return nil if (result.nil? || result.expired?)
        return result.threat
      end
      def self.update_old_result(ip, threat)
        result = Antispam::Ip.find_or_create_by(address: ip, provider: 'httpbl')
        result.update(threat: threat, expires_at: 24.hours.from_now)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
antispam-0.1.4 lib/antispam/blacklists/httpbl.rb
antispam-0.1.3 lib/antispam/blacklists/httpbl.rb
antispam-0.1.2 lib/antispam/blacklists/httpbl.rb
antispam-0.1.1 lib/antispam/blacklists/httpbl.rb
antispam-0.1.0 lib/antispam/blacklists/httpbl.rb