aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: 75fd4ce4257e01183f932089b6bf9104b289e667 (plain)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
[![Build Status](https://travis-ci.org/zoffixznet/perl6-IRC-Client.svg)](https://travis-ci.org/zoffixznet/perl6-IRC-Client)

# NAME

IRC::Client - Extendable Internet Relay Chat client

# SYNOPSIS

## Client script

```perl6
    use IRC::Client;
    use IRC::Client::Plugin::Debugger;

    IRC::Client.new(
        :host('localhost'),
        :debug,
        plugins => [ IRC::Client::Plugin::Debugger.new ]
    ).run;
```

## Custom plugins

### Basic response to an IRC command:

The plugin chain handling the message will stop after this plugin.

```perl6
unit class IRC::Client::Plugin::PingPong is IRC::Client::Plugin;
method irc-ping ($irc, $msg) { $irc.ssay("PONG {$irc.nick} $msg<params>[0]") }
```

### More involved handling

On startup, start sending message `I'm an annoying bot` to all channels
every five seconds. We also subscribe to all events and print some debugging
info. By returning a special constant, we tell other plugins to continue
processing the data.

```perl6
use IRC::Client::Plugin; # import constants
unit class IRC::Client::Plugin::Debugger is IRC::Client::Plugin;

method register($irc) {
    Supply.interval( 5, 5 ).tap({
        $irc.privmsg($_, "I'm an annoying bot!")
            for $irc.channels;
    })
}

method all-events ($irc, $e) {
    say "We've got a private message"
        if $e<command> eq 'PRIVMSG' and $e<params>[0] eq $irc.nick;

    # Store arbitrary data in the `pipe` for other plugins to use
    $e<pipe><respond-to-notice> = True
        if $e<command> eq 'PRIVMSG';

    say $e, :indent(4);
    return IRC_NOT_HANDLED;
}

```

# TABLE OF CONTENTS
- [NAME](#name)
- [SYNOPSIS](#synopsis)
    - [Client script](#client-script)
    - [Custom plugins](#custom-plugins)
        - [Basic response to an IRC command:](#basic-response-to-an-irc-command)
        - [More involved handling](#more-involved-handling)
- [TABLE OF CONTENTS](#table-of-contents)
- [DESCRIPTION](#description)
- [BLEED BRANCH](#bleed-branch)
- [METHODS](#methods)
    - [`new`](#new)
        - [`debug`](#debug)
        - [`host`](#host)
        - [`port`](#port)
        - [`nick`](#nick)
        - [`username`](#username)
        - [`userhost`](#userhost)
        - [`userreal`](#userreal)
        - [`channels`](#channels)
        - [`plugins`](#plugins)
        - [`plugins-essential`](#plugins-essential)
    - [`run`](#run)
- [METHODS FOR PLUGINS](#methods-for-plugins)
    - [`.ssay`](#ssay)
    - [`.privmsg`](#privmsg)
- [INCLUDED PLUGINS](#included-plugins)
    - [IRC::Client::Plugin::Debugger](#ircclientplugindebugger)
    - [IRC::Client::Plugin::PingPong](#ircclientpluginpingpong)
- [EXTENDING IRC::Client / WRITING YOUR OWN PLUGINS](#extending-ircclient--writing-your-own-plugins)
    - [Overview of the plugin system](#overview-of-the-plugin-system)
    - [Return value constants](#return-value-constants)
        - [`IRC_HANDLED`](#irc_handled)
        - [`IRC_NOT_HANDLED`](#irc_not_handled)
    - [Subscribing to IRC events](#subscribing-to-irc-events)
        - [Standard IRC commands](#standard-irc-commands)
    - [Special Events](#special-events)
        - [`irc-start-up`](#irc-start-up)
        - [`irc-connected`](#irc-connected)
        - [`irc-all-events`](#irc-all-events)
        - [`irc-privmsg-me`](#irc-privmsg-me)
        - [`irc-notice-me`](#irc-notice-me)
        - [`irc-unhandled`](#irc-unhandled)
    - [Contents of the parsed IRC message](#contents-of-the-parsed-irc-message)
        - [`command`](#command)
        - [`params`](#params)
        - [`pipe`](#pipe)
        - [`who`](#who)
- [REPOSITORY](#repository)
- [BUGS](#bugs)
- [AUTHOR](#author)
- [LICENSE](#license)

# DESCRIPTION

***Note: this is a preview dev release. Things might change and new things
might get added rapidly. The first stable version is currently planned
to appear by January 3, 2016***

This modules lets you create
[IRC clients](https://en.wikipedia.org/wiki/Internet_Relay_Chat)
in Perl 6. The plugin system lets you work on the behaviour, without worrying
about IRC layer.

# BLEED BRANCH

The master branch of this repository contains the latest working version
of the module. To get the bleeding-edge version, you can install from the
[bleed branch](https://github.com/zoffixznet/perl6-IRC-Client/tree/bleed), but
that code is not always guaranteed to be in working order.

# METHODS

## `new`

```perl6
my $irc = IRC::Client.new;
```

```perl6
# Defaults are shown
my $irc = IRC::Client.new(
    debug             => False,
    host              => 'localhost',
    port              => 6667,
    nick              => 'Perl6IRC',
    username          => 'Perl6IRC',
    userhost          => 'localhost',
    userreal          => 'Perl6 IRC Client',
    channels          => ['#perl6bot'],
    plugins           => [],
    plugins-essential => [ IRC::Client::Plugin::PingPong.new ],
);
```

Creates and returns a new `IRC::Client` objects. All arguments are optional
and are as follows:

### `debug`

```perl6
    debug => True,
```
Takes `True` and `False` values. When set to `True`, debugging information
will be printed by the modules on the STDOUT. **Defaults to:** `False`

### `host`

```perl6
    host => 'irc.freenode.net',
```
Specifies the hostname of the IRC server to connect to. **Defaults to:**
`localhost`

### `port`

```perl6
    port => 7000,
```
Specifies the port of the IRC server to connect to. **Defaults to:** `6667`

### `nick`

```perl6
    nick => 'Perl6IRC',
```
Specifies the nick for the client to use. **Defaults to:** `Perl6IRC`

### `username`

```perl6
    username => 'Perl6IRC',
```
Specifies the username for the client to user. **Defaults to:** `Perl6IRC`

### `userhost`

```perl6
    userhost => 'localhost',
```
Specifies the hostname for the client to use when sending messages.
**Defaults to:** `localhost` (Note: it's probably safe to leave this at
default. Currently, this attribute is fluid and might be changed or
removed in the future).

### `userreal`

```perl6
    userreal => 'Perl6 IRC Client',
```
Specifies the "real name" of the client. **Defaults to:** `Perl6 IRC Client`

### `channels`

```perl6
    channels => ['#perl6bot'],
```
Takes an array of channels for the client to join. **Defaults to:**
`['#perl6bot']`

### `plugins`

```perl6
    plugins => [ IRC::Client::Plugin::Debug.new ],
```
Takes an array of IRC::Client Plugin objects. To run while the client is
connected.

### `plugins-essential`

```perl6
    plugins-essential => [ IRC::Client::Plugin::PingPong.new ],
```
Same as `plugins`. The only difference is something will be set to
these by default, as these plugins are assumed to be essential to proper
working order of any IRC client. **Defaults to:**
`[ IRC::Client::Plugin::PingPong.new ]`

## `run`

    $irc.run;

Takes no arguments. Starts the IRC client. Exits when the connection
to the IRC server ends.

# METHODS FOR PLUGINS

You can make use of these `IRC::Client` methods in your plugins:

## `.ssay`

```perl6
    $irc.ssay("Foo bar!");
```
Sends a message to the server, automatically appending `\r\n`. Mnemonic:
**s**erver **say**.


## `.privmsg`

```perl6
    $irc.privmsg( 'Zoffix', 'Hallo!' );
```
Sends a `PRIVMSG` message specified in the second argument
to the user/channel specified as the first argument.

# INCLUDED PLUGINS

Currently, this distribution comes with two IRC Client plugins:

## IRC::Client::Plugin::Debugger

```perl6
    use IRC::Client;
    use IRC::Client::Plugin::Debugger;

    IRC::Client.new(
        :host('localhost'),
        :debug,
        plugins => [ IRC::Client::Plugin::Debugger.new ]
    ).run;
```

When run, it will pretty-print all of the events received by the client. It
does not stop plugin processing loop after handling a message.

## IRC::Client::Plugin::PingPong

```perl6
    use IRC::Client;
    IRC::Client.new.run; # automatically included in plugins-essential
```

This plugin makes IRC::Client respond to server's C<PING> messages and is
included in the [`plugins-essential`](#plugins-essential) by default.

# EXTENDING IRC::Client / WRITING YOUR OWN PLUGINS

## Overview of the plugin system

The core IRC::Client receives and parses IRC protocol messages from the
server that it then passes through a plugin chain. The plugins declared in
[`plugins-essential`](#plugins-essential) are executed first, followed by
plugins in [`plugins`](#plugins). The order is the same as the order specified
in those two lists.

A plugin can return a [special constant](#return-value-constants) that
indicates it handled the message and the plugin chain processing should stop.

To subscribe to handle a particular IRC command, a plugin simply declares a
method `irc-COMMAND`, where `COMMAND` is the name of the IRC command the
plugin wishes to handle. There are also a couple of
[special events](#special-events) the plugin can subscribe to, such as
intialization during start up or when the client receives a private message
or notice.

## Return value constants

```perl6
    use IRC::Client::Plugin;
    unit class IRC::Client::Plugin::Foo is IRC::Client::Plugin;
    ...
```

To make the constants available in your class, simply `use` IRC::Client::Plugin
class.

### `IRC_HANDLED`

```perl6
    # Returned by default
    method irc-ping ($irc, $e) { $irc.ssay("PONG {$irc.nick} $e<params>[0]") }

    # Explicit return
    method irc-privmsg ($irc, $e) { return IRC_HANDLED; }
```
Specifies that plugin handled the message and the plugin chain processing
should stop immediatelly. Plugins later in the chain won't know this
message ever came. Unless you explicitly return
[`IRC_NOT_HANDLED`](#irc_not_handled) constant, IRC::Client will assume
`IRC_HANDLED` was returned.

### `IRC_NOT_HANDLED`

```perl6
    return IRC_NOT_HANDLED;
```
Returning this constant indicates to IRC::Client that your plugin did
not "handle" the message and it should be propagated further down the
plugin chain for other plugins to handle.

## Subscribing to IRC events

### Standard IRC commands

```perl6
    method irc-privmsg ($irc, $e) { ... }
    method irc-notice  ($irc, $e) { ... }
    method irc-353     ($irc, $e) { ... }
```
To subscribe to an IRC event, simply declare a method named `irc-command`,
where `command` is the IRC command you want to handle, in **lower case**.
The method takes two positional arguments: an `IRC::Client` object and
the [parsed IRC message](#contents-of-the-parsed-irc-message).

You'll likely generate a response based on the content of the parsed message
and use one of the [METHODS FOR PLUGINS](#methods-for-plugins) to send that
response.

## Special Events

```perl6
    method irc-start-up   ($irc) { ... } # once per client run
    method irc-connected  ($irc) { ... } # once per server connection

    method irc-all-events ($irc, $e) { ... }
    method irc-privmsg-me ($irc, $e) { ... }
    method irc-notice-me  ($irc, $e) { ... }
    ... # all other handlers for standard IRC commands
    method irc-unhandled  ($irc, $e) { ... }
```
In addition to the [standard IRC commands](#standard-irc-commands), you can
register several special cases. They're handled in the event chain in the order
shown above (except for [`irc-start-up`](#irc-start-up) and
[`irc-connected`](#irc-connected) that do not offect command-triggered events).
That is, if a plugin returns [`IRC_HANDLED`](#irc_handled) after
processing, say, [`irc-all-events`](#irc-all-events) event, its
[`irc-notice-me`](#irc-notice-me) handler won't be triggered, even if it would
otherwise.

The available special events are as follows:

### `irc-start-up`

```perl6
    method irc-start-up ($irc) { ... }
```
Passed IRC::Client object as the only argument. Triggered right when the
IRC::Client is [`.run`](#run), which means most of
[METHODS FOR PLUGINS](#methods-for-plugins) **cannot** be used, as no connection
has been made yet. This event will be issued only once per [`.run`](#run)
and the method's return value is discarded.

### `irc-connected`

```perl6
    method irc-connected ($irc) { ... }
```
Passed IRC::Client object as the only argument. Triggered right when we
get a connection to the server, identify with it and issue `JOIN` commands
to enter the channels. Note that at this point it is not guaranteed that the
client is already in all the channels it's meant to join.
This event will be issued only once per connection to the server
and the method's return value is discarded.

### `irc-all-events`

```perl6
    method irc-all-events ($irc, $e) { ... }
```
Triggered for all IRC commands received, regardless of their content. As this
method will be triggered before any others, you can use this to
pre-process the message, for example. ***WARNING:*** **since
[`IRC_HANDLED` constant](#irc_handled) is returned by default, if you do not
explicitly return [`IRC_NOT_HANDLED`](#irc_not_handled), your client will
stop handling ALL other messages
***

### `irc-privmsg-me`

```perl6
    method irc-privmsg-me ($irc, $e) { ... }
```
Triggered when the IRC `PRIVMSG` command is received, where the recipient
is the client (as opposed to some channel).

### `irc-notice-me`

```perl6
    method irc-notice-me ($irc, $e) { ... }
```
Triggered when the IRC `NOTICE` command is received, where the recipient
is the client (as opposed to some channel).

### `irc-unhandled`

```perl6
    method irc-unhandled ($irc, $e) { ... }
```

This is the same as [`irc-all-events`](#irc-all-events), except it's triggered
**after** all other events were tried. This method can be used to catch
any unhandled events.

## Contents of the parsed IRC message

```perl6
    # method irc-366 ($irc, $e) { ... }
    {
        command => "366".Str,
        params  => [
            "Perl6IRC".Str,
            "#perl6bot".Str,
            "End of NAMES list".Str,
        ],
        pipe    => { },
        who     => {
            host => "irc.example.net".Str,
        },
    }

    # method irc-join ($irc, $e) { ... }
    {
        command => "JOIN".Str,
        params  => [
            "#perl6bot".Str,
        ],
        pipe    => { },
        who     => {
            host => "localhost".Str,
            nick => "ZoffixW".Str,
            user => "~ZoffixW".Str,
        },
    }

    # method irc-privmsg ($irc, $e) { ... }
    {
        command => "PRIVMSG".Str,
        params  => [
            "#perl6bot".Str,
            "Perl6IRC, hello!".Str,
        ],
        pipe    => { },
        who     => {
            host => "localhost".Str,
            nick => "ZoffixW".Str,
            user => "~ZoffixW".Str,
        },
    }

    # method irc-notice-me ($irc, $e) { ... }
    {
        command => "NOTICE".Str,
        params  => [
            "Perl6IRC".Str,
            "you there?".Str,
        ],
        pipe    => { },
        who     => {
            host => "localhost".Str,
            nick => "ZoffixW".Str,
            user => "~ZoffixW".Str,
        },
    }
```

The second argument to event handlers is the parsed IRC message that is a
hash with the following keys:

### `command`

```perl6
    command => "NOTICE".Str,
```
Contains the IRC command this message represents.

### `params`

```perl6
    params  => [
        "Perl6IRC".Str,
        "you there?".Str,
    ],
```
Constains the array of parameters for the IRC command.

### `pipe`

```perl6
    pipe => { },
```
This is a special key that can be used for communication between plugins.
While any plugin can modify any key of the parsed command's hash, the provided
`pipe` hash is simply a means to provide some standard, agreed-upon name
of a key to pass information around.

### `who`

```perl6
    #fdss
    who => {
        host => "localhost".Str,
        nick => "ZoffixW".Str,
        user => "~ZoffixW".Str,
    },

    who => {
        host => "irc.example.net".Str,
    },
```
A hash containing information on who sent the message. Messages sent by the
server do not have `nick`/`user` keys specified.

# REPOSITORY

Fork this module on GitHub:
https://github.com/zoffixznet/perl6-IRC-Client

# BUGS

To report bugs or request features, please use
https://github.com/zoffixznet/perl6-IRC-Client/issues

# AUTHOR

Zoffix Znet (http://zoffix.com/)

# LICENSE

You can use and distribute this module under the terms of the
The Artistic License 2.0. See the `LICENSE` file included in this
distribution for complete details.