diff --git a/config/praytime.ini b/config/praytime.ini index 5841421..6c6c8cd 100644 --- a/config/praytime.ini +++ b/config/praytime.ini @@ -30,5 +30,7 @@ isha = [Cron] # timings updating time time = 00:00 -# cron file +# crontab user playing adhan +user = a-sansara +# crontab update time - root required path = /etc/cron.d/praytime diff --git a/src/Pluie.PrayTime.vala b/src/Pluie.PrayTime.vala index 6a7da8c..c1f980c 100644 --- a/src/Pluie.PrayTime.vala +++ b/src/Pluie.PrayTime.vala @@ -188,7 +188,7 @@ class Pluie.PrayTime : GLib.Object int status; try { Process.spawn_command_line_sync ( - "curl "+url, + "curl "+url, out response, out std_error, out status @@ -199,20 +199,98 @@ class Pluie.PrayTime : GLib.Object return response; } + + private int get_user_crontab_content (string user, out string response) + { + stdout.printf("getting %s crontab content\n", user); + int status; + string std_error; + try { + Process.spawn_command_line_sync ( + "crontab -l -u "+user, + out response, + out std_error, + out status + ); + } catch (SpawnError e) { + stderr.printf ("%s\n", e.message); + } + return status; + } + + + private int install_user_crontab (string user, string path) + { + int status; + string std_error; + string response; + try { + Process.spawn_command_line_sync ( + "crontab %s %s".printf (path, user), + out response, + out std_error, + out status + ); + } catch (SpawnError e) { + stderr.printf ("%s\n", e.message); + } + stdout.printf ("install crontab %s :\n%s\n", user, response); + return status; + } + + + private string? get_user_crontab (ref string user, ref string content) + { + string data = ""; + string udata; + try { + if (this.get_user_crontab_content (user, out udata) == 0) { + var regex = new Regex (Path.build_filename (this.bin, "praytime").escape ()); + foreach (string line in udata.split ("\n")) { + if (!regex.match (line) && line != "") { + data += line + "\n"; + } + } + data += "\n" + content + "\n"; + } + } + catch (RegexError e) { + data = null; + stdout.printf ("Error %s\n", e.message); + } + stdout.printf ("crontab %s content :\n%s\n", user, data); + return data; + } + + private void set_cron (GLib.DateTime date) { try { - string bin = "root "+Path.build_filename(this.bin, "praytime"); + string bin = Path.build_filename(this.bin, "praytime"); string cron_path = this.get_config ("path", "Cron"); + string user = this.get_config ("user", "Cron"); string[] update = this.get_config ("time", "Cron").split (":", 2); - string content = "# %s\n%d %d * * * %s %s\n".printf (date.format ("%c"), int.parse (update[1]), int.parse(update[0]) , bin, "cron"); - KeyFile k = this.load_config ("praytime.daily.ini"); - foreach (string pray in PrayTime.PRAYLIST) { - string[] time = k.get_string ("Praytime", pray.down ()).split (":", 2); - content += "%s %s * * * %s %s %s\n".printf (time[1], time[0] , bin, "play", pray); - } + string content = "# %s\n%d %d * * * root %s cron\n".printf (date.format ("%c"), int.parse (update[1]), int.parse (update[0]) , bin); bool done = FileUtils.set_contents (cron_path, content); stdout.printf ("\n updating %s : %s\n", cron_path, done ? "ok" : "ko"); + if (done) { + KeyFile k = this.load_config ("praytime.daily.ini"); + content = "# > autogenerated by %s %s\n".printf(bin, date.format ("%c")); + string[] time = null; + foreach (string pray in PrayTime.PRAYLIST) { + time = k.get_string ("Praytime", pray.down ()).split (":", 2); + content += "%s %s * * * sh -c \"DISPLAY=:0 %s play %s\"\n".printf (time[1], time[0] , bin, pray); + } + content += "# < autogenerated by %s\n".printf(bin); + cron_path = Path.build_filename (Environment.get_tmp_dir (), "praytime.crontab"); + content = this.get_user_crontab (ref user, ref content); + if (content != null) { + if (FileUtils.set_contents (cron_path, content)) { + this.install_user_crontab (user, cron_path); + stdout.printf ("\n updating crontab %s : %s\n", user, done ? "ok" : "ko"); + } + } + } } catch (GLib.KeyFileError e) { this.on_error (e.message);