Posted by Don Juan Tue 13th Mar 2007 18:54 - Syntax is Perl - 136 views
Download | New Post | Modify | Hide line numbers
  1. dion@titan:~$ cat deoxy_bot/Carlo.pl
  2. #!/usr/bin/perl -w
  3.  
  4. use Digest::MD5 (md5 md5_hex md5_base64);
  5.  
  6. # Create a user agent object
  7. use LWP::UserAgent;
  8. use HTTP::Cookies;
  9.  
  10. $ua = LWP::UserAgent->new;
  11. $ua->agent("MyApp/0.1 ");
  12.  
  13. # setup a ua cookie-jar
  14. $ua->cookie_jar(HTTP::Cookies->new(file => "Carlo_cookies.txt",
  15.                                      autosave => 1));
  16.  
  17. # log on deoxy.org
  18.  
  19. my $req = HTTP::Request->new(POST => 'http://deoxy.org/forum/start_page.pl');
  20. $req->content_type('application/x-www-form-urlencoded');
  21. $req->content("Username=Carlo&Password=XHvkdq&option=Login");
  22. my $res = $ua->request($req);
  23.  
  24. "\nrequest:". $res->content if ($res->is_success);
  25.  
  26. # Create a display request
  27. my $req = HTTP::Request->new(GET => 'http://deoxy.org/forum/chat.pl?show=body');
  28. my $loop_cnt;
  29. my @prev_msg_hashes;
  30.  
  31. while(1) {
  32.  # Pass request to the user agent and get a response back
  33.  my $res = $ua->request($req);
  34.  
  35.  if ($res->is_success) {
  36.   # Get the actual chat messages
  37.  
  38.   my $messages = $1 if $res->content =~ /(
    (<|))/;
  39.  
  40.   #print "loop #$loop_cnt\n". join ',', @prev_msg_hashes;
  41.   #print "$messages\n";
  42.  
  43.   #print "extracted messages:\n";
  44.  
  45.   my @messages;
  46.  
  47.     @messages, {sign=>$1, msg=>$2, hash=>md5_hex($1.$2)};
  48.   }
  49.  
  50. #  print join "\n", map "md5: $_->{hash}\n<$_->{sign}> $_->{msg}", @messages;
  51.  
  52.   my @new_msgs;
  53.  
  54.   foreach $message (@messages) {
  55.     @new_msgs, $message if( ! (/$message->{hash}/, @prev_msg_hashes));
  56.   }
  57.  
  58.   "\n", "md5: $_->{hash}\n<$_->{sign}> $_->{msg}", @new_msgs;
  59.  
  60.   foreach $message (@new_msgs) {
  61.     say("Hi $message->{sign}, Im a bot") if ($message->{msg} =~ /Hi Carlo ?/i);
  62.   }
  63.  
  64.   @prev_msg_hashes = $_->{hash}, @messages;
  65.  }
  66.  $loop_cnt++;
  67.   8;
  68. }
  69.  
  70. sub say {
  71.  my $message = ;
  72.   "posting message: $message";
  73.  # Create a talk post request
  74.   my $req = HTTP::Request->new(POST => 'http://deoxy.org/forum/chat.pl?show=form');
  75.  $req->content_type('application/x-www-form-urlencoded');
  76.  $req->content("message=$message&submit=talk");
  77.  my $res = $ua->request($req);
  78. # print "\nrequest:". $res->content if ($res->is_success);
  79. }
  80.  

PermaLink to this entry https://pastebin.co.uk/11835
Posted by Don Juan Tue 13th Mar 2007 18:54 - Syntax is Perl - 136 views
Download | New Post | Modify | Hide line numbers

 

Comments: 0