NAME

Mail::Toaster::Utility - Common Perl scripting functions


SYNOPSIS

Mail::Toaster::Utility is a grouping of frequently used functions I've written.


DESCRIPTION

Just a big hodge podge of useful subs that I use in scripts all over the place. Peruse through, surely you too can find something of use.

InstallFromSources

        use Mail::Toaster::Utility;
        InstallFromSourcesPHP();

Downloads and installs a PHP program and installs it.

InstallFromSources

        use Mail::Toaster::Utility;
        InstallFromSources();

Downloads and installs a program from sources.

InstallPerlModule

        use Mail::Toaster::Utility;
        InstallPerlModule();

Downloads and installs a perl module.

GetMountedDrives

        GetMountedDrives($debug);

Uses mount to fetch a list of mounted drive/partitions.

IsProcessRunning

Verify if a process is running or not.

        use Mail::Toaster::Utility;
        IsProcessRunning($process);

InstallMailToaster

        use Mail::Toaster::Utility;
        InstallMailToaster();

Downloads and installs Mail::Toaster.

files_diff

        use Mail::Toaster::Utility;
        files_diff($file1, $file2, $type, $debug);
        if ( files_diff("foo", "bar") ) 
        { 
                print "different!\n"; 
        };

Determine if the files are different. $type is assumed to be text unless you set it otherwise. For anthing but text files, we do a MD5 checksum on the files to determine if they're different or not.

YesOrNo

        YesOrNo ($question)
        use Mail::Toaster::Utility;
        my $r = YesOrNo("Would you like fries with that?");
        print "yes" if $r;
        Passing $question is optional.
        $r is a scalar

StripLastDirFromPath

        StripLastDirFromPath ($dir)

Takes a path like ``/usr/home/matt'' and returns ``/usr/home'' and ``matt''

You (and I) should be using File::Basename instead as it's more portable.

WriteFile

        WriteFile ($file, @lines)
        use Mail::Toaster::Utility;
        my $file = "/tmp/foo";
        my @lines = "1", "2", "3";
        print "success" if (WriteFile($file, @lines));

$file is the file you want to write to @lines is a an array, each array element is a line in the file

A result code is returned, undef is failure, anything else is success.

ReadFile

        use Mail::Toaster::Utility;
        my @lines = ReadFile($file)

Reads in a file, and returns an array with the files contents, one line per array element. All lines in array have already been chomped.

FindTheBin

        use Mail::Toaster::Utility;
        my $r = FindTheBin($bin, $dir)

Check all the ``normal'' locations for a binary that should be on the system and returns the full path to the binary. Return zero if we can't find it.

If the optional $dir is sent, then check that directory first and if the binary exists there, returns it's full path.

SysCmd

        use Mail::Toaster::Utility;
        my $r = SysCmd ($cmd)
        print "SysCmd: error result: $r\n" if ($r != 0);

Just a little wrapper around system calls, that returns any failure codes and prints out the error(s) if present.

ArchiveFile

        ArchiveFile ($file)

Make a backup copy of a file by copying the file to $file.timestamp.

GetDirFiles

        GetDirFiles($dir, $debug)

$dir is a directory. The return will be an array of files names contained in that directory.

CleanTmpDir

        CleanTmpDir ($dir)

$dir is a directory. Running this will empty it. Be careful!

DeleteFile

        use Mail::Toaster::Utility;
        DeleteFile($file, $warn);

GetTheDate

        GetTheDate ($bump, $debug)

$bump is the optional offset (in seconds) to subtract from the date.

        use Mail::Toaster::Utility;
        my ($dd, $mm, $yy, $lm, $hh, $mn, $ss) = GetTheDate();
        $dd = day
        $mm = month
        $yy = year
        $lm = last month
        $hh = hours
        $mn = minutes
        $ss = seconds

FetchFile

        use Mail::Toaster::Utility;
        FetchFile($url, $debug);

Use an appropriate URL fetching utility (fetch, curl, wget, etc) based on your OS to download a file from the $url handed to us. Return a result code of 1 for success, 0 is failure.

FileAppend

        FileAppend ($file, @lines)

Pass this sub a filename and an array and it'll append the array to the file. It's that simple.

LogFileAppend

        LogFileAppend ($file, @lines)

Pass this sub a filename and an array and it'll append a timestamp and the array contents to the file. It's that simple.

ParseConfigFile

        use Mail::Toaster::Utility;
        my $vals = ParseConfigFile ( "example.conf", 1);
        ParseConfigFile ($file, $debug, $etcdir)

$file is the file to be parsed. $etcdir is where the file should be found. It defaults to /usr/local/etc and will also check the current working directory.

A hashref is returned with the key/value pairs.

SudoSetup

        use Mail::Toaster::Utility;
        my $sudo = SudoSetup();
        $r = SysCmd("$sudo mv /tmp/foo /etc/root-owned-file");

Often you want to run a script as an unprivileged user. However, the script may need elevated privileges for a plethora of reasons. Rather than running the script suid, or as root, configure sudo allowing the script to run system commands with appropriate permissions.


AUTHOR

        Matt Simerson <matt@cadillac.net>


BUGS

        None known. Report any to author.


TODO


SEE ALSO

        http://www.tnpi.biz/computing/perl/
        http://www.tnpi.biz/internet/
    Mail::Toaster::Apache, Mail::Toaster::DNS, Mail::Toaster::FreeBSD, 
    Mail::Toaster::Mysql, Mail::Toaster::Passwd, Mail::Toaster::Perl, 
    Mail::Toaster::Qmail, MATT::Quota, MATT::SSL, Mail::Toaster::Utility


COPYRIGHT

Copyright 2003, The Network People, Inc. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

Neither the name of the The Network People, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.