5 =head1 NAME $RCSfile: announceEmail.pl,v $
7 Monitors an IMAP Server and announce incoming emails by extracting the subject
8 line and from line and then pushing that into "GoogleTalk".
16 Andrew DeFaria <Andrew@DeFaria.com>
24 Thu Apr 4 13:40:10 MST 2019
28 $Date: 2019/04/04 13:40:10 $
34 Usage: announceEmail.pl [-usa|ge] [-h|elp] [-v|erbose] [-de|bug] [-da|emon]
35 [-use|rname <username>] [-p|assword <password>]
39 -usa|ge: Print this usage
41 -v|erbose: Verbose mode (Default: -verbose)
42 -de|bug: Turn on debugging (Default: Off)
43 -da|emon: Run in daemon mode (Default: -daemon)
44 -use|rname: User name to log in with (Default: $USER)
45 -p|assword: Password to use (Default: prompted)
46 -i|map: IMAP server to talk to (Default: defaria.com)
47 -s|leep: Number of minutes to sleep inbetween checking mail (Default: 1)
51 This script will connect to an IMAP server, login and then monitor the user's
52 INBOX. When new messages arrive it will extract the From address and Subject
53 from the message and compose a message to be used by "Google Talk" to announce
54 the email. The message will be similar to:
56 "<From> emailed <Subject>"
66 use Net::IMAP::Simple;
70 use lib "$FindBin::Bin/../lib";
76 my $defaultIMAPServer = 'defaria.com';
77 my $defaultSleeptime = 1;
83 usage => sub { pod2usage },
84 help => sub { pod2usage(-verbose => 2)},
85 verbose => sub { set_verbose },
86 debug => sub { set_debug },
88 username => $ENV{USER},
89 password => $ENV{PASSWORD},
90 imap => $defaultIMAPServer,
91 sleep => $defaultSleeptime,
97 for (my $i = 1; $i <= $IMAP->status; $i++) {
98 $unseenMsgs{$i} = 0 unless $IMAP->seen($i);
105 $log->msg("Connecting to $opts{imap} as $opts{username}");
107 $IMAP = Net::IMAP::Simple->new($opts{imap}) ||
108 error("Unable to connect to IMAP server $opts{imap}: " . $Net::IMAP::Simple::errstr, 1);
110 $log->msg("Connected");
112 $log->msg("Logging onto $opts{imap} as $opts{username}");
114 unless ($IMAP->login($opts{username}, $opts{password})) {
115 $log->err("Login to $opts{imap} as $opts{username} failed: " . $IMAP->errstr, 1);
118 $log->msg("Logged on");
120 # Focus on INBOX only
121 $IMAP->select('INBOX');
123 # Setup %unseen to have each unseen message index set to 0 meaning not read
125 %unseen = unseenMsgs;
129 $log->msg("Monitoring email");
132 # First close and reselect the INBOX to get its current status
134 $IMAP->select('INBOX');
136 # Go through all of the unseen messages and add them to %unseen if they were
137 # not there already from a prior run and read
138 my %newUnseen = unseenMsgs;
140 # Now clean out any messages in %unseen that were not in the %newUnseen and
141 # marked as previously read
143 if (defined $newUnseen{$_}) {
145 delete $newUnseen{$_};
152 for (keys %newUnseen) {
155 my $email = Email::Simple->new(join '', @{$IMAP->top($_)});
157 my $from = $email->header('From');
159 # Extract the name only when the email is of the format "name <email>"
160 if ($from =~ /^(.*)\<(\S*)>/) {
161 $from = $1 if $1 ne '';
164 my $subject = $email->header('Subject');
166 if ($subject =~ /=?\S+?(Q|B)\?(.+)\?=/) {
167 $subject = decode_base64($2);
171 my $logmsg = "From $from $subject";
172 my $msg = "Message from $from... " . quotemeta $subject;
181 my $cmd = "/usr/local/bin/gt \"$msg\"";
183 my ($status, @output) = Execute $cmd;
186 $log->err("Unable to execute $cmd" . join("\n", @output));
192 verbose "Sleeping for $opts{sleep} minutes";
193 sleep 60 * $opts{sleep};
194 verbose "Ah that was refreshing!";
199 $IMAP->quit if $IMAP;
216 unless ($opts{password}) {
217 verbose "I need $opts{username}'s password";
218 $opts{password} = GetPassword;
221 EnterDaemonMode if $opts{daemon};
225 timestamped => 'yes',