Update soundtest
[grml-scripts.git] / usr_bin / cicqhist
1 #!/usr/bin/perl -w
2
3 # Julius Plenz <jp@cvmx.de>
4 # http://www.plenz.com/
5 #
6 # $Id: cicqhist,v 0.4 2004/07/20 18:33:36 plenz Exp $
7 #
8 # This script has just been written because of a simple purpose:
9 # To learn Perl. Or at least to make first few steps with perl.
10 # It just aims to be like http://centericq.de/misc/cicq-history.sh -
11 # and maybe it's even a bit more powerful... ;-)
12 #
13 # Released under the GNU GPL.
14
15 use strict;
16 use Getopt::Std;
17
18 my %options = ();
19 getopts ('d:hisv', \%options);
20
21 $\ = "\n";
22 $0 =~ s/^.*\///;
23
24 my ($VER, $DATE);
25 $VER =  '$Revision: 0.4 $';
26 $VER =~ s/^.*: ([0-9\.,]+)\s*\$$/$1/;
27 $DATE = '$Date: 2004/07/20 18:33:36 $';
28 $DATE =~ s/^.*: ([0-9\/]{10})[0-9: ]*\$$/$1/;
29 $DATE =~ s/\//-/g;
30
31 if ($options{"v"}) {
32     print $0, ' ', $VER, ' ', $DATE;
33     exit;
34 }
35
36 if (!$ARGV[0]     ||
37     $options{"h"} ||
38     $ARGV[0] eq "") {
39
40     # print help
41     print $0, ' ', $VER, ' ', $DATE;
42     print 'by Julius Plenz <jp@cvmx.de>';
43     print 'http://www.plenz.com/';
44     print '(just to learn Perl :-)';
45     print '';
46     print 'Usage:   ' . $0 . ' [-d <dir>] [-h] [-i] [-s] <number>';
47     print '-d ...   Use ... as cicq-directory (default: ~/.centericq)';
48     print '-h       Display this message';
49     print '-i       Display initials rather than full nicknames';
50     print '-s       Display shortened date (yymmdd hh:mm)';
51     print '-v       Display program version';
52     exit;
53 }
54
55
56 # Change this, if you want!
57 my $mynick = $ENV{"USER"};
58 # my $mynick = "Goofy";
59
60
61 # The main directory
62 my $cicqdir;
63 if ($options{"d"}) {
64     $cicqdir = $options{"d"};
65 } else {
66     $cicqdir  = $ENV{"HOME"} . '/.centericq';
67 }
68
69 # Three basic settings
70 my $number   = $ARGV[0];
71 my $infofile = "$cicqdir/$number/info";
72 my $histfile = "$cicqdir/$number/history";
73
74 # Getting the nickname
75 open (INFO, $infofile) || die ("Couldn't open file $infofile");
76 my @lines = <INFO>;
77
78 my $i = $#lines;
79 $i++;
80
81 my $nickname;
82 foreach (@lines) {
83     $i--;
84     if ($i == 6) {
85         chomp;
86         $nickname = $_;
87     }
88 }
89
90 close (INFO);
91
92
93 # Opening History
94 print "--- Displaying conversation with $nickname ---\n";
95
96 open (HIST, $histfile) || die ("Couldn't open file $histfile");
97
98 my $linenumber = 0;
99 my $nick = '';
100 my $displaytime;
101
102 while (<HIST>) {
103     chomp;
104     my $line = $_;
105
106     if ($line eq "\f") {
107         $linenumber = 0;
108         next;
109     }
110
111     if ($linenumber < 4) {
112         if ($line =~ /^IN$/) {
113             $nick = sprintf ('%-13s', $nickname);
114         }
115
116         if ($line =~ /^OUT$/) {
117             $nick = sprintf ('%-13s', $mynick);
118         }
119
120         if (/^[0-9]+$/) {
121             my $tstamp = $_;
122             chomp ($tstamp);
123
124             if ($tstamp < 3) {
125                 next;
126             }
127
128             my ($sec, $min, $hour, $mday, $mon, $year) =
129                 localtime ($tstamp);
130
131             if ($options{"s"}) {
132                 $year += 1900;
133                 $displaytime = sprintf ('%02d%02d%02d %02d:%02d  ',
134                     substr ($year, 2, 2), $mon, $mday, $hour, $min);
135             } else {
136                 $displaytime =
137                     sprintf ('[%02d.%02d.%02d, %02d:%02d:%02d] ',
138                     $mday, $mon, (1900+$year), $hour, $min, $sec);
139             }
140         }
141
142         $linenumber++;
143         if ($linenumber == 4) {
144
145             $\ = "";
146
147             if ($options{"i"}) {
148                     print $displaytime . substr ($nick, 0, 1) . ':  ';
149                 } else {
150                     print $displaytime . $nick . ':  ';
151                 }
152
153             $\ = "\n";
154         }
155
156     } else {
157         print $line;
158     }
159
160 }
161
162 close (HIST);
163
164 if ($options{"i"}) {
165     print '';
166     print '';
167     print ' 'x8 . $number .' = '. substr ($nickname, 0, 1) .
168         ' = ' . $nickname;
169     print ' 'x8 . substr ($mynick, 0, 1) . ' = ' . $mynick;
170 }
171
172 print "\n--- End of conversation with $nickname ---";
173
174 # EOF  vim: set et sm nu ft=perl