aboutsummaryrefslogtreecommitdiff
path: root/docs/03-method-reference.md
blob: 9c5f1fa22c7584490c29f5c221f294b1f095e0fe (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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
[[back to doc map]](README.md)

# Method Reference

This document describes events available on various objects in use when working
with `IRC::Client`.

## Message Objects (`IRC::Client::Message` and subclasses)

All event handlers (except for special `irc-started`) receive one positional
argument that does `IRC::Client::Message` role and is refered to as
the Message Object throughout the documentation. The actual received
message object depends on the event the event handler is subscribed to.
See [event reference](02-event-reference.md) to learn which message objects
an event can receive.

### Message Object Hierarchy

All message objects reside in the `IRC::Client::Message` package and
follow the following hierarchy, with children having all the methods
and attributes of their parents.

```
IRC::Client::Message

├───IRC::Client::Message::Join
├───IRC::Client::Message::Nick
├───IRC::Client::Message::Numeric
├───IRC::Client::Message::Part
├───IRC::Client::Message::Ping
├───IRC::Client::Message::Quit
├───IRC::Client::Message::Unknown

├───IRC::Client::Message::Mode
│   ├───IRC::Client::Message::Mode::Channel
│   └───IRC::Client::Message::Mode::Me

├───IRC::Client::Message::Notice
│   ├───IRC::Client::Message::Notice::Channel
│   └───IRC::Client::Message::Notice::Me

└───IRC::Client::Message::Privmsg
    ├───IRC::Client::Message::Privmsg::Channel
    └───IRC::Client::Message::Privmsg::Me
```

### Methods and Attributes

Subclasses inherit all the methods and attributes of their parents (see
hierarchy chart above). Some event handlers can receive more than one
type of a message object. In many cases, the type can be differentiated
with a safe-method-call operator (`.?`):

```perl6
    method irc-privmsg ($e) {
        if $e.?channel {
            say '$e is a IRC::Client::Message::Privmsg::Channel';
        }
        else {
            say '$e is a IRC::Client::Message::Privmsg::Me';
        }
    }
```

---

#### `IRC::Client::Message`

Object is never sent to event handlers and merely provides commonality to
its subclasses.

##### `.irc`

Contains the `IRC::Client` object.

##### `.nick`

Contains the nick of the sender of the message.

##### `.username`

Contains the username of the sender of the message.

##### `.host`

Contains the host of the sender of the message.

##### `.usermask`

Contains the usermask of the sender of the message. That is
string constructed as `nick!username@host`

##### `.command`

The IRC command responsible for this event, such as `PRIVMSG` or `001`.

##### `.server`

The `IRC::Client::Server` object from which the event originates.

##### `.args`

A possibly-empty list of arguments, received for the IRC command that triggered
the event.

##### `.Str`

(affects stringification of message objects). Returns a string
constructed from `":$!usermask $!command $!args[]"`, but is overriden to
a different value by some message objects.

---

#### `IRC::Client::Message::Join`

##### `.channel`

Contains the channel name of the channel that was joined

---

#### `IRC::Client::Message::Nick`

##### `.new-nick`

Contains the new nick switched to (`.nick` attribute contains the old one).

---

#### `IRC::Client::Message::Numeric`

Does not offer any object-specific methods. Use the `.command` attribute
to find out the actual 3-digit IRC command that triggered the event.

---

#### `IRC::Client::Message::Part`

##### `.channel`

Contains the channel name of the channel that was parted. Use `.args`
attribute to get any potential parting messages.

---

#### `IRC::Client::Message::Ping`

**Included in the docs for completeness only.** Used internally. Not sent
to any event handlers and `irc-ping` is not a valid event.

##### `.reply`

Takes no arguments. Replies to the server with appropriate `PONG` IRC command.

---

#### `IRC::Client::Message::Quit`

Does not offer any object-specific methods. Use `.args`
attribute to get any potential quit messages.

---

#### `IRC::Client::Message::Unknown`

##### `.Str`

Overrides the default stringification string to
`"❚⚠❚ :$.usermask $.command $.args[]"`

---

#### `IRC::Client::Message::Mode`

Object is never sent to event handlers and merely provides commonality to
its subclasses.

##### `.modes`

Contains the modes set by the IRC command that triggered the event. When
modes are set on the channel, contains a list of `Pair`s where the key
is the sign of the mode (`+` or `-`) and the value if the mode letter itself.
When modes are set on the client, contains just a list of modes as strings,
without any signs.

---

#### `IRC::Client::Message::Mode::Channel`

##### `.channel`

Contains the channel on which the modes were set.

---

#### `IRC::Client::Message::Mode::Me`

Does not offer any object-specific methods.

---

#### `IRC::Client::Message::Notice`

Object is never sent to event handlers and merely provides commonality to
its subclasses.

##### `.text`

Writable attribute. Contains the text of the message.

##### `.replied`

Writable `Bool` attribute. Automatically gets set to `True` by the
`.reply` method. If set to `True`, indicates to the Client Object that
the event handler's value must not be used as a reply to the message.

##### `.Str`

Overrides stringification of the message object to be the value of the
`.text` attribute.

---

#### `IRC::Client::Message::Notice::Channel`

##### `.channel`

Contains the channel to which the message was sent.

##### `.reply`

```perl6
    $e.reply: "Hello, World!";
    $e.reply: "Hello, World!", :where<#perl6>;
    $e.reply: "Hello, World!", :where<Zoffix>;
```

Replies to the sender of the message using the `NOTICE` IRC command. The
optional `:where` argument specifies a channel or nick
where to send the message and defaults to the channel in which the message
originated.

---

#### `IRC::Client::Message::Notice::Me`

##### `.reply`

```perl6
    $e.reply: "Hello, World!";
    $e.reply: "Hello, World!", :where<Zoffix>;
    $e.reply: "Hello, World!", :where<#perl6>;
```

Replies to the sender of the message using the `NOTICE` IRC command. The
optional `:where` argument specifies a nick or channel
where to send the message and defaults to the nick from which the message
originated.

---

#### `IRC::Client::Message::Privmsg`

Object is never sent to event handlers and merely provides commonality to
its subclasses.

##### `.text`

Writable attribute. Contains the text of the message.

##### `.replied`

Writable `Bool` attribute. Automatically gets set to `True` by the
`.reply` method. If set to `True`, indicates to the Client Object that
the event handler's value must not be used as a reply to the message.

##### `.Str`

Overrides stringification of the message object to be the value of the
`.text` attribute.

---

#### `IRC::Client::Message::Privmsg::Channel`

##### `.channel`

Contains the channel to which the message was sent.

##### `.reply`

```perl6
    $e.reply: "Hello, World!";
    $e.reply: "Hello, World!", :where<#perl6>;
    $e.reply: "Hello, World!", :where<Zoffix>;
```

Replies to the sender of the message using the `PRIVMSG` IRC command. The
optional `:where` argument specifies a channel or nick
where to send the message and defaults to the channel in which the message
originated.

---

#### `IRC::Client::Message::Privmsg::Me`

##### `.reply`

```perl6
    $e.reply: "Hello, World!";
    $e.reply: "Hello, World!", :where<Zoffix>;
    $e.reply: "Hello, World!", :where<#perl6>;
```

Replies to the sender of the message using the `PRIVMSG` IRC command. The
optional `:where` argument specifies a nick or channel
where to send the message and defaults to the nick from which the message
originated.

---

## Server Object (`IRC::Client::Server`)

The Server Object represents one of the IRC servers the client is connected
to. It's returned by the `.server` attribute of the Message Object and
can be passed to various Client Object methods to indicate to which server
a command should be sent to.

Client Object's `%.servers` attribute contains all of the `IRC::Client::Server`
objects with their keys as labels and values as objects themselves.

### Labels

Each `IRC::Client::Server` object has a label that was given to it during
the creation of `IRC::Client` object. `IRC::Client::Server` objects stringify
to the value of their labels and methods that can take an
`IRC::Client::Server` object can also take a `Str` with the label of that server
instead.

### Methods and Attributes

#### `.label`

The label of this server.

#### `.channels`

A list of `Str` or `Pair` containing all the channels the client is in on this
server. Pairs represent channels with channel passwords, where the key is
the channel and the value is its password.

#### `.nick`

A list of nicks the client uses on this server. If one nick is
taken, next one in the list will be attempted to be used.

#### `.host`

The host of the server.

#### `.port`

The port of the server.

#### `.password`

The password of the server.

#### `.username`

Our username on this server.

#### `.userhost`

Our hostname on this server.

#### `.userreal`

The "real name" of our client.

#### `.Str`

Affects stringification of the object and makes it stringify to the value
of `.label` attribute.

#### Writable Non-Writable Attributes

The following attributes are writable, however, they are written to by
the internals and the behaviour when writing to them directly is undefined.

##### `.current-nick`

Writable attribute. Our currently-used nick. Will be one of the nicks returned
by `.nicks`.

##### `.is-connected`

Writable `Bool` attribute. Indicates whether we're currently in a state
where the server considers us connected. This defaults to `False`, then is set
to `True` when the server sends us `001` IRC command and set back to `False`
when the socket connection breaks.

##### `.has-quit`

Writable `Bool` attribute. Set to `True` when `.quit` method is called
on the Client Object and is used by the socket herder to determine whether
or not the socket connection was cut intentionally.

##### `.has-quit`

Writable `IO::Socket::Async` attribute. Contains an object representing
the socket connected to the server, although it may already be closed.

---

## Client Object (`IRC::Client`)

The Client Object is the heart of this module. The `IRC::Client` you instantiate
and `.run`. The running Client Object is available to your plugins via
`.irc` attribute that you can obtain by doing the `IRC::Client::Plugin` role
or via `.irc` attribute on Message Objects your event handler receives.

The client object's method let you control the client: e.g. joining or parting
channels, changing nicks, banning users, or sending text messages.

### Methods and Attributes

#### `.join`

```perl6
    $.irc.join: <#foo #bar #ber>, :$server;
```

Causes the client join the provided channels.
If `:server` named argument is given, will operate only on that server;
otherwise operates on all connected servers.

#### `.new`

```perl6
my $irc = IRC::Client.new:
    :debug
    :host<irc.freenode.net>
    :6667port
    :password<s3cret>
    :channels<#perl #perl6 #rust-lang>
    :nick<MahBot>
    :username<MahBot>
    :userhost<localhost>
    :userreal('Mah awesome bot!')
    :servers(
        freenode => %(),
        local    => %( :host<localhost> ),
    )
    :plugins(
        class { method irc-to-me ($ where /42/) { 'The answer to universe!' } }
    )
    :filters(
        -> $text where .lines > 5 or .chars > 300 { "Text is too big!!!" }
    )
```

Instantiates a new `IRC::Client` object. Takes the following named arguments:

##### `:channels`

```perl6
    :channels('#perl6bot', '#zofbot', '#myown' => 's3cret')
```

A list of `Str` or `Pair` containing the channels to join.
Pairs represent channels with channel passwords, where the key is
the channel and the value is its password.
**Defaults to:** `#perl6`

##### `:debug`

Takes an `Int`. When set to a positive number, causes debug output to be
generated. Install optional
[Terminal::ANSIColor]https://modules.perl6.org/repo/Terminal::ANSIColor] to
make output colourful. Debug levels:

* `0`—no debug output
* `1`—basic debug output
* `2`—also include list of emitted events
* `3`—also include `irc-all` in the list of emitted events

**Defaults to:** `0`

##### `:filters`

Takes a list of `Callable`s. Will attempt to call them for replies to
`PRIVMSG` and `NOTICE` events if the signatures accept `($text)` or
`($text, :$where)` calls, where `$text` is the reply text and `:$where` is
a named argument of the destination of the reply (either a user or a channel
name).

Callables with `:$where` in the signature must return two values: the new
text to reply with and the location. Otherwise, just one value needs to
be returned: the new text to reply with.

**By default** not specified.

##### `:host`

The hostname of the IRC server to connect to.
**Defaults to:** `localhost`

##### `:nick`

A list of nicknames to use. If set to just one value will automatically
generate three additional nicknames that have underscores appended
(e.g. `P6Bot`, `P6Bot_`, `P6Bot__`, `P6Bot___`).

If one of the given nicks is in use, the client will attempt to use the
next one in the list.

**Defaults to:** `P6Bot`

##### `:password`

The server password to use. On some networks (like Freenode), the server
password also functions as NickServ's password.
**By default** not specified.

##### `:plugins`

Takes a list of instantiated objects or type objects that implement the
functionality of the system. See [basics tutorial](01-basics.md) and
[event reference](02-event-reference.md) for more information on how
to implement plugin classes.

**By default** not specified.

##### `:port`

The port of the IRC server to connect to. Takes an `Int` between 0 and 65535.
**Defaults to:** `6667`

##### `:servers`

Takes an `Assosiative` with keys as labels of servers and values with
server-specific configuration. Valid keys in the configuration are
`:host`, `:port`, `:password`, `:channels`, `:nick`, `:username`,
`:userhost`, and `:userreal`. They take the same values as same-named arguments
of the `IRC::Client.new` method and if any key is omitted, the value
of the `.new`'s argument will be used.

If `:servers` is not specified, then a server will be created with the
label `_` (underscore).

**By default** not specified.

##### `:username`

The IRC username to use. **Defaults to:** `Perl6IRC`

##### `:userhost`

The hostname of your client. **Defaults to:** `localhost` and can probably
be left as is, unless you're having issues connecting.

##### `:userreal`

The "real name" of your client. **Defaults to:** `Perl6 IRC Client`

----

#### `.nick`

```perl6
    $.irc.nick: 'MahBot', :$server;
    $.irc.nick: <MahBot MahBot2 MahBot3 MahBot4 MahBot5>, :$server;
```

Causes the client to change its nick to the one provided and uses this
as new value of `.current-nick` and `.nick` attributes of the appropriate
`IRC::Client::Server` object. If only one nick
is given, another 3 nicks will be automatically generated by appending
a number of underscores. Will automatically retry nicks further in the list
if the currently attempted one is already in use.

If `:server` named argument is given, will operate only on that server;
 otherwise operates on all connected servers.

#### `.part`

```perl6
    $.irc.part: <#foo #bar #ber>, :$server;
```

Causes the client part the provided channels.
If `:server` named argument is given, will operate only on that server;
otherwise operates on all connected servers.

#### `.quit`

```perl6
    $.irc.quit;
    $.irc.quit: :$server;
```

Causes the client to quit the IRC server.
If `:server` named argument is given, will operate only on that server;
otherwise operates on all connected servers.

#### `.run`

Takes no arguments. Runs the IRC client, causing it to connect to servers
and do all of its stuff. Returns only if all of the servers the client connects
to have been explicitly `.quit` from.

#### `.send`

```perl6
    $.irc.send: :where<Zoffix> :text<Hello!>;
    $.irc.send: :where<#perl6> :text('I ♥ Perl 6!');
    $.irc.send: :where<Senpai> :text('Notice me!') :notice :$server;
```

Sends a `:text` message to `:where`, using either a `PRIVMSG` or `NOTICE`.
The `:where` can be either a user's nick or a channel name. If `:notice`
argument is set to a `True` value will use `NOTICE` otherwise will use
`PRIVMSG` (if you just want to send text to channel like you'd normally
do when talking with regular IRC clients, that'd be the `PRIVMSG` messages).

If `:server` named argument is given, will operate only on that server;
otherwise operates on all connected servers.

**NOTE:** calls to `.send` when server is not connected yet will be
**semi-silently ignored** (only debug output will mention that they were
ignored). This is done on purpose, to prevent `.send` calls from interfering
with the connection negotiation during server reconnects. Although not
free from race conditions, you can mitigate this issue by checking the
`.is-connected` attribute on the appropriate `IRC::Client::Server` object
before attempting the `.send`

## Up Next

Read [Big Picture behaviour](04-big-picture-behaviour.md) next.