1use Irssi 0.8.10 ();
2use strict;
3
4use vars qw($VERSION %IRSSI);
5
6$VERSION="0.1.7";
7%IRSSI = (
8	authors=> 'BC-bd',
9	contact=> 'bd@bc-bd.org',
10	name=> 'thankop',
11	description=> 'Remembers the last person oping you on a channel',
12	license=> 'GPL v2',
13	url=> 'https://bc-bd.org/svn/repos/irssi/trunk/',
14);
15
16# $Id #
17#
18#########
19# USAGE
20###
21#
22# Type '/thankop' in a channel window to thank the person opping you
23#
24##########
25# OPTIONS
26####
27#
28# /set thankop_command [command]
29#		* command  : to be executed. The following $'s are expanded
30#		    $N : Nick (some dude)
31#
32#		eg:
33#
34#		  /set thankop_command say $N: w00t!
35#
36#		Would say
37#
38#		  <nick>: w00t!
39#
40#		To the channel you got op in, with <nick> beeing the nick who
41#		opped you
42#
43################
44###
45# Changelog
46#
47# Version 0.1.7
48#  - fix crash if used in a window != CHANNEL
49#  - do not thank someone who has already left
50#
51# Version 0.1.6
52#  - added support for multiple networks, thanks to senneth
53#  - adapted to signal changes in 0.8.10
54#
55# Version 0.1.5
56#  - change back to setting instead of theme item
57#
58# Version 0.1.4
59#  - added theme item to customize the message (idea from mordeth)
60#
61# Version 0.1.3
62#  - removed '/' from the ->command (thx to mordeth)
63#  - removed debug messages (where commented out)
64#
65# Version 0.1.2
66#  - added version dependency, since some 0.8.4 users complained about a not
67#      working script
68#
69# Version 0.1.1
70#  - unsetting of hash values is done with delete not unset.
71#
72# Version 0.1.0
73#  - initial release
74#
75###
76################
77
78my %op;
79
80sub cmd_thankop {
81	my ($data, $server, $witem) = @_;
82
83	if (!$witem || ($witem->{type} =! "CHANNEL")) {
84		Irssi::print("thankop: Window not of type CHANNEL");
85		return;
86	}
87
88	my $tag = $witem->{server}->{tag}.'/'.$witem->{name};
89
90	# did we record who opped us here
91	if (!exists($op{$tag})) {
92		$witem->print("thankop: I don't know who op'ed you in here");
93		return;
94	}
95
96	my $by = $op{$tag};
97
98	# still here?
99	if (!$witem->nick_find($by)) {
100		$witem->print("thankop: $by already left");
101		return;
102	}
103
104	my $cmd = Irssi::settings_get_str('thankop_command');
105
106	$cmd =~ s/\$N/$by/;
107	$witem->command($cmd);
108}
109
110sub mode_changed {
111	my ($channel, $nick, $by, undef, undef) = @_;
112
113	return if ($channel->{server}->{nick} ne $nick->{nick});
114
115	# since 0.8.10 this is set after signals have been processed
116	return if ($channel->{chanop});
117
118	my $tag = $channel->{server}->{tag}.'/'.$channel->{name};
119
120	$op{$tag} = $by;
121}
122
123sub channel_destroyed {
124	my ($channel) = @_;
125
126	my $tag = $channel->{server}->{tag}.'/'.$channel->{name};
127
128	delete($op{$tag});
129}
130
131Irssi::command_bind('thankop','cmd_thankop');
132Irssi::signal_add_last('nick mode changed', 'mode_changed');
133
134Irssi::settings_add_str('thankop', 'thankop_command', 'say $N: opthx');
135