package AmphetaDesk::AmphetaDesk; ############################################################################### # AmphetaDesk::AmphetaDesk (c) 2000-2002 Disobey # # morbus@disobey.com http://www.disobey.com/amphetadesk/ # ############################################################################### # ABOUT THIS PACKAGE: # # This the starting point of everything related to AmphetaDesk::AmphetaDesk. The main # # purpose of this script is to act as a traffic cop between the webserver # # and the GUI libraries. It implements a pathetic queuing system, as well # # as sends all the data to the various modules and the web browser. # # # # LIST OF ROUTINES BELOW: # # init - creates the environment and sets up the queue based loop. # ############################################################################### # "moving on down the world. looking for a place." # ############################################################################### use strict; $|++; use CGI qw/:standard :cgi-lib/; use AmphetaDesk::AmphetaDesk::Channels; use AmphetaDesk::AmphetaDesk::ChannelsList; use AmphetaDesk::AmphetaDesk::MyChannels; use AmphetaDesk::AmphetaDesk::Settings; use AmphetaDesk::AmphetaDesk::Templates; use AmphetaDesk::AmphetaDesk::Utilities; use AmphetaDesk::AmphetaDesk::Versioning; use AmphetaDesk::AmphetaDesk::WebServer; use AmphetaDesk::AmphetaDesk::WWW; use File::Spec::Functions; require Exporter; use vars qw( @ISA @EXPORT ); @ISA = qw( Exporter ); @EXPORT = qw( init ); # define a quickie die message on quits. $SIG{INT} = sub { die "User cancelled" } unless $^O =~ /Mac/; # stops Linux/Darwin releases from randomly die'ing. $SIG{PIPE} = 'IGNORE' unless $^O =~ /Mac/; # where are we? ############################################################################### # init - creates the environment and sets up the queue based loop. # ############################################################################### # USAGE: # # init( $wrapper_ver ); # # # # NOTES: # # This routine accepts the version number of the wrapper file that # # calls the init() routine. This allows us some bit of backwards # # compatibility if the wrapper ever changes. # # # # RETURNS: # # n/a; if this routine fails, then Ampheta just ain't gonna work, bub. # ############################################################################### sub init { my ($wrapper_ver) = @_; ############################################################################ # 1.0 Initialization ("wake up, you angst-filled goose!") # # # # Set up everything neccesary for a happy operation, include log files, # # settings, OS determination, GUI starts, version checks, channel updates, # # and webserver binding. Once all this crap is done, start the infiniloop. # ############################################################################ # delete the logfile if it's over 250k, # then reopen it and try to redir STDERR. my $logfile = catfile($ENV{'DIRNAME'}, "AmphetaDesk.log"); if (-e $logfile) { unlink $logfile if -s $logfile > 250000; } open (LOG, ">>$logfile") or die "AmphetaDesk couldn't open the logfile for logging: $!"; open(STDERR,">&LOG") or die "AmphetaDesk couldn't redirect errors to the logfile: $!"; select(LOG); $|++; select(STDOUT); # turn on autoflushing for AmphetaDesk::AmphetaDesk.log. *AmphetaDesk::Utilities::LOG = \*LOG; # map our Utilities::LOG to this LOG filehandle. # load our settings. this routine is located in Settings.pm # and takes care of determining the OS, finding all the paths # to the relevant files, as well as making sure everything exists. load_my_settings( catfile($ENV{'DIRNAME'}, "data", "mySettings.xml") ); # load our os specific libraries. if we don't know, use the Linux # libraries, which currently default to STDOUT for all gui processing. if (get_setting("app_os") =~ /Win/) { require AmphetaDesk::AmphetaDesk::OS::Windows; import AmphetaDesk::AmphetaDesk::OS::Windows; } elsif (get_setting("app_os") =~ /Mac/) { require AmphetaDesk::AmphetaDesk::OS::MacOS; import AmphetaDesk::AmphetaDesk::OS::MacOS; MacPerl::Quit(3); } elsif (get_setting("app_os") =~ /darwin/) { require AmphetaDesk::AmphetaDesk::OS::MacOSX; import AmphetaDesk::AmphetaDesk::OS::MacOSX; } else { require AmphetaDesk::AmphetaDesk::OS::Linux; import AmphetaDesk::AmphetaDesk::OS::Linux; } # start gui. # os specific. &gui_init; # output a little hello. my $joy = ""; if (get_setting("app_os") eq "darwin") { $joy = "OS X? Good choice, my friend."; } my ($app_ver) = get_setting("app_version"); # wow. how sad is *that* easter egg. pffff. note("--------------------------------------------------------------------------------", 1); note("Disobey.com's AmphetaDesk::AmphetaDesk v$app_ver has started (using wrapper v$wrapper_ver).", 1); note( get_setting("app_copyright") . " - " . get_setting("app_url"), 1); note("The operating system is '" . get_setting("app_os") . "'. " . $joy, 0); note("--------------------------------------------------------------------------------", 1); # check for a newer version. # [located in Versioning.pm]. check_version; # load channel subscriptions, clean dead files, and download anything new. note("Downloading the latest channel data. This may take a few minutes.", 1); note("Wait patiently, eh? The latest news will be yours shortly!", 1); note("--------------------------------------------------------------------------------", 1); load_my_channels( get_setting("files_myChannels") ); remove_old_channel_files; download_my_channels; # set our timer variable # for repetitive downloading. my $last_update = time; # start up the webserver(s). # [located in WebServer.pm]. my $daemon = start_webserver; my $radio_daemon = start_radio_webserver if get_setting("user_start_radio_webserver"); # open a browser to # load our index page. open_url(); # os specific note("--------------------------------------------------------------------------------", 1); ############################################################################ # 2.0 Start the Loop ("around and around spun alice.") # # # # Now, we start the listening loop for our webserver. During our loop, we # # listen for specific connections and, if they're valid requests, we pass # # them to our AmphetaDesk::Text::Template module for processing out to the browser. # ############################################################################ # we put the user's "channels_check_interval" into a variable here, so # we don't have get_setting calls every time we go through our infinite loop. my $user_channels_check_interval = get_setting("user_channels_check_interval"); while ( 1 ) { # listen for # a gui event &gui_listen; # if now is later than the user's "channels_check_interval", then # we download all our channels over again. 60 minutes is the minimum. $user_channels_check_interval = 60 if $user_channels_check_interval < 60; if ((time - $last_update) > $user_channels_check_interval * 60) { $last_update = time; download_my_channels; # wHhheeEEE! } # if we receive a connection, suck it in. if we # don't, then we endlessly loop listening for either # webserver or GUI requests until we're closed. we use # one of those funky flipflops to determine if we should # try listening on the $radio_daemon or not (since that # functionality is off by default in our configuration). my $connection = defined($radio_daemon) ? $daemon->accept || $radio_daemon->accept : $daemon->accept; # no connection? move on. next unless defined $connection; # if we're this far, we've got a connection. # get the browser's request from our connection. my $request = $connection->get_request; next unless defined $request; # if this is an invalid URL (something funky with ..'s, or # other characters we're not really fond of), then we send # a cheapo message saying that we don't like them. this # should stop stuff like directory traversals, etc. # note, we ->print and not ->send_error because HTTP::Daemon # doesn't create a valid HTML document, and that's dumb. if ($request->url->path !~ /^[\/A-Za-z0-9\-_\.]+$/ || $request->url->path =~ /\.\./) { $connection->print("\n" . "