1 | package Net::IRC3; |
1 | package Net::IRC3; |
2 | use strict; |
2 | use strict; |
3 | use AnyEvent; |
3 | use AnyEvent; |
4 | use IO::Socket::INET; |
4 | use IO::Socket::INET; |
5 | |
|
|
6 | use Exporter; |
5 | use Exporter; |
7 | our @ISA = qw/Exporter/; |
6 | our @ISA = qw/Exporter/; |
8 | our @EXPORT_OK = |
7 | our @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 | |
12 | require Net::IRC3::Connection; |
11 | our $ConnectionClass = 'Net::IRC3::Connection'; |
13 | |
12 | |
14 | =head1 NAME |
13 | =head1 NAME |
15 | |
14 | |
16 | Net::IRC3 - An IRC Protocol module which is event system independend |
15 | Net::IRC3 - An IRC Protocol module which is event system independend |
17 | |
16 | |
18 | =head1 VERSION |
17 | =head1 VERSION |
19 | |
18 | |
20 | Version 0.01 |
19 | Version 0.2 |
21 | |
20 | |
22 | =cut |
21 | =cut |
23 | |
22 | |
24 | our $VERSION = '0.1'; |
23 | our $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 | |
71 | Tries to open a socket to the host C<$host> and the port C<$port>. |
70 | Tries to open a socket to the host C<$host> and the port C<$port>. |
72 | If successfull it will return a L<Net::IRC3::Connection> object. |
71 | If successfull it will return a L<Net::IRC3::Connection> object. |
73 | If an error occured it will die (use eval to catch the exception). |
72 | If an error occured it will die (use eval to catch the exception). |
74 | |
73 | |
75 | =cut |
74 | =cut |
76 | |
75 | |
77 | sub connect_server { |
76 | sub 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 | |
|
|
116 | Returns a key value list, where the key is C<"$host:$port"> and the |
|
|
117 | value is the connection object. Only 'active' connections are returned. |
|
|
118 | That means, if a connection is terminated somehow, it will also disappear |
|
|
119 | from this list. |
|
|
120 | |
|
|
121 | =cut |
|
|
122 | |
|
|
123 | sub connections { |
|
|
124 | my ($self) = @_; |
|
|
125 | return %{$self->{connections}}; |
|
|
126 | } |
|
|
127 | |
|
|
128 | =item B<connection ($host, $port)> or B<connection ("$host:$port")> |
|
|
129 | |
|
|
130 | Returns the L<Net::IRC3::Connection> object for the C<$host> C<$port> |
|
|
131 | pair. If no such connection exists, undef is returned. |
|
|
132 | |
|
|
133 | =cut |
|
|
134 | |
|
|
135 | sub 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 |