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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
# Show IRC gallery (http://irc-galleria.net, finnish only) information
# on /WHOIS or /GALLERY
# version 1.13
# for irssi 0.8.0 by Timo Sirainen
use strict;
use Symbol;
use vars qw($VERSION %IRSSI);
$VERSION = "1.13";
%IRSSI = (
authors => "Timo \'cras\' Sirainen",
contact => "tss\@iki.fi",
name => "ircgallery",
description => "Show IRC gallery (http://irc-galleria.net, finnish only) information on /WHOIS or /GALLERY",
license => "Public Domain",
url => "http://irssi.org/",
changed => "2002-03-04T22:47+0100"
);
Irssi::theme_register([
'whois_gallery', '{whois gallery $1}',
'gallery_header', '{hilight $0} - IRC gallery information',
'gallery_line', ' $[8]0 : $1',
'gallery_footer', 'End of info',
'gallery_notfound', '$0 is not in IRC gallery',
'gallery_nolist', 'Nick list of IRC gallery not downloaded yet - please wait'
]);
my $cache_path = glob "~/.irssi/ircgallery";
my @print_queue;
my $nicklist_path = "$cache_path/nicks.list";
my $gallery_nicks_time = 0;
my %gallery_nicks = {};
my $last_whois_nick;
sub get_view_url {
return 'http://irc-galleria.net/view.php?nick='.$_[0];
}
# print the gallery information - assumes the file is in cache directory
sub print_gallery {
my %print_notfound;
my $nick = shift;
my $found = 0;
my $next_channels = 0;
my $channels;
local $/ = "\n";
my $f = gensym;
if (!open($f, "<", "$cache_path/$nick")) {
Irssi::print("Couldn't open file $cache_path/$nick: $!", MSGLEVEL_CLIENTERROR);
return;
}
while (<$f>) {
last if (/\<title\>.*Etsi nick/); # unknown nick
if ($next_channels) {
if (m,\<a .*\>(#.*)\</a>,) {
$channels .= "$1 ";
next;
} else {
$next_channels = 0;
if ($channels) {
Irssi::printformat(MSGLEVEL_CRAP, 'gallery_line',
"channels", $channels);
$channels = "";
}
}
}
if (/\<h1\>[^\(]*\(([^\)]*)/) {
my $realname = $1;
Irssi::printformat(MSGLEVEL_CRAP, 'gallery_header', $nick);
Irssi::printformat(MSGLEVEL_CRAP, 'gallery_line',
"ircname", $realname);
Irssi::printformat(MSGLEVEL_CRAP, 'gallery_line',
"url", get_view_url($nick));
$found = 1;
next;
}
if (/\<img.*src="([^"]*)".*alt="$nick"/) {
Irssi::printformat(MSGLEVEL_CRAP, 'gallery_line',
"image", "http://irc-galleria.net/$1");
next;
}
my ($title, $value) = $_ =~ m,\<span class="otsikko"\>([^:]+):\</span\> (.*)\<br /\>,;
if ($value =~ m,\<a .*\>(.*)\</a\>,) {
$value = $1;
}
$next_channels = 1 if (m,\<span class="otsikko"\>Kanavat,);
if ($title && $value) {
if ($title eq "Maili") {
$title = "e-mail";
} elsif ($title =~ /Kaupunki/) {
$title = "city";
} elsif ($title eq "Syntynyt") {
$title = "birthday";
} elsif ($title eq "Muutettu") {
$title = "last modified";
}
Irssi::printformat(MSGLEVEL_CRAP, 'gallery_line', $title, $value);
}
}
close($f);
if ($found) {
Irssi::printformat(MSGLEVEL_CRAP, 'gallery_footer', $nick);
} elsif ($print_notfound{$nick}) {
Irssi::printformat(MSGLEVEL_CRAP, 'gallery_notfound', $nick);
}
delete $print_notfound{$nick};
}
# download the info from gallery to cache dir,
# if the files aren't there already.
sub download_nicks_info {
foreach my $nick (@_) {
my $filename = "$cache_path/$nick";
if (! -f $filename) {
# FIXME: we could do this ourself with sockets...
Irssi::command("exec - wget -O$filename.tmp -q -UMozilla ".get_view_url($nick)."; mv $filename.tmp $filename");
}
}
}
# print info from all given nicks that have file in cache dir
sub gallery_show {
foreach my $nick (@_) {
if (-f "$cache_path/$nick") {
print_gallery($nick);
} else {
push @print_queue, $nick;
}
}
}
sub print_whois_gallery {
my ($server, $nick) = @_;
if ($gallery_nicks{lc $nick}) {
$server->printformat($nick, MSGLEVEL_CRAP, 'whois_gallery',
$nick, get_view_url($nick));
}
}
# /WHOIS - print the gallery URL after realname
sub event_whois {
my ($server, $data) = @_;
my ($temp, $nick) = split(" ", $data);
print_whois_gallery($server, $last_whois_nick) if ($last_whois_nick);
$last_whois_nick = $nick;
}
sub event_end_of_whois {
my ($server) = @_;
if ($last_whois_nick) {
print_whois_gallery($server, $last_whois_nick);
$last_whois_nick = undef;
}
}
# /GALLERY <nicks>
sub cmd_gallery {
my @nicks = split(/[, ]/, $_[0]);
if (!$gallery_nicks_time) {
Irssi::printformat(MSGLEVEL_CLIENTERROR, 'gallery_nolist');
return;
}
my @new_list;
foreach my $nick (@nicks) {
my $gallery_nick = $gallery_nicks{lc $nick};
if (!$gallery_nick) {
Irssi::printformat(MSGLEVEL_CRAP, 'gallery_notfound', $nick);
} else {
push @new_list, $gallery_nick;
}
}
download_nicks_info(@new_list);
gallery_show(@new_list);
if ($gallery_nicks_time < time()-(3600*8)) {
# nicklist hasn't been updated for a while, refresh it
download_nicklist();
}
}
# parse all known nicks from nick index file
sub parse_nicks {
my $filename = shift;
%gallery_nicks = {};
$gallery_nicks_time = time();
my $f = gensym;
if (!open($f, "<", $filename)) {
Irssi::print("Couldn't open file $filename: $!", MSGLEVEL_CLIENTERROR);
return;
}
while (<$f>) {
if (m,\<a href="view.php.*\>(.*)\</a\>,) {
$gallery_nicks{lc $1} = $1;
}
}
close($f);
}
# /EXEC finished - maybe there's new files downloaded in cache dir?
sub sig_exec_remove {
my @new_queue;
if (-f $nicklist_path) {
parse_nicks($nicklist_path);
unlink($nicklist_path);
}
foreach my $nick (@print_queue) {
if (-f "$cache_path/$nick") {
print_gallery($nick);
} else {
push @new_queue, $nick;
}
}
@print_queue = @new_queue;
}
sub download_nicklist {
Irssi::command("exec - wget -O$nicklist_path -q -UMozilla http://irc-galleria.net/list.php?letter=_");
}
# clear cache dir
if (-d $cache_path) {
unlink(<$cache_path/*>);
} else {
mkdir($cache_path, 0700) || die "Can't create cache directory $cache_path";
}
# we need the nick list, get it once per hour
download_nicklist();
Irssi::signal_add_first('event 311', 'event_whois');
Irssi::signal_add_first('event 318', 'event_end_of_whois');
Irssi::signal_add('exec remove', 'sig_exec_remove');
Irssi::command_bind('gallery', 'cmd_gallery');
|