Skip to content

Instantly share code, notes, and snippets.

@rkulla
Last active September 26, 2015 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkulla/1116533 to your computer and use it in GitHub Desktop.
Save rkulla/1116533 to your computer and use it in GitHub Desktop.
Daemon to play sound alert when you have new E-Mail
#!/usr/bin/perl -w
# gotmail.pl -By Ryan Kulla
# I wrote this in the '90s to help learn perl
# Notifies you audibly when you have new mail.
# Created for fun. Rename to gotmaild, add to startup scripts, to make real daemon.
use POSIX;
$MAILBOX = "/path/to/mbox"; # change this to your actual mailbox
$PLAY_SOUND = "mplayer /path/to/alert.mp3"; # change to preferred player and sound file
$DELAY_IN_SECONDS = 10; # Check for new mail every 10 seconds
sub start_daemon {
my $pid = fork;
exit if $pid;
die "Couldn't fork: $!" unless defined($pid);
POSIX::setsid() or die "Can't start a new session: $!";
sub signal_handler {
$time_to_die = 1;
}
$SIG{INT} = $SIG{TERM} = $SIG{HUP} = \&signal_handler;
}
# Currently only works with mbox style mail. If you use maildir style just tweak this
# function to get a running count of the files in your maildir directory.
sub check_for_new_mail {
my $mail_count = 0;
open(my $fh, "<", "$MAILBOX") or die "Can't open $MAILBOX: $!";
while (<$fh>) {
$mail_count++;
}
close($fh);
return $mail_count;
}
sub main_loop {
my $last_value = 0;
my $mail_count = 0;
until ($time_to_die) {
for ($mail_count = check_for_new_mail()) {
sleep($DELAY_IN_SECONDS);
}
if ($mail_count > $last_value) {
`$PLAY_SOUND`;
}
$last_value = $mail_count;
}
}
start_daemon();
main_loop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment