1# thistory.pl v1.05 [10.03.2002]
2# Copyright (C) 2001, 2002 Teemu Hjelt <temex@iki.fi>
3#
4# Written for irssi 0.7.98 and later, idea from JSuvanto.
5#
6# Many thanks to fuchs, shasta, Paladin, Koffa and people
7# on #irssi for their help and suggestions.
8#
9# Keeps information about the most recent topics of the
10# channels you are on.
11# Usage: /thistory [channel] and /tinfo [channel]
12#
13# v1.00 - Initial release.
14# v1.02 - Months and topics with formatting were shown
15#         incorrectly. (Found by fuchs and shasta)
16# v1.03 - event_topic was occasionally using the wrong
17#         server tag. Also added few variables to ease
18#         changing the settings and behaviour of this
19#         script.
20# v1.04 - Minor bug-fixes.
21# v1.05 - Made the script more consistent with other
22#         Irssi scripts.
23
24use strict;
25use Irssi;
26use Irssi::Irc;
27use vars qw($VERSION %IRSSI);
28
29# Formatting character.
30my $fchar = '%';
31
32# Format of the line.
33my $format = '"%topic" %nick (%address) [%mday.%mon.%year %hour:%min:%sec]';
34
35# Amount of topics stored.
36my $tamount = 10;
37
38my %topiclist;
39###### Don't edit below this unless you know what you're doing ######
40
41$VERSION = "1.05";
42%IRSSI = (
43	authors     => "Teemu Hjelt",
44	contact     => "temex\@iki.fi",
45	name        => "topic history",
46	description => "Keeps information about the most recent topics of the channels you are on.",
47	license     => "GNU GPLv2 or later",
48	url         => "http://www.iki.fi/temex/",
49	changed     => "Sun Mar 10 14:53:59 EET 2002",
50);
51
52sub cmd_topicinfo {
53	my ($channel) = @_;
54	my $tag = Irssi::active_server()->{'tag'};
55	$channel =~ s/\s+//;
56	$channel =~ s/\s+$//;
57
58	if ($channel eq "") {
59		if (Irssi::channel_find(Irssi::active_win()->get_active_name())) {
60			$channel = Irssi::active_win()->get_active_name();
61		}
62	}
63	if ($channel ne "") {
64		if ($topiclist{lc($tag)}{lc($channel)}{0}) {
65			Irssi::print("%W$channel%n: " . $topiclist{lc($tag)}{lc($channel)}{0}, MSGLEVEL_CRAP);
66		} else {
67			Irssi::print("No topic information for %W$channel%n", MSGLEVEL_CRAP);
68		}
69	} else {
70		Irssi::print("Usage: /tinfo <channel>");
71	}
72}
73
74sub cmd_topichistory {
75	my ($channel) = @_;
76	my $tag = Irssi::active_server()->{'tag'};
77	$channel =~ s/\s+//;
78	$channel =~ s/\s+$//;
79
80        if ($channel eq "") {
81                if (Irssi::channel_find(Irssi::active_win()->get_active_name())) {
82                        $channel = Irssi::active_win()->get_active_name();
83                }
84        }
85	if ($channel ne "") {
86		if ($topiclist{lc($tag)}{lc($channel)}{0}) {
87			my $amount = &getamount($tag, $channel);
88			Irssi::print("Topic history for %W$channel%n:", MSGLEVEL_CRAP);
89			for (my $i = 0; $i < $amount; $i++) {
90				if ($topiclist{lc($tag)}{lc($channel)}{$i}) {
91					my $num = $i + 1;
92					if (length($amount) >= length($tamount) && length($i + 1) < length($tamount)) {
93						for (my $j = length($tamount); $j > length($i + 1); $j--) {
94							$num = " " . $num;
95						}
96					}
97					Irssi::print($num . ". " . $topiclist{lc($tag)}{lc($channel)}{$i}, MSGLEVEL_CRAP);
98				} else {
99					last;
100				}
101			}
102		} else {
103			Irssi::print("No topic history for %W$channel%n", MSGLEVEL_CRAP);
104		}
105	} else {
106		Irssi::print("Usage: /thistory <channel>");
107	}
108}
109
110sub event_topic {
111	my ($server, $data, $nick, $address) = @_;
112	my ($channel, $topic) = split(/ :/, $data, 2);
113	my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
114	my $tag = $server->{'tag'};
115	my $output = $format;
116
117	$topic =~ s/%/%%/g;
118	$topic .= '%n';
119
120	my %val;
121	$val{'sec'} = $sec < 10 ? "0$sec" : $sec;
122	$val{'min'} = $min < 10 ? "0$min" : $min;
123	$val{'hour'} = $hour < 10 ? "0$hour" : $hour;
124	$val{'mday'} = $mday < 10 ? "0$mday" : $mday;
125	$val{'mon'} = $mon + 1 < 10 ? "0" . ($mon + 1) : $mon + 1;
126	$val{'year'} = $year + 1900;
127
128	$val{'nick'} = $nick;
129	$val{'address'} = $address;
130	$val{'channel'} = $channel;
131	$val{'topic'} = $topic;
132	$val{'tag'} = $tag;
133
134	$output =~ s/$fchar(\w+)/$val{$1}/g;
135
136        for (my $i = (&getamount($tag, $channel) - 1); $i >= 0; $i--) {
137		if ($topiclist{lc($tag)}{lc($channel)}{$i}) {
138			$topiclist{lc($tag)}{lc($channel)}{$i + 1} = $topiclist{lc($tag)}{lc($channel)}{$i};
139		}
140        }
141        $topiclist{lc($tag)}{lc($channel)}{0} = $output;
142}
143
144sub getamount {
145	my ($tag, $channel) = @_;
146	my $amount = 0;
147
148	for (my $i = 0; $i < $tamount; $i++) {
149		if ($topiclist{lc($tag)}{lc($channel)}{$i}) {
150			$amount++;
151		}
152	}
153	return $amount;
154}
155
156Irssi::command_bind("topichistory", "cmd_topichistory");
157Irssi::command_bind("thistory", "cmd_topichistory");
158Irssi::command_bind("topicinfo", "cmd_topicinfo");
159Irssi::command_bind("tinfo", "cmd_topicinfo");
160Irssi::signal_add("event topic", "event_topic");
161
162Irssi::print("Loaded thistory.pl v$VERSION");
163