File Coverage

blib/lib/Net/Bot/IRC/NumericCodes.pm
Criterion Covered Total %
statement 132 136 97.0
branch 5 8 62.5
condition n/a
subroutine 6 6 100.0
pod 3 3 100.0
total 146 153 95.4


line stmt bran cond sub pod time code
1             package Net::Bot::IRC::NumericCodes;
2              
3 2     2   130679 use strict;
  2         14  
  2         60  
4 2     2   11 use warnings;
  2         4  
  2         51  
5              
6 2     2   11 use Carp;
  2         4  
  2         2079  
7              
8             =head1 NAME
9              
10             Net::Bot::IRC::NumericCodes - A module for abstracting IRC numeric codes.
11              
12             =head1 VERSION
13              
14             Version 1.0.0
15              
16             =cut
17              
18             our $VERSION = '1.0.0';
19              
20             =head1 SYNOPSIS
21              
22             use Net::Bot::IRC::NumericCodes;
23              
24             my $nc = IRC::NumericCodes->new();
25              
26             # Lookup the numeric based on the code string.
27             if ($code == $nc->str2num("RPL_WELCOME")) {
28             # Do something.
29             }
30              
31             # Look up the code string based on the numeric code.
32             if ($strcode eq $nc->num2str(001)) {
33             # Do some other stuff.
34             }
35              
36             For a full list of codes please read L<< http://tools.ietf.org/html/rfc2812 >>.
37              
38             =head1 FUNCTIONS
39              
40             =head2 new()
41              
42             =cut
43              
44             sub new {
45 1     1 1 124 my $class = shift;
46 1         3 my $self = {};
47              
48             # Numeric codes as defined in RFC 2812
49 1         3 $self->{_messages}->{RPL_WELCOME} = 001;
50 1         2 $self->{_messages}->{RPL_YOURHOST} = 002;
51 1         3 $self->{_messages}->{RPL_CREATED} = 003;
52 1         2 $self->{_messages}->{RPL_MYINFO} = 004;
53 1         2 $self->{_messages}->{RPL_BOUNCE} = 005;
54 1         3 $self->{_messages}->{RPL_TRACELINK} = 200;
55 1         2 $self->{_messages}->{RPL_TRACECONNECTING} = 201;
56 1         3 $self->{_messages}->{RPL_TRACEHANDSHAKE} = 202;
57 1         2 $self->{_messages}->{RPL_TRACEUNKNOWN} = 203;
58 1         2 $self->{_messages}->{RPL_TRACEOPERATOR} = 204;
59 1         2 $self->{_messages}->{RPL_TRACEUSER} = 205;
60 1         2 $self->{_messages}->{RPL_TRACESERVER} = 206;
61 1         3 $self->{_messages}->{RPL_TRACESERVICE} = 207;
62 1         2 $self->{_messages}->{RPL_TRACENEWTYPE} = 208;
63 1         2 $self->{_messages}->{RPL_TRACECLASS} = 209;
64 1         3 $self->{_messages}->{RPL_TRACERECONNECT} = 210;
65 1         2 $self->{_messages}->{RPL_STATSLINKINFO} = 211;
66 1         2 $self->{_messages}->{RPL_STATSCOMMANDS} = 212;
67 1         2 $self->{_messages}->{RPL_STATSCLINE} = 213;
68 1         2 $self->{_messages}->{RPL_STATSNLINE} = 214;
69 1         2 $self->{_messages}->{RPL_STATSILINE} = 215;
70 1         2 $self->{_messages}->{RPL_STATSKLINE} = 216;
71 1         2 $self->{_messages}->{RPL_STATSQLINE} = 217;
72 1         2 $self->{_messages}->{RPL_STATSYLINE} = 218;
73 1         25 $self->{_messages}->{RPL_ENDOFSTATS} = 219;
74 1         3 $self->{_messages}->{RPL_UMODEIS} = 221;
75 1         2 $self->{_messages}->{RPL_SERVICEINFO} = 231;
76 1         2 $self->{_messages}->{RPL_ENDOFSERVICES} = 232;
77 1         2 $self->{_messages}->{RPL_SERVICE} = 233;
78 1         3 $self->{_messages}->{RPL_SERVLIST} = 234;
79 1         2 $self->{_messages}->{RPL_SERVLISTEND} = 235;
80 1         2 $self->{_messages}->{RPL_STATSVLINE} = 240;
81 1         2 $self->{_messages}->{RPL_STATSLLINE} = 241;
82 1         2 $self->{_messages}->{RPL_STATSUPTIME} = 242;
83 1         2 $self->{_messages}->{RPL_STATSOLINE} = 243;
84 1         2 $self->{_messages}->{RPL_STATSHLINE} = 244;
85 1         2 $self->{_messages}->{RPL_STATSSLINE} = 244;
86 1         2 $self->{_messages}->{RPL_STATSPING} = 246;
87 1         2 $self->{_messages}->{RPL_STATSBLINE} = 247;
88 1         1 $self->{_messages}->{RPL_STATSDLINE} = 250;
89 1         2 $self->{_messages}->{RPL_LUSERCLIENT} = 251;
90 1         3 $self->{_messages}->{RPL_LUSEROP} = 252;
91 1         8 $self->{_messages}->{RPL_LUSERUNKNOWN} = 253;
92 1         3 $self->{_messages}->{RPL_LUSERCHANNELS} = 254;
93 1         2 $self->{_messages}->{RPL_LUSERME} = 255;
94 1         2 $self->{_messages}->{RPL_ADMINME} = 256;
95 1         7 $self->{_messages}->{RPL_ADMINLOC1} = 257;
96 1         4 $self->{_messages}->{RPL_ADMINLOC2} = 258;
97 1         2 $self->{_messages}->{RPL_ADMINEMAIL} = 259;
98 1         2 $self->{_messages}->{RPL_TRACELOG} = 261;
99 1         2 $self->{_messages}->{RPL_TRACEEND} = 262;
100 1         2 $self->{_messages}->{RPL_TRYAGAIN} = 263;
101 1         2 $self->{_messages}->{RPL_NONE} = 300;
102 1         2 $self->{_messages}->{RPL_AWAY} = 301;
103 1         2 $self->{_messages}->{RPL_USERHOST} = 302;
104 1         2 $self->{_messages}->{RPL_ISON} = 303;
105 1         2 $self->{_messages}->{RPL_UNAWAY} = 305;
106 1         2 $self->{_messages}->{RPL_NOWAWAY} = 306;
107 1         2 $self->{_messages}->{RPL_WHOISUSER} = 311;
108 1         2 $self->{_messages}->{RPL_WHOISSERVER} = 312;
109 1         2 $self->{_messages}->{RPL_WHOISOPERATOR} = 313;
110 1         2 $self->{_messages}->{RPL_WHOWASUSER} = 314;
111 1         2 $self->{_messages}->{RPL_ENDOFWHO} = 315;
112 1         4 $self->{_messages}->{RPL_WHOISCHANOP} = 316;
113 1         2 $self->{_messages}->{RPL_WHOISIDLE} = 317;
114 1         1 $self->{_messages}->{RPL_ENDOFWHOIS} = 318;
115 1         3 $self->{_messages}->{RPL_WHOISCHANNELS} = 319;
116 1         2 $self->{_messages}->{RPL_LISTSTART} = 321;
117 1         8 $self->{_messages}->{RPL_LIST} = 322;
118 1         3 $self->{_messages}->{RPL_LISTEND} = 323;
119 1         2 $self->{_messages}->{RPL_CHANNELMODEIS} = 324;
120 1         2 $self->{_messages}->{RPL_UNIQOPIS} = 325;
121 1         2 $self->{_messages}->{RPL_NOTOPIC} = 331;
122 1         8 $self->{_messages}->{RPL_TOPIC} = 332;
123 1         2 $self->{_messages}->{RPL_INVITING} = 341;
124 1         2 $self->{_messages}->{RPL_SUMMONING} = 342;
125 1         2 $self->{_messages}->{RPL_INVITELIST} = 346;
126 1         3 $self->{_messages}->{RPL_ENDOFINVITELIST} = 347;
127 1         2 $self->{_messages}->{RPL_EXCEPTLIST} = 348;
128 1         2 $self->{_messages}->{RPL_ENDOFEXCEPTLIST} = 349;
129 1         2 $self->{_messages}->{RPL_VERSION} = 351;
130 1         2 $self->{_messages}->{RPL_WHOREPLY} = 352;
131 1         2 $self->{_messages}->{RPL_NAMREPLY} = 353;
132 1         3 $self->{_messages}->{RPL_KILLDONE} = 361;
133 1         1 $self->{_messages}->{RPL_CLOSING} = 362;
134 1         2 $self->{_messages}->{RPL_CLOSEEND} = 363;
135 1         2 $self->{_messages}->{RPL_LINKS} = 364;
136 1         2 $self->{_messages}->{RPL_ENDOFLINKS} = 365;
137 1         2 $self->{_messages}->{RPL_ENDOFNAMES} = 366;
138 1         2 $self->{_messages}->{RPL_BANLIST} = 367;
139 1         2 $self->{_messages}->{RPL_ENDOFBANLIST} = 368;
140 1         2 $self->{_messages}->{RPL_ENDOFWHOWAS} = 369;
141 1         2 $self->{_messages}->{RPL_INFO} = 371;
142 1         2 $self->{_messages}->{RPL_MOTD} = 372;
143 1         2 $self->{_messages}->{RPL_INFOSTART} = 373;
144 1         2 $self->{_messages}->{RPL_ENDOFINFO} = 374;
145 1         2 $self->{_messages}->{RPL_MOTDSTART} = 375;
146 1         2 $self->{_messages}->{RPL_ENDOFMOTD} = 376;
147 1         2 $self->{_messages}->{RPL_YOUREOPER} = 381;
148 1         2 $self->{_messages}->{RPL_REHASHING} = 382;
149 1         2 $self->{_messages}->{RPL_YOURESERVICE} = 383;
150 1         2 $self->{_messages}->{RPL_MYPORTIS} = 384;
151 1         2 $self->{_messages}->{RPL_TIME} = 391;
152 1         8 $self->{_messages}->{RPL_USERSSTART} = 392;
153 1         3 $self->{_messages}->{RPL_USERS} = 393;
154 1         2 $self->{_messages}->{RPL_ENDOFUSERS} = 394;
155 1         2 $self->{_messages}->{RPL_NOUSERS} = 395;
156              
157 1         2 bless $self, $class;
158 1         4 return $self;
159             }
160              
161             =head2 num2str($numericCode)
162              
163             =cut
164              
165             sub num2str {
166 212     212 1 30085 my $self = shift;
167 212         352 my $num = shift;
168              
169 212 50       406 if (! $num) {
170 0         0 croak "No number was passed as an argument.";
171             }
172              
173             # Iterate through and try to find the corresponding string.
174 212         298 foreach my $str (keys(%{ $self->{_messages} })) {
  212         2073  
175 11416 100       20517 if ($self->{_messages}->{$str} == $num) {
176 212         1065 return $str;
177             }
178             }
179              
180             # If we get here then we couldn't resolve the numeric code.
181 0         0 croak "Unable to resolve numeric: $num.";
182             }
183              
184             =head2 str2num($strCode)
185              
186             =cut
187              
188             sub str2num {
189 212     212 1 29241 my $self = shift;
190 212         319 my $str = shift;
191              
192 212 50       407 if (! $str) {
193 0         0 croak "No string was passed as an argument.";
194             }
195              
196 212 50       420 if (exists $self->{_messages}->{$str}) {
197 212         774 return $self->{_messages}->{$str};
198             }
199             else {
200 0           croak "Unable to resolve the numeric code for \"$str\".";
201             }
202             }
203              
204             =head1 AUTHOR
205              
206             Caudill, Mark, L<< mailto:mcaudillATcpan.org >>
207              
208             =head1 BUGS
209              
210             Please report any bugs or feature requests to the maintainer.
211              
212              
213             =head1 SUPPORT
214              
215             You can find documentation for this module with the perldoc command.
216              
217             perldoc Net::Bot::IRC::NumericCodes
218              
219             =head1 DEVELOPMENT
220              
221             You can find the current sources for this at L<< git://github.com/markcaudill/Net-Bot-IRC-NumericCodes.git >>.
222              
223             =head1 COPYRIGHT & LICENSE
224              
225             Copyright 2023 Caudill, Mark.
226              
227             This program is free software; you can redistribute it and/or modify it
228             under the terms of either: the GNU General Public License as published
229             by the Free Software Foundation; or the Artistic License.
230              
231             See http://dev.perl.org/licenses/ for more information.
232              
233              
234             =cut
235              
236             1; # End of Net::Bot::IRC::NumericCodes.