ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-IRC3/lib/Net/IRC3.pm
(Generate patch)

Comparing Net-IRC3/lib/Net/IRC3.pm (file contents):
Revision 1.2 by elmex, Sun Jul 16 02:41:37 2006 UTC vs.
Revision 1.3 by elmex, Sun Jul 16 11:11:17 2006 UTC

1package Net::IRC3; 1package Net::IRC3;
2use strict; 2use strict;
3use AnyEvent; 3use AnyEvent;
4use IO::Socket::INET; 4use IO::Socket::INET;
5
6use Exporter; 5use Exporter;
7our @ISA = qw/Exporter/; 6our @ISA = qw/Exporter/;
8our @EXPORT_OK = 7our @EXPORT_OK =
9 qw(mk_msg parse_irc_msg split_prefix prefix_nick 8 qw(mk_msg parse_irc_msg split_prefix prefix_nick
10 prefix_user prefix_host); 9 prefix_user prefix_host);
11 10
12require Net::IRC3::Connection; 11our $ConnectionClass = 'Net::IRC3::Connection';
13 12
14=head1 NAME 13=head1 NAME
15 14
16Net::IRC3 - An IRC Protocol module which is event system independend 15Net::IRC3 - An IRC Protocol module which is event system independend
17 16
18=head1 VERSION 17=head1 VERSION
19 18
20Version 0.01 19Version 0.2
21 20
22=cut 21=cut
23 22
24our $VERSION = '0.1'; 23our $VERSION = '0.2';
25 24
26=head1 SYNOPSIS 25=head1 SYNOPSIS
27 26
28 use Net::IRC3; 27 use Net::IRC3;
29 28
30 my $irc3 = new Net::IRC3; 29 my $irc3 = new Net::IRC3;
31 30
32 my $con = $irc3->connect_server ("test.not.at.irc.net", 6667); 31 my $con = $irc3->connect ("test.not.at.irc.net", 6667);
33 32
34 ... 33 ...
35 34
36=head1 DESCRIPTION 35=head1 DESCRIPTION
37 36
64 bless $self, $class; 63 bless $self, $class;
65 64
66 return $self; 65 return $self;
67} 66}
68 67
69=item B<connect_server ($host, $port)> 68=item B<connect ($host, $port)>
70 69
71Tries to open a socket to the host C<$host> and the port C<$port>. 70Tries to open a socket to the host C<$host> and the port C<$port>.
72If successfull it will return a L<Net::IRC3::Connection> object. 71If successfull it will return a L<Net::IRC3::Connection> object.
73If an error occured it will die (use eval to catch the exception). 72If an error occured it will die (use eval to catch the exception).
74 73
75=cut 74=cut
76 75
77sub connect_server { 76sub connect {
78 my ($self, $host, $port) = @_; 77 my ($self, $host, $port) = @_;
79 78
80 defined $self->{connections}->{"$host:$port"} 79 defined $self->{connections}->{"$host:$port"}
81 and return; 80 and return;
82 81
85 PeerPort => $port, 84 PeerPort => $port,
86 Proto => 'tcp', 85 Proto => 'tcp',
87 Blocking => 0 86 Blocking => 0
88 ) or die "couldn't connect to irc server '$host:$port': $!\n";; 87 ) or die "couldn't connect to irc server '$host:$port': $!\n";;
89 88
89 eval "require $ConnectionClass";
90 my $con = Net::IRC3::Connection->new ($self, $sock, $host, $port); 90 my $con = $ConnectionClass->new ($self, $sock, $host, $port);
91 91
92 $con->{rw} = 92 $con->{rw} =
93 AnyEvent->io (poll => 'r', fh => $sock, cb => sub { 93 AnyEvent->io (poll => 'r', fh => $sock, cb => sub {
94 my $l = sysread $sock, my $data, 1024; 94 my $l = sysread $sock, my $data, 1024;
95 95
96 $con->feed_irc_data ($data); 96 $con->feed_irc_data ($data);
97 97
98 unless ($l) { 98 unless ($l) {
99 99
100 if (defined $l) { 100 if (defined $l) {
101 $con->disconnect_server ("EOF from IRC server '$host:$port'"); 101 $con->disconnect ("EOF from IRC server '$host:$port'");
102 return; 102 return;
103 103
104 } else { 104 } else {
105 $con->disconnect_server ("Error while reading from IRC server '$host:$port': $!"); 105 $con->disconnect ("Error while reading from IRC server '$host:$port': $!");
106 return; 106 return;
107 } 107 }
108 } 108 }
109 }); 109 });
110 110
111 return $con; 111 return $con;
112}
113
114=item B<connections ()>
115
116Returns a key value list, where the key is C<"$host:$port"> and the
117value is the connection object. Only 'active' connections are returned.
118That means, if a connection is terminated somehow, it will also disappear
119from this list.
120
121=cut
122
123sub connections {
124 my ($self) = @_;
125 return %{$self->{connections}};
126}
127
128=item B<connection ($host, $port)> or B<connection ("$host:$port")>
129
130Returns the L<Net::IRC3::Connection> object for the C<$host> C<$port>
131pair. If no such connection exists, undef is returned.
132
133=cut
134
135sub connection {
136 my ($self, $host, $port) = @_;
137 if ($host =~ m/^[^:]+:(\d+)$/) {
138 return $self->{connections}->{$host}
139 } else {
140 return $self->{connections}->{$host.':'.$port}
141 }
112} 142}
113 143
114=back 144=back
115 145
116=head1 FUNCTIONS 146=head1 FUNCTIONS

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines