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