]>
iEval git - app-lastmsg.git/blob - lib/App/Lastmsg.pm
8 $Config::Auto
::DisablePerl
= 1;
11 use List
::Util qw
/max/;
12 use POSIX qw
/strftime/;
14 our $OUTPUT_FILEHANDLE = \
*STDOUT
;
15 our $VERSION = '0.001002';
18 push @DEFAULT_INBOX, "/var/mail/$ENV{USER}" if exists $ENV{USER
};
19 push @DEFAULT_INBOX, "$ENV{HOME}/Maildir" if exists $ENV{HOME
};
22 my $config = Config
::Auto
->new(format
=> 'yaml')->parse;
23 die "No configuration file found\n" unless $config;
24 die "No addresses to track listed in config\n" unless $config->{track
};
26 $config->{inbox
} //= [];
27 $config->{sent
} //= [];
28 $config->{inbox
} = [$config->{inbox
}] unless ref $config->{inbox
};
29 $config->{sent
} = [$config->{sent
}] unless ref $config->{sent
};
30 $config->{inbox
} = \
@DEFAULT_INBOX unless @
{$config->{inbox
}};
32 my %track = %{$config->{track
}};
33 my %addr_to_id = map {
35 my $track = $track{$id};
36 $track = [$track] unless ref $track;
37 map { $_ => $id } @
$track
40 my (%lastmsg, %lastaddr);
42 my $process_message = sub {
43 my ($msg, @people) = @_;
44 for my $addr (@people) {
45 ($addr) = $addr =~ /<\s*(.+)\s*>/ if $addr =~ /</;
48 my $id = $addr_to_id{$addr};
50 my $date = str2time
$msg->header_raw('Date');
51 if (!exists $lastmsg{$id} || $lastmsg{$id} < $date) {
52 $lastmsg{$id} = $date;
53 $lastaddr{$id} = $addr;
58 for my $folder (@
{$config->{inbox
}}) {
59 next unless -e
$folder;
60 say "Scanning $folder (inbox)" if $ENV{LASTMSG_DEBUG
};
61 my $folder = Email
::Folder
->new($folder);
62 while (my $msg = $folder->next_message) {
63 my ($from) = grep { /^from$/i } $msg->header_names;
64 $from = $msg->header_raw($from);
65 if ($ENV{LASTMSG_DEBUG
}) {
66 my $mid = grep { /^message-id$/i } $msg->header_names;
67 say 'Processing ', $msg->header_raw('Message-ID'),
68 " from $from" if $ENV{LASTMSG_DEBUG
};
70 $process_message->($msg, $from);
74 for my $folder (@
{$config->{sent
}}) {
75 next unless -e
$folder;
76 say "Scanning $folder (sent)" if $ENV{LASTMSG_DEBUG
};
77 my $folder = Email
::Folder
->new($folder);
78 while (my $msg = $folder->next_message) {
79 my @hdrs = grep { /^(?:to|cc|bcc)$/i } $msg->header_names;
82 @people = (@people, split /,/, $msg->header_raw($hdr));
84 if ($ENV{LASTMSG_DEBUG
}) {
85 my $mid = grep { /^message-id$/i } $msg->header_names;
86 say 'Processing ', $msg->header_raw($mid),
87 ' sent to ', join ',', @people if $ENV{LASTMSG_DEBUG
};
89 $process_message->($msg, @people);
93 my $idlen = max
map { length } keys %track;
94 my $addrlen = max
map { length } values %lastaddr;
96 for (sort { $lastmsg{$b} <=> $lastmsg{$a} } keys %lastmsg) {
97 my $time = strftime
'%c', localtime $lastmsg{$_};
98 printf $OUTPUT_FILEHANDLE "%-${idlen}s %-${addrlen}s %s\n", $_, $lastaddr{$_}, $time;
101 for (grep { !exists $lastmsg{$_} } sort keys %track) {
102 printf $OUTPUT_FILEHANDLE "%-${idlen}s %-${addrlen}s NOT FOUND\n", $_, ''
113 App::Lastmsg - last(1) semblance for your inbox
122 This module contains the implementation of the L<lastmsg(1)> script.
123 See that script's documentation for information on what it does.
131 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
133 =head1 COPYRIGHT AND LICENSE
135 Copyright (C) 2016 by Marius Gavrilescu
137 This library is free software; you can redistribute it and/or modify
138 it under the same terms as Perl itself, either Perl version 5.24.1 or,
139 at your option, any later version of Perl 5 you may have available.
This page took 0.050273 seconds and 4 git commands to generate.