Mojo-IRC

 view release on metacpan or  search on metacpan

lib/Mojo/IRC.pm  view on Meta::CPAN

package Mojo::IRC;
use Mojo::Base 'Mojo::EventEmitter';
use Mojo::IOLoop;
use Mojo::Promise;
use File::Basename 'dirname';
use File::Spec::Functions 'catfile';
use IRC::Utils   ();
use Parse::IRC   ();
use Scalar::Util ();
use Unicode::UTF8;
use constant DEBUG        => $ENV{MOJO_IRC_DEBUG}     || 0;
use constant DEFAULT_CERT => $ENV{MOJO_IRC_CERT_FILE} || catfile dirname(__FILE__), 'mojo-irc-client.crt';
use constant DEFAULT_KEY  => $ENV{MOJO_IRC_KEY_FILE}  || catfile dirname(__FILE__), 'mojo-irc-client.key';

our $VERSION = '0.47';

our %NUMERIC2NAME = (470 => 'ERR_LINKCHANNEL');

my %CTCP_QUOTE = ("\012" => 'n', "\015" => 'r', "\0" => '0', "\cP" => "\cP");

my @DEFAULT_EVENTS = qw(
  irc_ping irc_nick irc_notice irc_rpl_welcome err_nicknameinuse
  irc_rpl_isupport ctcp_ping ctcp_time ctcp_version
);

has connect_timeout => sub { $ENV{MOJO_IRC_CONNECT_TIMEOUT} || 30 };
has ioloop          => sub { Mojo::IOLoop->singleton };
has local_address   => '';
has name            => 'Mojo IRC';
has nick            => sub { shift->_build_nick };
has parser          => sub { Parse::IRC->new; };
has pass            => '';
has real_host       => '';

has server_settings => sub {
  return {chantypes => '#', prefix => '(ov)@+'};
};

has tls  => undef;
has user => sub { $ENV{USER} || getlogin || getpwuid($<) || 'anonymous' };

sub new {
  my $self = shift->SUPER::new(@_);
  $self->on(message => \&_legacy_dispatch_message);
  return $self;
}

sub server {
  my ($self, $server) = @_;
  my $old = $self->{server} || '';

  Scalar::Util::weaken($self);
  return $old unless defined $server;
  return $self if $old and $old eq $server;
  $self->{server} = $server;
  return $self unless $self->{stream_id};
  $self->disconnect(sub {
    $self->connect(sub { });
  });
  $self;
}

sub connect {
  my ($self, $cb) = @_;
  my ($host, $port) = split /:/, $self->server;
  my @extra;

  if (!$host) {
    $self->ioloop->next_tick(sub { $self->$cb('server() is not set.') });
    return $self;
  }
  if ($self->{stream_id}) {
    $self->ioloop->next_tick(sub { $self->$cb('') });
    return $self;
  }

  if ($self->local_address) {
    push @extra, local_address => $self->local_address;
  }
  if (my $tls = $self->tls) {
    push @extra, tls         => 1;
    push @extra, tls_ca      => $tls->{ca} if $tls->{ca};  # not sure why this should be supported, but adding it anyway
    push @extra, tls_cert    => $tls->{cert} || DEFAULT_CERT;
    push @extra, tls_key     => $tls->{key}  || DEFAULT_KEY;
    push @extra, tls_verify  => 0x00                      if $tls->{insecure};    # Mojolicious < 9.0
    push @extra, tls_options => {SSL_verify_mode => 0x00} if $tls->{insecure};    # Mojolicious >= 9.0
  }

  $port ||= 6667;
  $self->{buffer} = '';
  $self->{debug_key} ||= "$host:$port";
  $self->register_default_event_handlers;

  Scalar::Util::weaken($self);
  $self->{stream_id} = $self->ioloop->client(
    address => $host,
    port    => $port,
    timeout => $self->connect_timeout,
    @extra,
    sub {



( run in 1.387 second using v1.01-cache-2.11-cpan-39bf76dae61 )