… | |
… | |
17 | |
17 | |
18 | our $VERSION = '0.21'; |
18 | our $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 | |
32 | L<Net::IRC3> itself is a simple building block for an IRC client. |
31 | L<Net::IRC3> itself is a simple building block for an IRC client. |
33 | It manages connections and parses and constructs IRC messages. |
32 | It manages connections and parses and constructs IRC messages. |
… | |
… | |
38 | the L<Net::IRC3::Client> page instead. |
37 | the L<Net::IRC3::Client> page instead. |
39 | |
38 | |
40 | Note that this module uses AnyEvent as it's IO event subsystem. |
39 | Note that this module uses AnyEvent as it's IO event subsystem. |
41 | You can integrate Net::IRC3 into any application with a event system |
40 | You can integrate Net::IRC3 into any application with a event system |
42 | that AnyEvent has support for (eg. L<Gtk2> or L<Event>). |
41 | that AnyEvent has support for (eg. L<Gtk2> or L<Event>). |
43 | |
|
|
44 | =head1 METHODS |
|
|
45 | |
|
|
46 | =over 4 |
|
|
47 | |
|
|
48 | =item B<new ()> |
|
|
49 | |
|
|
50 | This just creates a L<Net::IRC3> object, which is a management |
|
|
51 | class for creating and managing connections. |
|
|
52 | |
|
|
53 | =cut |
|
|
54 | |
|
|
55 | sub 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 | |
|
|
69 | Tries to open a socket to the host C<$host> and the port C<$port>. |
|
|
70 | If successfull it will return a L<Net::IRC3::Connection> object. |
|
|
71 | If an error occured it will die (use eval to catch the exception). |
|
|
72 | |
|
|
73 | =cut |
|
|
74 | |
|
|
75 | sub 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 | |
|
|
115 | Returns a key value list, where the key is C<"$host:$port"> and the |
|
|
116 | value is the connection object. Only 'active' connections are returned. |
|
|
117 | That means, if a connection is terminated somehow, it will also disappear |
|
|
118 | from this list. |
|
|
119 | |
|
|
120 | =cut |
|
|
121 | |
|
|
122 | sub connections { |
|
|
123 | my ($self) = @_; |
|
|
124 | return %{$self->{connections}}; |
|
|
125 | } |
|
|
126 | |
|
|
127 | =item B<connection ($host, $port)> or B<connection ("$host:$port")> |
|
|
128 | |
|
|
129 | Returns the L<Net::IRC3::Connection> object for the C<$host> C<$port> |
|
|
130 | pair. If no such connection exists, undef is returned. |
|
|
131 | |
|
|
132 | =cut |
|
|
133 | |
|
|
134 | sub 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 | |
147 | See the samples/ directory for some examples on how to use Net::IRC3. |
45 | See the samples/ directory for some examples on how to use Net::IRC3. |
148 | |
46 | |