Skip to content

Instantly share code, notes, and snippets.

@kompowiec
Last active September 1, 2024 11:51
Show Gist options
  • Save kompowiec/e8e4b274196ff7bac833bdb00896bbec to your computer and use it in GitHub Desktop.
Save kompowiec/e8e4b274196ff7bac833bdb00896bbec to your computer and use it in GitHub Desktop.
file format for Perl IRC Statistics Generator - https://pisg.github.io/
package Pisg::Parser::Format::IRCcloud;
use strict;
$^W = 1;
sub new
{
my ($type, %args) = @_;
my $self = {
cfg => $args{cfg},
normalline => qr/^\[\d{4}-\d{2}-\d{2} (\d{2}):(\d{2}):(\d{2})\] <([^>]+)> (.+)$/, # Matches: [2024-02-28 11:54:59] <nick> message
actionline => qr/^\[\d{4}-\d{2}-\d{2} (\d{2}):(\d{2}):(\d{2})\] — ([^ ]+) (.+)$/, # Matches: [2024-02-24 02:08:41] — nick action
thirdline => qr/^\[\d{4}-\d{2}-\d{2} (\d{2}):(\d{2}):(\d{2})\] (.+)$/, # Matches thirdline patterns (joining, quitting, mode changes, etc.)
};
bless($self, $type);
return $self;
}
# Parse a normal line - returns a hash with 'hour', 'min', 'sec', 'nick' and 'saying'
sub normalline
{
my ($self, $line, $lines) = @_;
my %hash;
if ($line =~ /$self->{normalline}/o) {
$hash{hour} = $1;
$hash{min} = $2;
$hash{sec} = $3;
$hash{nick} = $4;
$hash{saying} = $5;
return \%hash;
} else {
return;
}
}
# Parse an action line - returns a hash with 'hour', 'min', 'sec', 'nick' and 'saying'
sub actionline
{
my ($self, $line, $lines) = @_;
my %hash;
if ($line =~ /$self->{actionline}/o) {
$hash{hour} = $1;
$hash{min} = $2;
$hash{sec} = $3;
$hash{nick} = $4;
$hash{saying} = $5;
return \%hash;
} else {
return;
}
}
# Parses the 'third' line - matches everything else
sub thirdline
{
my ($self, $line, $lines) = @_;
my %hash;
if ($line =~ /$self->{thirdline}/o) {
$hash{hour} = $1;
$hash{min} = $2;
$hash{sec} = $3;
my $rest = $4;
# Handle joins
if ($rest =~ /^→ ([^ ]+) joined/) {
$hash{newjoin} = $1;
}
# Handle quits
elsif ($rest =~ /^⇐ ([^ ]+) quit.*: (.+)$/) {
$hash{nick} = $1;
$hash{reason} = $2;
}
# Handle nick changes
elsif ($rest =~ /^\* ([^ ]+) → ([^ ]+)$/) {
$hash{nick} = $1;
$hash{newnick} = $2;
}
# Handle mode changes (ops, voice, etc.)
elsif ($rest =~ /^\* ([^ ]+) set (channel )?modes ([+-][a-z]+) ([^ ]+)/) {
$hash{nick} = $1;
$hash{newmode} = $3;
$hash{modechanges} = $4;
# Update counts based on mode changes
if ($3 =~ /\+o/) {
$hash{gaveops} = 1;
}
elsif ($3 =~ /-o/) {
$hash{tookops} = 1;
}
if ($3 =~ /\+v/) {
$hash{gavevoice} = 1;
}
elsif ($3 =~ /-v/) {
$hash{tookvoice} = 1;
}
}
# Handle kicks
elsif ($rest =~ /^\* ([^ ]+) was kicked by ([^ ]+) \((.+)\)$/) {
$hash{nick} = $1;
$hash{kicker} = $2;
$hash{kicktext} = $3;
}
# Handle topic changes
elsif ($rest =~ /^\* ([^ ]+) set the topic to \[(.+)\]$/) {
$hash{nick} = $1;
$hash{newtopic} = $2;
# Update the API-specific topics array
push @{$self->{cfg}{topics}}, {
topic => $2,
nick => $1,
hour => $hash{hour},
min => $hash{min},
};
}
# Handle host changes
elsif ($rest =~ /^\* ([^ ]+) changed host: (.+) → (.+)$/) {
$hash{nick} = $1;
$hash{oldhost} = $2;
$hash{newhost} = $3;
}
# Add more handling as needed based on the specific log lines.
return \%hash;
} else {
return;
}
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment