fix ticketing system error on bootstrap of new install
[freeside.git] / rt / sbin / rt-email-digest.in
index 2fc7c00..52fa450 100644 (file)
@@ -1,41 +1,41 @@
 #!@PERL@
 # BEGIN BPS TAGGED BLOCK {{{
-# 
+#
 # COPYRIGHT:
-# 
-# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
-#                                          <jesse@bestpractical.com>
-# 
+#
+# This software is Copyright (c) 1996-2016 Best Practical Solutions, LLC
+#                                          <sales@bestpractical.com>
+#
 # (Except where explicitly superseded by other copyright notices)
-# 
-# 
+#
+#
 # LICENSE:
-# 
+#
 # This work is made available to you under the terms of Version 2 of
 # the GNU General Public License. A copy of that license should have
 # been provided with this software, but in any event can be snarfed
 # from www.gnu.org.
-# 
+#
 # This work is distributed in the hope that it will be useful, but
 # WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 # General Public License for more details.
-# 
+#
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 # 02110-1301 or visit their web page on the internet at
 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
-# 
-# 
+#
+#
 # CONTRIBUTION SUBMISSION POLICY:
-# 
+#
 # (The following paragraph is not intended to limit the rights granted
 # to you to modify and distribute this software under the terms of
 # the GNU General Public License and is only of importance to you if
 # you choose to contribute your changes and enhancements to the
 # community by submitting them to Best Practical Solutions, LLC.)
-# 
+#
 # By intentionally submitting any modifications, corrections or
 # derivatives to this work, or any other work intended for use with
 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
 # royalty-free, perpetual, license to use, copy, create derivative
 # works based on those contributions, and sublicense and distribute
 # those contributions and any derivatives thereof.
-# 
+#
 # END BPS TAGGED BLOCK }}}
 use warnings;
 use strict;
 
-BEGIN {
+BEGIN { # BEGIN RT CMD BOILERPLATE
     require File::Spec;
+    require Cwd;
     my @libs = ("@RT_LIB_PATH@", "@LOCAL_LIB_PATH@");
     my $bin_path;
 
     for my $lib (@libs) {
         unless ( File::Spec->file_name_is_absolute($lib) ) {
-            unless ($bin_path) {
-                if ( File::Spec->file_name_is_absolute(__FILE__) ) {
-                    $bin_path = ( File::Spec->splitpath(__FILE__) )[1];
-                }
-                else {
-                    require FindBin;
-                    no warnings "once";
-                    $bin_path = $FindBin::Bin;
-                }
-            }
+            $bin_path ||= ( File::Spec->splitpath(Cwd::abs_path(__FILE__)) )[1];
             $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
         }
         unshift @INC, $lib;
@@ -76,16 +68,15 @@ BEGIN {
 use Date::Format qw( strftime );
 use Getopt::Long;
 use RT;
-use RT::Interface::CLI qw( CleanEnv loc );
+use RT::Interface::CLI qw( loc );
 use RT::Interface::Email;
 
-CleanEnv();
 RT::LoadConfig();
 RT::Init();
 
 sub usage {
     my ($error) = @_;
-    print loc("Usage: ") . "$0 -m (daily|weekly) [--print] [--help]\n";
+    print loc("Usage:") . " $0 -m (daily|weekly) [--print] [--help]\n";
     print loc(
         "[_1] is a utility, meant to be run from cron, that dispatches all deferred RT notifications as a per-user digest.",
         $0
@@ -95,6 +86,7 @@ sub usage {
     print "\t-p, --print\t"
         . loc("Print the resulting digest messages to STDOUT; don't mail them. Do not mark them as sent")
         . "\n";
+    print "\t-v, --verbose\t" . loc("Give output even on messages successfully sent") . "\n";
     print "\t-h, --help\t" . loc("Print this message") . "\n";
 
     if ( $error eq 'help' ) {
@@ -105,10 +97,11 @@ sub usage {
     }
 }
 
-my ( $frequency, $print, $help ) = ( '', '', '' );
+my ( $frequency, $print, $verbose, $help ) = ( '', '', '', '' );
 GetOptions(
     'mode=s' => \$frequency,
     'print'  => \$print,
+    'verbose' => \$verbose,
     'help'   => \$help,
 );
 
@@ -134,7 +127,7 @@ sub run {
         my ( $contents_list, $contents_body ) = build_digest_for_user( $user, $all_digest->{$user} );
         # Now we have a content head and a content body.  We can send a message.
         if ( send_digest( $user, $contents_list, $contents_body ) ) {
-            print "Sent message to $user\n";
+            print "Sent message to $user\n" if $verbose;
             mark_transactions_sent( $frequency, $user, values %{$sent_transactions->{$user}} ) unless ($print);
         } else {
             print "Failed to send message to $user\n";
@@ -177,8 +170,10 @@ sub send_digest {
     }
 
     # Set our sender and recipient.
-    $digest_template->MIMEObj->head->replace( 'From', RT::Config->Get('CorrespondAddress') );
-    $digest_template->MIMEObj->head->replace( 'To',   $to );
+    $digest_template->MIMEObj->head->replace(
+        'From', Encode::encode( "UTF-8", RT::Config->Get('CorrespondAddress') ) );
+    $digest_template->MIMEObj->head->replace(
+        'To',   Encode::encode( "UTF-8", $to ) );
 
     if ($print) {
         $digest_template->MIMEObj->print;
@@ -188,13 +183,13 @@ sub send_digest {
     }
 }
 
-=item mark_transactions_sent( $frequency, $user, @txn_list );
-
-Takes a frequency string (either 'daily' or 'weekly'), a user  and one or more
-transaction objects as its arguments.  Marks the given deferred
-notifications as sent.
-
-=cut
+=item mark_transactions_sent( $frequency, $user, @txn_list );
+# 
+Takes a frequency string (either 'daily' or 'weekly'), a user  and one or more
+transaction objects as its arguments.  Marks the given deferred
+notifications as sent.
+# 
+=cut
 
 sub mark_transactions_sent {
     my ( $freq, $user, @txns ) = @_;
@@ -264,7 +259,7 @@ sub find_transactions {
         my $queue  = $txn->TicketObj->QueueObj->Name;
         # Xxx todo - may clobber if two queues have the same name
         foreach my $user ( $txn->DeferredRecipients($frequency) ) {
-            $all_digest->{$user}->{$queue}->{$ticket}->{ $txn->id } = $txn->ContentObj;
+            $all_digest->{$user}->{$queue}->{$ticket}->{ $txn->id } = $txn;
             $sent_transactions->{$user}->{ $txn->id } = $txn;
         }
     }
@@ -308,12 +303,14 @@ sub build_digest_for_user {
             # Spit out the messages for the transactions on this ticket.
             $contents_body .= "\n== $ticket_title\n";
             foreach my $txn ( sort keys %$tkt_txns ) {
-                my $msg = $tkt_txns->{$txn};
-
-                # $msg contains an RT::Attachment with our outgoing
-                # message.  Print a few headers for clarity's sake.
-                $contents_body .= "From: " . $msg->GetHeader('From') . "\n";
-                my $date = $msg->GetHeader('Date ');
+                my $top = $tkt_txns->{$txn}->Attachments->First;
+
+                # $top contains the top-most RT::Attachment with our
+                # outgoing message.  It may not be the MIME part with
+                # the content.  Print a few headers from it for
+                # clarity's sake.
+                $contents_body .= "From: " . $top->GetHeader('From') . "\n";
+                my $date = $top->GetHeader('Date ');
                 unless ($date) {
                     my $txn_obj = RT::Transaction->new( RT->SystemUser );
                     $txn_obj->Load($txn);
@@ -326,7 +323,7 @@ sub build_digest_for_user {
                         @{ [ localtime( $date_obj->Unix ) ] } );
                 }
                 $contents_body .= "Date: $date\n\n";
-                $contents_body .= $msg->Content . "\n";
+                $contents_body .= $tkt_txns->{$txn}->ContentObj->Content . "\n";
                 $contents_body .= "-------\n";
             }    # foreach transaction
         }    # foreach ticket
@@ -335,3 +332,42 @@ sub build_digest_for_user {
     return ( $contents_list, $contents_body );
 
 }
+
+__END__
+
+=head1 NAME
+
+rt-email-digest - dispatch deferred notifications as a per-user digest
+
+=head1 SYNOPSIS
+
+    rt-email-digest -m (daily|weekly) [--print] [--help]
+
+=head1 DESCRIPTION
+
+This script is a tool to dispatch all deferred RT notifications as a per-user
+object.
+
+=head1 OPTIONS
+
+=over
+
+=item mode
+
+Specify whether this is a daily or weekly run.
+
+--mode is equal to -m
+
+=item print
+
+Print the resulting digest messages to STDOUT; don't mail them. Do not mark them as sent
+
+--print is equal to -p
+
+=item help
+
+Print this message
+
+--help is equal to -h
+
+=back