]> jfr.im git - irssi-scripts.git/blob - urllog.pl
no autorun smshilight
[irssi-scripts.git] / urllog.pl
1 use Irssi;
2 use DBI;
3 use URI::Find qw(find_uris);
4
5 use strict;
6 use warnings;
7 use vars qw($VERSION %IRSSI $dbh);
8
9 $VERSION = "0.1";
10 %IRSSI = (
11 authors => "John Runyon",
12 name => "urllog",
13 description => "Log URLs to MySQL",
14 license => "public domain",
15 url => "https://github.com/zonidjan/irssi-scripts",
16 contact => "https://github.com/zonidjan"
17 );
18
19 our @MAPPING = split(//, 'abcdefghijklmnopqrstuvwxyz0123456789');
20
21
22 our $INURLLOG = 0;
23
24 sub urllog { our $INURLLOG; return if $INURLLOG; $INURLLOG = 1; _urllog(@_); $INURLLOG = 0; }
25 sub _urllog {
26 my ($dest, $text, $stripped) = @_;
27 return if $stripped =~ /URL:/;
28
29 if (defined($dest->{server}) && defined($dest->{server}->{tag}) && defined($dest->{target}) && $dest->{server}->{tag} && $dest->{target}) {
30 my $output = '';
31 find_uris($stripped, sub { found_url($dest->{server}->{tag}, $dest->{target}, \$output, $stripped, @_); });
32 unless ($output eq '') {
33 $dest->{window}->print($text =~ s/%/%%/gr, $dest->{level});
34 Irssi::signal_stop();
35 if (lc($dest->{target}) eq '#minix') {
36 $dest->{window}->command("msg ".$dest->{target}." URL: ".$output);
37 } else {
38 $dest->{window}->print("URL: ".$output, $dest->{level});
39 }
40 }
41 }
42 }
43
44 sub found_url { # found_url($server_tag, $target_name, \$output_buffer, $full_line, @find_uris_callback_args)
45 my ($tag, $target, $output, $line, $uriobj, $url) = @_;
46 my $ignorechansre = Irssi::settings_get_str('ignore_chans_re');
47 return if
48 ($ignorechansre && $target =~ m/$ignorechansre/i)
49 or $url =~ m!://jfr\.im/u[0-9a-z]+!i
50 or $line =~ m!\] has quit \[.*?\Q$url\E.*?\]$!;
51 $dbh->do("INSERT INTO urls(server,target,url,fullline) VALUES (?,?,?,?)", undef, $tag, $target, $url, $line);
52 $$output .= "http://jfr.im/u".numtoalpha($dbh->last_insert_id((undef) x 4))." ";
53 }
54
55 sub numtoalpha {
56 my $num = shift;
57
58 our @MAPPING;
59 my $alpha = '';
60
61 while ($num > 0) {
62 $alpha = $MAPPING[$num % scalar @MAPPING].$alpha;
63 $num = int($num / scalar @MAPPING);
64 }
65 return $alpha;
66 }
67
68
69 Irssi::settings_add_str('urllog', 'db_dsn', 'DBI:mysql:urllog');
70 Irssi::settings_add_str('urllog', 'db_username', 'urllog');
71 Irssi::settings_add_str('urllog', 'db_password', '');
72 Irssi::settings_add_str('urllog', 'ignore_chans_re', '');
73
74 $dbh = DBI->connect(
75 Irssi::settings_get_str('db_dsn'),
76 Irssi::settings_get_str('db_username'),
77 Irssi::settings_get_str('db_password'),
78 ) or warn $DBI::errstr;
79
80
81 Irssi::signal_add('print text', 'urllog');
82 Irssi::print('%G>>%n '.$IRSSI{name}.' '.$VERSION.' loaded');
83
84
85 __END__
86
87 CREATE DATABASE `urllog`;
88 CREATE TABLE `urllog`.`urls` (
89 `id` int(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
90 `server` varchar(25) NULL DEFAULT NULL,
91 `target` varchar(100) NULL DEFAULT NULL,
92 `nick` varchar(40) NULL DEFAULT NULL,
93 `url` text NOT NULL,
94 `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
95 `fullline` text NOT NULL
96 ) CHARSET=utf8;
97 GRANT INSERT ON `urllog`.`urls` TO 'urllog'@'localhost';