#
# Copyright (c) 2009 by KenjiE20 <longbow@longbowslair.co.uk>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

#
# Add 'Channel Monitor' bar to show IRC channel messages without
# constantly switching buffers
#

weechat::register("chanmon", "KenjiE20", "1.0", "GPL3", "Channel Monitor", "", "");
weechat::bar_item_new("chanmon", "chanmon_main", "");
weechat::bar_new("chanmon", "0", "0", "root", "", "bottom", "horizontal",
                 "vertical", "7", "0", "default", "default", "default", "1",
                 "chanmon");
weechat::hook_signal("*,irc_in2_privmsg", "chanmon_new_message", "");
weechat::hook_signal("*,irc_out_privmsg", "chanmon_new_message", "");
weechat::hook_command("monitor", "Toggles monitoring for a channel", "", "", "", "chanmon_toggle", "");
weechat::hook_config("plugins.var.perl.chanmon.*", "", "");

my $count = "0";
my $arr0 = "";
my $arr1 = "";
my $arr2 = "";
my $net = "";
my $chan = "";
my $nick = "";
my $text = "";
my $str = "";
my $toggle = "";

sub chanmon_main
{
	$str .= "\r";

#	DEBUG LINES
#	$count++;
#	$str .= $count." 0: ".$arr0." 1: ".$arr1." 2: ".$arr2."\r";

($sec,$min,$hour,$mday,$mon,$year,$wday) = localtime(time);
$hour =~ s/^(.)$/0$1/;
$min =~ s/^(.)$/0$1/;             # Fixes the display of mins/secs below
$sec =~ s/^(.)$/0$1/;             # it displays 03 instead of 3

	if ($toggle eq "on")
	{
		$nicename = $bufname;
		$nicename =~ s/(.*)\.#(.*)/$1#$2/;
		$str .= "[".$hour.":".$min."] ".$nicename.": Channel Monitoring Enabled";
		$toggle = "";
	}
	elsif ($toggle eq "off")
	{
		$nicename = $bufname;
		$nicename =~ s/(.*)\.#(.*)/$1#$2/;
		$str .= "[".$hour.":".$min."] ".$nicename.": Channel Monitoring Disabled";
		$toggle = "";
	}

	elsif ($arr1 =~ /(.*),irc_out_PRIVMSG/)
	{
		$net = $1;

		if ($arr2 =~ /PRIVMSG (.*)/)
		{
			my $msg = $1;
			my $nick = weechat::info_get("irc_nick", $net);
			if ($msg =~ /#(.*) :(.*)$/)
			{
				my ($chan, $text) = ($1, $2);
				while ($chan =~ /(.*) :(.*)/)
				{
					$chan = $1;
					$text = $2." :".$text;
				}
				if (weechat::config_get_plugin($net.".#".$chan) eq "")
				{
					weechat::config_set_plugin($net.".#".$chan, "True");
				}
				if (weechat::config_get_plugin($net.".#".$chan) eq "True")
				{
					$str .= "[".$hour.":".$min."] ".$net."#".$chan.": <".$nick."> ".$text;
				}
				elsif (weechat::config_get_plugin($net.".#".$chan) eq "False")
				{
					$str .= "";
				}
			}
		}
	}

	elsif ($arr1 =~ /(.*),irc_in2_PRIVMSG/)
	{
		$net = $1;

		if ($arr2 =~ /(.*) PRIVMSG (.*)/)
		{
			my ($host, $msg) = ($1, $2);
			$nick = weechat::info_get(irc_nick_from_host, $1);
			if ($msg =~ /#(.*) :(.*)/)
			{
				my ($chan, $text) = ($1, $2);
				while ($chan =~ /(.*) :(.*)/)
				{
					$chan = $1;
					$text = $2." :".$text;
				}
				if (weechat::config_get_plugin($net.".#".$chan) eq "")
				{
					weechat::config_set_plugin($net.".#".$chan, "True");
				}
				if (weechat::config_get_plugin($net.".#".$chan) eq "True")
				{
					$str .= "[".$hour.":".$min."] ".$net."#".$chan.": <".$nick."> ".$text;
				}
				elsif (weechat::config_get_plugin($net.".#".$chan) eq "False")
				{
					$str .= "";
				}
			}
		}
	}

	return $str;
}

sub chanmon_new_message
{
	$arr0 = $_[0];
	$arr1 = $_[1];
	$arr2 = $_[2];
	weechat::bar_item_update("chanmon");
	weechat::command(NULL, "/bar scroll chanmon * ye");
	return weechat::WEECHAT_RC_OK;
}

sub chanmon_toggle
{
	$bufname = weechat::buffer_get_string(weechat::current_buffer(), 'name');

	if (weechat::config_get_plugin($bufname) eq "False")
	{
		weechat::config_set_plugin($bufname, "True");
		$toggle = "on";
		weechat::bar_item_update("chanmon");
		weechat::command(NULL, "/bar scroll chanmon * ye");
		return weechat::WEECHAT_RC_OK;
	}
	elsif (weechat::config_get_plugin($bufname) eq "True")
	{
		weechat::config_set_plugin($bufname, "False");
		$toggle = "off";
		weechat::bar_item_update("chanmon");
		weechat::command(NULL, "/bar scroll chanmon * ye");
		return weechat::WEECHAT_RC_OK;
	}
}
