Skip to content

Instantly share code, notes, and snippets.

@Hello71
Forked from anonymous/znc.pl
Created February 25, 2014 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hello71/9200467 to your computer and use it in GitHub Desktop.
Save Hello71/9200467 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
our $VERSION = '0.1';
our %IRSSI = (
authors => 'Alex Xu',
contact => 'alex_y_xu@yahoo.ca',
description => 'Reformat znc buffer playback messages',
license => 'GPL',
name => 'znc',
url => 'https://gist.github.com/Hello71/9200467',
);
# 20:18:26 < ***> Buffer Playback...
# 20:18:26 < somebody> [18:42:09] foo
# 20:18:26 < somebodyelse> [18:49:10] bar
# 20:18:26 < *buffextras> [18:49:13] another!person@example.com set mode: +t
# 20:18:26 < ***> Playback Complete.
# will be changed to:
# 20:18:26 < ***> Buffer Playback...
# 18:42:09 < somebody> foo
# 18:49:10 < somebodyelse> bar
# 18:49:13 -!- mode/#channel [+t] by another
# 20:18:26 < ***> Playback Complete.
# If you use another timestamp/output format in znc than the one in the
# example above, you must change following regular expressions to
# match your format and capture the wanted parts.
my $TIMESTAMP_REGEX = qr/^\[(\d{2}:\d{2}:\d{2})\] /;
# Note, these regexes are executed *after* the timestamp regex above, so ^ will
# match as intended.
my $MODE_REGEX = qr/^([^ !]+)!?([^ ]*) set mode: (.+?) ?$/;
my $KICK_REGEX = qr/^([^ !]+)!?[^ ]* kicked ([^ ]+) Reason: \[(.*)\]$/;
my $QUIT_REGEX = qr/^([^ !]+)!?([^ ]*) quit with message: \[(.*)\]$/;
my $JOIN_REGEX = qr/^([^ !]+)!?([^ ]*) joined$/;
my $PART_REGEX = qr/^([^ !]+)!?([^ ]*) parted with message: \[(.*)\]$/;
my $NICK_REGEX = qr/^([^ !]+)!?[^ ]* is now known as ([^ ]+)$/;
my $TOPIC_REGEX = qr/^([^ !]+)!?[^ ]* changed the topic to: (.*)$/;
sub printformat {
my ($server, $target, $level, $module, $format, @args) = @_;
{
# deeeeeeep black magic.
local *CORE::GLOBAL::caller = sub { $module };
$server->printformat($target, $level, $format, @args);
}
}
my %PLAYBACK;
sub privmsg {
my ($server, $data, $nick, $address) = @_;
my ($target, $text) = split / :/, $data, 2;
my $continue = 0;
if ($nick eq '***') {
Irssi::signal_stop();
if ($text eq 'Buffer Playback...') {
$PLAYBACK{$target} = 1;
} elsif ($text eq 'Playback Complete.') {
delete $PLAYBACK{$target};
} else {
Irssi::print('*** message not recognized', Irssi::MSGLEVEL_CLIENTERROR);
}
} elsif ($PLAYBACK{$target}) {
my $timestamp_format = Irssi::settings_get_str('timestamp_format');
$text =~ s/$TIMESTAMP_REGEX//;
Irssi::settings_set_str('timestamp_format', $1);
if ($nick eq '*buffextras') {
if ($text =~ s/$MODE_REGEX//) {
if ($2) {
printformat($server, $target, Irssi::MSGLEVEL_MODES, 'fe-common/irc', 'chanmode_change', $target, $3, $1);
} else {
printformat($server, $target, Irssi::MSGLEVEL_MODES, 'fe-common/irc', 'server_chanmode_change', $target, $3, $1);
}
} elsif ($text =~ s/$JOIN_REGEX//) {
printformat($server, $target, Irssi::MSGLEVEL_JOINS, 'fe-common/core', 'join', $1, $2, $target);
} elsif ($text =~ s/$NICK_REGEX//) {
printformat($server, $target, Irssi::MSGLEVEL_NICKS, 'fe-common/core', 'nick_changed', $1, $2);
} elsif ($text =~ s/$PART_REGEX//) {
printformat($server, $target, Irssi::MSGLEVEL_PARTS, 'fe-common/core', 'part', $1, $2, $target, $3);
} elsif ($text =~ s/$QUIT_REGEX//) {
printformat($server, $target, Irssi::MSGLEVEL_QUITS, 'fe-common/core', 'quit', $1, $2, $3);
} elsif ($text =~ s/$KICK_REGEX//) {
printformat($server, $target, Irssi::MSGLEVEL_KICKS, 'fe-common/core', 'kick', $2, $target, $1, $3);
} elsif ($text =~ s/$TOPIC_REGEX//) {
if ($2) {
printformat($server, $target, Irssi::MSGLEVEL_TOPICS, 'fe-common/core', 'new_topic', $1, $target, $2);
} else {
printformat($server, $target, Irssi::MSGLEVEL_TOPICS, 'fe-common/core', 'topic_unset', $1, $target);
}
} else {
Irssi::print('*buffextras message not recognized, printing as is.', Irssi::MSGLEVEL_CLIENTERROR);
$continue = 1;
}
} else {
$continue = 1;
}
if ($continue) {
Irssi::signal_continue($server, $target . ' :' . $text, $nick, $address);
} else {
Irssi::signal_stop();
}
Irssi::settings_set_str('timestamp_format', $timestamp_format);
}
}
Irssi::signal_add('event privmsg', 'privmsg');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment