Getter.pm 2.3 KB

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