Skip to content

Commit

Permalink
Fix the WTPA includes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeresig committed Mar 8, 2011
1 parent c55cb48 commit 1b1cf9a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 53 deletions.
69 changes: 65 additions & 4 deletions utils.pl → WTPA.pm
@@ -1,3 +1,9 @@
package WTPA;

use 5.010;
use strict;
use warnings;

# Includes, please install all ahead of time
# All can be found on CPAN
use JSON;
Expand All @@ -7,11 +13,29 @@
use WWW::Shorten::Bitly;
use Net::Google::Calendar;
use Config::Abstract::Ini;
use base 'Exporter';

our @EXPORT = qw( utilInit calConnect pingConnect events places
loadBackup saveBackup loadPlaces savePlaces
addEvent updateEvent cancelEvent trimEvent
addPlace updatePlace findPlace placeURL
getTopic getToday
getTime parseDate );

our $VERSION = '0.1';

my $cal;
my $ping;
our $cal;
our $ping;
our $ini;
our $lastPull;

our @events;
our %places;

sub utilInit {
$lastPull = time();
$ini = shift;

loadBackup();
loadPlaces();
}
Expand Down Expand Up @@ -74,7 +98,7 @@ sub saveBackup {
# Post the topic to PingFM
if ( defined $ping ) {
eval {
$ping->post( $topic );
$ping->post( getTopic() );
};

if ( my $err = $@ ) {
Expand Down Expand Up @@ -134,7 +158,7 @@ sub addEvent {
eval {
my $entry = Net::Google::Calendar::Entry->new();
$entry->title( $data->{name} );
$entry->content( $msg->{body} );
$entry->content( "" );
$entry->location( findPlace( $data ) );
$entry->transparency( "transparent" );
$entry->visibility( "public" );
Expand Down Expand Up @@ -269,6 +293,43 @@ sub cancelEvent {
}
}

sub trimEvent {
my $remove = 0;
my $mtime = (stat( $ini->{config}{backup} ))[9];

# Check to see if the data was updated via the web interface
if ( $lastPull && $mtime > $lastPull ) {
loadBackup();
$remove = 1;
}

$lastPull = $mtime;

# Get the current day of the year for comparison
my $now = getTime( time() );
my $cur = $now->doy();

# Go through all the events
for ( my $i = 0; $i <= $#events; $i++ ) {
# Get their day of the year
my $when = getTime( $events[$i]->{when} );
my $day = $when->doy();

# If the event day is old we need to remove it
if ( time() > $events[$i]->{when} && $day != $cur ) {
print STDERR "Cleaning up $events[$i]->{name}\n";

# Remove the event (but don't remove the calendar entry)
splice( @events, $i, 1 );
$i--;

$remove++;
}
}

return $remove;
}

sub addPlace {
my ( $new_place, $new_address ) = @_;

Expand Down
15 changes: 6 additions & 9 deletions index.cgi
Expand Up @@ -5,19 +5,16 @@ use strict;
use warnings;

use CGI;

our @events;
our %places;
use WTPA;
use Config::Abstract::Ini;

my $cgi = new CGI();
my $action = $cgi->param('action') || "";
my $index = join( "", <DATA> );

require "utils.pl";

our $ini = (new Config::Abstract::Ini( 'config.ini' ))->get_all_settings;

utilInit();
utilInit( $ini );

if ( $action eq 'add' ) {
my $err = addEvent({
Expand Down Expand Up @@ -52,7 +49,7 @@ if ( $action eq 'add' ) {

my $toUpdate = "";

foreach my $event ( @events ) {
foreach my $event ( @WTPA::events ) {
my $when = getTime( $event->{when} );
my $name = $event->{name};
my $place = $event->{place} || "";
Expand Down Expand Up @@ -99,12 +96,12 @@ foreach my $event ( @events ) {

my $placeList = "";

foreach my $place ( sort { lc($a) cmp lc($b) } keys %places ) {
foreach my $place ( sort { lc($a) cmp lc($b) } keys %WTPA::places ) {
$placeList .= qq~<li><form action="" method="POST">
<input type="hidden" name="action" value="updateplace"/>
<input type="hidden" name="old" value="$place"/>
<label for="name">Name:</label><input type="text" name="name" value="$place"/>
<label for="address">Address:</label><input type="text" name="address" value="$places{$place}"/>
<label for="address">Address:</label><input type="text" name="address" value="$WTPA::places{$place}"/>
<input type="submit" value="Update"/>
</form></li>~;
}
Expand Down
45 changes: 5 additions & 40 deletions wtpa-bot.pl
Expand Up @@ -9,10 +9,13 @@

package main;

use WTPA;
use Config::Abstract::Ini;

our $ini = (new Config::Abstract::Ini( 'config.ini' ))->get_all_settings;

utilInit( $ini );

my $bot = WTPABot->new(
server => $ini->{irc}{server},
channels => [ '#' . $ini->{irc}{channel} ],
Expand All @@ -28,17 +31,10 @@ package main;
package WTPABot;
use base 'Bot::BasicBot';

# Main Code
our @events = ();
our %places = ();
our $lastPull = time();

require "utils.pl";
use WTPA;

# Load places, connect to Google Calendar and PingFM
sub init {
utilInit();

calConnect();
pingConnect();
}
Expand Down Expand Up @@ -179,38 +175,7 @@ sub re {
# check for old events to remove
sub tick {
my $self = shift;
my $remove = 0;
my $mtime = (stat( $ini->{config}{backup} ))[9];

# Check to see if the data was updated via the web interface
if ( $lastPull && $mtime > $lastPull ) {
loadBackup();
$remove = 1;
}

$lastPull = $mtime;

# Get the current day of the year for comparison
my $now = getTime( time() );
my $cur = $now->doy();

# Go through all the events
for ( my $i = 0; $i <= $#events; $i++ ) {
# Get their day of the year
my $when = getTime( $events[$i]->{when} );
my $day = $when->doy();

# If the event day is old we need to remove it
if ( time() > $events[$i]->{when} && $day != $cur ) {
print STDERR "Cleaning up $events[$i]->{name}\n";

# Remove the event (but don't remove the calendar entry)
splice( @events, $i, 1 );
$i--;

$remove++;
}
}
my $remove = trimEvent();

# Only update the topic if an item should be removed
if ( $remove > 0 ) {
Expand Down

0 comments on commit 1b1cf9a

Please sign in to comment.