bird-openwrt: update to version 0.3
[feed/routing.git] / bird-openwrt / bird4-openwrt / src / model / functions.lua
1 --[[
2 Copyright (C) 2014-2017 - Eloi Carbo
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 ]]--
17
18 local fs = require "nixio.fs"
19 local functions_dir = "/etc/bird4/functions/"
20 local lock_file = "/etc/bird4/function_lock"
21
22 m = SimpleForm("bird4", "Bird4 Functions", "<b>INFO:</b> New files are created using Timestamps.<br />In order to make it easier to handle, use SSH to connect to your terminal and rename those files.<br />If your file is not correctly shown in the list, please, refresh your browser.")
23
24 s = m:section(SimpleSection)
25 files = s:option(ListValue, "Files", "Function Files:")
26 local new_function = functions_dir .. os.date("function-%Y%m%d-%H%M")
27
28 -- New File Entry
29 files:value(new_function, "New File (".. new_function .. ")")
30 files.default = new_function
31
32 local i, file_list = 0, { }
33 for filename in io.popen("find " .. functions_dir .. " -type f"):lines() do
34 i = i + 1
35 files:value(filename, filename)
36 end
37
38 ld = s:option(Button, "_load", "Load File")
39 ld.inputstyle = "reload"
40
41 st_file = s:option(DummyValue, "_stfile", "Editing file:")
42 function st_file.cfgvalue(self, section)
43 if ld:formvalue(section) then
44 fs.writefile(lock_file, files:formvalue(section))
45 return files:formvalue(section)
46 else
47 fs.writefile(lock_file, "")
48 return ""
49 end
50 end
51
52 area = s:option(Value, "_functions")
53 area.template = "bird4/tvalue"
54 area.rows = 30
55 function area.cfgvalue(self,section)
56 if ld:formvalue(section) then
57 local contents = fs.readfile(files:formvalue(section))
58 if contents then
59 return contents
60 else
61 return ""
62 end
63 else
64 return ""
65 end
66 end
67
68 function area.write(self, section)
69 local locked_file = fs.readfile(lock_file)
70 if locked_file and not ld:formvalue(section) then
71 local text = self:formvalue(section):gsub("\r\n?", "\n")
72 fs.writefile(locked_file, text)
73 fs.writefile(lock_file, "")
74 end
75 end
76
77 return m