html/thistory.pl


   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 
  24 use Irssi;
  25 use Irssi::Irc;
  26 use vars qw($VERSION %IRSSI);
  27 
  28 # Formatting character.
  29 my $fchar = '%';
  30 
  31 # Format of the line.
  32 my $format = '"%topic" %nick (%address) [%mday.%mon.%year %hour:%min:%sec]';
  33 
  34 # Amount of topics stored.
  35 my $tamount = 10;
  36 
  37 ###### Don't edit below this unless you know what you're doing ######
  38 
  39 $VERSION = "1.05";
  40 %IRSSI = (
  41 	authors     => "Teemu Hjelt",
  42 	contact     => "temex\@iki.fi",
  43 	name        => "topic history",
  44 	description => "Keeps information about the most recent topics of the channels you are on.",
  45 	license     => "GNU GPLv2 or later",
  46 	url         => "http://www.iki.fi/temex/",
  47 	changed     => "Sun Mar 10 14:53:59 EET 2002",
  48 );
  49 
  50 sub cmd_topicinfo {
  51 	my ($channel) = @_;
  52 	my $tag = Irssi::active_server()->{'tag'};
  53 	$channel =~ s/\s+//;
  54 	$channel =~ s/\s+$//;
  55 
  56 	if ($channel eq "") {
  57 		if (Irssi::channel_find(Irssi::active_win()->get_active_name())) {
  58 			$channel = Irssi::active_win()->get_active_name();
  59 		}
  60 	}
  61 	if ($channel ne "") {
  62 		if ($topiclist{lc($tag)}{lc($channel)}{0}) {
  63 			Irssi::print("%W$channel%n: " . $topiclist{lc($tag)}{lc($channel)}{0}, MSGLEVEL_CRAP);
  64 		} else {
  65 			Irssi::print("No topic information for %W$channel%n", MSGLEVEL_CRAP);
  66 		}
  67 	} else {
  68 		Irssi::print("Usage: /tinfo <channel>");
  69 	}
  70 }
  71 
  72 sub cmd_topichistory {
  73 	my ($channel) = @_;
  74 	my $tag = Irssi::active_server()->{'tag'};
  75 	$channel =~ s/\s+//;
  76 	$channel =~ s/\s+$//;
  77 
  78         if ($channel eq "") {
  79                 if (Irssi::channel_find(Irssi::active_win()->get_active_name())) {
  80                         $channel = Irssi::active_win()->get_active_name();
  81                 }
  82         }
  83 	if ($channel ne "") {
  84 		if ($topiclist{lc($tag)}{lc($channel)}{0}) {
  85 			my $amount = &getamount($tag, $channel);
  86 			Irssi::print("Topic history for %W$channel%n:", MSGLEVEL_CRAP);
  87 			for (my $i = 0; $i < $amount; $i++) {
  88 				if ($topiclist{lc($tag)}{lc($channel)}{$i}) {
  89 					my $num = $i + 1;
  90 					if (length($amount) >= length($tamount) && length($i + 1) < length($tamount)) {
  91 						for (my $j = length($tamount); $j > length($i + 1); $j--) {
  92 							$num = " " . $num;
  93 						}
  94 					}
  95 					Irssi::print($num . ". " . $topiclist{lc($tag)}{lc($channel)}{$i}, MSGLEVEL_CRAP);
  96 				} else {
  97 					last;
  98 				}
  99 			}
 100 		} else {
 101 			Irssi::print("No topic history for %W$channel%n", MSGLEVEL_CRAP);
 102 		}
 103 	} else {
 104 		Irssi::print("Usage: /thistory <channel>");
 105 	}
 106 }
 107 
 108 sub event_topic {
 109 	my ($server, $data, $nick, $address) = @_;
 110 	my ($channel, $topic) = split(/ :/, $data, 2);
 111 	my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
 112 	my $tag = $server->{'tag'};
 113 	my $output = $format;
 114 
 115 	$topic =~ s/%/%%/g;
 116 	$topic .= '%n';
 117 
 118 	$val{'sec'} = $sec < 10 ? "0$sec" : $sec;
 119 	$val{'min'} = $min < 10 ? "0$min" : $min;
 120 	$val{'hour'} = $hour < 10 ? "0$hour" : $hour;
 121 	$val{'mday'} = $mday < 10 ? "0$mday" : $mday;
 122 	$val{'mon'} = $mon + 1 < 10 ? "0" . ($mon + 1) : $mon + 1;
 123 	$val{'year'} = $year + 1900;
 124 
 125 	$val{'nick'} = $nick;
 126 	$val{'address'} = $address;
 127 	$val{'channel'} = $channel;
 128 	$val{'topic'} = $topic;
 129 	$val{'tag'} = $tag;
 130 
 131 	$output =~ s/$fchar(\w+)/$val{$1}/g;
 132 
 133         for (my $i = (&getamount($tag, $channel) - 1); $i >= 0; $i--) {
 134 		if ($topiclist{lc($tag)}{lc($channel)}{$i}) {
 135 			$topiclist{lc($tag)}{lc($channel)}{$i + 1} = $topiclist{lc($tag)}{lc($channel)}{$i};
 136 		} 
 137         }
 138         $topiclist{lc($tag)}{lc($channel)}{0} = $output;
 139 }
 140 
 141 sub getamount {
 142 	my ($tag, $channel) = @_;
 143 	my $amount = 0;
 144 	
 145 	for (my $i = 0; $i < $tamount; $i++) {
 146 		if ($topiclist{lc($tag)}{lc($channel)}{$i}) {
 147 			$amount++;
 148 		}
 149 	}
 150 	return $amount;
 151 }
 152 
 153 Irssi::command_bind("topichistory", "cmd_topichistory");
 154 Irssi::command_bind("thistory", "cmd_topichistory");
 155 Irssi::command_bind("topicinfo", "cmd_topicinfo");
 156 Irssi::command_bind("tinfo", "cmd_topicinfo");
 157 Irssi::signal_add("event topic", "event_topic");
 158 
 159 Irssi::print("Loaded thistory.pl v$VERSION");