+ $log->dbug("Processing new unseen messages");
+ for (keys %newUnseen) {
+ next if $unseen{$_};
+
+ my $envelope = $IMAP->fetch($_, '(envelope)');
+ my $from = $envelope->{$_}{envelope}{From};
+ my $subject = $envelope->{$_}{envelope}{Subject};
+ $subject //= 'Unknown subject';
+
+ # Extract the name only when the email is of the format "name <email>"
+ if ($from =~ /^"?(.*?)"?\s*\<(\S*)>/) {
+ $from = $1 if $1 ne '';
+ } # if
+
+ if ($subject =~ /=?\S+?(Q|B)\?(.+)\?=/) {
+ $subject = decode_base64($2);
+ } # if
+
+ # Google Talk doesn't like #
+ $subject =~ s/\#//g;
+
+ # Remove long strings of numbers like order numbers. They are uninteresting
+ my $longNumber = 5;
+ $subject =~ s/\s+\S*\d{$longNumber,}\S*\s*//g;
+
+ # Now speak it!
+ my $logmsg = "From $from $subject";
+
+ my $greeting = $greetings[int rand $#greetings];
+ my $msg = "$greeting from $from... $subject";
+ my $hour = (localtime)[2];
+
+ # Only announce if after 6 Am. Note this will announce up until
+ # midnight but that's ok. I want midnight to 6 Am as silent time.
+ $log->dbug("About to speak/log");
+ if ($hour >= 7) {
+ $log->msg($logmsg);
+ $log->dbug("Calling speak");
+ speak $msg, $log;
+ } else {
+ $log->msg("$logmsg [silent nighttime]");
+ } # if
+
+ $unseen{$_} = 1;
+ } # for