File: README

package info (click to toggle)
libirc-utils-perl 0.12-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch, wheezy
  • size: 188 kB
  • ctags: 42
  • sloc: perl: 494; makefile: 2
file content (331 lines) | stat: -rw-r--r-- 11,947 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
NAME
    IRC::Utils - Common utilities for IRC-related tasks

SYNOPSIS
     use strict;
     use warnings;

     use IRC::Utils ':ALL';

     my $nickname = '^Lame|BOT[moo]';
     my $uppercase_nick = uc_irc($nickname);
     my $lowercase_nick = lc_irc($nickname);

     print "They're equivalent\n" if eq_irc($uppercase_nick, $lowercase_nick);

     my $mode_line = 'ov+b-i Bob sue stalin*!*@*';
     my $hashref = parse_mode_line($mode_line);

     my $banmask = 'stalin*';
     my $full_banmask = normalize_mask($banmask);

     if (matches_mask($full_banmask, 'stalin!joe@kremlin.ru')) {
         print "EEK!";
     }

     my $decoded = irc_decode($raw_irc_message);
     print $decoded, "\n";

     if (has_color($message)) {
        print 'COLOR CODE ALERT!\n";
     }

     my $results_hashref = matches_mask_array(\@masks, \@items_to_match_against);

     my $nick = parse_user('stalin!joe@kremlin.ru');
     my ($nick, $user, $host) = parse_user('stalin!joe@kremlin.ru');

DESCRIPTION
    The functions in this module take care of many of the tasks you are
    faced with when working with IRC. Mode lines, ban masks, message
    encoding and formatting, etc.

FUNCTIONS
  "uc_irc"
    Takes one mandatory parameter, a string to convert to IRC uppercase, and
    one optional parameter, the casemapping of the ircd (which can be
    'rfc1459', 'strict-rfc1459' or 'ascii'. Default is 'rfc1459'). Returns
    the IRC uppercase equivalent of the passed string.

  "lc_irc"
    Takes one mandatory parameter, a string to convert to IRC lowercase, and
    one optional parameter, the casemapping of the ircd (which can be
    'rfc1459', 'strict-rfc1459' or 'ascii'. Default is 'rfc1459'). Returns
    the IRC lowercase equivalent of the passed string.

  "eq_irc"
    Takes two mandatory parameters, IRC strings (channels or nicknames) to
    compare. A third, optional parameter specifies the casemapping. Returns
    true if the two strings are equivalent, false otherwise

     # long version
     lc_irc($one, $map) eq lc_irc($two, $map)

     # short version
     eq_irc($one, $two, $map)

  "parse_mode_line"
    Takes a list representing an IRC mode line. Returns a hashref.
    Optionally you can also supply an arrayref and a hashref to specify
    valid channel modes (default: "[qw(beI k l imnpstaqr)]") and status
    modes (default: "{o => '@', h => '%', v => '+'}"), respectively.

    If the modeline couldn't be parsed the hashref will be empty. On success
    the following keys will be available in the hashref:

    'modes', an arrayref of normalised modes;

    'args', an arrayref of applicable arguments to the modes;

    Example:

     my $hashref = parse_mode_line( 'ov+b-i', 'Bob', 'sue', 'stalin*!*@*' );

     # $hashref will be:
     {
        modes => [ '+o', '+v', '+b', '-i' ],
        args  => [ 'Bob', 'sue', 'stalin*!*@*' ],
     }

  "normalize_mask"
    Takes one parameter, a string representing an IRC mask. Returns a
    normalised full mask.

    Example:

     $fullbanmask = normalize_mask( 'stalin*' );

     # $fullbanmask will be: 'stalin*!*@*';

  "matches_mask"
    Takes two parameters, a string representing an IRC mask and something to
    match against the IRC mask, such as a nick!user@hostname string. Returns
    a true value if they match, a false value otherwise. Optionally, one may
    pass the casemapping (see "uc_irc"), as this function uses "uc_irc"
    internally.

  "matches_mask_array"
    Takes two array references, the first being a list of strings
    representing IRC masks, the second a list of somethings to test against
    the masks. Returns an empty hashref if there are no matches. Otherwise,
    the keys will be the masks matched, each value being an arrayref of the
    strings that matched it. Optionally, one may pass the casemapping (see
    "uc_irc"), as this function uses "uc_irc" internally.

  "unparse_mode_line"
    Takes one argument, a string representing a number of mode changes.
    Returns a condensed version of the changes.

      my $mode_line = unparse_mode_line('+o+o+o-v+v');
      $mode_line is now '+ooo-v+v'

  "gen_mode_change"
    Takes two arguments, strings representing a set of IRC user modes before
    and after a change. Returns a string representing what changed.

      my $mode_change = gen_mode_change('abcde', 'befmZ');
      $mode_change is now '-acd+fmZ'

  "parse_user"
    Takes one parameter, a string representing a user in the form
    nick!user@hostname. In a scalar context it returns just the nickname. In
    a list context it returns a list consisting of the nick, user and
    hostname, respectively.

  "is_valid_chan_name"
    Takes one argument, a channel name to validate. Returns true or false if
    the channel name is valid or not. You can supply a second argument, an
    array of characters of allowed channel prefixes. Defaults to "['#',
    '&']".

  "is_valid_nick_name"
    Takes one argument, a nickname to validate. Returns true or false if the
    nickname is valid or not.

  "numeric_to_name"
    Takes an IRC server numerical reply code (e.g. '001') as an argument,
    and returns the corresponding name (e.g. 'RPL_WELCOME').

  "name_to_numeric"
    Takes an IRC server reply name (e.g. 'RPL_WELCOME') as an argument, and
    returns the corresponding numerical code (e.g. '001').

  "has_color"
    Takes one parameter, a string of IRC text. Returns true if it contains
    any IRC color codes, false otherwise. Useful if you want your bot to
    kick users for (ab)using colors. :)

  "has_formatting"
    Takes one parameter, a string of IRC text. Returns true if it contains
    any IRC formatting codes, false otherwise.

  "strip_color"
    Takes one parameter, a string of IRC text. Returns the string stripped
    of all IRC color codes.

  "strip_formatting"
    Takes one parameter, a string of IRC text. Returns the string stripped
    of all IRC formatting codes.

  "decode_irc"
    This function takes a byte string (i.e. an unmodified IRC message) and
    returns a text string. Since the source encoding might have been UTF-8,
    you should store it with UTF-8 or some other Unicode encoding in your
    file/database/whatever to be safe. For a more detailed discussion, see
    "ENCODING".

     use IRC::Utils qw(decode_irc);

     sub message_handler {
         my ($nick, $channel, $message) = @_;

         # not wise, $message is a byte string of unkown encoding
         print $message, "\n";

         $message = decode_irc($what);

         # good, $message is a text string
         print $message, "\n";
     }

CONSTANTS
    Use the following constants to add formatting and mIRC color codes to
    IRC messages.

    Normal text:

     NORMAL

    Formatting:

     BOLD
     UNDERLINE
     REVERSE
     ITALIC
     FIXED

    Colors:

     WHITE
     BLACK
     BLUE
     GREEN
     RED
     BROWN
     PURPLE
     ORANGE
     YELLOW
     LIGHT_GREEN
     TEAL
     LIGHT_CYAN
     LIGHT_BLUE
     PINK
     GREY
     LIGHT_GREY

    Individual non-color formatting codes can be cancelled with their
    corresponding constant, but you can also cancel all of them at once with
    "NORMAL". To cancel the effect of color codes, you must use "NORMAL".
    which of course has the side effect of cancelling all other formatting
    codes as well.

     $msg = 'This word is '.YELLOW.'yellow'.NORMAL.' while this word is'.BOLD.'bold'.BOLD;
     $msg = UNDERLINE.BOLD.'This sentence is both underlined and bold.'.NORMAL;

ENCODING
  Messages
    The only encoding requirement the IRC protocol places on its messages is
    that they be 8-bits and ASCII-compatible. This has resulted in most of
    the Western world settling on ASCII-compatible Latin-1 (usually
    Microsoft's CP1252, a Latin-1 variant) as a convention. Recently,
    popular IRC clients (mIRC, xchat, certain irssi configurations) have
    begun sending a mixture of CP1252 and UTF-8 over the wire to allow more
    characters without breaking backward compatibility (too much). They send
    CP1252 encoded messages if the characters fit within that encoding,
    otherwise falling back to UTF-8, and likewise autodetecting the encoding
    (UTF-8 or CP1252) of incoming messages. Since writing text with mixed
    encoding to a file, terminal, or database is not a good idea, you need a
    way to decode messages from IRC. "decode_irc" will do that.

  Channel names
    The matter is complicated further by the fact that some servers allow
    non-ASCII characters in channel names. IRC modules generally don't
    explicitly encode or decode any IRC traffic, but they do have to
    concatenate parts of a message (e.g. a channel name and a message)
    before sending it over the wire. So when you do something like
    "privmsg($channel, 'æði')", where $channel is the unmodified channel
    name (a byte string) you got from an earlier IRC message, the channel
    name will get double-encoded when concatenated with your message (a
    non-ASCII text string) if the channel name contains non-ASCII bytes.

    To prevent this, you can't simply decode the channel name and then use
    it. '#æði' in CP1252 is not the same channel as '#æði' in UTF-8, since
    they are encoded as different sequences of bytes, and the IRC server
    only cares about the byte representation. Therefore, when using a
    channel name you got from the server (e.g. when replying to message),
    you should use the original byte string (before it has been decoded with
    "decode_irc"), and encode any other parameters (with "encode_utf8") so
    that your message will be concatenated correctly. At some point, you'll
    probably want to print the channel name, write it to a log file or use
    it in a filename, so you'll eventually have to decode it, at which point
    the UTF-8 "#æði" and CP1252 "#æði" will have to be considered
    equivalent.

     use Encode qw(encode_utf8 encode);

     sub message_handler {
         # these three are all byte strings
         my ($nick, $channel, $message) = @_;

         # bad: if $channel has any non-ASCII bytes, they will get double-encoded
         privmsg($channel, 'æði');

         # bad: if $message has any non-ASCII bytes, they will get double-encoded
         privmsg('#æði', $message);

         # good: both are byte strings already, so they will concatenate correctly
         privmsg($channel, $message);

         # good: both are text strings (Latin1 as per Perl's default), so
         # they'll be concatenated correctly
         privmsg('#æði', 'æði');

         # good: similar to the last one, except now they're using UTF-8, which
         # means that the channel is actually not the same as above
         use utf8;
         privmsg('#æði', 'æði');

         # good: $channel and $msg_bytes are both byte strings
         my $msg_bytes = encode_utf8('æði');
         privmsg($channel, $msg_bytes);

         # good: $chan_bytes and $message are both byte strings
         # here we're sending a message to the utf8-encoded #æði
         my $utf8_bytes = encode_utf8('#æði');
         privmsg($utf8_bytes, $message);

         # good: $chan_bytes and $message are both byte strings
         # here we're sending a message to the cp1252-encoded #æði
         my $cp1252_bytes = encode('cp1252', '#æði');
         privmsg($cp1252_bytes, $message);

         # bad: $channel is in an undetermined encoding
         log_message("Got message from $channel");

         # good: using the decoded version of $channel
         log_message("Got message from ".decode_irc($channel));
     }

    See also Encode, perluniintro, perlunitut, perlunicode, and perlunifaq.

AUTHOR
    Hinrik Örn Sigurðsson <hinrik.sig@gmail.com> ("Hinrik" irc.perl.org, or
    "literal" @ FreeNode).

    Chris "BinGOs" Williams <chris@bingosnet.co.uk>

SEE ALSO
    POE::Component::IRC

    POE::Component::Server::IRC