Local.pm 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 = $args_hr->{'subject'};
  36. my $text = ${ $args_hr->{'text_body'} };
  37. # my $html = ${ $args_hr->{'html_body'} };
  38. # Send it
  39. my $time = localtime;
  40. $time =~ tr/ /-/;
  41. my $user = getpwuid($<);
  42. my $file = "$DIR/$user/$time.txt";
  43. try {
  44. # Make the dir if it doesn't exist
  45. if( !-d "$DIR/$user" ) {
  46. my $path = '/';
  47. foreach my $component ( split( /\//, "$DIR/$user" ) ) {
  48. local $!;
  49. $path .= "$component/";
  50. mkdir( $path ) || do {
  51. die "Couldn't create $path: $!" if int $! != 17; # EEXISTS
  52. };
  53. }
  54. }
  55. open( my $fh, ">", $file ) || die "Couldn't open '$file': $!";
  56. print $fh "$subject\n\n$text\n";
  57. close $fh;
  58. }
  59. catch {
  60. require Cpanel::Exception;
  61. die Cpanel::Exception::create(
  62. 'ConnectionFailed',
  63. 'The system failed to save the message to “[_1]” due to an error: [_2]',
  64. [ $file, $_ ]
  65. );
  66. };
  67. return 1;
  68. }
  69. 1;