/usr/share/irssi/scripts/url_log.pl is in irssi-scripts 20120326.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | # url grabber, yes it sucks
#
# infected with the gpl virus
#
# Thomas Graf <tgraf@europe.com>
#
# version: 0.2
#
# Commands:
#
# /URL LIST
# /URL CLEAR
# /URL OPEN [<nr>]
# /URL QUOTE [<nr>]
# /URL HEAD [<nr>] !! Blocking !!
# /HEAD <url> !! Blocking !!
#
# Config Values
#
# [url logfile]
# url_log log urls to url_log_file
# url_log_file file to save urls
# url_log_format format in url logfile
# url_log_timestamp format of timestamp in url logfile
#
# [url log in memory]
# url_log_browser command to execute to open url, %f will be replaced with the url
# url_log_size keep that many urls in the list
#
# [http head stuff]
# url_head_format format of HEAD output
# url_auto_head do a head on every url received
# url_auto_head_format format of auto head output
#
#
# Database installation
# - create database and user
# - create table url ( id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
# time INT UNSIGNED, nick VARCHAR(25), target VARCHAR(25), url VARCHAR(255));
# or similiar :)
#
#
# todo:
#
# - fix XXX marks
# - xml output?
# - don't output "bytes" if content-length is not available
# - prefix with http:// if no prefix is given
use Irssi;
use Irssi::Irc;
$VERSION = "0.2";
%IRSSI = (
authors => 'Thomas Graf',
contact => 'irssi@reeler.org',
name => 'url_log',
description => 'logs urls to textfile or/and database, able to list, quote, open or `http head` saved urls.',
license => 'GNU GPLv2 or later',
url => 'http://irssi.reeler.org/url/',
);
use LWP;
use LWP::UserAgent;
use HTTP::Status;
use DBI;
use POSIX qw(strftime);
use strict;
my @urls;
my $user_agent = new LWP::UserAgent;
$user_agent->agent("IrssiUrlLog/0.2");
# hmm... stolen..
# -verbatim- import expand
sub expand {
my ($string, %format) = @_;
my ($exp, $repl);
$string =~ s/%$exp/$repl/g while (($exp, $repl) = each(%format));
return $string;
}
# -verbatim- end
sub print_msg
{
Irssi::active_win()->print("@_");
}
#
# open url in brower using url_log_brower command
#
sub open_url
{
my ($data) = @_;
my ($nick, $target, $url) = split(/ /, $data);
my $pid = fork();
if ($pid) {
Irssi::pidwait_add($pid);
} elsif (defined $pid) { # $pid is zero here if defined
my $data = expand(Irssi::settings_get_str("url_log_browser"), "f", $url);
# XXX use exec
system $data;
exit;
} else {
# weird fork error
print_msg "Can't fork: $!";
}
}
sub head
{
my ($url) = @_;
my $req = new HTTP::Request HEAD => $url;
my $res = $user_agent->request($req);
return $res;
}
#
# do a HEAD
#
sub do_head
{
my ($url) = @_;
my $res = head($url);
if ($res->code ne RC_OK) {
Irssi::active_win()->printformat(MSGLEVEL_CRAP, 'url_head', $url, "\n" .
$res->status_line());
} else {
my $t = expand(Irssi::settings_get_str("url_head_format"),
"u", $url,
"t", scalar $res->content_type,
"l", scalar $res->content_length,
"s", scalar $res->server);
Irssi::active_win()->printformat(MSGLEVEL_CRAP, 'url_head', $url, $t);
}
}
#
# called if url is detected, should do a HEAD and print a 1-liner
#
sub do_auto_head
{
my ($url, $window) = @_;
return if ($url !~ /^http:\/\//);
my $res = head($url);
if ($res->code ne RC_OK) {
$window->printformat(MSGLEVEL_CRAP, 'url_auto_head', $res->status_line());
} else {
my $t = expand(Irssi::settings_get_str("url_auto_head_format"),
"u", $url,
"c", $res->code,
"t", scalar $res->content_type,
"l", scalar $res->content_length,
"s", scalar $res->server);
$window->printformat(MSGLEVEL_CRAP, 'url_auto_head', $t);
}
}
#
# log url to file
#
sub log_to_file
{
my ($nick, $target, $text) = @_;
my ($lfile) = glob Irssi::settings_get_str("url_log_file");
if ( open(LFD, ">> $lfile") ) {
my %h = {
time => time,
nick => $nick,
target => $target,
url => $text
};
print LFD expand(Irssi::settings_get_str("url_log_format"),
"s", strftime(Irssi::settings_get_str("url_log_timestamp_format"), localtime),
"n", $nick,
"t", $target,
"u", $text), "\n";
close LFD;
} else {
print_msg "Warning: Unable to open file $lfile $!";
}
}
#
# log url to database
#
sub log_to_database
{
my ($nick, $target, $text) = @_;
# this is quite expensive, but...
my $dbh = DBI->connect(Irssi::settings_get_str("url_log_db_dsn"),
Irssi::settings_get_str("url_log_db_user"),
Irssi::settings_get_str("url_log_db_password"))
or print_msg "Can't connect to database " . $DBI::errstr;
if ($dbh) {
my $sql = "INSERT INTO url (time, nick, target, url) VALUES (UNIX_TIMESTAMP()," .
$dbh->quote($nick) . "," . $dbh->quote($target) . "," . $dbh->quote($text) . ")";
$dbh->do($sql) or print_msg "Can't execute sql command: " . $DBI::errstr;
$dbh->disconnect();
}
}
#
# head command handler
#
sub sig_head
{
my ($cmd_line, $server, $win_item) = @_;
my @args = split(' ', $cmd_line);
my $url;
if (@args <= 0) {
if ($#urls eq 0) {
return;
}
$url = $urls[$#urls];
$url =~ s/^.*?\s.*?\s//;
} else {
$url = lc(shift(@args));
}
do_head($url);
}
#
# msg handler
#
sub sig_msg
{
my ($server, $data, $nick, $address) = @_;
my ($target, $text) = split(/ :/, $data, 2);
# very special, but better than just \w::/* and www.*
while ($text =~ s#.*?(^|\s)(\w+?://.+?|[\w\.]{3,}/[\w~\.]+?(/|/\w+?\.\w+?))(\s|$)(.*)#$5#i) {
return if ($1 =~ /^\.\./);
push @urls, "$nick $target $2";
# XXX resize correctly if delta is > 1
if ($#urls >= Irssi::settings_get_int("url_log_size")) {
shift @urls;
}
my $ischannel = $server->ischannel($target);
my $level = $ischannel ? MSGLEVEL_PUBLIC : MSGLEVEL_MSGS;
$target = $nick unless $ischannel;
my $window = $server->window_find_closest($target, $level);
if ( Irssi::settings_get_bool("url_log_auto_head") ) {
do_auto_head($2, $window);
}
if ( Irssi::settings_get_bool("url_log") ) {
log_to_file($nick, $target, $2);
}
if ( Irssi::settings_get_bool("url_log_db") ) {
log_to_database($nick, $target, $2);
}
}
}
sub print_url_list_item
{
my ($n, $data) = @_;
my ($src, $dst, $url) = split(/ /, $data);
Irssi::active_win()->printformat(MSGLEVEL_CRAP, 'url_list', $n, $src, $dst, $url);
}
#
# url command handler
#
sub sig_url
{
my ($cmd_line, $server, $win_item) = @_;
my @args = split(' ', $cmd_line);
if (@args <= 0) {
print_msg "URL LIST [<nr>] list all url(s)";
print_msg " OPEN [<nr>] open url in browser";
print_msg " QUOTE [<nr>] quote url (print to current channel)";
print_msg " HEAD send HEAD to server";
print_msg " CLEAR clear url list";
return;
}
my $action = lc(shift(@args));
if ($action eq "list") {
if (@args > 0) {
my $i = shift(@args);
print_url_list_item($i, $urls[$i]);
} else {
my $i = 0;
foreach my $l (@urls) {
print_url_list_item($i, $l);
$i++;
}
}
} elsif($action eq "open") {
my $i = $#urls;
if (@args > 0) {
$i = shift(@args);
}
open_url($urls[$i]);
} elsif ($action eq "quote") {
my $i = $#urls;
if (@args > 0) {
$i = shift(@args);
}
Irssi::active_win()->command("SAY URL: " . $urls[$i]);
} elsif ($action eq "clear") {
splice @urls;
} elsif ($action eq "head") {
my $i = $#urls;
if (@args > 0) {
$i = shift(@args);
}
my $url = $urls[$i];
$url =~ s/^.*?\s.*?\s//;
do_head($url);
} else {
print_msg "Unknown action";
}
}
Irssi::command_bind('head', 'sig_head');
Irssi::command_bind('url', 'sig_url');
Irssi::signal_add_first('event privmsg', 'sig_msg');
Irssi::settings_add_bool("url_log", "url_log", 1);
Irssi::settings_add_bool("url_log", "url_log_auto_head", 1);
Irssi::settings_add_bool("url_log", "url_log_db", 0);
Irssi::settings_add_str("url_log", "url_log_db_dsn", 'DBI:mysql:irc_url:localhost');
Irssi::settings_add_str("url_log", "url_log_db_user", 'irc_url');
Irssi::settings_add_str("url_log", "url_log_db_password", 'nada');
Irssi::settings_add_str("url_log", "url_log_file", "~/.irssi/url");
Irssi::settings_add_str("url_log", "url_log_timestamp_format", '%c');
Irssi::settings_add_str("url_log", "url_log_format", '%s %n %t %u');
Irssi::settings_add_str("url_log", "url_log_browser", 'sensible-browser %f > /dev/null');
Irssi::settings_add_int("url_log", "url_log_size", 25);
Irssi::settings_add_str("url_log", "url_auto_head_format", '%c %t %l bytes');
Irssi::settings_add_str("url_log", "url_head_format", '
Content-Type: %t
Length: %l bytes
Server: %s');
Irssi::theme_register(['url_head', '[%gHTTP Head%n %g$0%n]$1-',
'url_auto_head', '[%gHEAD%n] $0-',
'url_list', '[$0] $1 %W$2%n $3-']);
|