luci-app-radicale: New app to support Radicale CalDAV/CardDAV server
[project/luci.git] / applications / luci-app-radicale / luasrc / controller / radicale.lua
1 -- Copyright 2014 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 UTIL  = require("luci.util")
12 local SYS   = require("luci.sys")
13
14 function index()
15         entry( {"admin", "services", "radicale"}, alias("admin", "services", "radicale", "edit"), _("CalDAV/CardDAV"), 58)
16         entry( {"admin", "services", "radicale", "edit"}, cbi("radicale") ).leaf = true
17         entry( {"admin", "services", "radicale", "logview"}, call("_logread") ).leaf = true
18         entry( {"admin", "services", "radicale", "startstop"}, call("_startstop") ).leaf = true
19         entry( {"admin", "services", "radicale", "status"}, call("_status") ).leaf = true
20 end
21
22 -- called by XHR.get from detail_logview.htm
23 function _logread()
24         -- read application settings
25         local uci     = UCI.cursor()
26         local logfile = uci:get("radicale", "radicale", "logfile") or "/var/log/radicale"
27         uci:unload("radicale")
28
29         local ldata=NXFS.readfile(logfile)
30         if not ldata or #ldata == 0 then
31                 ldata="_nodata_"
32         end
33         HTTP.write(ldata)
34 end
35
36 -- called by XHR.get from detail_startstop.htm
37 function _startstop()
38         local pid = get_pid()
39         if pid > 0 then
40                 SYS.call("/etc/init.d/radicale stop")
41                 NX.nanosleep(1)         -- sleep a second
42                 if NX.kill(pid, 0) then -- still running
43                         NX.kill(pid, 9) -- send SIGKILL
44                 end
45                 pid = 0
46         else
47                 SYS.call("/etc/init.d/radicale start")
48                 NX.nanosleep(1)         -- sleep a second
49                 pid = get_pid()
50                 if pid > 0 and not NX.kill(pid, 0) then
51                         pid = 0         -- process did not start
52                 end
53         end
54         HTTP.write(tostring(pid))       -- HTTP needs string not number
55 end
56
57 -- called by XHR.poll from detail_startstop.htm
58 function _status()
59         local pid = get_pid()
60         HTTP.write(tostring(pid))       -- HTTP needs string not number
61 end
62
63 -- Application / Service specific information functions ########################
64 function luci_app_name()
65         return  "luci-app-radicale"
66 end
67
68 function service_name()
69         return  "radicale"
70 end
71 function service_required()
72         return  "0.10-1"
73 end
74 function service_installed()
75         local v = ipkg_ver_installed("radicale-py2")
76         if not v or #v == 0 then v = ipkg_ver_installed("radicale-py3") end
77         if not v or #v == 0 then v = "0" end
78         return v
79 end
80 function service_ok()
81         return  ipkg_ver_compare(service_installed(),">=",service_required())
82 end
83
84 function app_title_main()
85         return  [[</a><a href="javascript:alert(']]
86                         .. I18N.translate("Version Information")
87                         .. [[\n\n]] .. luci_app_name()
88                         .. [[\n\t]] .. I18N.translate("Version") .. [[:\t]]
89                                 .. (ipkg_ver_installed(luci_app_name()) == ""
90                                         and I18N.translate("NOT installed")
91                                         or ipkg_ver_installed(luci_app_name()) )
92                         .. [[\n\n]] .. service_name() .. [[ ]] .. I18N.translate("required") .. [[:]]
93                         .. [[\n\t]] .. I18N.translate("Version") .. [[:\t]]
94                                 .. service_required() .. [[ ]] .. I18N.translate("or higher")
95                         .. [[\n\n]] .. service_name() .. [[ ]] .. I18N.translate("installed") .. [[:]]
96                         .. [[\n\t]] .. I18N.translate("Version") .. [[:\t]]
97                                 .. (service_installed() == "0"
98                                         and I18N.translate("NOT installed")
99                                         or service_installed())
100                         .. [[\n\n]]
101                 .. [[')">]]
102                 .. I18N.translate("Radicale CalDAV/CardDAV Server")
103 end
104 function app_title_back()
105         return  [[</a><a href="]]
106                         .. DISP.build_url("admin", "services", "radicale")
107                 .. [[">]]
108                 .. I18N.translate("Radicale CalDAV/CardDAV Server")
109 end
110 function app_description()
111         return  I18N.translate("The Radicale Project is a complete CalDAV (calendar) and CardDAV (contact) server solution.") .. [[<br />]]
112              .. I18N.translate("Calendars and address books are available for both local and remote access, possibly limited through authentication policies.") .. [[<br />]]
113              .. I18N.translate("They can be viewed and edited by calendar and contact clients on mobile phones or computers.")
114 end
115
116 -- other multiused functions ###################################################
117
118 --return pid of running process
119 function get_pid()
120         return tonumber(SYS.exec([[ps | grep "[p]ython.*[r]adicale" 2>/dev/null | awk '{print $1}']])) or 0
121 end
122
123 -- compare versions using "<=" "<" ">" ">=" "=" "<<" ">>"
124 function ipkg_ver_compare(ver1, comp, ver2)
125         if not ver1 or not (#ver1 > 0)
126         or not ver2 or not (#ver2 > 0)
127         or not comp or not (#comp > 0) then return nil end
128         -- correct compare string
129         if comp == "<>" or comp == "><" or comp == "!=" or comp == "~=" then comp = "~="
130         elseif comp == "<=" or comp == "<" or comp == "=<" then comp = "<="
131         elseif comp == ">=" or comp == ">" or comp == "=>" then comp = ">="
132         elseif comp == "="  or comp == "==" then comp = "=="
133         elseif comp == "<<" then comp = "<"
134         elseif comp == ">>" then comp = ">"
135         else return nil end
136
137         local av1 = UTIL.split(ver1, "[%.%-]", nil, true)
138         local av2 = UTIL.split(ver2, "[%.%-]", nil, true)
139
140         for i = 1, math.max(table.getn(av1),table.getn(av2)), 1  do
141                 local s1 = av1[i] or ""
142                 local s2 = av2[i] or ""
143                 local n1 = tonumber(s1)
144                 local n2 = tonumber(s2)
145
146                 -- one numeric and other empty string then set other to 0
147                 if n1 and not n2 and (not s2 or #s2 == 0) then n2 = 0 end
148                 if n2 and not n1 and (not s1 or #s1 == 0) then n1 = 0 end
149
150                 local nc = (n1 and n2)  -- numeric compare
151
152                 if nc then
153                         -- first "not equal" found return true
154                         if comp == "~=" and (n1 ~= n2) then return true end
155                         -- first "lower" found return true
156                         if (comp == "<" or comp == "<=") and (n1 < n2) then return true end
157                         -- first "greater" found return true
158                         if (comp == ">" or comp == ">=") and (n1 > n2) then return true end
159                         -- not equal then return false
160                         if (n1 ~= n2) then return false end
161                 else
162                         if comp == "~=" and (s1 ~= s2) then return true end
163                         if (comp == "<" or comp == "<=") and (s1 < s2) then return true end
164                         if (comp == ">" or comp == ">=") and (s1 > s2) then return true end
165                         if (s1 ~= s2) then return false end
166                 end
167         end
168         -- all equal then true
169         return true
170 end
171
172 -- read version information for given package if installed
173 function ipkg_ver_installed(pkg)
174         local version = ""
175         local control = io.open("/usr/lib/opkg/info/%s.control" % pkg, "r")
176         if control then
177                 local ln
178                 repeat
179                         ln = control:read("*l")
180                         if ln and ln:match("^Version: ") then
181                                 version = ln:gsub("^Version: ", "")
182                                 break
183                         end
184                 until not ln
185                 control:close()
186         end
187         return version
188 end
189
190 -- replacement of build-in Flag.parse of cbi.lua
191 -- modified to mark section as changed if value changes
192 -- current parse did not do this, but it is done AbstaractValue.parse()
193 function flag_parse(self, section)
194         local fexists = self.map:formvalue(
195                 luci.cbi.FEXIST_PREFIX .. self.config .. "." .. section .. "." .. self.option)
196
197         if fexists then
198                 local fvalue = self:formvalue(section) and self.enabled or self.disabled
199                 local cvalue = self:cfgvalue(section)
200                 if fvalue ~= self.default or (not self.optional and not self.rmempty) then
201                         self:write(section, fvalue)
202                 else
203                         self:remove(section)
204                 end
205                 if (fvalue ~= cvalue) then self.section.changed = true end
206         else
207                 self:remove(section)
208                 self.section.changed = true
209         end
210 end