Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Tidy up logging using a few virtual methods

  • Loading branch information...
commit 51d9111021f2f24956608a1ee828f34668cc21c2 1 parent 22304e4
@purcell authored
Showing with 15 additions and 8 deletions.
  1. +15 −8 bin/postfix-policy-whois
View
23 bin/postfix-policy-whois
@@ -7,6 +7,13 @@ PORT = 8787
require 'syslog'
$LOG = Syslog.open($0, Syslog::LOG_PID | Syslog::LOG_PERROR, Syslog::LOG_MAIL)
+class << $LOG
+ %w(info err notice debug warning crit alert).each do |level|
+ define_method level do |*args|
+ log(Syslog.const_get("LOG_#{level.upcase}"), *args)
+ end
+ end
+end
require 'resolv'
require 'whois'
@@ -36,22 +43,22 @@ def dodgy_whois?(domain)
end
def dodgy?(domain)
- $LOG.log(Syslog::LOG_INFO, "Checking domain: #{domain}")
+ $LOG.info("Checking domain: #{domain}")
if parent_domain = dodgy_dns?(domain)
- $LOG.log(Syslog::LOG_INFO, "Suspicious nameserver: #{parent_domain}")
+ $LOG.info("Suspicious nameserver: #{parent_domain}")
dodgy = dodgy_whois?(parent_domain)
- $LOG.log(Syslog::LOG_NOTICE, "Private registration: #{parent_domain}") if dodgy
+ $LOG.notice("Private registration: #{parent_domain}") if dodgy
dodgy
end
end
require 'socket'
server = TCPServer.new("0.0.0.0", PORT)
-$LOG.log(Syslog::LOG_INFO, "Listening on 0.0.0.0:#{PORT}")
+$LOG.info("Listening on 0.0.0.0:#{PORT}")
while true
Thread.new(server.accept) do |client|
- $LOG.log(Syslog::LOG_DEBUG, "Got connection")
+ $LOG.debug("Got connection")
attr = Hash.new
while rp = client.gets
break if rp == "\n"
@@ -63,13 +70,13 @@ while true
dodgy = false
begin
- $LOG.log(Syslog::LOG_INFO, "Checking sender: #{sender}")
+ $LOG.info("Checking sender: #{sender}")
dodgy = dodgy?(sender.scan(/@(.*)/).flatten.first.downcase)
if dodgy
- $LOG.log(Syslog::LOG_NOTICE, "Rejecting mail due to registrar: #{sender}")
+ $LOG.notice("Rejecting mail due to registrar: #{sender}")
end
rescue
- $LOG.log(Syslog::LOG_ERR, "Error checking registrar: #{$!}")
+ $LOG.err("Error checking registrar: #{$!}")
end
client.write("action=" + (dodgy ? "REJECT" : "DUNNO") + "\n")
Please sign in to comment.
Something went wrong with that request. Please try again.