Amethyst

 view release on metacpan or  search on metacpan

Amethyst/Brain/Infobot/Module/Time.pm  view on Meta::CPAN

package Amethyst::Brain::Infobot::Module::Time;

use strict;
use vars qw(@ISA $ZONEDIR);
use Date::Format;
use Text::Soundex;
use Amethyst::Brain::Infobot::Module;

@ISA = qw(Amethyst::Brain::Infobot::Module);
$ZONEDIR = "/usr/share/zoneinfo/";

sub new {
	my $class = shift;

	my $self  = $class->SUPER::new(
					Name		=> 'Time',
					# Regex		=> qr/^(what is the? )?time/i,
					Usage		=> '(what is the )? time in <zone>',
					Description	=> "Print the time in a particular " .
									"place.",
					@_
						);

	$ZONEDIR = $self->{Zonedir} if exists $self->{Zonedir};

	my @zones = split("\n", qx{find $ZONEDIR -type f});
	@zones = map { s/^$ZONEDIR//go; $_ } @zones;
	@zones = grep { $_ !~ /^(posix|right|SystemV|Etc)/ } @zones;
	@zones = grep { $_ !~ /\.tab$/ } @zones;

	my %zones = ();

	foreach (@zones) {
		my $x = lc $_;
		$zones{$x} = $_;
		$x =~ s,.*/,,;
		$zones{$x} = $_ unless exists $zones{$x};
	}

	# Special cases... *cough*
	$zones{bath} = $zones{london};

	$self->{Zonemap} = \%zones;

	return bless $self, $class;
}

sub process {
    my ($self, $message) = @_;

	my $content = lc $message->content;
	$content =~ s/\s+/ /g;

	unless (($content =~ /^what is the time/) ||
		($content =~ /^what time is it/) ||
		($content =~ /^time in /)) {
		return undef;
	}

	my $zone = undef;

	if ($content =~ / in (.*)/) {
		my $zonename = lc $1;
		$zonename =~ s/ /_/g;
		$zonename =~ s/[?\.\s]*$//;

		$zone = $self->{Zonemap}->{$zonename};
		unless ($zone) {
			my $sdx = soundex($zonename);
			my @zones = grep { soundex($_) eq $sdx }
							keys %{$self->{Zonemap}};

			unless (@zones) {
				my $init = substr($zonename, 0, 2);
				@zones = grep { /^$init/ } keys %{$self->{Zonemap}};



( run in 0.490 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )