7 use File
::Spec
::Functions
;
14 my %defaults = ( format
=> '%Y/%m/%d [%H:%M:%S]',
15 basedir
=> catfile
($ENV{HOME
}, '.centerim'),
21 # print error message if one is provided
22 print STDERR
$_[0] . "\n" if $_[0];
28 \t-u <username> User name to print history from. This must be an account
29 \t name under the CenterIM basedir. This parameter is required.
35 \t-s <start> Timestamp to start from. This parameter is required.
36 \t You can use timestamps or strings like '3 hours ago'.
37 \t-e <end> Timestamp to end with. This parameter is required.
39 \t-f <format> Format to print timestamps in. Must be a strftime compatible
40 \t string. This parameter is optional.
41 \t-b <basedir> CenterIM basedir to use. This parameter is optional.
42 \t Defaults to $defaults{basedir}.
43 \t-h This help message.
46 \t$0 -u mfriendofmine -s "yesterday" -e "2 hours ago"
54 # get command line parameters and display usage if neccessary
56 usage
("Unrecognized parameter found.") unless getopt
('u:s:e:f:b:h', \
%params);
57 usage
() if $params{h
};
59 # put param$ters in %options hash and display usage if necessary
61 if ($params{u
}) { $options{username
} = $params{u
} } else { usage
("Required parameter -u not found.") };
62 if ($params{s
}) { $options{start
} = $params{s
} } else { usage
("Required parameter -s not found.") };
63 if ($params{e
}) { $options{end
} = $params{e
} } else { usage
("Required parameter -e not found.") };
65 $options{format
} = $params{f
} ?
$params{f
} : $defaults{format
};
66 $options{basedir
} = $params{b
} ?
$params{b
} : $defaults{basedir
};
68 $options{userdir
} = catfile
($options{basedir
}, $options{username
});
70 # sanity checking of parameters
71 $options{start
} = parsedate
($options{start
}) || usage
("Illegal date format for parameter -s.");
72 $options{end
} = parsedate
($options{end
}) || usage
("Illegal date format for parameter -e.");
73 ($options{start
}, $options{end
}) = ($options{end
}, $options{start
}) if $options{start
} > $options{end
};
75 -d
$options{basedir
} || usage
("Basedir '" . $options{basedir
} . "' is not a directory.");
76 -d
$options{userdir
} || usage
("User dir '" . $options{userdir
} . "' is not a directory.");
80 my $histfile = catfile
($options{userdir
}, "history");
83 open my $HISTORY, '<', $histfile;
86 while (my $msg_struct = <$HISTORY>) {
87 my ($msg_dir, $ts_sent, $ts_rcvd, $msg_text) =
88 $msg_struct =~ m
{ ^(IN
|OUT
)$ \n # direction of message
89 ^MSG
$ \n # message marker
90 ^(\d
+)$ \n # timestamp of sending
91 ^(\d
+)$ \n # timestamp of arriving
92 ^(.*) # message content
95 next if !defined $msg_dir; # no match, not a message
96 next if $ts_sent < $options{start
};
97 last if $ts_sent > $options{end
};
99 # UNIXize newlines and remove end-of-record marker
100 $msg_text =~ s/\r\n/\n/go;
101 $msg_text =~ s/(?:\f|\n)+$//go;
103 push @
$histarray, { direction
=> $msg_dir, ts_sent
=> $ts_sent, ts_arrived
=> $ts_rcvd, message
=> $msg_text };
110 sub prettyprint_history
{
113 for my $hist_item (@
$history) {
114 my $timestamp = strftime
$options{format
}, localtime $hist_item->{ts_sent
};
115 $timestamp .= " {rcv @ " . stftime
($options{format
}, localtime $hist_item->{ts_arrived
}) . " }" if ($hist_item->{ts_sent
} != $hist_item->{ts_arrived
});
117 my $direction = ($hist_item->{direction
} eq 'IN') ?
'<<<' : '>>>';
118 my $message = $hist_item->{message
};
120 # properly indent multiple lines
121 my $indent = ' ' x
(length($timestamp) + 1 + length($direction) + 1);
122 $message =~ s/\n/\n$indent/g;
124 print "$timestamp $direction $message\n";
131 my $history = extract_history
();
132 prettyprint_history
($history);