summaryrefslogtreecommitdiffstats
path: root/mailhops
blob: d42caa01275490a1d6466f9d0568d0157440e076 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/perl -w
# Copyright (c) 1999 Marius Gedminas 
# Shows the route of an Internet mail message
# Version 0.0.1pre-alpha
#
# Patched by Roland Rosenfeld 
# $ Id: mailhops,v 1.3 2000/01/25 20:18:24 roland Exp roland $

use strict;
use POSIX qw(mktime);
use Date::Parse;

my $verbose = 0;

# Setup
my %Months = (
    Jan => 1,
    Feb => 2,
    Mar => 3,
    Apr => 4,
    May => 5,
    Jun => 6,
    Jul => 7,
    Aug => 8,
    Sep => 9,
    Oct => 10,
    Nov => 11,
    Dec => 12,
    );

# Read headers
$/ = '';
my $head = <>;
$head =~ s/\n\s+/ /g;
my @headers = split("\n", $head);

# Parse headers
my @hops;
for (@headers) {
  next unless /^(>?Received|Date):/;
  my $time;
  my $host;
  my $from;
  if (/^Date:\s+(.*)/) {
    $host = "Date:";
    $time = $1;
    $from = "";
  } else {
    $host = "(unknown)";
    $host = $1 if /\sby\s+([a-z0-9\-_+.]+)\s/ && $1 ne "uid";
    $from = "(unknown)";
    $from = $1 if /\sfrom\s+([a-z0-9\-_+.]+(?:\s+[(].+?[)]))\s/;
    $time = "(unknown)";
    $time = $1 if /;\s+(.+)$/;
    $time =~ s/using.*//;
  }

  my $epoch = str2time ($time);

  unshift @hops, { HOST => $host, FROM => $from, TIME => $epoch};
}

# Print output
print "    Host                            Date received (local)   Lag       Total lag\n";
my $nr = 0;
my ($first, $prev);
for (@hops) {
  my $host = $_->{HOST};
  my $from = $_->{FROM};
  my $time = $_->{TIME};
  $first = $prev = $time unless defined $first;
  printf "%2d. %-31.31s", ++$nr, $host;
  do { print "\n"; next } unless defined $time;

  my $delta = $time - $prev;
  my $neg = $delta < 0; $delta = abs($delta);
  my $delta_h = int($delta / 3600);
  my $delta_m = int(($delta - $delta_h * 3600) / 60);
  my $delta_s = ($delta - $delta_h * 3600 - $delta_m * 60);

  my ($sec,$min,$hour,$day,$mon,$year,undef,undef,$dst) = localtime($time);

  printf "  %4d-%02d-%02d  %02d:%02d:%02d  %s%02d:%02d:%02d",
         1900+$year, $mon+1, $day, $hour, $min, $sec,
	 $neg ? '-' : ' ', $delta_h, $delta_m, $delta_s;

  $delta = $time - $first;
  $neg = $delta < 0; $delta = abs($delta);
  $delta_h = int($delta / 3600);
  $delta_m = int(($delta - $delta_h * 3600) / 60);
  $delta_s = ($delta - $delta_h * 3600 - $delta_m * 60);
  printf "  %s%02d:%02d:%02d\n",
	 $neg ? '-' : ' ', $delta_h, $delta_m, $delta_s;
  print  "     from $from\n" if $verbose;
  $prev = $time;
}