#webliberty::App::Tb.pm (2006/02/27)
#Copyright(C) 2002-2006 Knight, All rights reserved.

package webliberty::App::Tb;

use strict;
use base qw(webliberty::Basis);
use webliberty::Parser;
use webliberty::String;
use webliberty::Configure;
use webliberty::Lock;
use webliberty::Sendmail;
use webliberty::App::Init;
use webliberty::App::Diary;

### コンストラクタ
sub new {
	my $class = shift;

	my $self = {
	};
	bless $self, $class;

	return $self;
}

### メイン処理
sub run {
	my $self = shift;

	$self->regist;
	$self->output_complete;

	return;
}

### トラックバック受信
sub regist {
	my $self = shift;

	my $tb_id;
	if ($ENV{'PATH_INFO'} =~ /\/(\d+)$/) {
		$tb_id = $1;
	} else {
		$self->error('No TrackBack ID (tb_id)');
	}

	my $init_ins   = new webliberty::App::Init;
	my $parser_ins = new webliberty::Parser(max => $init_ins->get_init('parse_size'), jcode => 1);
	my $config_ins = new webliberty::Configure($init_ins->get_init('data_dir') . $init_ins->get_init('data_config'));

	my $title_ins   = new webliberty::String($parser_ins->get_query('title'));
	my $url_ins     = new webliberty::String($parser_ins->get_query('url'));
	my $excerpt_ins = new webliberty::String($parser_ins->get_query('excerpt'));
	my $blog_ins    = new webliberty::String($parser_ins->get_query('blog_name'));

	$title_ins->create_line;
	$url_ins->create_line;
	$excerpt_ins->create_line;
	$blog_ins->create_line;

	$url_ins->replace_string('~', '%7E');

	if (!$url_ins->get_string) {
		$self->error('No URL (url)');
	}

	my $lock_ins = new webliberty::Lock($init_ins->get_init('data_dir') . $init_ins->get_init('data_lock'));
	if (!$lock_ins->file_lock) {
		$self->error('Now Locking');
	}

	my $index_file = $init_ins->get_init('data_dir') . $init_ins->get_init('data_tb_dir') . $init_ins->get_init('data_tb_index');

	open(FH, $index_file) or $self->error("Read Error : $index_file");
	my @index = sort { $b <=> $a } <FH>;
	close(FH);

	my $new_no         = (split(/\t/, $index[0]))[0] + 1;
	my $trackback_file = $init_ins->get_init('data_dir') . $init_ins->get_init('data_tb_dir') . $tb_id . '.' . $init_ins->get_init('data_ext');

	if (!-e $trackback_file) {
		open(FH, ">$trackback_file") or $self->error("Write Error : $trackback_file");
		close(FH);

		if ($self->{init}->{chmod_mode}) {
			if ($init_ins->get_init('suexec_mode')) {
				chmod(0600, "$trackback_file") or $self->error("Chmod Error : $trackback_file");
			} else {
				chmod(0606, "$trackback_file") or $self->error("Chmod Error : $trackback_file");
			}
		}
	}

	open(FH, ">>$trackback_file") or $self->error("Write Error : $trackback_file");
	print FH "$new_no\t$tb_id\t1\t" . time . "\t" . $blog_ins->get_string . "\t" . $title_ins->get_string . "\t" . $url_ins->get_string . "\t" . $excerpt_ins->get_string . "\n";
	close(FH);

	unshift(@index, "$new_no\t$tb_id\t1\t" . time . "\t" . $blog_ins->get_string . "\t" . $title_ins->get_string . "\t" . $url_ins->get_string . "\n");

	open(FH, ">$index_file") or $self->error("Write Error : $index_file");
	print FH @index;
	close(FH);

	$lock_ins->file_unlock;

	if ($config_ins->get_config('sendmail_tb_mode')) {
		my $sendmail_ins = new webliberty::Sendmail($config_ins->get_config('sendmail_path'));
		foreach (split(/<>/, $config_ins->get_config('sendmail_list'))) {
			if (!$_) {
				next;
			}

			my($flag, $message) = $sendmail_ins->sendmail(
				send_to => $_,
				subject => $config_ins->get_config('site_title') . "がトラックバックを受信しました",
				name    => $blog_ins->get_string,
				message => $config_ins->get_config('site_title') . "が以下のトラックバックを受信しました。\n\n送信元：" . $blog_ins->get_string . "\n\n" . $excerpt_ins->get_string
			);
			if (!$flag) {
				$self->error($message);
			}
		}
	}

	my $dammy;
	$dammy->{query}->{no} = $tb_id;

	my $diary_ins = new webliberty::App::Diary($init_ins->get_init, $config_ins->get_config, $dammy->{query});
	$diary_ins->update;

	return;
}

### 受信完了画面表示
sub output_complete {
	my $self = shift;

	print <<"_XML_";
Content-Type: text/xml

<?xml version="1.0" encoding="iso-8859-1" ?>
<response>
	<error>0</error>
</response>
_XML_

	return;
}

### エラー出力
sub error {
	my $self    = shift;
	my $message = shift;

	print <<"_XML_";
Content-Type: text/xml

<?xml version="1.0" encoding="iso-8859-1" ?>
<response>
	<error>1</error>
	<message>$message</message>
</response>
_XML_

	exit;
}

1;
