Local.pm 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package Cpanel::iContact::Provider::Local;
  2. use strict;
  3. use warnings;
  4. use parent 'Cpanel::iContact::Provider';
  5. use Try::Tiny;
  6. =encoding utf-8
  7. =head1 NAME
  8. Cpanel::iContact::Provider::Local - Backend for the Local iContact module
  9. =head1 SYNOPSIS
  10. use Cpanel::iContact::Provider::Local;
  11. my $notifier = Cpanel::iContact::Provider::Local->new();
  12. $notifier->send();
  13. =head1 DESCRIPTION
  14. Provide backend accessor for the Local iContact module.
  15. =cut
  16. =head2 send
  17. Sends off the notification over to /var/cpanel/iContact
  18. =over 2
  19. =item Input
  20. =over 3
  21. None
  22. =back
  23. =item Output
  24. =over 3
  25. Truthy value on success, exception on failure.
  26. =back
  27. =back
  28. =cut
  29. our $DIR = '/var/cpanel/iContact_notices';
  30. sub send {
  31. my ($self) = @_;
  32. my $args_hr = $self->{'args'};
  33. my $contact_hr = $self->{'contact'};
  34. my @errs;
  35. my $subject_copy = $args_hr->{'subject'};
  36. my $text_copy = ${ $args_hr->{'text_body'} };
  37. my $html_copy = ${ $args_hr->{'html_body'} };
  38. require Encode;
  39. my $subject = Encode::decode_utf8( $subject_copy, $Encode::FB_QUIET );
  40. my $text = Encode::decode_utf8( $text_copy, $Encode::FB_QUIET );
  41. my $html = Encode::decode_utf8( $html_copy, $Encode::FB_QUIET );
  42. # Send it
  43. my $time = time;
  44. $time =~ tr/ /-/;
  45. my $user = getpwuid($<);
  46. my $file = "$DIR/$user/$time.json";
  47. try {
  48. # Make the dir if it doesn't exist
  49. if( !-d "$DIR/$user" ) {
  50. my $path = '/';
  51. foreach my $component ( split( /\//, "$DIR/$user" ) ) {
  52. local $!;
  53. $path .= "$component/";
  54. mkdir( $path ) || do {
  55. die "Couldn't create $path: $!" if int $! != 17; # EEXISTS
  56. };
  57. }
  58. }
  59. require Cpanel::AdminBin::Serializer;
  60. open( my $fh, ">", $file ) || die "Couldn't open '$file': $!";
  61. print $fh Cpanel::AdminBin::Serializer::Dump( { 'subject' => $subject, 'text' => $text, 'html' => $html } );
  62. close $fh;
  63. }
  64. catch {
  65. require Cpanel::Exception;
  66. die Cpanel::Exception::create(
  67. 'ConnectionFailed',
  68. 'The system failed to save the message to “[_1]” due to an error: [_2]',
  69. [ $file, $_ ]
  70. );
  71. };
  72. return 1;
  73. }
  74. 1;