179
|
1 #!/usr/bin/perl -w |
|
2 # |
|
3 # usage: cicq2mcabber.pl cicqhistoryfile > mcabberhistoryfile |
|
4 # Convert a centericq history file to mcabber format |
|
5 # |
|
6 # See histolog.c for the mcabber format. |
|
7 # |
|
8 # MiKael, 2005/05/05 |
|
9 |
|
10 use strict; |
|
11 |
|
12 my $line; |
|
13 my $inblock = 0; |
|
14 |
|
15 my %bdata = (); |
|
16 |
|
17 sub print_entry() |
|
18 { |
|
19 my ($type, $info, $date, $len, $data); |
|
20 |
|
21 return unless(defined $bdata{"msg"}); |
|
22 |
|
23 if ($bdata{"type"} eq "MSG") { |
|
24 $type = "M"; |
|
25 if ($bdata{"inout"} eq "OUT") { |
|
26 $info = "S"; |
|
27 } elsif ($bdata{"inout"} eq "IN") { |
|
28 $info = "R"; |
|
29 } else { |
|
30 print STDERR "Neither IN nor OUT!?\n"; |
|
31 return; |
|
32 } |
|
33 } else { |
|
34 print STDERR "Data type not handled.\n"; |
|
35 return; |
|
36 } |
|
37 $date = $bdata{"timestamp"}; |
|
38 $len = $bdata{"nb"}; |
|
39 $data = $bdata{"msg"}; |
|
40 |
242
|
41 # Date conversion to iso8601 |
|
42 my ($ss,$mm,$hh,$DD,$MM,$YYYY) = gmtime($date); |
|
43 $date = sprintf "%04d%02d%02dT%02d:%02d:%02dZ", |
|
44 $YYYY+1900, $MM+1,$DD,$hh,$mm,$ss; |
|
45 |
|
46 printf("%s%s %18.18s %03d %s", $type, $info, $date, $len, $data); |
179
|
47 } |
|
48 |
|
49 while ($line = <>) { |
|
50 chomp $line; |
|
51 # The separator is ^L ; I use substr to exclude the EOL |
|
52 if ($line eq "") { |
|
53 print_entry() if ($inblock); |
|
54 # reset data for new block |
|
55 %bdata = (); |
|
56 $inblock = 1; |
|
57 next; |
|
58 } |
|
59 # Skip garbage or unrecognized stuff... |
|
60 if (!$inblock) { |
|
61 print STDERR "Skipping line\n"; |
|
62 next; |
|
63 } |
|
64 |
|
65 # 1 IN/OUT line |
|
66 unless (exists $bdata{"inout"}) { |
|
67 if (($line eq "IN") || ($line eq "OUT")) { |
|
68 $bdata{"inout"} = $line; |
|
69 } else { |
|
70 print STDERR "No IN/OUT information ($line)!\n"; |
|
71 $inblock = 0; |
|
72 } |
|
73 next; |
|
74 } |
|
75 # 1 type line (MSG...) |
|
76 unless (exists $bdata{"type"}) { |
|
77 # We don't handle NOTE and AUTH yet. |
|
78 if ($line eq "MSG") { |
|
79 $bdata{"type"} = $line; |
|
80 } else { |
|
81 print STDERR "Not a MSG type ($line)\n"; |
|
82 $inblock = 0; |
|
83 } |
|
84 next; |
|
85 } |
|
86 # 2 date lines |
|
87 unless (exists $bdata{"timestamp"}) { |
|
88 $bdata{"timestamp"} = $line; |
|
89 last unless defined ($line = <>); |
|
90 chomp $line; |
|
91 $bdata{"timestamp2"} = $line; |
|
92 if (! $bdata{"timestamp2"} eq $bdata{"timestamp2"}) { |
|
93 print STDERR "Timestamps differ...\n"; |
|
94 } |
|
95 next; |
|
96 } |
|
97 # ... The message itself |
|
98 $line =~ s/ |
|
99 $//; |
|
100 if (exists $bdata{"msg"}) { |
|
101 $bdata{"msg"} .= $line."\n"; |
|
102 $bdata{"nb"}++; |
|
103 } else { |
|
104 $bdata{"msg"} = $line."\n"; |
|
105 $bdata{"nb"} = 0; |
|
106 } |
|
107 } |
|
108 print_entry() if ($inblock); |
|
109 |