Code Monkey home page Code Monkey logo

mojo-irc's Introduction

NAME

Mojo::IRC - IRC Client for the Mojo IOLoop

SYNOPSIS

my $irc = Mojo::IRC->new(
            nick => 'test123',
            user => 'my name',
            server => 'irc.perl.org:6667',
          );

$irc->on(irc_join => sub {
  my($self, $message) = @_;
  warn "yay! i joined $message->{params}[0]";
});

$irc->on(irc_privmsg => sub {
  my($self, $message) = @_;
  say $message->{prefix}, " said: ", $message->{params}[1];
});

$irc->connect(sub {
  my($irc, $err) = @_;
  return warn $err if $err;
  $irc->write(join => '#mojo');
});

Mojo::IOLoop->start;

DESCRIPTION

Mojo::IRC is a non-blocking IRC client using Mojo::IOLoop from the wonderful Mojolicious framework.

It features IPv6 and TLS, with additional optional modules: IO::Socket::IP and IO::Socket::SSL.

By default this module will only emit standard IRC events, but by settings "parser" to a custom object it will also emit CTCP events. Example:

my $irc = Mojo::IRC->new;
$irc->parser(Parse::IRC->new(ctcp => 1);
$irc->on(ctcp_action => sub {
  # ...
});

It will also set up some default events: "ctcp_ping", "ctcp_time", and "ctcp_version".

This class inherits from Mojo::EventEmitter.

TESTING

The module Test::Mojo::IRC is useful if you want to write tests without having a running IRC server.

MOJO_IRC_OFFLINE (from v0.20) is now DEPRECATED in favor of Test::Mojo::IRC.

EVENTS

close

$self->on(close => sub { my ($self) = @_; });

Emitted once the connection to the server closes.

error

$self->on(error => sub { my ($self, $err) = @_; });

Emitted once the stream emits an error.

message

$self->on(message => sub { my ($self, $msg) = @_; });

Emitted when a new IRC message arrives. Will dispatch to a default handler, which will again emit "err_event_name" "ctcp_event_name" and "irc_event_name" below.

Here is an example $msg:

{
  command  => "PRIVMSG",
  event    => "privmsg",
  params   => ["#convos", "hey!"],
  prefix   => "jan_henning",
  raw_line => ":jan_henning PRIVMSG #convos :hey",
}

err_event_name

Events that start with "err_" are emitted when there is an IRC response that indicates an error. See Mojo::IRC::Events for sample events.

ctcp_event_name

Events that start with "ctcp_" are emitted if the "parser" can understand CTCP messages, and there is a CTCP response.

$self->parser(Parse::IRC->new(ctcp => 1);

See Mojo::IRC::Events for sample events.

irc_event_name

Events that start with "irc_" are emitted when there is a normal IRC response. See Mojo::IRC::Events for sample events.

ATTRIBUTES

connect_timeout

$int = $self->connect_timeout;
$self = $self->connect_timeout(60);

Maximum amount of time in seconds establishing a connection may take before getting canceled, defaults to the value of the MOJO_IRC_CONNECT_TIMEOUT environment variable or 30.

ioloop

Holds an instance of Mojo::IOLoop.

local_address

$str = $self->local_address;
$self = $self->local_address("10.20.30.40");

Local address to bind to. See "local_address" in Mojo::IOLoop::Client.

name

The name of this IRC client. Defaults to "Mojo IRC".

nick

IRC nick name accessor. Default to "user".

parser

$self = $self->parser($obj);
$self = $self->parser(Parse::IRC->new(ctcp => 1));
$obj = $self->parser;

Holds a Parse::IRC object by default.

pass

Password for authentication

real_host

Will be set by "irc_rpl_welcome". Holds the actual hostname of the IRC server that we are connected to.

server

Server name and, optionally, a port to connect to. Changing this while connected to the IRC server will issue a reconnect.

server_settings

$hash = $self->server_settings;

Holds information about the server. See https://github.com/jhthorsen/mojo-irc/blob/master/t/ua-channel-users.t for example data structure.

Note that this attribute is EXPERIMENTAL and the structure of the values it holds.

user

IRC username. Defaults to current logged in user or falls back to "anonymous".

tls

$self->tls(undef) # disable (default)
$self->tls({}) # enable

Default is "undef" which disables TLS. Setting this to an empty hash will enable TLS and this module will load in default certs. It is also possible to set custom cert/key:

$self->tls({ cert => "/path/to/client.crt", key => ... })

This can be generated using

# certtool --generate-privkey --outfile client.key
# certtool --generate-self-signed --load-privkey client.key --outfile client.crt

To disable the verification of server certificates, the "insecure" option can be set:

$self->tls({insecure => 1});

METHODS

connect

$self = $self->connect(\&callback);

Will log in to the IRC "server" and call &callback. The &callback will be called once connected or if connect fails. The second argument will be an error message or empty string on success.

ctcp

$str = $self->ctcp(@str);

This message will quote CTCP messages. Example:

$self->write(PRIVMSG => nickname => $self->ctcp(TIME => time));

The code above will write this message to IRC server:

PRIVMSG nickname :\001TIME 1393006707\001

disconnect

$self->disconnect(\&callback);

Will disconnect form the server and run the callback once it is done.

new

$self = Mojo::IRC->new(%attrs);

Object constructor.

register_default_event_handlers

$self->register_default_event_handlers;

This method sets up the default "DEFAULT EVENT HANDLERS" unless someone has already subscribed to the event.

write

$self->write(@str, \&callback);

This method writes a message to the IRC server. @str will be concatenated with " " and "\r\n" will be appended. &callback is called once the message is delivered over the stream. The second argument to the callback will be an error message: Empty string on success and a description on error.

write_p

$promise = $self->write_p(@str);

Like "write", but returns a Mojo::Promise instead of taking a callback. The promise will be resolved on success, or rejected with the error message on error.

DEFAULT EVENT HANDLERS

ctcp_ping

Will respond to the sender with the difference in time.

Ping reply from $sender: 0.53 second(s)

ctcp_time

Will respond to the sender with the current localtime. Example:

TIME Fri Feb 21 18:56:50 2014

NOTE! The localtime format may change.

ctcp_version

Will respond to the sender with:

VERSION Mojo-IRC $VERSION

NOTE! Additional information may be added later on.

irc_nick

Used to update the "nick" attribute when the nick has changed.

irc_notice

Responds to the server with "QUOTE PASS ..." if the notice contains "Ident broken...QUOTE PASS...".

irc_ping

Responds to the server with "PONG ...".

irc_rpl_isupport

Used to populate "server_settings" with information about the server.

irc_rpl_welcome

Used to get the hostname of the server. Will also set up automatic PING requests to prevent timeout and update the "nick" attribute.

err_nicknameinuse

This handler will add "_" to the failed nick before trying to register again.

COPYRIGHT

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.

AUTHOR

Marcus Ramberg - [email protected]

Jan Henning Thorsen - [email protected]

mojo-irc's People

Contributors

dsteinbrunner avatar grinnz avatar hadfl avatar jhthorsen avatar kanatohodets avatar marcusramberg avatar syohex avatar zoffixznet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

mojo-irc's Issues

ssl support

just a placeholder issue for when SSL support is implemented

insecure TLS should not force default cert / key

The following code in Mojo::IRC sets a default cert / key including when using "insecure" tls. I do not think these should be forced when using this mode.

  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
  } 

One other point, because of the way this is implemented it makes it rather inconvenient to work around. It would have been better if the defaults were defined as a package scope variable rather than constants (inlined subs) or even if the "//" operator was used so an empty string could be provided.

t/ua-mode.t fails

As seen on cpan testers and I can report locally, t/ua-mode.t fails on the two user mode test attempts (4 total test failures, 2 per test attempt). I spent some time trying to debug but I couldn't figure out what was wrong.

Suggestion: Change "name" to include $0

I wonder if we should change the default "name" attribute to include the application name. Something like this:

has name => sub {
  my $app = basename $0;
  return "Mojo-IRC/$app";
};

Mojo::IRC is failing on Win32

http://www.cpantesters.org/cpan/report/c3d6464d-6bf7-1014-be1a-716fa945bbef

Output from 'C:\strawberry181\c\bin\dmake.exe test':

C:\strawberry181\perl\bin\perl.exe "-MExtUtils::Command::MM" "-e" "test_harness(0, 'blib\lib', 'blib\arch')" t/*.t
Too late to run CHECK block at C:/strawberry181/perl/site/lib/EV.pm line 123.
t/00-basic.t .............. ok
Possible attempt to put comments in qw() list at t/ctcp.t line 46.
Name "Mojo::IRC::time" used only once: possible typo at t/ctcp.t line 5.
Name "Mojo::IRC::localtime" used only once: possible typo at t/ctcp.t line 4.
# Connected
t/ctcp.t .................. ok
t/default-handlers.t ...... ok
Name "main::buf" used only once: possible typo at t/disconnect.t line 13.
Name "main::close" used only once: possible typo at t/disconnect.t line 14.
t/disconnect.t ............ ok
Mojo::Reactor::EV: Read failed: Mojo::IRC: Bad file descriptor at C:/strawberry181/perl/site/lib/Mojo/EventEmitter.pm line 17.

#   Failed test 'could still connect'
#   at t/error.t line 54.
#          got: 'close'
#     expected: 'connected'
# Looks like you failed 1 test of 4.
t/error.t ................. 
Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/4 subtests 
# read irc.perl.org
# read welcome
# read join.mojo
t/join.t .................. ok
t/nick.t .................. ok
t/offline-mode-avatar.t ... ok
t/offline-mode-err.t ...... ok
t/offline-mode-numeric.t .. ok
# PASS s4cret
# NICK fooman
# USER foo 8 * :the end
t/register.t .............. ok
t/tls.t ................... skipped: Need TEST_TLS=1

Test Summary Report
-------------------
t/error.t               (Wstat: 256 Tests: 4 Failed: 1)
  Failed test:  4
  Non-zero exit status: 1
Files=12, Tests=42, 14 wallclock secs ( 0.08 usr +  0.12 sys =  0.20 CPU)
Result: FAIL
Failed 1/12 test programs. 1/42 subtests failed.
dmake.exe:  Error code 255, while making 'test_dynamic'

Do we care @marcusramberg ..? I don't mind closing this with "wontfix" and just skip the test on Win32 until someone complains.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.