luci-app-radicale: bump to version 1.1.0
[project/luci.git] / applications / luci-app-radicale / luasrc / controller / radicale.lua
1 -- Copyright 2014-2016 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
2 -- Licensed under the Apache License, Version 2.0
3
4 module("luci.controller.radicale", package.seeall)
5
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")
14
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"
21
22 function index()
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
28 end
29
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.")
35 end
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"))
47                         .. [[\n\n]]
48                 .. [[')">]]
49                 .. I18N.translate(app_title)
50                 .. [[</a>]]
51 end
52 function app_title_back()
53         return  [[<a href="]]
54                         .. DISP.build_url("admin", "services", "radicale")
55                 .. [[">]]
56                 .. I18N.translate(app_title)
57                 .. [[</a>]]
58 end
59 function app_err_value()
60         if not service_version() then
61                 return [[<h3><strong><br /><font color="red">&nbsp;&nbsp;&nbsp;&nbsp;]]
62                         .. I18N.translate("Software package '%s' is not installed." % srv_name)
63                         .. [[</font><br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]]
64                         .. I18N.translate("required") .. [[: ]] .. srv_name .. [[ ]] .. srv_ver_min
65                         .. [[<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;]]
66                         .. [[<a href="]] .. DISP.build_url("admin", "system", "packages") ..[[">]]
67                         .. I18N.translate("Please install current version !")
68                         .. [[</a><br />&nbsp;</strong></h3>]]
69         else
70                 return [[<h3><strong><br /><font color="red">&nbsp;&nbsp;&nbsp;&nbsp;]]
71                         .. I18N.translate("Software package '%s' is outdated." % srv_name)
72                         .. [[</font><br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]]
73                         .. I18N.translate("installed") .. [[: ]] .. srv_name .. [[ ]] .. service_version()
74                         .. [[<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]]
75                         .. I18N.translate("required") .. [[: ]] .. srv_name .. [[ ]] .. srv_ver_min
76                         .. [[<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;]]
77                         .. [[<a href="]] .. DISP.build_url("admin", "system", "packages") ..[[">]]
78                         .. I18N.translate("Please update to current version !")
79                         .. [[</a><br />&nbsp;</strong></h3>]]
80         end
81 end
82
83 function service_version()
84         local ver = nil
85         IPKG.list_installed(srv_name, function(n, v, d)
86                         if v and (#v > 0) then ver = v end
87                 end
88         )
89         if not ver or (#ver == 0) then
90                 ver = UTIL.exec(srv_ver_cmd)
91                 if #ver == 0 then ver = nil end
92         end
93         return  ver
94 end
95 function service_ok()
96         return  IPKG.compare_versions((service_version() or "0"), ">=", srv_ver_min)
97 end
98
99 -- called by XHR.get from detail_logview.htm
100 function _logread()
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")
105
106         local ldata=NXFS.readfile(logfile)
107         if not ldata or #ldata == 0 then
108                 ldata="_nodata_"
109         end
110         HTTP.write(ldata)
111 end
112
113 -- called by XHR.get from detail_startstop.htm
114 function _startstop()
115         local pid = get_pid()
116         if pid > 0 then
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
121                 end
122                 pid = 0
123         else
124                 SYS.call("/etc/init.d/radicale start")
125                 NX.nanosleep(1)         -- sleep a second
126                 pid = get_pid()
127                 if pid > 0 and not NX.kill(pid, 0) then
128                         pid = 0         -- process did not start
129                 end
130         end
131         HTTP.write(tostring(pid))       -- HTTP needs string not number
132 end
133
134 -- called by XHR.poll from detail_startstop.htm
135 function _status()
136         local pid = get_pid()
137         HTTP.write(tostring(pid))       -- HTTP needs string not number
138 end
139
140 --return pid of running process
141 function get_pid()
142         return tonumber(SYS.exec([[ps | grep "[p]ython.*[r]adicale" 2>/dev/null | awk '{print $1}']])) or 0
143 end
144
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
155
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)
160                 if eq_cfg then
161                         for i=1, #fvalue do
162                                 if cvalue[i] ~= fvalue[i] then
163                                         eq_cfg = false
164                                 end
165                         end
166                 end
167                 if eq_cfg then
168                         fvalue = cvalue
169                 end
170         end
171
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)
175
176         -- error handling; validate return "nil"
177         if not vvalue then
178                 if novld then           -- and "novld" set
179                         return          -- then exit without raising an error
180                 end
181
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
188                 elseif errtxt then
189                         self:add_error(section, "invalid", errtxt)
190                         return
191                 end
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) )
203         end
204
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
209
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
214                 end
215                 return
216         end
217
218         -- not forcewrite and no changes, so nothing to write
219         if not self.forcewrite and eq_cfg then
220                 return
221         end
222
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) )
234
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
238         end
239 end