<commit_before>package Dancer::Plugin::IRCNotice;

use 5.008_005;
use strict;
use warnings;

use Dancer ':syntax';
use Dancer::Plugin;
use IO::Socket::IP;

our $VERSION = '0.03';

register notify => sub {
  my ($message) = @_;

  info "Sending notification: $message";

  my $config = plugin_setting;

  $config->{host}    ||= 'chat.freenode.net';
  $config->{nick}    ||= sprintf('dpin%04u', int(rand() * 10000));
  $config->{name}    ||= $config->{nick};
  $config->{channel} ||= '#dpintest';

  fork and return if $config->{fork};

  # Add default port
  $config->{host} .= ':6667' unless $config->{host} =~ /:\d+$/;

  my $socket = IO::Socket::IP->new($config->{host})
    or warning "Cannot create socket: $@" and return;

  # TODO error handling srsly

  info "Registering as $config->{nick}";

  $socket->say("NICK $config->{nick}");
  $socket->say("USER $config->{nick} . . :$config->{name}");

  while (my $line = $socket->getline) {
    info "Got $line";

    if ($line =~ /End of \/MOTD/) {
      info "Sending notice to $config->{channel}";

      $socket->say("NOTICE $config->{channel} :$message");
      $socket->say("QUIT");

      info "Notice sent";
      return;
    }
  }

  info "Notice not sent";

  return;
};

register_plugin;

1;
__END__

=encoding utf-8

=head1 NAME

Dancer::Plugin::IRCNotice - Send IRC notices from your dancer app

=head1 SYNOPSIS

  use Dancer::Plugin::IRCNotice;

  notify('This is a notification');

=head1 DESCRIPTION

Dancer::Plugin::IRCNotice provides a quick and dirty way to send IRC NOTICEs to
a specific channel.<commit_msg>Exit from forked child instead of returning<commit_after>package Dancer::Plugin::IRCNotice;

use 5.008_005;
use strict;
use warnings;

use Dancer ':syntax';
use Dancer::Plugin;
use IO::Socket::IP;

our $VERSION = '0.03';

register notify => sub {
  my ($message) = @_;

  info "Sending notification: $message";

  my $config = plugin_setting;

  $config->{host}    ||= 'chat.freenode.net';
  $config->{nick}    ||= sprintf('dpin%04u', int(rand() * 10000));
  $config->{name}    ||= $config->{nick};
  $config->{channel} ||= '#dpintest';

  fork and return if $config->{fork};

  # Add default port
  $config->{host} .= ':6667' unless $config->{host} =~ /:\d+$/;

  my $socket = IO::Socket::IP->new($config->{host})
    or warning "Cannot create socket: $@" and return;

  # TODO error handling srsly

  info "Registering as $config->{nick}";

  $socket->say("NICK $config->{nick}");
  $socket->say("USER $config->{nick} . . :$config->{name}");

  while (my $line = $socket->getline) {
    info "Got $line";

    if ($line =~ /End of \/MOTD/) {
      info "Sending notice to $config->{channel}";

      $socket->say("NOTICE $config->{channel} :$message");
      $socket->say("QUIT");

      info "Notice sent";
      exit if $config->{fork};
      return;
    }
  }

  info "Notice not sent";

  exit if $config->{fork};
  return;
};

register_plugin;

1;
__END__

=encoding utf-8

=head1 NAME

Dancer::Plugin::IRCNotice - Send IRC notices from your dancer app

=head1 SYNOPSIS

  use Dancer::Plugin::IRCNotice;

  notify('This is a notification');

=head1 DESCRIPTION

Dancer::Plugin::IRCNotice provides a quick and dirty way to send IRC NOTICEs to
a specific channel.
{'fraction_non_alphanumeric': 0.18411330049261085, 'fraction_numerical': 0.01600985221674877, 'mean_word_length': 3.674820143884892, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2988864', 'n_tokens_mistral': 1231, 'n_tokens_neox': 1128, 'n_words': 305}