Debian Codegraph @

ireminder

raw
1 #!/usr/bin/perl
2 # -*-Perl-*-
3
4 #---
5 # $Id: ireminder 10752 2006-10-11 20:52:09Z nadim $
6 #
7 # ------------
8 # Description:
9 # ------------
10 # Copyright (c) 2006, Arabeyes, Nadim Shaikli
11 #
12 # This is a prayer (and other Islamic events) reminder program.
13 # Its a wrapper that uses Arabeyes.org's ITL package (ipraytime
14 # to be specific) to attain all its values. The script is meant
15 # to be light, simple and flexible.
16 #
17 # Based on a concept/idea from
18 # - Ahmed El-Mahmoudy (aelmahmoudy -at- users sf net)
19 #
20 # -----------------
21 # Revision Details: (Updated by Revision Control System)
22 # -----------------
23 # $Date: 2006-10-11 22:52:09 +0200 (Wed, 11 Oct 2006) $
24 # $Author: nadim $
25 # $Revision: 10752 $
26 # $Source$
27 #
28 # (www.arabeyes.org - under GPL license)
29 #---
30
31 # TODO:
32 # - have it fork in the background if a similar process doesn't already
33 # exists else kill the other process post a confirm (optionally)
34 # - have it print to the console not stdout (optionally)
35
36 # SAMPLES:
37 # - to engage a GUI:
38 # ireminder -execr "xmessage %m -nearmouse -timeout 30"
39 # ireminder -execr "xmessage %m" -execs "xmessage %m"
40 # ireminder -execr "xmessage Next salat is %s"
41
42 use Getopt::Long;
43
44 use POSIX;
45 use FileHandle;
46 STDOUT->autoflush(1);
47
48 ##
49 # Specify global variable values and init some Variables
50 $in_script = $0;
51 $in_script =~ s|^.*/([^/]*)|$1|;
52
53 ##
54 # Find how many spaces the scripts name takes up (nicety)
55 $in_spaces = $in_script;
56 $in_spaces =~ s/\S/ /g;
57
58 $version = " (v - %I%)";
59 $ID = "[$in_script]";
60
61 # Process the command line
62 &get_args();
63
64 # Make it simple, does user need help ?
65 if ($opt{help}) { &help(); }
66
67 # Account for the various ways a user might enter a list
68 %opt = %{&listify_input(\%opt, "skip")};
69 %opt = %{&listify_input(\%opt, "reminder")};
70
71 # Account for various places the ipraytime binary could reside-in
72 @bin_paths = ("/usr/bin/", "/usr/local/bin/", "");
73 foreach my $entry (@bin_paths)
74 {
75 $bin_path = "${entry}ipraytime";
76 if (-x $bin_path)
77 {
78 # OK, found something I can use
79 last;
80 }
81 }
82 $ipraytime = $opt{ipraytime} || $bin_path;
83
84 # Set some default reminder settings
85 @reminders = @{$opt{reminder}} ? @{$opt{reminder}} : (1, 5, 10, 20);
86
87 # Enumerate the events we'll track and report on
88 $DAY_TOT_SEC = (24 * 60 * 60);
89 @EVENTS = (qw/imsaak fajr shorooq zuhr asr maghrib isha/);
90
91 # Set some defaults
92 $doing_2moro = 0;
93 $time_to_use = time();
94
95 if (!-x $ipraytime)
96 {
97 &usage(1, "can't find '$ipraytime'");
98 }
99
100 # Continuously cycle through
101 for (;;)
102 {
103 # Get current start time
104 $ref_time_used = &get_time($time_to_use);
105 %time_used = %{$ref_time_used};
106
107 # Specify date to get info on
108 $date_to_use = sprintf( "%d%02d%02d",
109 $time_used{year},
110 $time_used{month},
111 $time_used{day} );
112
113 # Run the actual program executable and process its output
114 $ref_today_events = &get_ipraytime($date_to_use);
115 %today_events = %{$ref_today_events};
116
117 # Cycle through all the prayers (and events)
118 for ($i = 0; $i <= $#EVENTS; $i++)
119 {
120 # Skip any events a user might have noted on the command-line
121 if ( defined $opt{skip} )
122 {
123 foreach my $option (@{$opt{skip}})
124 {
125 # Make sure to make the compare case insensitive
126 if ( uc($option) eq uc($EVENTS[$i]) )
127 {
128 $skip_event = 1;
129 last;
130 }
131 }
132
133 # If a skip-hit occured, cycle out of the for loop
134 if ($skip_event)
135 {
136 $skip_event = 0;
137 next;
138 }
139 }
140
141 # Determine current time's placement within event times
142 $before_last = ( ($time_used{hour} <
143 $today_events{$EVENTS[$#EVENTS]}{hour}) ||
144 (($time_used{hour} ==
145 $today_events{$EVENTS[$#EVENTS]}{hour}) &&
146 ($time_used{min} <
147 $today_events{$EVENTS[$#EVENTS]}{min})) );
148 $after_current = ( ($time_used{hour} >
149 $today_events{$EVENTS[$i]}{hour}) ||
150 (($time_used{hour} ==
151 $today_events{$EVENTS[$i]}{hour}) &&
152 ($time_used{min} >=
153 $today_events{$EVENTS[$i]}{min})) );
154
155 # If we are dealing with next day (upon start), cycle through
156 if (!$before_last && !$doing_2moro) { last; }
157
158 # Skip the past while making sure we didn't go over
159 if ($before_last && $after_current)
160 {
161 next;
162 }
163 else
164 {
165 # If I'm here, then its immaterial which day I'm dealing with
166 $doing_2moro = 0;
167
168 # See how many seconds we have left till next prayer
169 if ($before_last)
170 {
171 # Get time difference between prayer and wallclock time
172 $sec_diff = ( $today_events{$EVENTS[$i]}{tot_sec} -
173 $time_used{tot_sec} );
174 }
175 else
176 {
177 # Dealing with next day prayer
178 # - Get portion of day left in today + tomorrow's
179 $sec_diff = ( ($DAY_TOT_SEC - $time_used{tot_sec}) +
180 $today_events{$EVENTS[$i]}{tot_sec} );
181 }
182
183 # Account for the reminder time and the time delta seconds
184 $sec_sleep = ( $sec_diff - $time_used{sec} );
185
186 # Tell user what to expect next
187 $str = "$ID NOTE - Next event '$EVENTS[$i]' is at ";
188 $str .= "$today_events{$EVENTS[$i]}";
189 &display(0, $EVENTS[$i], $str);
190
191 # Sort numerically descending
192 foreach $reminder (sort {$b <=> $a} @reminders)
193 {
194 $sec_reminder = ( $reminder * 60 );
195 if ($sec_sleep < $sec_reminder)
196 {
197 next;
198 }
199
200 $sec_sleep -= $sec_reminder;
201
202 # Take a nap and wait :-)
203 &do_reminder($EVENTS[$i],
204 $sec_sleep,
205 \%today_events,
206 $reminder);
207
208 $sec_sleep = $sec_reminder;
209 }
210
211 # Take a nap and wait (without reminders :-)
212 &do_reminder($EVENTS[$i],
213 $sec_sleep,
214 0,
215 0);
216
217 &do_alarm($EVENTS[$i], \%today_events);
218
219 # Take a nap and wait before next event/reminder
220 sleep(300);
221
222 # Establish new time for next prayer
223 # - This is opted for instead of accounting for passed sec's
224 $ref_time_used = &get_time(time());
225 %time_used = %{$ref_time_used};
226 }
227 }
228
229 # Cycle through & advance calc 1 day forward (ie. look at next day)
230 $time_to_use = ( time() + $DAY_TOT_SEC );
231 $doing_2moro = 1;
232 }
233
234 ### ###
235 ######## Procedures ########
236 ### ###
237
238 ##
239 # Print on screen passed-in string
240 sub display
241 {
242 my ($salat_now,
243 $salat_name,
244 $string) = @_;
245
246 # See if user is opting to use his/her own external trigger
247 if (defined $opt{execr} || ($salat_now && defined $opt{execs}))
248 {
249 # Escape various characters just in case external application cares
250 $string =~ s/\'/\\\'/g;
251 $string =~ s/\(/\\\(/g;
252 $string =~ s/\)/\\\)/g;
253
254 # Kill a previous application in case its still running
255 # - session/group PIDs (note '-')
256 if ( $cpid ) { kill "KILL", -$cpid; }
257
258 $ext_trigger = ( $salat_now ? $opt{execs} : $opt{execr} );
259
260 # Do various user substitutions
261 # - %m for normal printable message string
262 # - %s for salat name string
263 $ext_trigger =~ s/\%m/$string/g;
264 $ext_trigger =~ s/\%s/$salat_name/g;
265 $cpid = &fork_exec("$ext_trigger");
266 }
267 else
268 {
269 # Do some clean-up and backtrack per use command-line directives
270 if ($opt{inplace})
271 {
272 print "\r";
273 print " " x 78;
274 print "\r";
275 }
276 print $string;
277
278 if (!$opt{inplace})
279 {
280 print "\n";
281 }
282 }
283 }
284
285 ##
286 # Adjust user input to end-up with a proper array
287 sub listify_input
288 {
289 my ($ref_in_opt,
290 $in_str) = @_;
291
292 my (
293 %in_opt,
294 @in_arr,
295 @mod_arr,
296 );
297
298 %in_opt = %{$ref_in_opt};
299 @in_arr = @{$in_opt{$in_str}};
300
301 # Make sure to accept for instance,
302 # 1. -skip one -skip two
303 # 2. -skip "one two"
304 # 3. -skip "one, two"
305 foreach my $string (@in_arr)
306 {
307 if ($string =~ /\s+/)
308 {
309 my @sub_string = split(/[,]*\s+/, $string);
310 push (@mod_arr, @sub_string);
311 next;
312 }
313 push (@mod_arr, $string);
314 }
315
316 $in_opt{$in_str} = \@mod_arr;
317 return(\%in_opt);
318 }
319
320 ##
321 # Populate time hash/struct using the passed-in time value
322 sub get_time
323 {
324 my ($in_time) = @_;
325
326 my (
327 %tm,
328 );
329
330 # seconds, minutes, hours, day, month, year, day.week, day.year, DST
331 ($tm{sec},
332 $tm{min},
333 $tm{hour},
334 $tm{day},
335 $tm{month},
336 $tm{year},
337 $tm{dayofweek},
338 $tm{dayofyear},
339 $tm{dst}) = localtime($in_time);
340
341 # Some reability corrections
342 $tm{month} = ($tm{month} + 1);
343 $tm{year} = (1900 + $tm{year});
344
345 # Set some needed values
346 $tm{tot_min} = ($tm{min} + (60 * $tm{hour}));
347 $tm{tot_sec} = (60 * $tm{tot_min});
348
349 return(\%tm);
350 }
351
352 ##
353 # Populate event hash/struct based on ipraytime's output using date passed-in
354 sub get_ipraytime
355 {
356 my ($in_date) = @_;
357
358 my (
359 $aux,
360 @out_ipray,
361 $ipray,
362 @times,
363 );
364
365 if (defined $in_date)
366 {
367 $aux = "--date $in_date ";
368 }
369
370 @out_ipray = `$ipraytime $aux`;
371
372 # Date Fajr Shorooq Zuhr Asr Maghrib Isha
373 #--------------------------------------------------------------------
374 # [22-07-2004] 5:22 6:50 13:27 16:43 20:04 21:34
375 #
376 #Today's Imsaak : 5:17
377
378 foreach my $line (@out_ipray)
379 {
380 # Remove the carriage returns
381 chomp($line);
382
383 # Find the proper line to process
384 if ($line =~ /\[\d+-\d+-\d+\]/)
385 {
386 @entries = split (/\s\s+/, $line);
387 $ipray{fajr} = $entries[1];
388 $ipray{shorooq} = $entries[2];
389 $ipray{zuhr} = $entries[3];
390 $ipray{asr} = $entries[4];
391 $ipray{maghrib} = $entries[5];
392 $ipray{isha} = $entries[6];
393 }
394
395 # Grab today's imsaak time
396 if ($line =~ /^Today.*Imsaak\s*:\s*(\d+:\d+)\s*/)
397 {
398 $ipray{imsaak} = $1;
399 }
400 }
401
402 # Get the total number of seconds for each prayer and event
403 for (my $i = 0; $i <= $#EVENTS; $i++)
404 {
405 @times = split(/\:/, $ipray{$EVENTS[$i]});
406 $ipray{$EVENTS[$i]}{hour} = $times[0];
407 $ipray{$EVENTS[$i]}{min} = $times[1];
408 $ipray{$EVENTS[$i]}{tot_min} = ($ipray{$EVENTS[$i]}{min} +
409 (60 * $ipray{$EVENTS[$i]}{hour}));
410 $ipray{$EVENTS[$i]}{tot_sec} = (60 * $ipray{$EVENTS[$i]}{tot_min});
411
412 }
413
414 # Return the newly populated hash/structure
415 return (\%ipray);
416 }
417
418 ##
419 # Trigger a reminder
420 sub do_reminder
421 {
422 my ($in_name,
423 $in_time,
424 $ref_event_struct,
425 $in_rem) = @_;
426
427 my (
428 $name,
429 $str,
430 );
431
432 %event_struct = %{$ref_event_struct};
433 $name = ucfirst($in_name);
434
435 # Take a nap and wait
436 sleep($in_time);
437
438 # See if a reminder note is warranted
439 if ($in_rem > 0)
440 {
441
442 NAME_CASE:
443 {
444 if ($in_name eq "imsaak")
445 {
446 $str = "Fasting is to commence ($event_struct{$in_name})";
447 last NAME_CASE;
448 }
449 if ($in_name eq "shorooq")
450 {
451 $str = "Salat Al-Fajr expires ($event_struct{$in_name})";
452 last NAME_CASE;
453 }
454 #default
455 $str = "Salat Al-$name ($event_struct{$in_name})";
456 }
457
458 &play_sound();
459 &display(0, $in_name, "$ID REMINDER - $str in $in_rem minutes !!");
460 }
461 }
462
463 ##
464 # Trigger the alarm
465 sub do_alarm
466 {
467 my ($in_name,
468 $ref_event_struct) = @_;
469
470 my (
471 $name,
472 $event_struct,
473 $num_rings,
474 $str,
475 );
476
477 $name = ucfirst($in_name);
478 %event_struct = %{$ref_event_struct};
479 $num_rings = 4;
480
481 NAME_CASE:
482 {
483 if ($in_name eq "imsaak")
484 {
485 $str = "to start your Fast";
486 last NAME_CASE;
487 }
488 if ($in_name eq "shorooq")
489 {
490 $str = "notes passing of Salat Al-Fajr";
491 last NAME_CASE;
492 }
493 #default
494 $str = "for Salat Al-$name";
495 }
496
497 &display(1, $in_name, "$ID $event_struct{$in_name} - Time NOW $str !!");
498
499 for(my $i = 0; $i < $num_rings; $i++)
500 {
501 &play_sound();
502 sleep(1);
503 }
504 }
505
506 ##
507 # Play a sound file (if possible) else gimme a beep :-)
508 sub play_sound
509 {
510 print "\a";
511 }
512
513 ##
514 # Use this function instead of "system" (to catch Interrupts/^C and/or kills)
515 sub fork_exec
516 {
517 my ($command) = @_;
518
519 FORK_IT:
520 {
521 if ( $child_pid = fork ) # parent here
522 {
523 return($child_pid);
524 }
525 elsif ( defined $child_pid ) # child here (PID --> 0)
526 {
527 &POSIX::setsid(); # set current PID (child) to be session/group leader
528 exec "$command" or &err(1, "Can't exec '$command': $!");
529 }
530 elsif ( $! =~ /no more/i ) # No more Processes (sleep & retry ?)
531 {
532 # sleep 5; redo FORK_IT;
533 &err(1, "No more processes !!!");
534 }
535 else # weird fork error
536 {
537 &err(1, "Can't fork: $!");
538 }
539 }
540 }
541
542 ##
543 # Dump out an error message and possibly quit
544 sub err
545 {
546 my ($die_after,
547 $msg) = @_;
548
549 print "$ID ERROR: $msg\n";
550
551 if ( $die_after ) { exit(1); }
552 }
553
554 ##
555 # Print short usage info
556 sub usage
557 {
558 my ($die_after,
559 $err_msg) = @_;
560
561 # if (defined $err_msg) { print $err_msg; }
562 if (defined $err_msg) { &err(0, $err_msg); }
563 print qq
564 |Usage: $in_script [-ipraytime path]
565 $in_spaces [-skip event_name]
566 $in_spaces [-reminder minutes]
567 $in_spaces [-inplace]
568 $in_spaces [-execr cmd]
569 $in_spaces [-execs cmd]
570 $in_spaces [-help]
571 |;
572 if ( $die_after ) { exit(5); }
573 }
574
575 ##
576 # Print one-liner help
577 sub help
578 {
579 &usage(0);
580
581 print qq|
582 -> remind user visually and via audio of Islamic prayer times & misc events
583
584 Options:
585
586 [-ipraytime path] : Specify the executable path to program
587 [-skip event_name] : Specify name of prayer/event to skip (list ok)
588 [-reminder minutes] : Specify prior to how many minutes to remind (list ok)
589 [-inplace] : Specify to print output in-place (without scrolling)
590 [-execr cmd] : Specify an external application trigger for reminders
591 [-execs cmd] : Specify an external application trigger for salat NOW
592 [-help] : Produce this help screen
593
594 |;
595 exit(10);
596 }
597
598 ##
599 # Get the command line arguments
600 sub get_args
601 {
602 &GetOptions(
603 \%opt, # Hash to store all input options in
604 "ipraytime=s", # specify the binary ipraytime executable
605 "skip=s@", # array'ed list of skip events
606 "reminder=s@", # array'ed list of reminder times (minutes)
607 "inplace", # specify to print in-place outputs (no scroll)
608 "execr=s", # specify an external trigger (for reminders)
609 "execs=s", # specify an external trigger (for salat_now)
610 "help", # print a brief help screen
611 ) || ( $? = 257, &usage(1, "Invalid argument !!") );
612 }