1
0

radicale.lua 8.4 KB

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