]>
iEval git - app-lastmsg.git/blob - Lastmsg.pm
8f7be5ed663814c2b79960e88a2d5cf757965b05
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.002';
18 push @DEFAULT_INBOX, "/var/mail/$ENV{USER}" if exists $ENV{USER
};
19 push @DEFAULT_INBOX, "$ENV{HOME}/Maildir" if exists $ENV{HOME
};
21 sub format_time
{ strftime
'%c', localtime shift }
24 my $config = Config
::Auto
->new(format
=> 'yaml')->parse;
25 die "No configuration file found\n" unless $config;
26 die "No addresses to track listed in config\n" unless $config->{track
};
28 $config->{inbox
} //= [];
29 $config->{sent
} //= [];
30 $config->{inbox
} = [$config->{inbox
}] unless ref $config->{inbox
};
31 $config->{sent
} = [$config->{sent
}] unless ref $config->{sent
};
32 $config->{inbox
} = \
@DEFAULT_INBOX unless @
{$config->{inbox
}};
34 my %track = %{$config->{track
}};
35 my %addr_to_id = map {
37 my $track = $track{$id};
38 $track = [$track] unless ref $track;
39 map { $_ => $id } @
$track
42 my (%lastmsg, %lastaddr);
44 my $process_message = sub {
45 my ($msg, @people) = @_;
46 for my $addr (@people) {
47 ($addr) = $addr =~ /<\s*(.+)\s*>/ if $addr =~ /</;
50 my $id = $addr_to_id{$addr};
52 my $date = str2time
$msg->header_raw('Date');
53 if (!exists $lastmsg{$id} || $lastmsg{$id} < $date) {
54 $lastmsg{$id} = $date;
55 $lastaddr{$id} = $addr;
60 for my $folder (@
{$config->{inbox
}}) {
61 next unless -e
$folder;
62 say STDERR
"Scanning $folder (inbox)" if $ENV{LASTMSG_DEBUG
};
63 my $folder = Email
::Folder
->new($folder);
64 while (my $msg = $folder->next_message) {
65 my ($from) = grep { /^from$/i } $msg->header_names;
66 $from = $msg->header_raw($from);
67 if ($ENV{LASTMSG_DEBUG
}) {
68 my ($mid) = grep { /^message-id$/i } $msg->header_names;
69 say STDERR
'Processing ', $msg->header_raw($mid), " from $from";
71 $process_message->($msg, $from);
75 for my $folder (@
{$config->{sent
}}) {
76 next unless -e
$folder;
77 say STDERR
"Scanning $folder (sent)" if $ENV{LASTMSG_DEBUG
};
78 my $folder = Email
::Folder
->new($folder);
79 while (my $msg = $folder->next_message) {
80 my @hdrs = grep { /^(?:to|cc|bcc)$/i } $msg->header_names;
83 @people = (@people, split /,/, $msg->header_raw($hdr));
85 if ($ENV{LASTMSG_DEBUG
}) {
86 my ($mid) = grep { /^message-id$/i } $msg->header_names;
87 say STDERR
'Processing ', $msg->header_raw($mid),
88 ' sent to ', join ',', @people;
90 $process_message->($msg, @people);
94 my $idlen = max
map { length } keys %track;
95 my $addrlen = max
map { length } values %lastaddr;
97 for (sort { $lastmsg{$b} <=> $lastmsg{$a} } keys %lastmsg) {
98 my $time = format_time
$lastmsg{$_};
99 printf $OUTPUT_FILEHANDLE "%-${idlen}s %-${addrlen}s %s\n", $_, $lastaddr{$_}, $time;
102 for (grep { !exists $lastmsg{$_} } sort keys %track) {
103 printf $OUTPUT_FILEHANDLE "%-${idlen}s %-${addrlen}s NOT FOUND\n", $_, ''
114 App::Lastmsg - last(1) semblance for your inbox
123 This module contains the implementation of the L<lastmsg(1)> script.
124 See that script's documentation for information on what it does.
132 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
134 =head1 COPYRIGHT AND LICENSE
136 Copyright (C) 2016-2017 by Marius Gavrilescu
138 This library is free software; you can redistribute it and/or modify
139 it under the same terms as Perl itself, either Perl version 5.24.1 or,
140 at your option, any later version of Perl 5 you may have available.
This page took 0.053142 seconds and 3 git commands to generate.