Skip to content

Instantly share code, notes, and snippets.

@xtetsuji
Last active December 17, 2015 07:58
Show Gist options
  • Select an option

  • Save xtetsuji/5576501 to your computer and use it in GitHub Desktop.

Select an option

Save xtetsuji/5576501 to your computer and use it in GitHub Desktop.
Using AnyEvent::IRC::Client code skeleton.
#!/usr/bin/env perl
# Writte by OGATA Tetsuji at 2013/05/14
use strict;
use warnings;
use utf8;
use AnyEvent;
use AnyEvent::IRC::Client;
use Encode;
use Getopt::Long ();
use Pod::Usage qw(pod2usage);
use constant DEBUG => $ENV{DEBUG};
use constant RECONNECT_CHECK_INTERVALS => 60;
use constant ACCEPT_INVITE => 1;
my $p = Getopt::Long::Parser->new(
config => [qw(posix_default no_ignore_case auto_help)]
);
$p->getoptions(
\my %opt,
'password=s', 'nick=s', 'real=s', 'server=s', 'port=i', 'channel=s',
'charset=s',
'ssl=s',
'help', 'usage',
);
$opt{charset} ||= 'utf-8';
$opt{port} ||= 6667;
pod2usage(0) if $opt{help} || $opt{usage};
pod2usage(1) if !$opt{server};
my $cv = AnyEvent->condvar;
my $irc = AnyEvent::IRC::Client->new();
$irc->enable_ssl() if $opt{ssl};
$irc->reg_cb(
connect => sub {
my ($irc, $error) = @_;
if ( defined $error ) {
warn "Can't connect: $error";
$cv->send();
}
},
publicmsg => sub {
my ($irc, $channel, $msg) = @_;
my $comment = decode($opt{charset}, $msg->{params}->[1]);
my ($username) = $msg->{prefix} =~ /^(,+?)!/;
my $reply = make_reply($comment);
$irc->send_chan( $channel => PRIVMSG => $channel => encode($opt{charset}, $reply) );
},
irc_invite => sub {
my ($irc, $arg) = @_;
my $channel = $arg->{params}->[1];
if ( ACCEPT_INVITE ) {
$irc->send_srv( JOIN => $channel );
}
},
disconnect => sub {
# Do you want to reconnect? See $reconnect_timer.
},
);
connect_irc($irc, \%opt);
my $reconnect_timer = AnyEvent->timer(
after => RECONNECT_CHECK_INTERVALS,
interval => RECONNECT_CHECK_INTERVALS,
cb => sub { connect_irc($irc, \%opt) if !$irc->registered(); },
);
my %sig_hook;
if ( DEBUG ) {
$sig_hook{USR1} = AnyEvent->signal(
signal => "USR1",
cb => sub {
warn $irc->registered() ? "Registered OK!\n" : "Registered NOT OK!\n";
},
);
my $disconnect_cb = sub {
warn "Give interrupt.\n";
$irc->disconnect("Give interrupt.\n");
exit;
};
$sig_hook{INT} = AnyEvent->signal( signal => "INT", cb => $disconnect_cb );
$sig_hook{HUP} = AnyEvent->signal( signal => "HUP", cb => $disconnect_cb );
}
$cv->recv();
$irc->disconnect('Bye');
exit;
sub connect_irc {
my ($irc, $data) = @_;
my $opt = {};
for my $key (qw/password nick real/) {
next if !defined $data->{$key};
$opt->{$key} = $data->{$key};
}
$irc->connect( @$data{qw/server port/}, $opt );
$irc->send_srv( JOIN => $data->{channel} ) if $data->{channel};
}
sub make_reply {
my $comment = shift;
my $reply = '';
#
# ... parse $comment and make $reply ...
#
### Sample: Parrot
$reply = qq(You say "$comment");
return $reply
}
=pod
=head1 NAME
ae-irc-skeleton - AnyEvent::IRC::Client simple/sample skeleton code.
=head1 SYNOPSIS
perl ae-irc-skeleton --nick=john --real="John Smith" \
--server="irc.example.jp" --port=6667 --password=XXXXXXX \
--charset=iso-2022-jp \
--channel="#chat"
perl ae-irc-skeleton --help
=head1 OPTIONS
=over
=item * nick
--nick=john
Specify nickname.
This word limitation is depended by IRC server.
Many IRC server allows only max 9 character length words.
=item * real
--real="John Smith"
Speicyf real name.
=item * server
--server="irc.example.jp"
Specify IRC server.
This option is required.
=item * port
--port=6667
Specify IRC server's port.
This default value is 6667.
=item * password
--password=XXXXXXXX
Specify IRC server's password.
This is optional. If you want to connect password protected IRC server,
then specify this parameter.
=item * charset
--charset=iso-2022-jp
Specify IRC server's charset.
This is optional. Default is "utf-8".
If IRC server you connect is use non-utf-8 charset,
then specify this parameter.
=item * channel
--channel="#chat"
Specify initial join channel name.
=item * ssl
--ssl=1
Specify if you have to use SSL connection.
=back
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2013 by OGATA Tetsuji
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment