Updating ChangeLog for 4.22.10
[centerim.git] / misc / cimextracthistory.pl
blobbaa8cb4ccd90042aec6da07da7da8513f272036d
1 #!/usr/bin/perl
3 use warnings;
4 use strict;
6 use Getopt::Std;
7 use File::Spec::Functions;
8 use Time::ParseDate;
9 use POSIX;
10 use Data::Dumper;
13 # Default values
14 my %defaults = ( format => '%Y/%m/%d [%H:%M:%S]',
15 basedir => catfile($ENV{HOME}, '.centerim'),
19 # Helper functions
20 sub usage {
21 # print error message if one is provided
22 print STDERR $_[0] . "\n" if $_[0];
24 print STDERR <<"EOT";
25 Usage: $0 [options]
27 Options:
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.
30 \t * ICQ: 123456789
31 \t * MSN: mmsnuser
32 \t * Jab: jjabberuser
33 \t * Yah: yyahoouser
34 \t * ...
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.
38 \t Format: see -f.
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.
45 Example:
46 \t$0 -u mfriendofmine -s "yesterday" -e "2 hours ago"
47 EOT
48 exit;
51 my %options;
53 sub init {
54 # get command line parameters and display usage if neccessary
55 my %params;
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
60 # required parameters
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.") };
64 # optional parameters
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.");
79 sub extract_history {
80 my $histfile = catfile($options{userdir}, "history");
82 my $histarray;
83 open my $HISTORY, '<', $histfile;
85 local $/ = "\f\n";
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
93 }xmso;
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 };
107 return $histarray;
110 sub prettyprint_history {
111 my ($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";
129 # Main program
130 init();
131 my $history = extract_history();
132 prettyprint_history($history);