Guest User

Untitled

a guest
Nov 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. use strict;
  2. use warnings;
  3. use Encode;
  4. use Encode::Guess ();
  5. use Mojo::UserAgent;
  6. use Mojo::IOLoop;
  7. use AnySan;
  8. use AnySan::Provider::IRC;
  9.  
  10.  
  11. my $ua = Mojo::UserAgent->new;
  12.  
  13. my $channel_name = '#bottest';
  14. my $irc = irc '',
  15. nickname => 'logbot',
  16. channels => {
  17. $channel_name => {},
  18. };
  19.  
  20. AnySan->register_listener(
  21. logbot => {
  22. cb => sub {
  23. my $receive = shift;
  24. my $message = $receive->message;
  25. return unless $receive->attribute('command') eq 'PRIVMSG' && $message;
  26.  
  27. my $http_URL_regex =
  28. q{\b(?:https?|shttp|ftp)://(?:(?:[-_.!~*'()a-zA-Z0-9;:&=+$,]|%[0-9A-Fa-f} .
  29. q{][0-9A-Fa-f])*@)?(?:(?:[a-zA-Z0-9](?:[-a-zA-Z0-9]*[a-zA-Z0-9])?\.)} .
  30. q{*[a-zA-Z](?:[-a-zA-Z0-9]*[a-zA-Z0-9])?\.?|[0-9]+\.[0-9]+\.[0-9]+\.} .
  31. q{[0-9]+)(?::[0-9]*)?(?:/(?:[-_.!~*'()a-zA-Z0-9:@&=+$,]|%[0-9A-Fa-f]} .
  32. q{[0-9A-Fa-f])*(?:;(?:[-_.!~*'()a-zA-Z0-9:@&=+$,]|%[0-9A-Fa-f][0-9A-} .
  33. q{Fa-f])*)*(?:/(?:[-_.!~*'()a-zA-Z0-9:@&=+$,]|%[0-9A-Fa-f][0-9A-Fa-f} .
  34. q{])*(?:;(?:[-_.!~*'()a-zA-Z0-9:@&=+$,]|%[0-9A-Fa-f][0-9A-Fa-f])*)*)} .
  35. q{*)?(?:\?(?:[-_.!~*'()a-zA-Z0-9;/?:@&=+$,]|%[0-9A-Fa-f][0-9A-Fa-f])} .
  36. q{*)?(?:#(?:[-_.!~*'()a-zA-Z0-9;/?:@&=+$,]|%[0-9A-Fa-f][0-9A-Fa-f])*} .
  37. q{)?};
  38.  
  39. my $r = qr/$http_URL_regex/;
  40.  
  41. my $url; = $1;
  42. if ( $message =~ m/($r)/ ) {
  43. $url = $1;
  44. }
  45.  
  46. if ( $url ) {
  47.  
  48. my $tx = $ua->max_redirects(3)->get($url);
  49. my @titles = $tx->res->dom->at('title')->text;
  50. if ( ! @titles ) {
  51. @titles = $tx->res->dom->at('h1')->text;
  52. }
  53.  
  54. my $title = $titles[0];
  55.  
  56. if ( ! Encode::is_utf8($title) ) {
  57. my $enc = Encode::Guess::guess_encoding($tx->res->to_string, qw/euc-jp shiftjis 7bit-jis/);
  58. $title = $enc->decode($title);
  59. $title = encode_utf8( $title );
  60. }
  61.  
  62. if ( $title ) {
  63. $receive->send_reply($title . ' ' . $url);
  64. }
  65. }
  66. },
  67. },
  68. );
  69. AnySan->run;
Add Comment
Please, Sign In to add comment