Guest User

Untitled

a guest
Feb 19th, 2018
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use vars qw($VERSION %IRSSI);
  5.  
  6. use Irssi;
  7. use DBI;
  8.  
  9. $VERSION = '0.1';
  10.  
  11. %IRSSI = (
  12. author => 'Geoffrey Bachelet',
  13. contact => 'geoffrey@excusemycode.com',
  14. name => 'Debug Bot',
  15. description => 'Quick Bot Script',
  16. license => 'Not to be redistributed',
  17. );
  18.  
  19. Irssi::signal_add('event privmsg', 'event_privmsg');
  20.  
  21. sub trim {
  22. my $string = shift;
  23. my $char = chop $string;
  24. if ($char ne ' ') {
  25. $string .= $char;
  26. }
  27. return $string;
  28. }
  29.  
  30. our ($home, $url_regexp, %dbi, $dontgrab);
  31.  
  32. $home = '/home/ash/irssibot/debug/';
  33. $url_regexp = `/usr/bin/perl -wT $home/scripts/library/url3.pl`;
  34. %dbi = (
  35. datasource => 'dbi:mysql:irssi_debug',
  36. username => 'irssi',
  37. password => '.jeJbLcPRxfr8ucL',
  38. );
  39. $dontgrab = 'pastey.net|paste2.org|pastie.caboo.se|pastebin|localhost|http:\/\/10.|example.com|r.phpug.se';
  40.  
  41. sub get_dbh {
  42. return DBI->connect($dbi{'datasource'}, $dbi{'username'}, $dbi{'password'}) or die $DBI::errstr;
  43. }
  44.  
  45. sub event_privmsg {
  46. my ($server, $data, $nick, $mask) = @_;
  47. my ($target, $text) = $data =~ /^(\S*)\s:(.*)/;
  48. my $mode = 'public';
  49. if ($target =~ /^debug/) {
  50. $mode = 'query';
  51. $target = $nick;
  52. }
  53.  
  54. my $dbh = &get_dbh();
  55.  
  56. if ($text =~ /($url_regexp)/) {
  57. if ($1 =~ /$dontgrab/) {
  58. return 0;
  59. }
  60. my $sql = qq { INSERT INTO urls (url, context, channel, nick, datetime) VALUES(?, ?, ?, ?, NOW()) };
  61. my $sth = $dbh->prepare($sql);
  62. $sth->bind_param(1, $1);
  63. $sth->bind_param(2, $text);
  64. $sth->bind_param(3, $target);
  65. $sth->bind_param(4, $nick);
  66. if ($sth->execute()) {
  67. print 'url registered: '.$1;
  68. }
  69. }
  70.  
  71. if (my ($id) = ($text =~ /^\$url del (\d+)$/)) {
  72. if ($nick eq 'ash' || $nick eq 'mirmo') {
  73. my $sql = qq { DELETE FROM urls WHERE id = ? };
  74. my $sth = $dbh->prepare($sql);
  75. $sth->bind_param(1, $id);
  76. $sth->execute();
  77. } else {
  78. $server->command('MSG '.$target.' NAON !!!');
  79. }
  80. }
  81.  
  82. if (my ($id) = ($text =~ /^\$url( \d+)?$/)) {
  83. my @rows;
  84. if ($id ne '') {
  85. @rows = $dbh->selectrow_array(qq { SELECT id, url FROM urls WHERE id = ? }, undef, $id);
  86. } else {
  87. @rows = $dbh->selectrow_array(qq { SELECT id, url FROM urls ORDER BY RAND() LIMIT 1 });
  88. }
  89. my ($id, $url) = @rows;
  90. if ($id ne '') {
  91. $server->command('MSG '.$target.' #'.$id.' '.$url);
  92. } else {
  93. $server->command('MSG '.$target.' url not found: '.$1);
  94. }
  95. }
  96.  
  97. if ($text =~ /^\$url like ([A-Za-z0-9_.%?-]+)( ([0-9]+))?$/) {
  98. my $limit = $3 ? qq { LIMIT $3,5 } : 'LIMIT 5';
  99. my $sql = qq { SELECT SQL_CALC_FOUND_ROWS id, url FROM urls WHERE url LIKE '%$1%' ORDER BY datetime DESC $limit };
  100. my $sth = $dbh->prepare($sql);
  101. $sth->execute();
  102. my $counth = $dbh->prepare('SELECT FOUND_ROWS();');
  103. $counth->execute();
  104. my ($found_rows);
  105. $counth->bind_columns(undef, \$found_rows);
  106. $counth->fetch();
  107. $server->command('MSG '.$target.' '.$found_rows.' urls matching your pattern');
  108. my ($id, $url);
  109. $sth->bind_columns(undef, \$id, \$url);
  110. while ($sth->fetch()) {
  111. $server->command('MSG '.$target.' #'.$id.' '.$url);
  112. }
  113. }
  114.  
  115. if ($text =~ /^\$url last$/) {
  116. my $sql = qq { SELECT id, url FROM urls ORDER BY datetime DESC LIMIT 1 };
  117. my $sth = $dbh->prepare($sql);
  118. $sth->execute();
  119. my ($id, $url);
  120. $sth->bind_columns(undef, \$id, \$url);
  121. while ($sth->fetch()) {
  122. $server->command('MSG '.$target.' #'.$id.' '.$url);
  123. }
  124. }
  125.  
  126. if ($text =~ /^\$url tail$/) {
  127. my $sql = qq { SELECT id, url FROM urls ORDER BY datetime DESC LIMIT 5 };
  128. my $sth = $dbh->prepare($sql);
  129. $sth->execute();
  130. my ($_id, $_url);
  131. $sth->bind_columns(undef, \$_id, \$_url);
  132. while ($sth->fetch()) {
  133. $server->command('MSG '.$target.' #'.$_id.' '.$_url);
  134. }
  135. }
  136.  
  137. if ($text =~ /^\$url info (\d+)$/) {
  138. my $sql = qq { SELECT id, url, datetime, channel, nick FROM urls WHERE id = ? };
  139. my $sth = $dbh->prepare($sql);
  140. $sth->bind_param(1, $1);
  141. $sth->execute();
  142. my ($_id, $_url, $_datetime, $_channel, $_nick);
  143. $sth->bind_columns(undef, \$_id, \$_url, \$_datetime, \$_channel, \$_nick);
  144. while ($sth->fetch()) {
  145. $server->command('MSG '.$target.' #'.$_id.' said by '.$_nick.' on '.$_channel.' at '.$_datetime);
  146. }
  147. }
  148. }
Add Comment
Please, Sign In to add comment