123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- package Cpanel::iContact::Provider::Local::Getter;
- use strict;
- use warnings;
- # Specifically for the constant that is the dir
- use Cpanel::iContact::Provider::Local ();
- use Cpanel::AdminBin::Serializer ();
- use File::Slurper ();
- =encoding utf-8
- =head1 NAME
- Cpanel::iContact::Provider::Local::Getter - Get notifications stored by the "Store Locally" iContact module
- =head1 SYNOPSIS
- use lib '/var/cpanel/perl';
- use Cpanel::iContact::Provider::Local::Getter;
- # Get the latest notification
- my @notification_times = Cpanel::iContact::Provider::Local::Getter::get_notice_times( 'user' => 'root' );
- my $notification = Cpanel::iContact::Provider::Local::Getter::get_notice( $notification_times[0], 'user' => 'root');
- ...
- # OK, let's get ALL notifications AND their text
- my %notifications = Cpanel::iContact::Provider::Local::Getter::get_all_notices( 'user' => 'root' );
- =head1 DESCRIPTION
- Provide a way to retrieve locally saved iContact notices.
- =cut
- =head2 get_notice_times
- =over 2
- =item Input (hash)
- =over 3
- user: (required) specify the user to get these things for
- =back
- =item Output (array)
- =over 3
- UNIX Epoch timestamps sorted by latest date first.
- =back
- =back
- =cut
- # Note, since there's only one hash param (for now) just ref second item in array
- sub get_notice_times {
- return sort( map { substr( $_, rindex( $_, '/' ) + 1, -5 ) } grep { qr/\d+\.json/ } glob( $Cpanel::iContact::Provider::Local::DIR . "/$_[1]/*.json" ) );
- }
- =head2 get_notice
- =over 2
- =item Input (mixed)
- =over 3
- time: (integer/string) Time to look for, get em from get_notice_times above
- user: (hash param) specify the user to get these things for
- =back
- =item Output (string)
- =over 3
- Notification contents.
- =back
- =back
- =cut
- sub get_notice {
- my ( $time, %opts ) = @_;
- local $/;
- my $file = $Cpanel::iContact::Provider::Local::DIR . "/$opts{'user'}/$time.json";
- return Cpanel::AdminBin::Serializer::Load(File::Slurper::read_binary($file));
- }
- =head2 get_all_notices
- =over 2
- =item Input (hash)
- =over 3
- user: (required) specify the user to get these things for
- =back
- =item Output (hash)
- =over 3
- time => notification contents.
- =back
- =back
- =cut
- sub get_all_notices {
- my $user = $_[1];
- return map {
- my $time = substr( $_, rindex( $_, '/' ) + 1, -5 ); $time => get_notice( $time, 'user' => $user );
- } grep {
- qr/\d+\.json/
- } glob( $Cpanel::iContact::Provider::Local::DIR . "/$user/*.json" );
- }
- 1;
|