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