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.6 by elmex, Sun Jul 16 19:44:50 2006 UTC vs.
Revision 1.7 by elmex, Mon Jul 17 15:09:20 2006 UTC

17 17
18our $VERSION = '0.21'; 18our $VERSION = '0.21';
19 19
20=head1 SYNOPSIS 20=head1 SYNOPSIS
21 21
22 use Net::IRC3; 22 use Net::IRC3::Connection;
23 23
24 my $irc3 = new Net::IRC3; 24 my $con = new Net::IRC3::Connection;
25 25
26 my $con = $irc3->connect ("test.not.at.irc.net", 6667); 26 $con->connect ("test.not.at.irc.net", 6667);
27 27
28 ...
29 28
30=head1 DESCRIPTION 29=head1 DESCRIPTION
31 30
32L<Net::IRC3> itself is a simple building block for an IRC client. 31L<Net::IRC3> itself is a simple building block for an IRC client.
33It manages connections and parses and constructs IRC messages. 32It manages connections and parses and constructs IRC messages.
38the L<Net::IRC3::Client> page instead. 37the L<Net::IRC3::Client> page instead.
39 38
40Note that this module uses AnyEvent as it's IO event subsystem. 39Note that this module uses AnyEvent as it's IO event subsystem.
41You can integrate Net::IRC3 into any application with a event system 40You can integrate Net::IRC3 into any application with a event system
42that AnyEvent has support for (eg. L<Gtk2> or L<Event>). 41that AnyEvent has support for (eg. L<Gtk2> or L<Event>).
43
44=head1 METHODS
45
46=over 4
47
48=item B<new ()>
49
50This just creates a L<Net::IRC3> object, which is a management
51class for creating and managing connections.
52
53=cut
54
55sub new
56{
57 my $this = shift;
58 my $class = ref($this) || $this;
59
60 my $self = { };
61
62 bless $self, $class;
63
64 return $self;
65}
66
67=item B<connect ($host, $port)>
68
69Tries to open a socket to the host C<$host> and the port C<$port>.
70If successfull it will return a L<Net::IRC3::Connection> object.
71If an error occured it will die (use eval to catch the exception).
72
73=cut
74
75sub connect {
76 my ($self, $host, $port) = @_;
77
78 defined $self->{connections}->{"$host:$port"}
79 and return;
80
81 my $sock = IO::Socket::INET->new (
82 PeerAddr => $host,
83 PeerPort => $port,
84 Proto => 'tcp',
85 Blocking => 0
86 ) or die "couldn't connect to irc server '$host:$port': $!\n";;
87
88 eval "require $ConnectionClass";
89 my $con = $ConnectionClass->new ($self, $sock, $host, $port);
90
91 $con->{rw} =
92 AnyEvent->io (poll => 'r', fh => $sock, cb => sub {
93 my $l = sysread $sock, my $data, 1024;
94
95 $con->feed_irc_data ($data);
96
97 unless ($l) {
98
99 if (defined $l) {
100 $con->disconnect ("EOF from IRC server '$host:$port'");
101 return;
102
103 } else {
104 $con->disconnect ("Error while reading from IRC server '$host:$port': $!");
105 return;
106 }
107 }
108 });
109
110 return $con;
111}
112
113=item B<connections ()>
114
115Returns a key value list, where the key is C<"$host:$port"> and the
116value is the connection object. Only 'active' connections are returned.
117That means, if a connection is terminated somehow, it will also disappear
118from this list.
119
120=cut
121
122sub connections {
123 my ($self) = @_;
124 return %{$self->{connections}};
125}
126
127=item B<connection ($host, $port)> or B<connection ("$host:$port")>
128
129Returns the L<Net::IRC3::Connection> object for the C<$host> C<$port>
130pair. If no such connection exists, undef is returned.
131
132=cut
133
134sub connection {
135 my ($self, $host, $port) = @_;
136 if ($host =~ m/^[^:]+:(\d+)$/) {
137 return $self->{connections}->{$host}
138 } else {
139 return $self->{connections}->{$host.':'.$port}
140 }
141}
142
143=back
144 42
145=head1 EXAMPLES 43=head1 EXAMPLES
146 44
147See the samples/ directory for some examples on how to use Net::IRC3. 45See the samples/ directory for some examples on how to use Net::IRC3.
148 46

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines