summaryrefslogtreecommitdiff
path: root/rt/sbin/rt-email-dashboards
blob: d46e0fe204a4809be86d28696ca0397af1555add (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
#!/usr/bin/perl
# BEGIN BPS TAGGED BLOCK {{{
# 
# COPYRIGHT:
# 
# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
#                                          <jesse@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
# you are the copyright holder for those contributions and you grant
# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
# 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 strict;
use warnings;

# fix lib paths, some may be relative
BEGIN {
    require File::Spec;
    my @libs = ("lib", "local/lib");
    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;
                }
            }
            $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
        }
        unshift @INC, $lib;
    }

}

use RT;
use RT::Interface::Web;
use RT::Interface::Web::Handler;
use RT::Dashboard;
use RT::Interface::CLI qw{ CleanEnv loc };

use Getopt::Long;
use HTML::Mason;
use HTML::RewriteAttributes::Resources;
use HTML::RewriteAttributes::Links;
use MIME::Types;
use POSIX 'tzset';
use File::Temp 'tempdir';

# Clean out all the nasties from the environment
CleanEnv();

# Load the config file
RT::LoadConfig();

# Connect to the database and get RT::SystemUser and RT::Nobody loaded
RT::Init();

$HTML::Mason::Commands::r = RT::Dashboard::FakeRequest->new;

no warnings 'once';

# Read in the options
my %opts;
GetOptions( \%opts,
    "help", "dryrun", "verbose", "debug", "epoch=i", "all", "skip-acl"
);

if ($opts{'help'}) {
    require Pod::Usage;
    import Pod::Usage;
    pod2usage(-message => "RT Email Dashboards\n", -verbose => 1);
    exit 1;
}

# helper functions
sub verbose  { print loc(@_), "\n" if $opts{debug} || $opts{verbose}; 1 }
sub debug    { print loc(@_), "\n" if $opts{debug}; 1 }
sub error    { $RT::Logger->error(loc(@_)); verbose(@_); 1 }
sub warning  { $RT::Logger->warning(loc(@_)); verbose(@_); 1 }

my $now = $opts{epoch} || time;
verbose "Using time [_1]", scalar localtime($now);

my $from = get_from();
debug "Sending email from [_1]", $from;

# look through each user for her subscriptions
my $Users = RT::Users->new($RT::SystemUser);
$Users->LimitToPrivileged;

while (defined(my $user = $Users->Next)) {
    if ($user->PrincipalObj->Disabled) {
        debug "Skipping over "
            . $user->Name
            . " due to having a disabled account.";
        next;
    }

    my ($hour, $dow, $dom) = hour_dow_dom_in($user->Timezone || RT->Config->Get('Timezone'));
    $hour .= ':00';
    debug "Checking [_1]'s subscriptions: hour [_2], dow [_3], dom [_4]",
          $user->Name, $hour, $dow, $dom;

    my $currentuser = RT::CurrentUser->new;
    $currentuser->LoadByName($user->Name);

    # look through this user's subscriptions, are any supposed to be generated
    # right now?
    for my $subscription ($user->Attributes->Named('Subscription')) {
        my $counter = $subscription->SubValue('Counter') || 0;

        if (!$opts{all}) {
            debug "Checking against subscription with frequency [_1], hour [_2], dow [_3], dom [_4]",
                $subscription->SubValue('Frequency'), $subscription->SubValue('Hour'),
                $subscription->SubValue('Dow'), $subscription->SubValue('Dom');

            next if $subscription->SubValue('Frequency') eq 'never';

            # correct hour?
            next if $subscription->SubValue('Hour') ne $hour;

            # if weekly, correct day of week?
            if ( $subscription->SubValue('Frequency') eq 'weekly' ) {
                next if $subscription->SubValue('Dow') ne $dow;
                my $fow       = $subscription->SubValue('Fow') || 1;
                if ( $counter % $fow ) {
                    $subscription->SetSubValues( Counter => $counter + 1 )
                      unless $opts{'dryrun'};
                    next;
                }
            }

            # if monthly, correct day of month?
            elsif ($subscription->SubValue('Frequency') eq 'monthly') {
                next if $subscription->SubValue('Dom') != $dom;
            }

            elsif ($subscription->SubValue('Frequency') eq 'm-f') {
                next if $dow eq 'Sunday' || $dow eq 'Saturday';
            }
        }

        my $email = $subscription->SubValue('Recipient')
                 || $user->EmailAddress;

        eval { send_dashboard($currentuser, $email, $subscription) };
        if ( $@ ) {
            error 'Caught exception: ' . $@;
        }
        else {
            $subscription->SetSubValues(
                Counter => $counter + 1 )
              unless $opts{'dryrun'};
        }
    }
}

sub send_dashboard {
    my ($currentuser, $email, $subscription) = @_;

    my $rows = $subscription->SubValue('Rows');

    my $dashboard = RT::Dashboard->new($currentuser);

    my ($ok, $msg) = $dashboard->LoadById($subscription->SubValue('DashboardId'));

    # failed to load dashboard. perhaps it was deleted or it changed privacy
    if (!$ok) {
        warning "Unable to load dashboard [_1] of subscription [_2] for user [_3]: [_4]",
            $subscription->SubValue('DashboardId'),
            $subscription->Id,
            $currentuser->Name,
            $msg;

        my $ok = RT::Interface::Email::SendEmailUsingTemplate(
            From      => $from,
            To        => $email,
            Template  => 'Error: Missing dashboard',
            Arguments => {
                SubscriptionObj => $subscription,
            },
        );

        # only delete the subscription if the email looks like it went through
        if ($ok) {
            my ($deleted, $msg) = $subscription->Delete();
            if ($deleted) {
                verbose("Deleted an obsolete subscription: [_1]", $msg);
            }
            else {
                warning("Unable to delete an obsolete subscription: [_1]", $msg);
            }
        }
        else {
            warning("Unable to notify [_1] of an obsolete subscription", $currentuser->Name);
        }

        return;
    }

    verbose 'Creating dashboard "[_1]" for user "[_2]":',
            $dashboard->Name,
            $currentuser->Name;

    if ($opts{'dryrun'}) {
        print << "SUMMARY";
    Dashboard: @{[ $dashboard->Name ]}
    User:   @{[ $currentuser->Name ]} <$email>
SUMMARY
        return;
    }

    $HTML::Mason::Commands::session{CurrentUser} = $currentuser;
    my $contents = run_component(
        '/Dashboards/Render.html',
        id      => $dashboard->Id,
        Preview => 0,
    );

    for (@{ RT->Config->Get('EmailDashboardRemove') || [] }) {
        $contents =~ s/$_//g;
    }

    debug "Got [_1] characters of output.", length $contents;

    $contents = HTML::RewriteAttributes::Links->rewrite(
        $contents,
        RT->Config->Get('WebURL') . '/Dashboards/Render.html',
    );

    email_dashboard($currentuser, $email, $dashboard, $subscription, $contents);
}

sub email_dashboard {
    my ($currentuser, $email, $dashboard, $subscription, $content) = @_;

    verbose 'Sending dashboard "[_1]" to user [_2] <[_3]>',
            $dashboard->Name,
            $currentuser->Name,
            $email;

    my $subject = sprintf '[%s] ' .  RT->Config->Get('DashboardSubject'),
        RT->Config->Get('rtname'),
        ucfirst($subscription->SubValue('Frequency')),
        $dashboard->Name;

    my $entity = build_email($content, $from, $email, $subject);

    my $ok = RT::Interface::Email::SendEmail(
        Entity => $entity,
    );

    debug "Done sending dashboard to [_1] <[_2]>",
          $currentuser->Name, $email
              and return if $ok;

    error 'Failed to email dashboard to user [_1] <[_2]>',
          $currentuser->Name, $email;
}

sub build_email {
    my ($content, $from, $to, $subject) = @_;
    my @parts;
    my %cid_of;

    $content = HTML::RewriteAttributes::Resources->rewrite($content, sub {
            my $uri = shift;

            # already attached this object
            return "cid:$cid_of{$uri}" if $cid_of{$uri};

            $cid_of{$uri} = time() . $$ . int(rand(1e6));
            my ($data, $filename, $mimetype, $encoding) = get_resource($uri);

            # downgrade non-text strings, because all strings are utf8 by
            # default, which is wrong for non-text strings.
            if ( $mimetype !~ m{text/} ) {
                utf8::downgrade( $data, 1 ) or warning "downgrade $data failed";
            }

            push @parts, MIME::Entity->build(
                Top          => 0,
                Data         => $data,
                Type         => $mimetype,
                Encoding     => $encoding,
                Disposition  => 'inline',
                Name         => $filename,
                'Content-Id' => $cid_of{$uri},
            );

            return "cid:$cid_of{$uri}";
        },
        inline_css => sub {
            my $uri = shift;
            my ($content) = get_resource($uri);
            return $content;
        },
        inline_imports => 1,
    );

    my $entity = MIME::Entity->build(
        From    => $from,
        To      => $to,
        Subject => $subject,
        Type    => "multipart/mixed",
    );

    $entity->attach(
        Data        => Encode::encode_utf8($content),
        Type        => 'text/html',
        Charset     => 'UTF-8',
        Disposition => 'inline',
    );

    for my $part (@parts) {
        $entity->add_part($part);
    }

    return $entity;
}

sub get_from {
    RT->Config->Get('DashboardAddress') || RT->Config->Get('OwnerEmail')
}

{
    my $mason;
    my $outbuf = '';
    my $data_dir = '';

    sub mason {
        unless ($mason) {
            debug "Creating Mason object.";

            # user may not have permissions on the data directory, so create a
            # new one
            $data_dir = tempdir(CLEANUP => 1);

            $mason = HTML::Mason::Interp->new(
                RT::Interface::Web::Handler->DefaultHandlerArgs,
                out_method => \$outbuf,
                autohandler_name => '', # disable forced login and more
                data_dir => $data_dir,
            );
        }
        return $mason;
    }

    sub run_component {
        mason->exec(@_);
        my $ret = $outbuf;
        $outbuf = '';
        return $ret;
    }
}

{
    my %cache;

    sub hour_dow_dom_in {
        my $tz = shift;
        return @{$cache{$tz}} if exists $cache{$tz};

        my ($hour, $dow, $dom);

        {
            local $ENV{'TZ'} = $tz;
            ## Using POSIX::tzset fixes a bug where the TZ environment variable
            ## is cached.
            tzset();
            (undef, undef, $hour, $dom, undef, undef, $dow) = localtime($now);
        }
        tzset(); # return back previous value

        $hour = "0$hour"
            if length($hour) == 1;
        $dow = (qw/Sunday Monday Tuesday Wednesday Thursday Friday Saturday/)[$dow];

        return @{$cache{$tz}} = ($hour, $dow, $dom);
    }
}

sub get_resource {
    my $uri = URI->new(shift);
    my ($content, $filename, $mimetype, $encoding);

    verbose "Getting resource [_1]", $uri;

    # strip out the equivalent of WebURL, so we start at the correct /
    my $path = $uri->path;
    my $webpath = RT->Config->Get('WebPath');
    $path =~ s/^\Q$webpath//;

    # add a leading / if needed
    $path = "/$path"
        unless $path =~ m{^/};

    # grab the query arguments
    my %args;
    for (split /&/, ($uri->query||'')) {
        my ($k, $v) = /^(.*?)=(.*)$/
            or die "Unable to parse query parameter '$_'";

        for ($k, $v) { s/%(..)/chr hex $1/ge }

        # no value yet, simple key=value
        if (!exists $args{$k}) {
            $args{$k} = $v;
        }
        # already have key=value, need to upgrade it to key=[value1, value2]
        elsif (!ref($args{$k})) {
            $args{$k} = [$args{$k}, $v];
        }
        # already key=[value1, value2], just add the new value
        else {
            push @{ $args{$k} }, $v;
        }
    }

    debug "Running component '[_1]'", $path;
    $content = run_component($path, %args);

    # guess at the filename from the component name
    $filename = $1 if $path =~ m{^.*/(.*?)$};

    # the rest of this was taken from Email::MIME::CreateHTML::Resolver::LWP
    ($mimetype, $encoding) = MIME::Types::by_suffix($filename);

    my $content_type = $HTML::Mason::Commands::r->content_type;
    if ($content_type) {
        $mimetype = $content_type;

        # strip down to just a MIME type
        $mimetype = $1 if $mimetype =~ /(\S+);\s*charset=(.*)$/;
    }

    #If all else fails then some conservative and general-purpose defaults are:
    $mimetype ||= 'application/octet-stream';
    $encoding ||= 'base64';

    debug "Resource [_1]: length=[_2] filename='[_3]' mimetype='[_4]', encoding='[_5]'",
        $uri,
        length($content),
        $filename,
        $mimetype,
        $encoding;

    return ($content, $filename, $mimetype, $encoding);
}

package RT::Dashboard::FakeRequest;
sub new { bless {}, shift }
sub header_out { shift }
sub headers_out { shift }
sub content_type {
    my $self = shift;
    $self->{content_type} = shift if @_;
    return $self->{content_type};
}

=head1 NAME

rt-email-dashboards - Send email dashboards

=head1 SYNOPSIS

    /opt/rt3/local/sbin/rt-email-dashboards [options]

=head1 DESCRIPTION

This tool will send users email based on how they have subscribed to
dashboards. A dashboard is a set of saved searches, the subscription controls
how often that dashboard is sent and how it's displayed.

Each subscription has an hour, and possibly day of week or day of month. These
are taken to be in the user's timezone if available, UTC otherwise.

=head1 SETUP

You'll need to have cron run this script every hour. Here's an example crontab
entry to do this.

    0 * * * * /usr/bin/perl /opt/rt3/local/sbin/rt-email-dashboards

This will run the script every hour on the hour. This may need some further
tweaking to be run as the correct user.

=head1 OPTIONS

This tool supports a few options. Most are for debugging.

=over 8

=item --help

Display this documentation

=item --dryrun

Figure out which dashboards would be sent, but don't actually generate them

=item --epoch SECONDS

Instead of using the current time to figure out which dashboards should be
sent, use SECONDS (usually since midnight Jan 1st, 1970, so C<1192216018> would
be Oct 12 19:06:58 GMT 2007).

=item --verbose

Print out some tracing information (such as which dashboards are being
generated and sent out)

=item --debug

Print out more tracing information (such as each user and subscription that is
being considered)

=item --all

Ignore subscription frequency when considering each dashboard (should only be
used with --dryrun)

=back

=cut