| 1 | package Amethyst::Brain::Infobot::Module::Time; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use vars qw(@ISA $ZONEDIR); | ||
| 5 | use Date::Format; | ||
| 6 | use Text::Soundex; | ||
| 7 | use Amethyst::Brain::Infobot::Module; | ||
| 8 | |||
| 9 | @ISA = qw(Amethyst::Brain::Infobot::Module); | ||
| 10 | $ZONEDIR = "/usr/share/zoneinfo/"; | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my $class = shift; | ||
| 14 | |||
| 15 | my $self = $class->SUPER::new( | ||
| 16 | Name => 'Time', | ||
| 17 | # Regex => qr/^(what is the? )?time/i, | ||
| 18 | Usage => '(what is the )? time in <zone>', | ||
| 19 | Description => "Print the time in a particular " . | ||
| 20 | "place.", | ||
| 21 | @_ | ||
| 22 | ); | ||
| 23 | |||
| 24 | $ZONEDIR = $self->{Zonedir} if exists $self->{Zonedir}; | ||
| 25 | |||
| 26 | my @zones = split("\n", qx{find $ZONEDIR -type f}); | ||
| 27 | @zones = map { s/^$ZONEDIR//go; $_ } @zones; | ||
| 28 | @zones = grep { $_ !~ /^(posix|right|SystemV|Etc)/ } @zones; | ||
| 29 | @zones = grep { $_ !~ /\.tab$/ } @zones; | ||
| 30 | |||
| 31 | my %zones = (); | ||
| 32 | |||
| 33 | foreach (@zones) { | ||
| 34 | my $x = lc $_; | ||
| 35 | $zones{$x} = $_; | ||
| 36 | $x =~ s,.*/,,; | ||
| 37 | $zones{$x} = $_ unless exists $zones{$x}; | ||
| 38 | } | ||
| 39 | |||
| 40 | # Special cases... *cough* | ||
| 41 | $zones{bath} = $zones{london}; | ||
| 42 | |||
| 43 | $self->{Zonemap} = \%zones; | ||
| 44 | |||
| 45 | return bless $self, $class; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub process { | ||
| 49 | my ($self, $message) = @_; | ||
| 50 | |||
| 51 | my $content = lc $message->content; | ||
| 52 | $content =~ s/\s+/ /g; | ||
| 53 | |||
| 54 | unless (($content =~ /^what is the time/) || | ||
| 55 | ($content =~ /^what time is it/) || | ||
| 56 | ($content =~ /^time in /)) { | ||
| 57 | return undef; | ||
| 58 | } | ||
| 59 | |||
| 60 | my $zone = undef; | ||
| 61 | |||
| 62 | if ($content =~ / in (.*)/) { | ||
| 63 | my $zonename = lc $1; | ||
| 64 | $zonename =~ s/ /_/g; | ||
| 65 | $zonename =~ s/[?\.\s]*$//; | ||
| 66 | |||
| 67 | $zone = $self->{Zonemap}->{$zonename}; | ||
| 68 | unless ($zone) { | ||
| 69 | my $sdx = soundex($zonename); | ||
| 70 | my @zones = grep { soundex($_) eq $sdx } | ||
| 71 | keys %{$self->{Zonemap}}; | ||
| 72 | |||
| 73 | unless (@zones) { | ||
| 74 | my $init = substr($zonename, 0, 2); | ||
| 75 | @zones = grep { /^$init/ } keys %{$self->{Zonemap}}; | ||
| 76 | } | ||
| 77 | |||
| 78 | my $msg = "I have not heard of $zonename."; | ||
| 79 | if (@zones == 0) { | ||
| 80 | my $reply = $self->reply_to($message, $msg); | ||
| 81 | $reply->send; | ||
| 82 | return 1; | ||
| 83 | } | ||
| 84 | elsif (@zones == 1) { | ||
| 85 | $msg .= " I think you mean " . $zones[0] . "."; | ||
| 86 | my $reply = $self->reply_to($message, $msg); | ||
| 87 | $reply->send; | ||
| 88 | $zone = $self->{Zonemap}->{$zones[0]}; | ||
| 89 | } | ||
| 90 | else { | ||
| 91 | @zones = grep { $_ !~ m,/, } @zones if @zones > 10; | ||
| 92 | |||
| 93 | $msg .= " Perhaps you mean one of " . join(", ", @zones) | ||
| 94 | if @zones; | ||
| 95 | my $reply = $self->reply_to($message, $msg); | ||
| 96 | $reply->send; | ||
| 97 | return 1; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | local $ENV{TZ} = $zone; | ||
| 103 | my $date = ctime(time); | ||
| 104 | chomp($date); | ||
| 105 | $date .= " ($zone)" if $zone; | ||
| 106 | my $reply = $self->reply_to($message, $date); | ||
| 107 | $reply->send; | ||
| 108 | |||
| 109 | return 1; | ||
| 110 | } | ||
| 111 | |||
| 112 | 1; |