commit 3bc13fbbeed050a5635b42ea44aa69410ec34cf9 from: Izzy Blacklock date: Mon Jul 10 02:21:49 2023 UTC Added date/time and email functions from ponbot code corrected $dbLevel to $verbosity in export_ok list Also added POD documents to IRCNOW::IO.pm commit - b1d17a526fbc80fe1a7caf5b1801d441c2c65b37 commit + 3bc13fbbeed050a5635b42ea44aa69410ec34cf9 blob - 0257da8cb85ccd468a9e1d1b94742073d9d3fe57 blob + d5ce0cde1c5086dfd065d28a2c30aeef13e5b49b --- lib/IRCNOW/IO.pm +++ lib/IRCNOW/IO.pm @@ -3,7 +3,9 @@ use Exporter 'import'; our @EXPORT_OK = qw( readarray readstr writefile appendfile - debug NONE INFO ERRORS WARNINGS ALL $dbLevel + debug NONE INFO ERRORS WARNINGS ALL $verbosity + date gettime + mail ); # create debug tag so you can import the debug sub and messages # this enables loading lists of exports by tag like so: @@ -11,9 +13,12 @@ our @EXPORT_OK = qw( our %EXPORT_TAGS = ( DEBUG=>[qw(debug NONE INFO ERRORS WARNINGS ALL $verbosity)], FILEIO=>[qw(readarray readstr writefile appendfile)], + DateTime=>[qw(date gettime)], + eMail=>[qw(mail)], ); -Exporter::export_ok_tags('debug', '$verbosity'); +Exporter::export_ok_tags('debug', '$verbosity', 'DateTime', 'eMail'); + use File::Copy qw(copy); use File::Basename; @@ -83,5 +88,143 @@ sub appendfile { close $fh; } + + +####################################################################################### +# Date/time Functions # +####################################################################################### +# Returns date in YYYYMMDD format +sub date { + my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(); + my $localtime = sprintf("%04d%02d%02d", $year+1900, $mon+1, $mday); + return $localtime; +} + +# Returns timestamp in "Day MM DD HH:MM:SS" format +sub gettime { + my @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); + my @days = qw(Sun Mon Tue Wed Thu Fri Sat Sun); + my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(); + my $localtime = sprintf("%s %s %d %02d:%02d:%02d", $days[$wday], $months[$mon], $mday, $hour, $min, $sec); + return $localtime; +} + + +####################################################################################### +# email # +####################################################################################### +sub mail { + my( $from, $to, $fromname, $subject, $body )=@_; +my $msg = <<"EOF"; +From: $from +To: $to +Subject: $subject +MIME-Version: 1.0 +Content-Type: text/plain; charset=us-ascii +Content-Disposition: inline + +$body +EOF +open(my $fh, "| /usr/sbin/sendmail -tv -F '$fromname' -f $from") or die "Could not send mail $!"; +print $fh $msg; +close $fh; +return "true"; +} + + + + 1; +=pod + +=head1 NAME + +IRCNOW::IO - Simple file read/write, date/time, and debug logging interfaces. + +=head1 SYNOPSIS + + ## Export tags + use IRCNOW::IO qw(:DEBUG :FILEIO :DateTime :eMail); + + ## Debug Constants - Used for setting verbosity level + use IRCNOW::IO qw(NONE INFO ERRORS WARNINGS ALL) + + ## Debug variables + use IRCNOW::IO qw($verbosity); + $verbosity = ERRORS # set error level to print to stdout. + # This isn't needed as specifying the package name has the same effect. + IRCNOW::IO::$verbosity = ERROR; + + ## Debug functions + use IRCNOW::IO qw(debug); + debug(ERROR, $msg); + + ## FileIO Functions + use IRCNOW::IO qw(readarry readstr writefile appendfile); + + ## Email Functions + use IRCNOW::IO qw(mail); + + ## DateTime Functions + use IRCNOW::IO qw(date gettime); + +=head1 DESCRIPTION + +IRCNOW::IO contains simple functions for handling io to files, logs, email, and date/time formatting. +These functions originated from the botnow irc bot. + +=head2 Debug Exports + +These exports implement the original debug($level,$msg) log output system from botnow. I new logging system is underdevelopent as IRCNOW::Logger. This system is provided for compatability with the original implementation. + + #!/usr/bin/perl + + use IRCNOW::IO qw(:DEBUG); + + $verbosity = ERROR; # Only print messages at level $verbosity or below. + + debug(INFO, "This is a message at level INFO (1)"); + debug(ERRORS, "This is a message at level ERRORS (2)"); + debug(WARNINGS, "This is a message at level WARNINGS (3)"); + debug(ALL, "This is a message at level ALL (4)"); + +=head2 File IO Exports + + #!/usr/bin/perl + use IRCNOW::IO qw(:FILEIO); + + my $filename="./inputfile.txt"; + + # Open file and read contents into array of lines without newline + my $lines=readarray($filename); + + # Read from a file and return contents as a string + my $str = readstr($filename); + + # Write String to a file - creating a backup first + writefile($filename, $str); + + # Append string to a file. + appendfile($filename, $str); + +=head2 Date/Time Exports + + #!/usr/bin/perl + use IRCNOW::IO qw(:DateTime); + + # Return current date in YYYYMMDD format. + my $date = date(); + + # Returns timestamp in "Day MM DD HH:MM:SS" format + my $date = gettime(); + +=head2 eMail exports + + #!/usr/bin/perl + use IRCNOW::IO qw(:eMail); + + # Send an email using a pipe to sendmail + mail( $from, $to, $fromname, $subject, $body); + +=cut