summaryrefslogtreecommitdiffstats
path: root/lib/ASM/Log.pm
blob: 45d55315f9a270467f16dabf7533cde84e4af8c0 (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
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
package ASM::Log;
no autovivification;

use warnings;
use strict;

use ASM::Util;
use POSIX qw(strftime);
use Data::UUID;

no if $] >= 5.017011, warnings => 'experimental::smartmatch';

sub new
{
  my $module = shift;
  my ($conn) = @_;
  my $self = {};
  $self->{CONFIG} = $::settings->{log};
  $self->{backlog} = {};
  $self->{CONN} = $conn;
  $self->{UUID} = Data::UUID->new;
  bless($self);
  mkdir($self->{CONFIG}->{dir});
  $conn->add_handler('public',    sub { logg($self, @_); }, "before");
  $conn->add_handler('join',      sub { logg($self, @_); }, "before");
  $conn->add_handler('part',      sub { logg($self, @_); }, "before");
  $conn->add_handler('caction',   sub { logg($self, @_); }, "before");
  $conn->add_handler('nick',      sub { logg($self, @_); }, "after"); #allow state tracking to molest this
  $conn->add_handler('quit',      sub { logg($self, @_); }, "after"); #allow state tracking to molest this too
  $conn->add_handler('kick',      sub { logg($self, @_); }, "before");
  $conn->add_handler('notice',    sub { logg($self, @_); }, "before");
  $conn->add_handler('mode',      sub { logg($self, @_); }, "before");
  $conn->add_handler('topic',     sub { logg($self, @_); }, "before");
  return $self;
}

sub actionlog
{
  my ($self, $event, $modedata1, $modedata2) = @_;
  my ($action, $reason, $channel,
        $nick,   $user,   $host,   $gecos,   $account, $ip,
      $bynick, $byuser, $byhost, $bygecos, $byaccount);
  my ($lcnick, $lcbynick);

  if ($event->{type} eq 'mode') {
    $action = $modedata1;
    $nick = $modedata2;
    $channel = lc $event->{to}->[0];
    $bynick = $event->{nick};
    $byuser = $event->{user};
    $byhost = $event->{host};
  } elsif ($event->{type} eq 'quit') {
    my $quitmsg = $event->{args}->[0];
    if ($quitmsg =~ /^Killed \((\S+) \((.*)\)\)$/) {
      $bynick = $1;
      $reason = $2 unless ($2 eq '<No reason given>');
      return if (($reason // '') =~ /Nickname regained by services/);
      $action = 'kill';
    } elsif ($quitmsg =~ /^K-Lined$/) {
      $action = 'k-line';
    } else {
      return; #quit not forced/tracked
    }
    $nick = $event->{nick};
    $user = $event->{user};
    $host = $event->{host};
  } elsif (($event->{type} eq 'part') && ($event->{args}->[0] =~ /^requested by (\S+) \((.*)\)/)) {
    $bynick = $1;
    $reason = $2 unless (lc $2 eq lc $event->{nick});
    $action = 'remove';
    $nick = $event->{nick};
    $user = $event->{user};
    $host = $event->{host};
    $channel = $event->{to}->[0];
  } elsif ($event->{type} eq 'kick') {
    $action = 'kick';
    $bynick = $event->{nick};
    $byuser = $event->{user};
    $byhost = $event->{host};
    $reason = $event->{args}->[1] unless ($event->{args}->[1] eq $event->{to}->[0]);
    $nick = $event->{to}->[0];
    $channel = $event->{args}->[0];
  }
  return unless defined($action);
  $lcbynick = lc $bynick if defined $bynick; #we will lowercase the NUHGA info later.
  if ( (defined($bynick)) && (defined($::sn{$lcbynick})) ) { #we have the nick taking the action available, fill in missing NUHGA info
    $byuser //= $::sn{$lcbynick}{user};
    $byhost //= $::sn{$lcbynick}{host};
    $bygecos //= $::sn{$lcbynick}{gecos};
    $byaccount //= $::sn{$lcbynick}{account};
    if (($byaccount eq '0') or ($byaccount eq '*')) {
      $byaccount = undef;
    }
  }
  $lcnick = lc $nick if defined $nick;
  if ( (defined($nick)) && (defined($::sn{$lcnick})) ) { #this should always be true, else something has gone FUBAR
    $user //= $::sn{$lcnick}{user};
    $host //= $::sn{$lcnick}{host};
    $gecos //= $::sn{$lcnick}{gecos};
    $account //= $::sn{$lcnick}{account};
    if (($account eq '0') or ($account eq '*')) {
      $account = undef;
    }
    $ip = ASM::Util->getNickIP($lcnick);
  }

  return $::db->resultset('Actionlog')->create({
      action => $action,
      reason => $reason,
      channel => $channel,

      nick    => $nick,
      user    => $user,
      host    => $host,
      gecos   => $gecos,
      account => $account,
      ip      => $ip,

      bynick    => $bynick,
      byuser    => $byuser,
      byhost    => $byhost,
      bygecos   => $bygecos,
      byaccount => $byaccount,
    })->id;
# $::sn{ow} looks like:
#$VAR1 = { 
#          "account" => "afterdeath",
#          "gecos" => "William Athanasius Heimbigner",
#          "user" => "icxcnika",
#          "mship" => [ 
#                       "#baadf00d",
#                       "#antispammeta-debug",
#                       "#antispammeta"
#                     ],
#          "host" => "freenode/weird-exception/network-troll/afterdeath"
#        };

}

sub incident
{
  my $self = shift;
  my ($chan, $nick, $user, $host, $gecos, $risk, $id, $reason) = @_;
  $chan = lc $chan;
  my $uuid = $self->{UUID}->create_str();

  my $is_opalert = ($risk eq 'opalert');

  my $header;
  if ($is_opalert) {
    $header = "$chan: $nick requested op attention\n";
  }
  else {
    $header = "$chan: $risk risk: $nick - $reason\n";
  }

  open(FH, '>', $self->{CONFIG}->{detectdir} . $uuid . '.txt');
  print FH $header;
  if (defined($self->{backlog}->{$chan})) {
    print FH join('', @{$self->{backlog}->{$chan}});
  }
  print FH "\n\n";
  close(FH);

  return $uuid if $is_opalert;

  $gecos //= "NOT_DEFINED";

  $::db->resultset('Alertlog')->create({
      channel => $chan,
      nick    => $nick,
      user    => $user,
      host    => $host,
      gecos   => $gecos,
      level   => $risk,
      id      => $id,
      reason  => $reason,
    });

  return $uuid;
}

#writes out the backlog to a file which correlates to ASM's SQL actionlog table
sub sqlIncident
{
  my $self = shift;
  my ($channel, $index) = @_;
  $channel = lc $channel;
  my @chans = split(/,/, $channel);
  open(FH, '>', $self->{CONFIG}->{actiondir} . $index . '.txt');
  foreach my $chan (@chans) {
    if (defined($self->{backlog}->{$chan})) {
      say FH "$chan";
      say FH join('', @{$self->{backlog}->{$chan}});
    }
  }
  close(FH);
}

sub logg
{
  my $self = shift;
  my ($conn, $event) = @_;
  my $cfg = $self->{CONFIG};
  my @chans = @{$event->{to}};
  @chans = ( $event->{args}->[0] ) if ($event->{type} eq 'kick');
  my @time = ($cfg->{zone} eq 'local') ? localtime : gmtime;
  foreach my $chan ( @chans )
  {
    $chan = lc $chan;
    next if ($chan eq '$$*');
    $chan =~ s/^[@+]//;
    if ($chan eq '*') {
      ASM::Util->dprint("$event->{nick}: $event->{args}->[0]", 'snotice');
      next;
    }
    my $path = ">>$cfg->{dir}${chan}/${chan}" . strftime($cfg->{filefmt}, @time);
    $_ = '';
    $_ =    "<$event->{nick}> $event->{args}->[0]"                      if $event->{type} eq 'public';
    $_ = "*** $event->{nick} has joined $chan"                          if $event->{type} eq 'join';
    $_ = "*** $event->{nick} has left $chan ($event->{args}->[0])"      if $event->{type} eq 'part';
    $_ =   "* $event->{nick} $event->{args}->[0]"                       if $event->{type} eq 'caction';
    $_ = "*** $event->{nick} is now known as $event->{args}->[0]"       if $event->{type} eq 'nick';
    $_ = "*** $event->{nick} has quit ($event->{args}->[0])"            if $event->{type} eq 'quit';
    $_ = "*** $event->{to}->[0] was kicked by $event->{nick}"           if $event->{type} eq 'kick';
    $_ =    "-$event->{nick}- $event->{args}->[0]"                      if $event->{type} eq 'notice';
    $_ = "*** $event->{nick} sets mode: " . join(" ",@{$event->{args}}) if $event->{type} eq 'mode';
    $_ = "*** $event->{nick} changes topic to \"$event->{args}->[0]\""  if $event->{type} eq 'topic';
    my $nostamp = $_;
    $_ = strftime($cfg->{timefmt}, @time) . $_ . "\n";
    my $line = $_;
    my @backlog = ();
    if (defined($self->{backlog}->{$chan})) {
      @backlog = @{$self->{backlog}->{$chan}};
      if (scalar @backlog >= 30) {
        shift @backlog;
      }
    }
    push @backlog, $line;
    $self->{backlog}->{$chan} = \@backlog;
  }
}

1;
# vim: ts=2:sts=2:sw=2:expandtab