-
Notifications
You must be signed in to change notification settings - Fork 6
/
lastcontact.pl
182 lines (159 loc) · 5.34 KB
/
lastcontact.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Usage:
# Load the script and do /statusbar window add typednick
# When you type a nickname of someone who is in the current channel,
# info will be shown in your statusbar.
#
# TODO:
# - also use irssi's internal info
# - userhost reply has ip, not hostname on some ircds own host
# - 'flood' protection when saying/pasting a lot of nicks
# - update when changing window/windowitem
# - show when nick quits/parts/get kicked/rejoins
# - get info from friends-scripts
# - make better configureable
# - react to hilights
use strict;
use vars qw($VERSION %IRSSI);
use Irssi;
use Irssi::TextUI;
use Data::Dumper;
$VERSION = '0.0.0';
%IRSSI = (
authors => 'Wouter Coekaerts',
contact => '[email protected], coekie@#irssi',
name => 'typednicksb',
description => 'show info about a nick you talk to/about in the statusbar',
license => 'GPLv2',
url => 'http://wouter.coekaerts.be/irssi/',
changed => '25/01/04',
);
# after how many seconds should info of a nickname be thrown away
my $INFO_MAXAGE = 10;
my $INFO_MAX_QUEUE = 3;
my $sb_text = "hello"; # text in the statusbar
my $last_word; # last word typed
my $last_nick_typed; # nick last typed
#my $last_tag; # tag of server active in window when last nick typed
# array of hashes with info about the last nicks typed
my @nick_infos;
# 'nick' => nickname
# 'tag' => servertag
Irssi::signal_add_last ('gui key pressed', sub {
# get the prompt
my $prompt = Irssi::parse_special('$L');
# make statusbar empty again if prompt is empty
if ($prompt eq '' && $last_nick_typed) {
set_sb_text('');
$last_nick_typed = $last_word = '';
return;
}
# last word is chars allowed in nick (\w[]{}`^|), followed by chars not allowed
my ($word) = ($prompt =~ /([\w\[\]\{\}\\`\^\|]*)[^\w\[\]\{\}\\`\^\|]+$/);
# stop if there isn't a new word
if (!$word || $word eq $last_word) {
return;
}
$last_word = $word;
# get the channel object, or stop if there's no channel active
my $channel = Irssi::active_win->{'active'};
if (!$channel || ref($channel) ne 'Irssi::Irc::Channel' || $channel->{'type'} ne 'CHANNEL' || !$channel->{'names_got'}) {
return;
}
# get the nick object, or stop if the word is not a nick
my $nick = $channel->nick_find($word);
if (!$nick) {
return;
}
# word typed is a nick in current chan, remember it and get info
$last_nick_typed = $word;
cleanup_infos();
my $info = find_info($nick->{'nick'},$channel->{'server'}->{'tag'});
if ($info && $info->{'last_check'}) {
set_sb_info($info);
} else {
unshift @nick_infos, {
'nick' => $nick->{'nick'},
'tag' => $channel->{'server'}->{'tag'}
};
# $channel->print("sending userhost $nick->{'nick'}");
$channel->{'server'}->redirect_event("userhost", 1, $nick->{'nick'}, 0, 'redir typednick_error',
{"event 302" => "redir typednick_userhost"});
$channel->{'server'}->send_raw('USERHOST ' . $word);
}
});
####################
### MANAGE INFOS ###
####################
# if userhost failed, show error in statusbar
Irssi::signal_add('redir typednick_error', sub {
set_sb_text("typednick: error from server");
});
# userhost answer received, store info and update statusbar
Irssi::signal_add('redir typednick_userhost', sub {
my ($server, $data) = @_;
my ($mynick, $reply) = split(/ +/, $data);
my ($nick, $oper, $away, $user, $host) = $reply =~ /^:(.*?)(\*?)=([+-])(.*?)@(.*)/;
my $info = find_info($nick,$server->{'tag'});
if (!$info) {
Irssi::print("typednicksb: warning: got userhost reply for $nick on ".$server->{'tag'}.", but no record for that");
return;
}
$info->{'oper'} = $oper;
$info->{'away'} = $away;
$info->{'user'} = $user;
$info->{'host'} = $host;
$info->{'last_check'} = time();
set_sb_info($info);
});
# finds the info hash for the given nick & tag
# returns undef if not found
sub find_info {
my ($nick, $tag) = @_;
foreach my $info (@nick_infos) {
if ($info->{'nick'} eq $nick && $info->{'tag'} eq $tag) { # TODO compare nicks better...
# print "found $nick on $tag";
return $info;
}
}
# print "can't find $nick on $tag";
return undef;
}
# removes nick info hashes that are too old
sub cleanup_infos {
# iterate over all infos from last to first
for (my $i=@nick_infos - 1; $i >= 0; $i--) {
if ($nick_infos[$i]->{'last_check'} && time() - $nick_infos[$i]->{'last_check'} > $INFO_MAXAGE) {
# print 'cleanup '.$nick_infos[$i]->{'nick'};
splice @nick_infos,$i,1;
}
}
}
#################
### STATUSBAR ###
#################
# changes the text in the statusbar to display the given nick info hash
sub set_sb_info {
my ($info) = @_;
my $away = ($info->{'away'} eq '-' ? ' %Yaway%n' : '');
set_sb_text($info->{'nick'} . ($info->{'oper'} ? ' (oper)' : '') . ' is ' . $info->{'user'} . '@' . $info->{'host'} . $away);
}
# changes the text in the statusbar to $text
sub set_sb_text {
my ($text) = @_;
$sb_text = $text;
Irssi::statusbar_items_redraw('lastcontact');
}
# function irssi calls to get statusbar text
sub sb_lastcontact {
my ( $sbItem, $get_size_only ) = @_;
#my $line = "hello";
#my $format = "{sb ".$line."}";
#$item->{min_size} = $item->{max_size} = length($line);
#$item->default_handler($get_size_only, $format, 0, 1);
$sbItem->default_handler ( $get_size_only, "{sb $sb_text}", undef, 1 );
}
############
### INIT ###
############
Irssi::statusbar_item_register('lastcontact', '{sb $0-}', 'sb_lastcontact');
Irssi::command_bind("tn", sub {print Data::Dumper->new([@nick_infos])->Dump});