modules/freifunk: Fix ff_mapupdate and close #128
[project/luci.git] / modules / freifunk / root / usr / sbin / ff_mapupdate
1 #!/usr/bin/lua
2
3 require("luci.fs")
4 local uci = require "luci.model.uci"
5 local x = uci.cursor()
6
7 local update_url = "http://map.berlin.freifunk.net/freifunkmap.php?update=%.15f,%.15f&note=%s&olsrip=%s"
8 local update_all = ( arg[1] and arg[1] == "all" ) and true or false
9
10 local file
11 x:foreach("olsrd", "LoadPlugin", function(s)
12         if s.library == "olsrd_nameservice.so.0.3" then
13                 luci.fs.copy (s.latlon_file, "/tmp/ff_mapupdate.latlon")
14                 file = io.open("/tmp/ff_mapupdate.latlon")
15         end
16 end)
17
18 if file then
19         local ln
20         local count = 0
21         while true do
22                 ln = file:read("*l")
23                 if not ln then break end
24                 if update_all and ln:match("^Node%(") then
25                         local ip, lat, lon, note = ln:match("Node%('(%S+)',([%d%.]+),([%d%.]+),%d+,'%S+','(%S+)'%)")
26                         lat = tonumber(lat)
27                         lon = tonumber(lon)
28                 
29                         if ip and lat ~= 0.0 and lon ~= 0.0 and note then
30                                 note = note:gsub("[^%w%-%.]+", "_")
31                                 os.execute("wget -qO/dev/null %q" % string.format(update_url, lat, lon, note, ip))
32                                 count = count + 1
33                         end
34
35                 elseif ln:match("^Self%(") then
36                         local ip, lat, lon, note = ln:match("Self%('(%S+)',([%d%.]+),([%d%.]+),%d+,'%S+','(%S+)'%)")
37                         lat = tonumber(lat)
38                         lot = tonumber(lon)
39                         
40                         if ip and lat ~= 0.0 and lon ~= 0.0 and note then
41                                 note = note:gsub("[^%w%-%.]+", "_")
42                                 os.execute("wget -qO/dev/null %q" % string.format(update_url, lat, lon, note, ip))
43                                 count = count + 1
44                         end
45                 end
46         end
47
48         os.execute("logger -t 'mapupdate' 'Updated %d entries in freifunk map'" % count)
49
50         file:close()
51 end