luci-app-ddns: patches for #298 #334 #335
[project/luci.git] / applications / luci-app-ddns / luasrc / tools / ddns.lua
index ad7b5e8..6d53931 100644 (file)
@@ -1,20 +1,5 @@
---[[
-LuCI - Lua Configuration Interface
-
-shared module for luci-app-ddns
-Copyright 2014 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
-
-function parse_url copied from https://svn.nmap.org/nmap/nselib/url.lua
-Parses a URL and returns a table with all its parts according to RFC 2396.
-@author Diego Nehab    @author Eddie Bell <ejlbell@gmail.com>
-
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-]]--
+-- Copyright 2014 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
+-- Licensed to the public under the Apache License 2.0.
 
 module("luci.tools.ddns", package.seeall)
 
@@ -50,7 +35,7 @@ end
 
 -- check if Wget with SSL support or cURL installed
 function check_ssl()
-       if (SYS.call([[ grep -iq "\+ssl" /usr/bin/wget 2>/dev/null ]]) == 0) then
+       if (SYS.call([[ grep -i "\+ssl" /usr/bin/wget >/dev/null 2>&1 ]]) == 0) then
                return true
        else
                return NXFS.access("/usr/bin/curl")
@@ -60,12 +45,12 @@ end
 -- check if Wget with SSL or cURL with proxy support installed
 function check_proxy()
        -- we prefere GNU Wget for communication
-       if (SYS.call([[ grep -iq "\+ssl" /usr/bin/wget 2>/dev/null ]]) == 0) then
+       if (SYS.call([[ grep -i "\+ssl" /usr/bin/wget >/dev/null 2>&1 ]]) == 0) then
                return true
 
        -- if not installed cURL must support proxy
        elseif NXFS.access("/usr/bin/curl") then
-               return (SYS.call([[ grep -iq all_proxy /usr/lib/libcurl.so* 2>/dev/null ]]) == 0)
+               return (SYS.call([[ grep -i all_proxy /usr/lib/libcurl.so* >/dev/null 2>&1 ]]) == 0)
 
        -- only BusyBox Wget is installed
        else
@@ -111,33 +96,71 @@ function get_pid(section)
        return pid
 end
 
--- read version information for given package if installed
-function ipkg_version(package)
-       if not package then
-               return nil
-       end
-       local info = OPKG.info(package)
-       local data = {}
-       local version = ""
-       local i = 0
-       for k, v in pairs(info) do
-               if v.Package == package and v.Status.installed then
-                       version = v.Version
-                       i = i + 1
+-- compare versions using "<=" "<" ">" ">=" "=" "<<" ">>"
+function ipkg_ver_compare(ver1, comp, ver2)
+       if not ver1 or not (#ver1 > 0)
+       or not ver2 or not (#ver2 > 0)
+       or not comp or not (#comp > 0) then return nil end
+       -- correct compare string
+       if comp == "<>" or comp == "><" or comp == "!=" or comp == "~=" then comp = "~="
+       elseif comp == "<=" or comp == "<" or comp == "=<" then comp = "<="
+       elseif comp == ">=" or comp == ">" or comp == "=>" then comp = ">="
+       elseif comp == "="  or comp == "==" then comp = "=="
+       elseif comp == "<<" then comp = "<"
+       elseif comp == ">>" then comp = ">"
+       else return nil end
+
+       local av1 = UTIL.split(ver1, "[%.%-]", nil, true)
+       local av2 = UTIL.split(ver2, "[%.%-]", nil, true)
+
+       for i = 1, math.max(table.getn(av1),table.getn(av2)), 1  do
+               local s1 = av1[i] or ""
+               local s2 = av2[i] or ""
+               local n1 = tonumber(s1)
+               local n2 = tonumber(s2)
+
+               -- one numeric and other empty string then set other to 0
+               if n1 and not n2 and (not s2 or #s2 == 0) then n2 = 0 end
+               if n2 and not n1 and (not s1 or #s1 == 0) then n1 = 0 end
+
+               local nc = (n1 and n2)  -- numeric compare
+
+               if nc then
+                       -- first "not equal" found return true
+                       if comp == "~=" and (n1 ~= n2) then return true end
+                       -- first "lower" found return true
+                       if (comp == "<" or comp == "<=") and (n1 < n2) then return true end
+                       -- first "greater" found return true
+                       if (comp == ">" or comp == ">=") and (n1 > n2) then return true end
+                       -- not equal then return false
+                       if (n1 ~= n2) then return false end
+               else
+                       if comp == "~=" and (s1 ~= s2) then return true end
+                       if (comp == "<" or comp == "<=") and (s1 < s2) then return true end
+                       if (comp == ">" or comp == ">=") and (s1 > s2) then return true end
+                       if (s1 ~= s2) then return false end
                end
        end
-       if i > 1 then   -- more then one valid record
-               return data
+       -- all equal then true
+       return true
+end
+
+-- read version information for given package if installed
+function ipkg_ver_installed(pkg)
+       local version = nil
+       local control = io.open("/usr/lib/opkg/info/%s.control" % pkg, "r")
+       if control then
+               local ln
+               repeat
+                       ln = control:read("*l")
+                       if ln and ln:match("^Version: ") then
+                               version = ln:gsub("^Version: ", "")
+                               break
+                       end
+               until not ln
+               control:close()
        end
-       local sver = UTIL.split(version, "[%.%-]", nil, true)
-       data = {
-               version = version,
-               major   = tonumber(sver[1]) or 0,
-               minor   = tonumber(sver[2]) or 0,
-               patch   = tonumber(sver[3]) or 0,
-               build   = tonumber(sver[4]) or 0
-       }
-       return data
+       return version
 end
 
 -- replacement of build-in read of UCI option