Getter.pm 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package Cpanel::iContact::Provider::Local::Getter;
  2. use strict;
  3. use warnings;
  4. # Specifically for the constant that is the dir
  5. use Cpanel::iContact::Provider::Local ();
  6. use Cpanel::AdminBin::Serializer ();
  7. =encoding utf-8
  8. =head1 NAME
  9. Cpanel::iContact::Provider::Local::Getter - Get notifications stored by the "Store Locally" iContact module
  10. =head1 SYNOPSIS
  11. use lib '/var/cpanel/perl';
  12. use Cpanel::iContact::Provider::Local::Getter;
  13. # Get the latest notification
  14. my @notification_times = Cpanel::iContact::Provider::Local::Getter::get_notice_times( 'user' => 'root' );
  15. my $notification = Cpanel::iContact::Provider::Local::Getter::get_notice( $notification_times[0], 'user' => 'root');
  16. ...
  17. # OK, let's get ALL notifications AND their text
  18. my %notifications = Cpanel::iContact::Provider::Local::Getter::get_all_notices( 'user' => 'root' );
  19. =head1 DESCRIPTION
  20. Provide a way to retrieve locally saved iContact notices.
  21. =cut
  22. =head2 get_notice_times
  23. =over 2
  24. =item Input (hash)
  25. =over 3
  26. user: (required) specify the user to get these things for
  27. =back
  28. =item Output (array)
  29. =over 3
  30. UNIX Epoch timestamps sorted by latest date first.
  31. =back
  32. =back
  33. =cut
  34. # Note, since there's only one hash param (for now) just ref second item in array
  35. sub get_notice_times {
  36. return sort( map { substr( $_, rindex( $_, '/' ), -5 ) } grep { qr/\d+\.json/ } glob( $Cpanel::iContact::Provider::Local::DIR . "/$_[1]/*.json" ) );
  37. }
  38. =head2 get_notice
  39. =over 2
  40. =item Input (mixed)
  41. =over 3
  42. time: (integer/string) Time to look for, get em from get_notice_times above
  43. user: (hash param) specify the user to get these things for
  44. =back
  45. =item Output (string)
  46. =over 3
  47. Notification contents.
  48. =back
  49. =back
  50. =cut
  51. sub get_notice {
  52. my ( $time, %opts ) = @_;
  53. local $/;
  54. my $file = $Cpanel::iContact::Provider::Local::DIR . "/$opts{'user'}/$time.json";
  55. open my $fh, '<', $file or die "can't open $file: $!";
  56. return Cpanel::AdminBin::Serializer::Load(<$fh>);
  57. }
  58. =head2 get_all_notices
  59. =over 2
  60. =item Input (hash)
  61. =over 3
  62. user: (required) specify the user to get these things for
  63. =back
  64. =item Output (hash)
  65. =over 3
  66. time => notification contents.
  67. =back
  68. =back
  69. =cut
  70. sub get_all_notices {
  71. my $user = $_[1];
  72. return map {
  73. my $time = substr( $_, rindex( $_, '/' ), -5 ); $time => get_notice( $time, 'user' => $user );
  74. } grep {
  75. qr/\d+\.json/
  76. } glob( $Cpanel::iContact::Provider::Local::DIR . "/$user/*.json" );
  77. }
  78. 1;