# -*- mode: perl; coding: utf-8 -*-
# keitairc/lib/plugins/00postme

# The line number (1 incremented) and filename below must be
# actual. see perlsyn.
# line 9 "keitairc/lib/plugins/00postme"

use Net::SMTP;

$plugin = {
	name => 'postme',
	action_imprementation => sub {
		my ($request, $name, $session_id, $param_string) = @_;
		my $ci = new Keitairc::ClientInfo($request);
		my $view = new Keitairc::View($::cf, $ci);
		my $mail_charset = 'iso-2022-jp';

		{
			my $error = {};
			$error->{no_smtp_server} = 1 unless $::cf->smtp_server();
			$error->{no_smtp_from} = 1 unless $::cf->smtp_from();
			$error->{no_smtp_to} = 1 unless $::cf->smtp_to();
			if(keys(%{$error})){
				$error->{has_error} = 1;
				$error->{session_id} = $session_id;
				return $view->render('postme.html', $error);
			}
		}

		my $message = $request->content();
		$message =~ s/^text=//;
		$message = ::uri_unescape($message);
		Encode::from_to($message, $::cf->web_charset(), $mail_charset);

		my $smtp = Net::SMTP->new($::cf->smtp_server());
		$smtp->mail($::cf->smtp_from());
		$smtp->to($::cf->smtp_to());
		$smtp->data();
		$smtp->datasend(
			$view->template('postme.eml', {
				from => $::cf->smtp_from(),
				to => $::cf->smtp_to(),
				message => $message,
				})
			);
		$smtp->dataend();
		$smtp->quit();

		my $p = {};
		$p->{smtp_to} = $::cf->smtp_to();
		$p->{session_id} = $session_id;
		return $view->render('postme.html', $p);
	}
};

1;
