html/mailcheck_mbox_flux.pl
1 #!/usr/bin/perl -w
2
3 use Irssi;
4
5 use vars qw($VERSION %IRSSI);
6 $VERSION = "0.1";
7 %IRSSI = (
8 authors => "Erkki Seppälä",
9 contact => "flux\@inside.org",
10 name => "Mail Check",
11 description => "Polls your unix mailbox for new mail",
12 license => "Public Domain",
13 url => "http://xulfad.inside.org/~flux/software/irssi/",
14 changed => "Mon Mar 4 23:25:18 EET 2002"
15 );
16
17 use strict;
18
19 sub getMessages( $ ) {
20 local *F;
21 open(F, $_[0]) or return ();
22 my $inHeaders = 0;
23 my $headers;
24 my %result = ();
25 my $time;
26 while (<F>) {
27 chomp;
28 if (/^From /) {
29 my @fields = /^From [^ ]+ (.*)/;
30 $time = $fields[0];
31 $inHeaders = 1;
32 } elsif ($inHeaders) {
33 if ($_ eq "") {
34 $result{$time} = $headers;
35
36 $inHeaders = 0;
37 $headers = {};
38 } else {
39 my @fields = /^([^:]+): (.*)$/;
40 if (@fields == 2) {
41 $headers->{$fields[0]} = $fields[1];
42 }
43 }
44 }
45 }
46 close(F);
47
48 return %result;
49 }
50
51 # assumes both headers are in time order
52 # format: From flux@xulfad.ton.tut.fi Wed Jan 24 23:44:00 2001
53 sub newMail ( $$ ) {
54 my ($box, $contents) = @_;
55 my @newMail;
56 foreach my $mail (keys %{$contents}) {
57 if (!exists $box->{contents}->{$mail}) {
58 push @newMail, {%{$contents->{$mail}}, BOX=>$box};
59 }
60 }
61 return @newMail;
62 }
63
64 sub checkMail( $ ) {
65 my $boxes = shift;
66 my @changed = ();
67 foreach my $box (keys %{$boxes}) {
68 # Irssi::print "Checking $box";
69 my @st = stat($box);
70 my $mtime = $st[9];
71 if ($mtime != $boxes->{$box}->{time}) {
72 my %contents = getMessages($box);
73 if ($boxes->{$box}->{time}) {
74 push @changed, newMail($boxes->{$box}, \%contents);
75 }
76 $boxes->{$box}->{contents} = \%contents;
77 $boxes->{$box}->{time} = $mtime;
78 }
79 }
80 return @changed;
81 }
82
83 sub coalesce {
84 while (@_) {
85 if (defined $_[0]) {
86 return $_[0];
87 }
88 shift;
89 }
90 return undef;
91 }
92
93 my @boxes = ("/var/spool/mail/flux", "/home/flux/mail/vv");
94 my %boxes;
95 # ("/var/spool/mail/flux" => {name=>"INBOX", time=>0} );
96
97 for (my $c = 0; $c < @boxes; ++$c) {
98 $boxes{$boxes[$c]}->{time} = 0;
99 if ($c == 0) {
100 $boxes{$boxes[$c]}->{name} = "INBOX";
101 } else {
102 my @f = $boxes[$c] =~ /([^\/]*)$/;
103 $boxes{$boxes[$c]}->{name} = $f[0];
104 }
105 }
106
107 sub check {
108 my @newMail = checkMail(\%boxes);
109 foreach my $mail (@newMail) {
110 my $row = $mail->{BOX}->{name} . " ::: " . $mail->{From} . ": " . coalesce($mail->{Subject}, "(no subject)");
111 Irssi::print($row);
112 # active_server()->print($row);
113 }
114 }
115
116 Irssi::timeout_add(10000, "check", "");
117
118 check();