Skip to content

Instantly share code, notes, and snippets.

@walkure
Last active December 10, 2015 15:08
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 walkure/4452314 to your computer and use it in GitHub Desktop.
Save walkure/4452314 to your computer and use it in GitHub Desktop.
IRCプロキシ経由で発言するperl module
package IRCSay;
use strict;
use warnings;
use IO::Socket;
use Encode;
sub new
{
my $class = shift;
my $self = bless {}, $class;
$self->init(@_);
return $self;
}
sub init
{
my($self, %args) = @_;
$self->{host} = $args{host} || '127.0.0.1';
$self->{port} = $args{port} || 6667;
$self->{nick} = $args{nick} || 'bot';
$self->{name} = $args{name} || 'bot';
$self->{pass} = $args{pass};
$self->{charset} = $args{charset} || 'iso-2022-jp';
$self->connect();
}
sub connect
{
my $self = shift;
my $c = IO::Socket::INET->new(
PeerAddr => $self->{host},
PeerPort => $self->{port},
Proto => 'tcp',
);
print $c "PASS $self->{pass}\n" if defined $self->{pass};
print $c "NICK $self->{nick}\n";
print $c "USER $self->{nick} 1 *:$self->{name}\n";
$self->{sock} = $c;
}
sub notice
{
my $self = shift;
my ($ch,$msg) = @_;
$self->send_message('NOTICE',$ch,$msg);
}
sub privmsg
{
my $self = shift;
my ($ch,$msg) = @_;
$self->send_message('PRIVMSG',$ch,$msg);
}
sub _sock
{
my $self = shift;
return $self->connect unless defined($self->{sock});
return $self->connect unless defined($self->{sock}->connected);
$self->{sock};
}
sub send_message
{
my($self,$type,$channel,$msg) = @_;
my $e_msg = Encode::encode($self->{charset},$msg);
my $c = $self->_sock;
print $c "$type $channel : $e_msg\n";
}
1;
=pod
=head1 NAME
IRCSay send messages to IRC proxy
=head1 SYNOPSIS
my $c = IRCSay->new(
host => '127.0.0.1',
port => 16667,
charset => 'iso-2022-jp',
pass => 'password');
$c->privmsg('#foo',Encode::decode('utf-8','あああ'));
$c->notice('#bar','aaa');
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment