]> source.dussan.org Git - rspamd.git/commitdiff
Fix rbl plugin.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 19 Nov 2013 00:47:22 +0000 (00:47 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 19 Nov 2013 00:47:22 +0000 (00:47 +0000)
src/plugins/lua/rbl.lua

index 91205c045e62c9c6ca4818a7887e56c459e23a4e..096c7f3c9775f1c1355cde9764249ec8f620210d 100644 (file)
 
 local rbls = {}
 
-function revipv6(ip)
-       local c = 0
-       local i = 1
-       local t = {}
-       for o in string.gmatch(ip, "%p-%x+%p-") do
-               o = string.gsub(o, ":", "")
-               while(#o < 4) do
-                       o = "0" .. o
-               end
-               t[i] = o
-               i = i+1
-       end
-       if #t < 8 then
-               for i=1,8 do
-                       if(t[i] == nil) then
-                               c = c+1
-                       end
-               end
-               for i=(8-c),#t do
-                       t[i+c] = t[i]
-                       t[i] = "0000"
-               end
-               for i=1,8 do
-                       if(t[i] == nil) then
-                               t[i] = "0000"
-                       end
-               end
-       end
-       x=table.concat(t,"")
-       x=string.reverse(x)
-       rbl_str = ""
-       for i in string.gmatch(x, "%x") do
-               rbl_str = rbl_str .. i .. "."
+local function ip_to_rbl(ip, rbl)
+       octets = ip:inversed_str_octets()
+       local str = ''
+       for _,o in ipairs(octets) do
+               str = str .. o .. '.'
        end
-       return rbl_str
+       str = str .. rbl
+
+       return str
 end
 
-function dns_cb(task, to_resolve, results, err, sym)
+local function rbl_dns_cb(task, to_resolve, results, err, sym)
        if results then
                task:insert_result(sym, 1)
        end
 end
 
-function rbl_cb (task)
+local function rbl_cb (task)
        local rip = task:get_from_ip()
        if(rip ~= nil) then
-               if not string.match(rip, ":") then
-                       local _,_,o1,o2,o3,o4 = string.find(rip, '^(%d+)%.(%d+)%.(%d+)%.(%d+)$')
-                       for _,rbl in pairs(rbls) do
-                               if(rbl['ipv4'] and rbl['from']) then
-                                       rbl_str = o4 .. '.' .. o3 .. '.' .. o2 .. '.' .. o1 .. '.' .. rbl['rbl']
-                                       task:resolve_dns_a(rbl_str, 'dns_cb', rbl['symbol'])
-                               end
-                       end
-               else
-                       for _,rbl in pairs(rbls) do
-                               if(rbl['ipv6'] and rbl['from']) then
-                                       rbl_str = revipv6(rip) .. rbl['rbl']
-                                       task:resolve_dns_a(rbl_str, 'dns_cb', rbl['symbol'])
-                               end
-                       end
+               for _,rbl in pairs(rbls) do
+                       task:resolve_dns_a(ip_to_rbl(rip, rbl['rbl']), rbl_dns_cb, rbl['symbol'])
                end
        end
        local recvh = task:get_received_headers()
        for _,rh in ipairs(recvh) do
                if rh['real_ip'] then
-                       if not string.match(rh['real_ip'], ":") then
-                               local _,_,o1,o2,o3,o4 = string.find(rh['real_ip'], '^(%d+)%.(%d+)%.(%d+)%.(%d+)$')
-                               if o1 and o2 and o3 and o4 then
-                                       for _,rbl in pairs(rbls) do
-                                               if(rbl['ipv4'] and rbl['received']) then
-                                                       rbl_str = o4 .. '.' .. o3 .. '.' .. o2 .. '.' .. o1 .. '.' .. rbl['rbl']
-                                                       task:resolve_dns_a(rbl_str, 'dns_cb', rbl['symbol'])
-                                               end
-                                       end
-                               end
-                       else
-                               for _,rbl in pairs(rbls) do
-                                       if(rbl['ipv6'] and rbl['received']) then
-                                               rbl_str = revipv6(rh['real_ip']) .. rbl['rbl']
-                                               task:resolve_dns_a(rbl_str, 'dns_cb', rbl['symbol'])
-                                       end     
-                               end
+                       for _,rbl in pairs(rbls) do
+                               task:resolve_dns_a(ip_to_rbl(rip, rbl['rbl']), rbl_dns_cb, rbl['symbol'])
                        end
                        end
        end
@@ -117,7 +63,7 @@ end
 
 -- Configuration
 local opts = rspamd_config:get_all_opt('rbl')
-if(opts == nil) then
+if not opts or type(opts) ~= 'table' then
        return
 end
 if(opts['default_ipv4'] == nil) then
@@ -132,16 +78,19 @@ end
 if(opts['default_from'] == nil) then
        opts['default_from'] = false
 end
-for _,rbl in pairs(opts['rbls']) do
+for key,rbl in pairs(opts['rbls']) do
        local o = { "ipv4", "ipv6", "from", "received" }
-       for i=1,#o do
+       for i=1,table.maxn(o) do
                if(rbl[o[i]] == nil) then
                        rbl[o[i]] = opts['default_' .. o[i]]
                end
        end
+       if not rbl['symbol'] then
+               rbl['symbol'] = key
+       end
        if type(rspamd_config.get_api_version) ~= 'nil' then
                rspamd_config:register_virtual_symbol(rbl['symbol'], 1)
        end
        table.insert(rbls, {symbol = rbl['symbol'], rbl = rbl['rbl'], ipv6 = rbl['ipv6'], ipv4 = rbl['ipv4'], received = rbl['received'], from = rbl['from']})
-       rspamd_config:register_symbol(rbl['symbol'], 1.0, 'rbl_cb')
+       rspamd_config:register_symbol(rbl['symbol'], 1.0, rbl_cb)
 end