]> git.uio.no Git - usit-rt.git/blame - sbin/rt-email-digest
Upgrade 4.0.17 clean.
[usit-rt.git] / sbin / rt-email-digest
CommitLineData
84fb5b46
MKG
1#!/usr/bin/perl
2# BEGIN BPS TAGGED BLOCK {{{
3#
4# COPYRIGHT:
5#
403d7b0b 6# This software is Copyright (c) 1996-2013 Best Practical Solutions, LLC
84fb5b46
MKG
7# <sales@bestpractical.com>
8#
9# (Except where explicitly superseded by other copyright notices)
10#
11#
12# LICENSE:
13#
14# This work is made available to you under the terms of Version 2 of
15# the GNU General Public License. A copy of that license should have
16# been provided with this software, but in any event can be snarfed
17# from www.gnu.org.
18#
19# This work is distributed in the hope that it will be useful, but
20# WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22# General Public License for more details.
23#
24# You should have received a copy of the GNU General Public License
25# along with this program; if not, write to the Free Software
26# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27# 02110-1301 or visit their web page on the internet at
28# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
29#
30#
31# CONTRIBUTION SUBMISSION POLICY:
32#
33# (The following paragraph is not intended to limit the rights granted
34# to you to modify and distribute this software under the terms of
35# the GNU General Public License and is only of importance to you if
36# you choose to contribute your changes and enhancements to the
37# community by submitting them to Best Practical Solutions, LLC.)
38#
39# By intentionally submitting any modifications, corrections or
40# derivatives to this work, or any other work intended for use with
41# Request Tracker, to Best Practical Solutions, LLC, you confirm that
42# you are the copyright holder for those contributions and you grant
43# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
44# royalty-free, perpetual, license to use, copy, create derivative
45# works based on those contributions, and sublicense and distribute
46# those contributions and any derivatives thereof.
47#
48# END BPS TAGGED BLOCK }}}
49use warnings;
50use strict;
51
52BEGIN {
53 require File::Spec;
54 my @libs = ("lib", "local/lib");
55 my $bin_path;
56
57 for my $lib (@libs) {
58 unless ( File::Spec->file_name_is_absolute($lib) ) {
59 unless ($bin_path) {
60 if ( File::Spec->file_name_is_absolute(__FILE__) ) {
61 $bin_path = ( File::Spec->splitpath(__FILE__) )[1];
62 }
63 else {
64 require FindBin;
65 no warnings "once";
66 $bin_path = $FindBin::Bin;
67 }
68 }
69 $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
70 }
71 unshift @INC, $lib;
72 }
73
74}
75
76use Date::Format qw( strftime );
77use Getopt::Long;
78use RT;
79use RT::Interface::CLI qw( CleanEnv loc );
80use RT::Interface::Email;
81
82CleanEnv();
83RT::LoadConfig();
84RT::Init();
85
86sub usage {
87 my ($error) = @_;
88 print loc("Usage:") . " $0 -m (daily|weekly) [--print] [--help]\n";
89 print loc(
90 "[_1] is a utility, meant to be run from cron, that dispatches all deferred RT notifications as a per-user digest.",
91 $0
92 ) . "\n";
93 print "\n\t-m, --mode\t"
94 . loc("Specify whether this is a daily or weekly run.") . "\n";
95 print "\t-p, --print\t"
96 . loc("Print the resulting digest messages to STDOUT; don't mail them. Do not mark them as sent")
97 . "\n";
01e3b242 98 print "\t-v, --verbose\t" . loc("Give output even on messages successfully sent") . "\n";
84fb5b46
MKG
99 print "\t-h, --help\t" . loc("Print this message") . "\n";
100
101 if ( $error eq 'help' ) {
102 exit 0;
103 } else {
104 print loc("Error") . ": " . loc($error) . "\n";
105 exit 1;
106 }
107}
108
01e3b242 109my ( $frequency, $print, $verbose, $help ) = ( '', '', '', '' );
84fb5b46
MKG
110GetOptions(
111 'mode=s' => \$frequency,
112 'print' => \$print,
01e3b242 113 'verbose' => \$verbose,
84fb5b46
MKG
114 'help' => \$help,
115);
116
117usage('help') if $help;
118usage("Mode argument must be 'daily' or 'weekly'")
119 unless $frequency =~ /^(daily|weekly)$/;
120
121run( $frequency, $print );
122
123sub run {
124 my $frequency = shift;
125 my $print = shift;
126
127## Find all the tickets that have been modified within the time frame
128## described by $frequency.
129
130 my ( $all_digest, $sent_transactions ) = find_transactions($frequency);
131
132## Iterate through our huge hash constructing the digest message
133## for each user and sending it.
134
135 foreach my $user ( keys %$all_digest ) {
136 my ( $contents_list, $contents_body ) = build_digest_for_user( $user, $all_digest->{$user} );
137 # Now we have a content head and a content body. We can send a message.
138 if ( send_digest( $user, $contents_list, $contents_body ) ) {
01e3b242 139 print "Sent message to $user\n" if $verbose;
84fb5b46
MKG
140 mark_transactions_sent( $frequency, $user, values %{$sent_transactions->{$user}} ) unless ($print);
141 } else {
142 print "Failed to send message to $user\n";
143 }
144 }
145}
146exit 0;
147
148# Subroutines.
149
150sub send_digest {
151 my ( $to, $index, $messages ) = @_;
152
153 # Combine the index and the messages.
154
155 my $body = "============== Tickets with activity in the last "
156 . ( $frequency eq 'daily' ? "day" : "seven days" ) . "\n\n";
157
158 $body .= $index;
159 $body .= "\n\n============== Messages recorded in the last "
160 . ( $frequency eq 'daily' ? "day" : "seven days" ) . "\n\n";
161 $body .= $messages;
162
163 # Load our template. If we cannot load the template, abort
164 # immediately rather than failing through many loops.
165 my $digest_template = RT::Template->new( RT->SystemUser );
166 my ( $ret, $msg ) = $digest_template->Load('Email Digest');
167 unless ($ret) {
168 print loc("Failed to load template")
169 . " 'Email Digest': "
170 . $msg
171 . ". Cannot continue.\n";
172 exit 1;
173 }
174 ( $ret, $msg ) = $digest_template->Parse( Argument => $body );
175 unless ($ret) {
176 print loc("Failed to parse template")
177 . " 'Email Digest'. Cannot continue.\n";
178 exit 1;
179 }
180
181 # Set our sender and recipient.
182 $digest_template->MIMEObj->head->replace( 'From', RT::Config->Get('CorrespondAddress') );
183 $digest_template->MIMEObj->head->replace( 'To', $to );
184
185 if ($print) {
186 $digest_template->MIMEObj->print;
187 return 1;
188 } else {
189 return RT::Interface::Email::SendEmail( Entity => $digest_template->MIMEObj)
190 }
191}
192
193# =item mark_transactions_sent( $frequency, $user, @txn_list );
194#
195# Takes a frequency string (either 'daily' or 'weekly'), a user and one or more
196# transaction objects as its arguments. Marks the given deferred
197# notifications as sent.
198#
199# =cut
200
201sub mark_transactions_sent {
202 my ( $freq, $user, @txns ) = @_;
203 return unless $freq =~ /(daily|weekly)/;
204 return unless @txns;
205 foreach my $txn (@txns) {
206
207 # Grab the attribute, mark the "sent" as true, and store the new
208 # value.
209 if ( my $attr = $txn->FirstAttribute('DeferredRecipients') ) {
210 my $deferred = $attr->Content;
211 $deferred->{$freq}->{$user}->{'_sent'} = 1;
212 $txn->SetAttribute(
213 Name => 'DeferredRecipients',
214 Description => 'Deferred recipients for this message',
215 Content => $deferred,
216 );
217 }
218 }
219}
220
221sub since_date {
222 my $frequency = shift;
223
224 # Specify a short time for digest overlap, in case we aren't starting
225 # this process exactly on time.
226 my $OVERLAP_HEDGE = -30;
227
228 my $since_date = RT::Date->new( RT->SystemUser );
229 $since_date->Set( Format => 'unix', Value => time() );
230 if ( $frequency eq 'daily' ) {
231 $since_date->AddDays(-1);
232 } else {
233 $since_date->AddDays(-7);
234 }
235
236 $since_date->AddSeconds($OVERLAP_HEDGE);
237
238 return $since_date;
239}
240
241sub find_transactions {
242 my $frequency = shift;
243 my $since_date = since_date($frequency);
244
245 my $txns = RT::Transactions->new( RT->SystemUser );
246
247 # First limit to recent transactions.
248 $txns->Limit(
249 FIELD => 'Created',
250 OPERATOR => '>',
251 VALUE => $since_date->ISO
252 );
253
254 # Next limit to ticket transactions.
255 $txns->Limit(
256 FIELD => 'ObjectType',
257 OPERATOR => '=',
258 VALUE => 'RT::Ticket',
259 ENTRYAGGREGATOR => 'AND'
260 );
261 my $all_digest = {};
262 my $sent_transactions = {};
263
264 while ( my $txn = $txns->Next ) {
265 my $ticket = $txn->Ticket;
266 my $queue = $txn->TicketObj->QueueObj->Name;
267 # Xxx todo - may clobber if two queues have the same name
268 foreach my $user ( $txn->DeferredRecipients($frequency) ) {
269 $all_digest->{$user}->{$queue}->{$ticket}->{ $txn->id } = $txn->ContentObj;
270 $sent_transactions->{$user}->{ $txn->id } = $txn;
271 }
272 }
273
274 return ( $all_digest, $sent_transactions );
275}
276
277sub build_digest_for_user {
278 my $user = shift;
279 my $user_digest = shift;
280
281 my $contents_list = ''; # Holds the digest index.
282 my $contents_body = ''; # Holds the digest body.
283
284 # Has the user been disabled since a message was deferred on his/her
285 # behalf?
286 my $user_obj = RT::User->new( RT->SystemUser );
287 $user_obj->LoadByEmail($user);
288 if ( $user_obj->PrincipalObj->Disabled ) {
289 print STDERR loc("Skipping disabled user") . " $user\n";
290 next;
291 }
292
293 print loc("Message for user") . " $user:\n\n" if $print;
294 foreach my $queue ( keys %$user_digest ) {
295 $contents_list .= "Queue $queue:\n";
296 $contents_body .= "Queue $queue:\n";
297 foreach my $ticket ( sort keys %{ $user_digest->{$queue} } ) {
298 my $tkt_txns = $user_digest->{$queue}->{$ticket};
299 my $ticket_obj = RT::Ticket->new( RT->SystemUser );
300 $ticket_obj->Load($ticket);
301
302 # Spit out the index entry for this ticket.
303 my $ticket_title = sprintf(
304 "#%d %s [%s]\t%s\n",
305 $ticket, $ticket_obj->Status, $ticket_obj->OwnerObj->Name,
306 $ticket_obj->Subject
307 );
308 $contents_list .= $ticket_title;
309
310 # Spit out the messages for the transactions on this ticket.
311 $contents_body .= "\n== $ticket_title\n";
312 foreach my $txn ( sort keys %$tkt_txns ) {
313 my $msg = $tkt_txns->{$txn};
314
315 # $msg contains an RT::Attachment with our outgoing
316 # message. Print a few headers for clarity's sake.
317 $contents_body .= "From: " . $msg->GetHeader('From') . "\n";
318 my $date = $msg->GetHeader('Date ');
319 unless ($date) {
320 my $txn_obj = RT::Transaction->new( RT->SystemUser );
321 $txn_obj->Load($txn);
322 my $date_obj = RT::Date->new( RT->SystemUser );
323 $date_obj->Set(
324 Format => 'sql',
325 Value => $txn_obj->Created
326 );
327 $date = strftime( '%a, %d %b %Y %H:%M:%S %z',
328 @{ [ localtime( $date_obj->Unix ) ] } );
329 }
330 $contents_body .= "Date: $date\n\n";
331 $contents_body .= $msg->Content . "\n";
332 $contents_body .= "-------\n";
333 } # foreach transaction
334 } # foreach ticket
335 } # foreach queue
336
337 return ( $contents_list, $contents_body );
338
339}
340
341__END__
342
343=head1 NAME
344
345rt-email-digest - dispatch deferred notifications as a per-user digest
346
347=head1 SYNOPSIS
348
349 rt-email-digest -m (daily|weekly) [--print] [--help]
350
351=head1 DESCRIPTION
352
353This script is a tool to dispatch all deferred RT notifications as a per-user
354object.
355
356=head1 OPTIONS
357
358=over
359
360=item mode
361
362Specify whether this is a daily or weekly run.
363
364--mode is equal to -m
365
366=item print
367
368Print the resulting digest messages to STDOUT; don't mail them. Do not mark them as sent
369
370--print is equal to -p
371
372=item help
373
374Print this message
375
376--help is equal to -h
377
378=back