1use strict;
2use Irssi; # developed using irssi 0.8.9.CVS
3
4# I recommend rebinding irssi's default 'BAN' to 'bantimes' (/alias BAN BANTIME)
5
6use vars qw($VERSION %IRSSI);
7$VERSION = '1.03';
8%IRSSI = (
9	authors		=> "David O\'Rourke",
10	contact		=> "phyber [at] #irssi",
11	name		=> "bantime",
12	description	=> "Print time when ban was set in a nicer way. eg. 23m, 40s ago.",
13	license		=> "GPLv2",
14	changed		=> "02/03/2009",
15);
16
17sub duration {
18	my ($when) = @_;
19
20	my $diff = (time - $when);
21	my $day = int($diff / 86400); $diff -= ($day * 86400);
22	my $hrs = int($diff / 3600); $diff -= ($hrs * 3600);
23	my $min = int($diff / 60); $diff -= ($min * 60);
24	my $sec = $diff;
25
26	my $str;
27	$str .= "${day}d " if $day;
28	$str .= "${hrs}h " if $day or $hrs;
29	$str .= "${min}m " if $day or $hrs or $min;
30	$str .= "${sec}s"; # seconds should always be shown
31
32	return $str;
33}
34
35sub cmd_bans {
36	my ($args, $server, $witem) = @_;
37	return if not ($witem && $witem->{type} eq "CHANNEL");
38	my $channel = $witem->{name};
39
40	if (!$witem->bans()) {
41		$witem->printformat(
42			MSGLEVEL_CLIENTCRAP,
43			'bantime_nobans',
44			$channel);
45		return;
46	}
47
48	my $count = 1;
49	foreach my $ban ($witem->bans()) {
50		if (!$ban->{setby} || !$ban->{time}) {
51			$witem->printformat(
52				MSGLEVEL_CLIENTCRAP,
53				'bantime',
54				$count,
55				$channel,
56				$ban->{ban});
57		}
58		else {
59			my $bantime;
60			if (Irssi::settings_get_bool('bantime_show_date')) {
61				$bantime = localtime($ban->{time}) . ": ";
62				$bantime =~ s/\s+/ /g;
63			}
64			$bantime .= duration($ban->{time});
65			$witem->printformat(
66				MSGLEVEL_CLIENTCRAP,
67				'bantime_long',
68				$count,
69				$channel,
70				$ban->{ban},
71				$ban->{setby},
72				$bantime);
73		}
74		$count++;
75	}
76}
77
78Irssi::theme_register([
79	'bantime', '{line_start}$0 - {channel $1}: ban {ban $2}',
80	'bantime_long', '{line_start}$0 - {channel $1}: ban {ban $2} {comment by {nick $3}, $4 ago}',
81	'bantime_nobans', '{line_start}{hilight Irssi:} No bans in channel {channel $0}'
82]);
83Irssi::command_bind('bantime', 'cmd_bans');
84Irssi::print("Loaded $IRSSI{name} $VERSION");
85Irssi::settings_add_bool('bantime', 'bantime_show_date' => 0);
86
87#############
88# ChangeLog #
89#############
90# 02.03.2009: 1.03
91# Minor cosmetic changes to the script.
92# 28.02.2007: 1.03
93# duration() now returns a nicer string.  Fields arn't visible if they're zero.
94# Random bits cleaned up.
95# 28.04.2005: 1.01
96# Removed redundant '$bantime2' variable, left over from a setting that was removed earlier.
97# 19.03.2005: 1.0
98# Removed dependancy on Time::Duration by using duration().
99# Removed obsolete 'bantime_short_format' setting.
100# Increased version to 1.0
101# 11.01.2004: Jan 11 2004: 04:30
102# Added new bantime_show_date setting. Displays the date the ban was set along with the time info.
103# 11.01.2004: Jan 11 2004: 04:05
104# Added new bantime_short_format setting. Displays the time in a nice short format. (#irssi: ban *!*@test.testing [by phyber, 3d 5h 54m 59s ago])
105# 11.01.2004: Jan 11 2004: 03:49
106# Changed handling bans without setby/time information closer to how irssi does.
107# 08.01.2004: Jan 08 2004: 02:46
108# Fixed a bug which occured if the IRCd didn't tell us who set the bans at which time. eg. IRCNet if a user doesn't have +o.
109# 08.01.2004: Jan 08 2004: 01:52
110# Initial Release.  Many thanks to coekie for helping me with my scripting.
111