#!/usr/bin/perl -w
#
# Травля гутника в комментах.
# Поиск последних комментов целевого жж-юзера (гутника)
# и отправка ответов на них (массив @phrases).
#
# Капча и иные подобные исключения не обрабатываются.
# Ставить в cron и радоваться :)
#
# Строку с указанием прокси раскомментировать при необходимости.
#
use strict;
use LWP::UserAgent;
use HTTP::Cookies;
use Compress::Zlib;

our $VERSION = 0.4;

my $lj_login = 'gutenberger';
my $lj_password = '123asdfg';
my $target = 'gutnik-rea%2C+gutnik-real';
#my $target_comment_search_link = 'http://blogs.yandex.ru/search.xml?text=&ft=comments&server=livejournal.com&author='.$target.'&holdres=mark';
my $target_comment_search_link = 'http://blogs.yandex.ru/search.xml?text=&ft=comments&link=&server=livejournal.com&from_day=&from_month=&from_year=&to_day=&to_month=&to_year=&whom=on&author='.$target.'&journal=&f_user=&geo=&gender=&age_from=&age_to=&music=&mood=&category=&suptest=';

my @phrases = (
'Поди на хуй, Гутник!',
'Гутник! На хуй иди!',
);

our $cookies = HTTP::Cookies->new('file'=>'./cookies.lwp','autosave'=>0);
my $browser = LWP::UserAgent->new('agent' => 'Opera/9.10 (X11; Linux i686; U; en)',
								  'cookie_jar' => $cookies,
								  'requests_redirectable' => ['GET', 'POST']);
$browser->default_header('Accept' => 'text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1',
						'Accept-Charset' => 'iso-8859-1, utf-8, utf-16, *;q=0.1',
						'Accept-Language' => 'ru,en;q=0.9',
						'Accept-Encoding' => 'deflate, gzip, x-gzip, identity, *;q=0',
						);
#$browser->proxy(['http',], 'http://127.0.0.1:8118');

########## MAIN #############
my @comments = &YandexCommentParser();
&LJ_login($lj_login,$lj_password);
foreach (@comments){
  shift;
  my ($target_user,$target_post_id,$target_comment_id) = split(/,/);
	&PostComment($target_user,$target_post_id,$target_comment_id,\@phrases);
	sleep (int(rand(3))+2);
}

exit;

########## PROCEDURES ##########
sub PostComment($$$){
#print "$_[0]\t$_[1]\t$_[2]\n"; exit;
my ($target_user,$target_post_id,$target_comment_id,$phrases) = @_;

  my $response = $browser->get('http://'.$target_user.'.livejournal.com/'.$target_post_id.'.html?replyto='.$target_comment_id.'&format=light');
  my $source = Compress::Zlib::memGunzip(\($response->content)[0]);
	
	my ($lj_form_auth,$chal,$resp,$replyto,$parenttalkid,$chrp1) = ($1,$2,$3,$4,$5,$6) if ($source =~ m!name="lj_form_auth" value="(.+?)".+id='login_chal' value='(.+?)'.+id='login_response' value='(.*?)'.+name="replyto" value="(\d+?)".+name="parenttalkid" value="(\d+?)".+name="chrp1" value="(.+?)" />!);
		
	$response = $browser->post('http://www.livejournal.com/talkpost_do.bml',
		[
		'lj_form_auth'	=> $lj_form_auth,
		'chal'			=> $chal,
		'response'		=> $resp,
		'replyto'		=> $replyto,
		'parenttalkid'	=> $parenttalkid,
		'itemid'		=> $target_post_id,
		'journal'		=> $target_user,
		'stylemine'		=> '',
		'chrp1'			=> $chrp1,
		'usertype'		=> 'cookieuser',
		'cookieuser'	=> $lj_login,
		'subject'		=> 'Гутник!',
		'body'			=> @{$phrases}[int(rand($#$phrases+1))],
		'submitpost'	=> 'Post Comment',
		'editid'		=> '0',
		]
	);
	do{print STDOUT "Connection error: ".$response->status_line,"\r\n";next;} unless $response->is_success;
}

sub LJ_login($$){
	my $response = $browser->post('http://www.livejournal.com/login.bml?ret=1',
    [
     'mode' => 'login',
     'user' => $lj_login,
     'password' => $lj_password,
    ]);
	do{print STDOUT "Connection error: ".$response->status_line,"\r\n";exit;} unless $response->is_success;
}

sub YandexCommentParser(){
  my @comments;
	my $response = $browser->get($target_comment_search_link);
	do{print STDOUT "Connection error: ".$response->status_line,"\r\n";exit;} unless $response->is_success;
	my $source = $response->content;
	while ($source =~ m#