1 -- Copyright 2014-2016 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
2 -- Licensed under the Apache License, Version 2.0
4 module("luci.controller.radicale", package.seeall)
6 local NX = require("nixio")
7 local NXFS = require("nixio.fs")
8 local DISP = require("luci.dispatcher")
9 local HTTP = require("luci.http")
10 local I18N = require("luci.i18n") -- not globally avalible here
11 local IPKG = require("luci.model.ipkg")
12 local UTIL = require("luci.util")
13 local SYS = require("luci.sys")
15 local srv_name = "radicale"
16 local srv_ver_min = "1.1" -- minimum version of service required
17 local srv_ver_cmd = [[/usr/bin/radicale --version]]
18 local app_name = "luci-app-radicale"
19 local app_title = I18N.translate("Radicale CalDAV/CardDAV Server")
20 local app_version = "1.1.0-1"
23 entry( {"admin", "services", "radicale"}, alias("admin", "services", "radicale", "edit"), _("CalDAV/CardDAV"), 58)
24 entry( {"admin", "services", "radicale", "edit"}, cbi("radicale") ).leaf = true
25 entry( {"admin", "services", "radicale", "logview"}, call("_logread") ).leaf = true
26 entry( {"admin", "services", "radicale", "startstop"}, post("_startstop") ).leaf = true
27 entry( {"admin", "services", "radicale", "status"}, call("_status") ).leaf = true
30 -- Application / Service specific information functions
31 function app_description()
32 return I18N.translate("The Radicale Project is a complete CalDAV (calendar) and CardDAV (contact) server solution.") .. [[<br />]]
33 .. I18N.translate("Calendars and address books are available for both local and remote access, possibly limited through authentication policies.") .. [[<br />]]
34 .. I18N.translate("They can be viewed and edited by calendar and contact clients on mobile phones or computers.")
36 function app_title_main()
37 return [[<a href="javascript:alert(']]
38 .. I18N.translate("Version Information")
39 .. [[\n\n]] .. app_name
40 .. [[\n\t]] .. I18N.translate("Version") .. [[:\t]] .. app_version
41 .. [[\n\n]] .. srv_name .. [[ ]] .. I18N.translate("required") .. [[:]]
42 .. [[\n\t]] .. I18N.translate("Version") .. [[:\t]]
43 .. srv_ver_min .. [[ ]] .. I18N.translate("or higher")
44 .. [[\n\n]] .. srv_name .. [[ ]] .. I18N.translate("installed") .. [[:]]
45 .. [[\n\t]] .. I18N.translate("Version") .. [[:\t]]
46 .. (service_version() or I18N.translate("NOT installed"))
49 .. I18N.translate(app_title)
52 function app_title_back()
54 .. DISP.build_url("admin", "services", "radicale")
56 .. I18N.translate(app_title)
59 function app_err_value()
60 if not service_version() then
61 return [[<h3><strong><br /><font color="red"> ]]
62 .. I18N.translate("Software package '%s' is not installed." % srv_name)
63 .. [[</font><br /><br /> ]]
64 .. I18N.translate("required") .. [[: ]] .. srv_name .. [[ ]] .. srv_ver_min
65 .. [[<br /><br /> ]]
66 .. [[<a href="]] .. DISP.build_url("admin", "system", "packages") ..[[">]]
67 .. I18N.translate("Please install current version !")
68 .. [[</a><br /> </strong></h3>]]
70 return [[<h3><strong><br /><font color="red"> ]]
71 .. I18N.translate("Software package '%s' is outdated." % srv_name)
72 .. [[</font><br /><br /> ]]
73 .. I18N.translate("installed") .. [[: ]] .. srv_name .. [[ ]] .. service_version()
74 .. [[<br /> ]]
75 .. I18N.translate("required") .. [[: ]] .. srv_name .. [[ ]] .. srv_ver_min
76 .. [[<br /><br /> ]]
77 .. [[<a href="]] .. DISP.build_url("admin", "system", "packages") ..[[">]]
78 .. I18N.translate("Please update to current version !")
79 .. [[</a><br /> </strong></h3>]]
83 function service_version()
85 IPKG.list_installed(srv_name, function(n, v, d)
86 if v and (#v > 0) then ver = v end
89 if not ver or (#ver == 0) then
90 ver = UTIL.exec(srv_ver_cmd)
91 if #ver == 0 then ver = nil end
96 return IPKG.compare_versions((service_version() or "0"), ">=", srv_ver_min)
99 -- called by XHR.get from detail_logview.htm
101 -- read application settings
102 local uci = UCI.cursor()
103 local logfile = uci:get("radicale", "radicale", "logfile") or "/var/log/radicale"
104 uci:unload("radicale")
106 local ldata=NXFS.readfile(logfile)
107 if not ldata or #ldata == 0 then
113 -- called by XHR.get from detail_startstop.htm
114 function _startstop()
115 local pid = get_pid()
117 SYS.call("/etc/init.d/radicale stop")
118 NX.nanosleep(1) -- sleep a second
119 if NX.kill(pid, 0) then -- still running
120 NX.kill(pid, 9) -- send SIGKILL
124 SYS.call("/etc/init.d/radicale start")
125 NX.nanosleep(1) -- sleep a second
127 if pid > 0 and not NX.kill(pid, 0) then
128 pid = 0 -- process did not start
131 HTTP.write(tostring(pid)) -- HTTP needs string not number
134 -- called by XHR.poll from detail_startstop.htm
136 local pid = get_pid()
137 HTTP.write(tostring(pid)) -- HTTP needs string not number
140 --return pid of running process
142 return tonumber(SYS.exec([[ps | grep "[p]ython.*[r]adicale" 2>/dev/null | awk '{print $1}']])) or 0
145 -- replacement of build-in parse of "Value"
146 -- modified AbstractValue.parse(self, section, novld) from cbi.lua
147 -- validate is called if rmempty/optional true or false
148 -- before write check if forcewrite, value eq default, and more
149 function value_parse(self, section, novld)
150 local fvalue = self:formvalue(section)
151 local fexist = ( fvalue and (#fvalue > 0) ) -- not "nil" and "not empty"
152 local cvalue = self:cfgvalue(section)
153 local rm_opt = ( self.rmempty or self.optional )
154 local eq_cfg -- flag: equal cfgvalue
156 -- If favlue and cvalue are both tables and have the same content
157 -- make them identical
158 if type(fvalue) == "table" and type(cvalue) == "table" then
159 eq_cfg = (#fvalue == #cvalue)
162 if cvalue[i] ~= fvalue[i] then
172 -- removed parameter "section" from function call because used/accepted nowhere
173 -- also removed call to function "transfer"
174 local vvalue, errtxt = self:validate(fvalue)
176 -- error handling; validate return "nil"
178 if novld then -- and "novld" set
179 return -- then exit without raising an error
182 if fexist then -- and there is a formvalue
183 self:add_error(section, "invalid", errtxt)
184 return -- so data are invalid
185 elseif not rm_opt then -- and empty formvalue but NOT (rmempty or optional) set
186 self:add_error(section, "missing", errtxt)
187 return -- so data is missing
189 self:add_error(section, "invalid", errtxt)
192 -- error ("\n option: " .. self.option ..
193 -- "\n fvalue: " .. tostring(fvalue) ..
194 -- "\n fexist: " .. tostring(fexist) ..
195 -- "\n cvalue: " .. tostring(cvalue) ..
196 -- "\n vvalue: " .. tostring(vvalue) ..
197 -- "\n vexist: " .. tostring(vexist) ..
198 -- "\n rm_opt: " .. tostring(rm_opt) ..
199 -- "\n eq_cfg: " .. tostring(eq_cfg) ..
200 -- "\n eq_def: " .. tostring(eq_def) ..
201 -- "\n novld : " .. tostring(novld) ..
202 -- "\n errtxt: " .. tostring(errtxt) )
205 -- lets continue with value returned from validate
206 eq_cfg = ( vvalue == cvalue ) -- update equal_config flag
207 local vexist = ( vvalue and (#vvalue > 0) ) and true or false -- not "nil" and "not empty"
208 local eq_def = ( vvalue == self.default ) -- equal_default flag
210 -- (rmempty or optional) and (no data or equal_default)
211 if rm_opt and (not vexist or eq_def) then
212 if self:remove(section) then -- remove data from UCI
213 self.section.changed = true -- and push events
218 -- not forcewrite and no changes, so nothing to write
219 if not self.forcewrite and eq_cfg then
223 -- we should have a valid value here
224 assert (vvalue, "\n option: " .. self.option ..
225 "\n fvalue: " .. tostring(fvalue) ..
226 "\n fexist: " .. tostring(fexist) ..
227 "\n cvalue: " .. tostring(cvalue) ..
228 "\n vvalue: " .. tostring(vvalue) ..
229 "\n vexist: " .. tostring(vexist) ..
230 "\n rm_opt: " .. tostring(rm_opt) ..
231 "\n eq_cfg: " .. tostring(eq_cfg) ..
232 "\n eq_def: " .. tostring(eq_def) ..
233 "\n errtxt: " .. tostring(errtxt) )
235 -- write data to UCI; raise event only on changes
236 if self:write(section, vvalue) and not eq_cfg then
237 self.section.changed = true