summaryrefslogtreecommitdiffstats
path: root/extensions/Push/lib/Connector/File.pm
blob: ae249ffe5a14791231ce295da2c5690d22e49cb7 (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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

package Bugzilla::Extension::Push::Connector::File;

use 5.10.1;
use strict;
use warnings;

use base 'Bugzilla::Extension::Push::Connector::Base';

use Bugzilla::Constants;
use Bugzilla::Extension::Push::Constants;
use Bugzilla::Extension::Push::Util;
use Encode;
use FileHandle;

sub init {
    my ($self) = @_;
}

sub options {
    return (
        {
            name     => 'filename',
            label    => 'Filename',
            type     => 'string',
            default  => 'push.log',
            required => 1,
            validate => sub {
                my $filename = shift;
                $filename =~ m#^/#
                    && die "Absolute paths are not permitted\n";
                $filename =~ m#\.\.#
                    && die "Relative paths are not permitted\n";
            },
        },
    );
}

sub should_send {
    my ($self, $message) = @_;
    return 1;
}

sub send {
    my ($self, $message) = @_;

    # pretty-format json payload
    my $payload = $message->payload_decoded;
    $payload = to_json($payload, 1);

    my $filename = bz_locations()->{'datadir'} . '/' . $self->config->{filename};
    Bugzilla->push_ext->logger->debug("File: Appending to $filename");
    my $fh = FileHandle->new(">>$filename");
    $fh->binmode(':utf8');
    $fh->print(
        "[" . scalar(localtime) . "]\n" .
        $payload . "\n\n"
    );
    $fh->close;

    return PUSH_RESULT_OK;
}

1;