#!/usr/bin/perl
#
# HourlyMark
# (c) 2007 Gregor Jehle <gjehle@gmail.com>
#
# this script can act as a kind of timestamp substitude
# to help you tell the rough time of channel events / text
# without having a timestamp in front of each line
#
# idea based on JrHourlyMarker by mouser
# http://www.donationcoder.com/Software/Mouser/mircpak/index.html
#
# mouser 'suggested' i port it to xchat ;-)
# 

package HOURLYMARK;

use POSIX qw(strftime);

my $VERSION = "1.0";
my $NAME = "HourlyMark";
my $DESC = "Displays and hourly marker in all windows";

Xchat::register($NAME,$VERSION,$DESC,"");
Xchat::hook_timer(1000,'HOURLYMARK::timer_secs');
Xchat::print("$NAME $VERSION loaded.");

my $_LASTHOUR = 0;
my $_WASINIT = 0;

sub get_hour
{
   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
    return $hour;
}

sub fmt_time()
{
    return strftime "%a %b %e %H:%M:%S %Y", localtime;
}

sub is_next_full_hour
{
    if((!$_WASINIT) || (get_hour() != $_LASTHOUR))
    {
        $_WASINIT = 1;
        $_LASTHOUR = get_hour();
        return 1;
    }
    return 0;
}

sub to_all_windows
{
    $msg = shift;
    @channels = Xchat::get_list("channels");
    for($i = 0; $i <= scalar $#channels; $i++)
    {
        $chan = $channels[$i];
        Xchat::print($msg,$chan->{"channel"},$chan->{"server"});
    }

}

sub timer_secs
{
    if(is_next_full_hour())
    {
       to_all_windows("\n@@@@@@@@@@@@@@@@@@ ". fmt_time() ." @@@@@@@@@@@@@@@@@\n\n");
    }

    return Xchat::KEEP;
}
