Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use vars qw($VERSION %IRSSI);
- use Irssi;
- use DBI;
- $VERSION = '0.1';
- %IRSSI = (
- author => 'Geoffrey Bachelet',
- contact => 'geoffrey@excusemycode.com',
- name => 'Debug Bot',
- description => 'Quick Bot Script',
- license => 'Not to be redistributed',
- );
- Irssi::signal_add('event privmsg', 'event_privmsg');
- sub trim {
- my $string = shift;
- my $char = chop $string;
- if ($char ne ' ') {
- $string .= $char;
- }
- return $string;
- }
- our ($home, $url_regexp, %dbi, $dontgrab);
- $home = '/home/ash/irssibot/debug/';
- $url_regexp = `/usr/bin/perl -wT $home/scripts/library/url3.pl`;
- %dbi = (
- datasource => 'dbi:mysql:irssi_debug',
- username => 'irssi',
- password => '.jeJbLcPRxfr8ucL',
- );
- $dontgrab = 'pastey.net|paste2.org|pastie.caboo.se|pastebin|localhost|http:\/\/10.|example.com|r.phpug.se';
- sub get_dbh {
- return DBI->connect($dbi{'datasource'}, $dbi{'username'}, $dbi{'password'}) or die $DBI::errstr;
- }
- sub event_privmsg {
- my ($server, $data, $nick, $mask) = @_;
- my ($target, $text) = $data =~ /^(\S*)\s:(.*)/;
- my $mode = 'public';
- if ($target =~ /^debug/) {
- $mode = 'query';
- $target = $nick;
- }
- my $dbh = &get_dbh();
- if ($text =~ /($url_regexp)/) {
- if ($1 =~ /$dontgrab/) {
- return 0;
- }
- my $sql = qq { INSERT INTO urls (url, context, channel, nick, datetime) VALUES(?, ?, ?, ?, NOW()) };
- my $sth = $dbh->prepare($sql);
- $sth->bind_param(1, $1);
- $sth->bind_param(2, $text);
- $sth->bind_param(3, $target);
- $sth->bind_param(4, $nick);
- if ($sth->execute()) {
- print 'url registered: '.$1;
- }
- }
- if (my ($id) = ($text =~ /^\$url del (\d+)$/)) {
- if ($nick eq 'ash' || $nick eq 'mirmo') {
- my $sql = qq { DELETE FROM urls WHERE id = ? };
- my $sth = $dbh->prepare($sql);
- $sth->bind_param(1, $id);
- $sth->execute();
- } else {
- $server->command('MSG '.$target.' NAON !!!');
- }
- }
- if (my ($id) = ($text =~ /^\$url( \d+)?$/)) {
- my @rows;
- if ($id ne '') {
- @rows = $dbh->selectrow_array(qq { SELECT id, url FROM urls WHERE id = ? }, undef, $id);
- } else {
- @rows = $dbh->selectrow_array(qq { SELECT id, url FROM urls ORDER BY RAND() LIMIT 1 });
- }
- my ($id, $url) = @rows;
- if ($id ne '') {
- $server->command('MSG '.$target.' #'.$id.' '.$url);
- } else {
- $server->command('MSG '.$target.' url not found: '.$1);
- }
- }
- if ($text =~ /^\$url like ([A-Za-z0-9_.%?-]+)( ([0-9]+))?$/) {
- my $limit = $3 ? qq { LIMIT $3,5 } : 'LIMIT 5';
- my $sql = qq { SELECT SQL_CALC_FOUND_ROWS id, url FROM urls WHERE url LIKE '%$1%' ORDER BY datetime DESC $limit };
- my $sth = $dbh->prepare($sql);
- $sth->execute();
- my $counth = $dbh->prepare('SELECT FOUND_ROWS();');
- $counth->execute();
- my ($found_rows);
- $counth->bind_columns(undef, \$found_rows);
- $counth->fetch();
- $server->command('MSG '.$target.' '.$found_rows.' urls matching your pattern');
- my ($id, $url);
- $sth->bind_columns(undef, \$id, \$url);
- while ($sth->fetch()) {
- $server->command('MSG '.$target.' #'.$id.' '.$url);
- }
- }
- if ($text =~ /^\$url last$/) {
- my $sql = qq { SELECT id, url FROM urls ORDER BY datetime DESC LIMIT 1 };
- my $sth = $dbh->prepare($sql);
- $sth->execute();
- my ($id, $url);
- $sth->bind_columns(undef, \$id, \$url);
- while ($sth->fetch()) {
- $server->command('MSG '.$target.' #'.$id.' '.$url);
- }
- }
- if ($text =~ /^\$url tail$/) {
- my $sql = qq { SELECT id, url FROM urls ORDER BY datetime DESC LIMIT 5 };
- my $sth = $dbh->prepare($sql);
- $sth->execute();
- my ($_id, $_url);
- $sth->bind_columns(undef, \$_id, \$_url);
- while ($sth->fetch()) {
- $server->command('MSG '.$target.' #'.$_id.' '.$_url);
- }
- }
- if ($text =~ /^\$url info (\d+)$/) {
- my $sql = qq { SELECT id, url, datetime, channel, nick FROM urls WHERE id = ? };
- my $sth = $dbh->prepare($sql);
- $sth->bind_param(1, $1);
- $sth->execute();
- my ($_id, $_url, $_datetime, $_channel, $_nick);
- $sth->bind_columns(undef, \$_id, \$_url, \$_datetime, \$_channel, \$_nick);
- while ($sth->fetch()) {
- $server->command('MSG '.$target.' #'.$_id.' said by '.$_nick.' on '.$_channel.' at '.$_datetime);
- }
- }
- }
Add Comment
Please, Sign In to add comment