From 1b1cf9a81e607bba93208f487c4ae5243bebee04 Mon Sep 17 00:00:00 2001 From: jeresig Date: Tue, 8 Mar 2011 16:57:42 -0500 Subject: [PATCH] Fix the WTPA includes. --- utils.pl => WTPA.pm | 69 ++++++++++++++++++++++++++++++++++++++++++--- index.cgi | 15 ++++------ wtpa-bot.pl | 45 ++++------------------------- 3 files changed, 76 insertions(+), 53 deletions(-) rename utils.pl => WTPA.pm (88%) diff --git a/utils.pl b/WTPA.pm similarity index 88% rename from utils.pl rename to WTPA.pm index f13232b..00aa6cb 100644 --- a/utils.pl +++ b/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; @@ -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(); } @@ -74,7 +98,7 @@ sub saveBackup { # Post the topic to PingFM if ( defined $ping ) { eval { - $ping->post( $topic ); + $ping->post( getTopic() ); }; if ( my $err = $@ ) { @@ -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" ); @@ -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 ) = @_; diff --git a/index.cgi b/index.cgi index 17b8ce2..1febabf 100755 --- a/index.cgi +++ b/index.cgi @@ -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( "", ); -require "utils.pl"; - our $ini = (new Config::Abstract::Ini( 'config.ini' ))->get_all_settings; -utilInit(); +utilInit( $ini ); if ( $action eq 'add' ) { my $err = addEvent({ @@ -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} || ""; @@ -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~
  • - +
  • ~; } diff --git a/wtpa-bot.pl b/wtpa-bot.pl index 6adddd8..7b55ce1 100755 --- a/wtpa-bot.pl +++ b/wtpa-bot.pl @@ -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} ], @@ -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(); } @@ -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 ) {