Advertisement
Guest User

Untitled

a guest
Jan 17th, 2013
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.64 KB | None | 0 0
  1. #!/usr/bin/perl
  2. package MyBot;
  3. use POE::Kernel;
  4. use POE::Component::IRC::Plugin::AutoJoin;
  5. use POE::Component::IRC::Plugin::NickServID;
  6. use Data::Validate::URI qw(is_http_uri is_uri);
  7. use Bot::BasicBot;
  8. use LWP::Simple;
  9. use HTTP::Headers;
  10. use LWP::UserAgent;
  11. use DBI;
  12. use base qw( Bot::BasicBot );
  13. use warnings;
  14. use strict;
  15.  
  16. #IRC Server Settings
  17. my $server   =  "irc.freenode.net";
  18. my $port     =  "6667";
  19. my $channel  =  [ "#squixy", "#reddit", "#Ubuntu", "#perl", "##freebsd" ];
  20. my $nick     =  "Squixy_";
  21. my $name =  "Kyle-Work";
  22.  
  23. #Rejoin Settings;
  24. my $rejoin = POE::Component::IRC::Plugin::AutoJoin->new(
  25.   Channels => $channel,
  26.   RejoinOnKick => 1,
  27.   Rejoin_delay => 67,
  28. );
  29.  
  30. #Nickserv Password
  31. my $pass = POE::Component::IRC::Plugin::NickServID->new(
  32.   Password => '********'
  33. );
  34.  
  35. #MySQL Database Settings
  36. my $host = "localhost";
  37. my $database = "dbi:mysql:ircz";
  38. my $tablename = "images";
  39. my $user = "root";
  40. my $pw = "****************";
  41.  
  42. #Connect to database
  43. my $dbh = DBI->connect($database,$user,$pw) or die "Connection Error: $DBI::errstr\n";
  44.  
  45. #Setup Image Type Hashes
  46. my %imagetypes = (
  47.   "image/png" => "png",
  48.   "image/jpeg" => "jpeg",
  49.   "image/jpg" => "jpg",
  50.   "image/gif" => "gif",
  51.   "image/bmp" => "bmp"
  52. );
  53.  
  54. #Detect URL's
  55. sub said {
  56.   my ($speak, $message) = @_;
  57.   my @text = split(' ',$message->{body});
  58.   my $chan = $message->{channel};
  59.   for my $string (@text) {
  60.     urlval($string, $chan);
  61.   }
  62. }
  63.  
  64. #Test if URL is valid
  65. sub urlval {
  66.   my ($url, $chan) = @_;
  67.   if (length($url) < 200) {
  68.     if(is_http_uri($url)){
  69.       if ($url =~ /wiki/){
  70.         return 0;
  71.       }
  72.       image_test($url, $chan);
  73.     }
  74.   }
  75. }
  76.  
  77. #Test if URL is an image
  78. sub image_test {
  79.   my ($url, $chan) = @_;
  80.   my $ua = LWP::UserAgent->new;
  81.   $ua->timeout(10);
  82.   my $response = $ua->get($url);
  83.   my $imgtype = $response ->header('Content-Type');
  84.   if ($imgtype =~ m,image/,) {
  85.     my $type = $imagetypes{$imgtype};
  86.     if (!$type) {
  87.       return 0;
  88.     }
  89.     my $size = $response ->header('Content-Length');
  90.     if ($size > 5242880) {
  91.       return 0;
  92.     }
  93.     database($url, $size, $type, $chan);
  94.   }
  95.  
  96. }
  97.  
  98. #Write image to database
  99. sub database {
  100.   my ($url, $size, $type, $chan) = @_;
  101.   my $sql = "select URL from images where URL = \"$url\" or Size = \"$size\";";
  102.   my $sth = $dbh->prepare($sql);
  103.   $sth->execute or die "SQL Error: $DBI::errstr\n";
  104.   if(!$sth->fetchrow_array) {
  105.     my $sql = "insert into images (URL, FileType, Size, Channel) values (\"$url\", \"$type\", \"$size\", \"$chan\");";
  106.     my $sth = $dbh->prepare($sql);
  107.     $sth->execute or die "SQL Error: $DBI::errstr\n";
  108.     download($url, $type);
  109.   }
  110. }
  111.  
  112. #CREATE TABLE images( ID INT NOT NULL AUTO_INCREMENT,
  113. #                      URL VARCHAR(200),
  114. #                      Date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  115. #                      FileType VARCHAR(4),
  116. #                      Size VARCHAR(7),
  117. #                      Channel VARCHAR(30),
  118. #                      PRIMARY KEY(ID)
  119. #);
  120.  
  121. sub download {
  122.   my ($url, $type) = @_;
  123.   my $sql = "select ID from images where URL = \"$url\";";
  124.   my $sth = $dbh->prepare($sql);
  125.   $sth->execute or die "SQL Error: $DBI::errstr\n";
  126.   my $ID = $sth->fetchrow_array;
  127.   my $file = "/var/www/images/$ID.$type";
  128.   getstore($url,$file);
  129. }
  130.  
  131. sub help { return 0; }
  132.  
  133. #Create bot and connect
  134. my $bot = MyBot->new(
  135.   server     => $server,
  136.   port       => $port,
  137.   channels   => $channel,
  138.   nick       => $nick,
  139.   name       => $name,
  140.   no_run     => 1
  141. );
  142.  
  143. $bot->run();
  144. $bot->pocoirc->plugin_add('AutoJoin', $rejoin);
  145. $bot->pocoirc->plugin_add('NickServID', $pass);
  146. POE::Kernel->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement