Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 12th, 2010 | Syntax: None | Size: 4.78 KB | Hits: 87 | Expires: Never
Copy text to clipboard
  1. #!/usr/bin/perl
  2.  
  3. # Twitter IRC Bot
  4. # This bot is one way communication with twitter.
  5.  
  6. # to use just edit the bot and change the twitter username and pass.
  7. # then set it up in a channel
  8.  
  9. # then to send an update type:
  10. # !twitter update text
  11.  
  12. # an update will be sent right away.
  13.  
  14. # the bot will also send a twitter update on topic change.
  15.  
  16. # awesome
  17. # - harper
  18.  
  19. # thanks to b0iler for his page "Bare Bones IRC Bot In Perl"
  20. # http://b0iler.eyeonsecurity.org/tutorials/ircperlbot.htm
  21.  
  22. # this script is largely based upon his barebones framework
  23.  
  24. use LWP::Simple;
  25. use LWP::UserAgent;
  26. use HTML::TokeParser;
  27. use IO::Socket;
  28. use URI::Escape;
  29.  
  30. # configure variables
  31. my $ircserver = "irc.yossman.net";
  32. my $ircchannel = "#quake";
  33. my $nickname = "rodcal";
  34. my $username = "crazyoldman";
  35. my $twituser = "pointyoumiss";
  36. my $twitpass= "byxnet";
  37.  
  38. my $helpmessage = "I am the rodcal insanity machine. I will update twitter for ".$ircchannel.". If you want to send an update just enter: !pointyoumiss <update text>. If you want to see all updates made - please visit twitter.com/".$twituser;
  39.  
  40. my $browser = LWP::UserAgent->new;
  41.  
  42. # connect to the IRC server
  43. $sock = IO::Socket::INET->new(
  44.         PeerAddr => $ircserver,
  45.         PeerPort => 6667,
  46.         Proto => 'tcp' ) or die "could not make the connection";
  47.        
  48. while($line = <$sock>){
  49.         print $line;
  50.         if($line =~ /(NOTICE AUTH).*(checking ident)/i){
  51.                 print $sock "NICK $nickname\nUSER $username 0 0 :just a bot\n";
  52.                 last;
  53.         }
  54. }
  55.  
  56. while($line = <$sock>){
  57.         print $line;    
  58.         #use next line if the server asks for a ping
  59.         if($line =~ /^PING/){
  60.                 print $sock "PONG :" . (split(/ :/, $line))[1];
  61.         }
  62.         if($line =~ /(376|422)/i){
  63.                 #print $sock "NICKSERV :identify nick_password\n";
  64.                 last;
  65.         }
  66. }
  67.  
  68. sleep 3;
  69.  
  70. # join the channel
  71. print $sock "JOIN $ircchannel \n";
  72.  
  73. # main loop
  74. print "/------------------------------------------------------------------------\n";
  75. print "| Twitter IRC Bot \n";
  76. print "|----------------------------------------------\n";
  77. print "|\n";
  78. while ($line = <$sock>) {
  79.         #$text is the stuff from the ping or the text from the server
  80.         ($command, $text) = split(/ :/, $line);  
  81.         if ($command eq 'PING'){
  82.                 #while there is a line break - many different ways to do this
  83.                 while ( (index($text,"\r") >= 0) || (index($text,"\n") >= 0) ){ chop($text); }
  84.                 print $sock "PONG $text\n";
  85.                 next;
  86.         }
  87.         #done with ping handling
  88.        
  89.         ($nick,$type,$channel) = split(/ /, $line); #split by spaces
  90.        
  91.         ($nick,$hostname) = split(/!/, $nick); #split by ! to get nick and hostname seperate
  92.        
  93.         $nick =~ s/://; #remove :'s
  94.         #$text =~ s/://;
  95.        
  96.         #get rid of all line breaks.  Again, many different way of doing this.
  97.         $/ = "\r\n";
  98.         while($text =~ m#$/$#){ chomp($text); }
  99.        
  100.  
  101.         if ($command =~ /TOPIC/){
  102.                 my $topic_update = "Topic changed by $nick:  $text\n\n\n";
  103.                 $topic_update =~ s/</[/g;
  104.         $topic_update =~ s/>/]/g;
  105.                 my $topic_delurl = "http://" . $twituser . ":" . $twitpass ."\@twitter.com/statuses/update.xml?status=".$topic_update;
  106.                 print $topic_delurl;
  107.                 my $topic_response = $browser->post( $topic_delurl );
  108.                 my $topic_responsetext = $topic_response->content;
  109.                 print $topic_responsetext;
  110.                 if ($topic_responsetext =~ /\<created_at\>/){
  111.                         print $sock "PRIVMSG $ircchannel :* Twitter updated: ".$topic_update."\n";
  112.                 }else{
  113.                         print $sock "PRIVMSG $ircchannel :* Twitter update failed\n" ;
  114.                 }
  115.                 $topic_responsetext = "";
  116.                 $topic_update = "";
  117.         }
  118.  
  119.        
  120.         if($channel eq $ircchannel){
  121.                 print "<$nick> $text\n";
  122.                 if($text =~ /^!twitterhelp(.*)/){
  123.                 print $sock "PRIVMSG $ircchannel :* ".$helpmessage."\n";
  124.                 }
  125.                 if($text =~ /^!pointyoumiss (.*)/){
  126.                         my $update = "[".$nick."] ".$1;
  127.                         $update =~ s/</[/g;
  128.             $update =~ s/>/]/g;
  129.                         my $delurl = "http://" . $twituser . ":" . $twitpass ."\@twitter.com/statuses/update.xml?status=". urlencode($update);
  130.                         my $response = $browser->post( $delurl );
  131.                         my $responsetext = $response->content;
  132.                         print $responsetext;
  133.                         if ($responsetext =~ /\<created_at\>/){
  134.                                 print $sock "PRIVMSG $ircchannel :* Twitter updated: ".$update."\n";
  135.                         }else{
  136.                                 print $sock "PRIVMSG $ircchannel :* Twitter update failed\n" ;
  137.                         }
  138.                         $responsetext = "";
  139.                         $update = "";
  140.                        
  141.                 }
  142.         }
  143. }
  144.  
  145.  
  146. ###########
  147. # subroutine: urlencode a string
  148. ###########
  149.  
  150. sub urlencode {
  151.  
  152. my $ask = shift @_;
  153. my @a2 = unpack "C*", $ask;
  154. my $s2 = "";
  155. while (@a2) {
  156.     $s2 .= sprintf "%%%X", shift @a2;
  157. }
  158. return $s2;
  159.  
  160. }