1use strict;
2use vars qw($VERSION %IRSSI);
3
4use Irssi;
5
6$VERSION = '1.00';
7%IRSSI = (
8    authors     => 'Pascal Hakim',
9    contact     => 'pasc@redellipse.net',
10    name        => 'topic-diff',
11    description => 'This script shows you changes in the topic. ',
12    license     => 'GPL'
13);
14
15my %topics;
16
17sub new_channel {
18    my ($channel) = @_;
19    $topics{$channel->{server}->{tag}."_".$channel->{name}} = $channel->{topic};
20}
21
22sub new_topic {
23    my ($server, $channel, $topic, $user, $real) = @_;
24    my $i;
25    my $diff;
26    my $i = 0;
27    my $j = 0;
28    my $k = 0;
29
30#    $server->print ($channel, $server->{tag});
31
32    if ($topics{$server->{tag}."_".$channel}) {
33	$topics{$server->{tag}."_".$channel} =~ s/^ +| +$//g;
34	$topic =~ s/^ +| +$//g;
35	my @original = split /\s*\|\s*|\s+-\s+/, $topics{$server->{tag}."_".$channel};
36	my @modified = split /\s*\|\s*|\s+-\s+/, $topic;
37
38
39      outer: while( $i <= $#original) {
40	  if ($j <= $#modified && $original[$i] eq $modified[$j]) {
41	      $modified[$j] = '';
42	      $i += 1;
43	      $j += 1;
44	      next;
45
46	  }  else {
47	      # First two don't match, check the rest of the list
48	      for ($k = $j ; $k <= $#modified; $k++) {
49		  if ($modified[$k] eq $original[$i])
50		  {
51		      $modified[$k] = '';
52		      $i += 1;
53		      next outer;
54		  }
55	      }
56	      $diff = ($diff ? $diff." | " : "").$original[$i];
57	      $i += 1;
58	  }
59      }
60
61
62	if ($diff ne '') { $server->print ($channel, "Topic: -: ".$diff);}
63
64	$diff = join " | ", (grep {$_ ne ''} @modified);
65
66	if ($diff ne '') { $server->print ($channel, "Topic: +: ".$diff);}
67
68    }
69    $topics{$server->{tag}."_".$channel} = $topic;
70
71}
72
73
74# Start by reading all the channels currently opened, and recording their topic
75
76my @channels = Irssi::channels () ;
77
78foreach my $channel (@channels) {
79	$topics{$channel->{server}->{tag}."_".$channel->{name}} = $channel->{topic};
80}
81
82# Topic has changed
83Irssi::signal_add 'message topic' => \& new_topic;
84
85# We've joined a new channel
86Irssi::signal_add 'channel joined' => \& new_channel;
87