summaryrefslogtreecommitdiffstats
path: root/lib/Crawl/Bot/Plugin/Logging.pm
blob: 1382392de2b04b36bb2694e4da96bac01040c5ac (plain) (blame)
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
package Crawl::Bot::Plugin::Logging;
use Moose;
extends 'Crawl::Bot::Plugin';

use autodie;

has date => (
    is      => 'ro',
    isa     => 'Str',
    lazy    => 1,
    clearer => 'clear_date',
    builder => '_build_date',
);

sub _build_date {
    my ($year, $month, $day) = (localtime)[5, 4, 3];
    $year += 1900;
    $month++;
    return sprintf '%04d%02d%02d', $year, $month, $day;
}

has logfile => (
    is      => 'ro',
    isa     => 'FileHandle',
    lazy    => 1,
    clearer => 'clear_logfile',
    default => sub {
        my $self = shift;
        my $filepath = File::Spec->catfile($self->logfilepath, ($self->bot->channels)[0] . '-' . $self->date . '.log');
        open my $fh, '>>', $filepath;
        $fh->autoflush(1);
        return $fh;
    }
);

has logfilepath => (
    is      => 'ro',
    isa     => 'Str',
    lazy    => 1,
    default => sub {
        my $self = shift;
        my $path = File::Spec->catdir($self->data_dir, 'log');
        mkdir $path unless -d $path;
        return $path;
    },
);

before logfile => sub {
    my $self = shift;
    if ($self->date ne $self->_build_date) {
        $self->clear_date;
        $self->clear_logfile;
    }
};

sub log_message {
    my $self = shift;
    my ($message) = @_;

    my $timestamp = sprintf '%02d:%02d:%02d', (localtime)[2, 1, 0];
    $self->logfile->print("$timestamp $message\n");
}

sub said {
    my $self = shift;
    my ($args) = @_;

    $self->log_message("<$args->{who}> $args->{body}");
    return;
}

sub emoted {
    my $self = shift;
    my ($args) = @_;

    $self->log_message("* $args->{who} $args->{body}");
    return;
}

sub chanjoin {
    my $self = shift;
    my ($args) = @_;

    $self->log_message("-!- $args->{who} has joined $args->{channel}");
    return;
}

sub chanpart {
    my $self = shift;
    my ($args) = @_;

    $self->log_message("-!- $args->{who} has left $args->{channel}");
    return;
}

sub nick_change {
    my $self = shift;
    my ($args) = @_;

    $self->log_message("-!- $args->{from} is now known as $args->{to}");
    return;
}

sub kicked {
    my $self = shift;
    my ($args) = @_;

    $self->log_message("-!- $args->{kicked} was kicked from $args->{channel} by $args->{who} [$args->{reason}]");
    return;
}

sub topic {
    my $self = shift;
    my ($args) = @_;

    if (defined($args->{who})) {
        $self->log_message("-!- $args->{who} changed the topic of $args->{channel} to: $args->{topic}");
    }
    else {
        $self->log_message("-!- The topic of $args->{channel} is: $args->{topic}");
    }
    return;
}

sub userquit {
    my $self = shift;
    my ($args) = @_;

    $self->log_message("-!- $args->{who} has quit [$args->{body}]");
    return;
}

sub sent {
    my $self = shift;
    my ($args) = @_;

    $self->log_message("<$args->{who}> $args->{body}");
    return;
}

1;