Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement user list and automatic .tell
Previously evalable6 and unicodable6 had to run NAMES command in order to see if there's a specific user on the channel. Now bots automatically keep track of join/part/quit/nick events, relying on NAMES only to get the initial list. They will also use NAMES to refresh the list from time to time, which is somehow needed in my IRC client during netsplits and such. Also, thanks to this tellable6 now implements automatic .tell when you try to speak to a real user who is no longer on the channel.
- Loading branch information
1 parent
fc4d257
commit a956d12
Showing
6 changed files
with
162 additions
and
99 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# Copyright © 2016-2019 | ||
# Aleks-Daniel Jakimenko-Aleksejev <alex.jakimenko@gmail.com> | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use Whateverable::Bits; | ||
|
||
#| Keep track of users | ||
unit role Whateverable::Userlist; | ||
|
||
# This is a temporary solution (aha, sure). See this bug report: | ||
# * https://github.com/zoffixznet/perl6-IRC-Client/issues/29 | ||
|
||
# We'll need at least one lock no matter what, so let's | ||
# use *just* one to make things simpler | ||
has %!userlist; #= %( channel0 => %(:user0, …), … ); | ||
has $!userlist-lock = Lock.new; | ||
|
||
method userlist($msg) { | ||
$!userlist-lock.protect: { | ||
%!userlist{$msg.channel} // %() | ||
} | ||
} | ||
|
||
#| Impersonate other bots | ||
method make-believe($msg, @nicks, &play) { | ||
my @found-nicks = self.userlist($msg){@nicks}:exists; | ||
if @found-nicks.none { | ||
$_ but Reply($msg) with play; | ||
} | ||
} | ||
|
||
#| Nick change event | ||
method irc-nick($event) { | ||
$!userlist-lock.protect: { | ||
for %!userlist.keys -> $channel { | ||
%!userlist{$channel}{$event.nick}:delete; | ||
%!userlist{$channel}{$event.new-nick} = True; | ||
} | ||
} | ||
} | ||
method irc-join($event) { | ||
$!userlist-lock.protect: { | ||
if not %!userlist{$event.channel} or (^30).pick == 0 { # self-healing behavior | ||
$event.irc.send-cmd: ‘NAMES’, $event.channel; | ||
} | ||
%!userlist{$event.channel}{$event.nick} = True; | ||
} | ||
} | ||
method irc-part($event) { | ||
$!userlist-lock.protect: { | ||
%!userlist{$event.channel}{$event.nick}:delete; | ||
} | ||
} | ||
method irc-quit($event) { | ||
$!userlist-lock.protect: { | ||
for %!userlist.keys -> $channel { | ||
%!userlist{$channel}{$event.nick}:delete; | ||
} | ||
} | ||
} | ||
|
||
has %!userlist-temp; # for storing partial messages | ||
|
||
#| Receive a user list (one or more messages) | ||
method irc-n353($event) { | ||
my $channel = $event.args[2]; | ||
# Try to filter out privileges ↓ | ||
my @nicks = $event.args[3].words.map: { m/ (<.&irc-nick>) $ /[0].Str }; | ||
$!userlist-lock.protect: { | ||
%!userlist-temp{$channel}{@nicks} = True xx @nicks | ||
} | ||
} | ||
|
||
# XXX What if we receive a `join` right here? Whatever… | ||
|
||
#| Receive a user list (final message) | ||
method irc-n366($event) { | ||
my $channel = $event.args[1]; | ||
$!userlist-lock.protect: { | ||
%!userlist{$channel} = %!userlist-temp{$channel}; | ||
%!userlist-temp{$channel}:delete | ||
} | ||
} | ||
|
||
# vim: expandtab shiftwidth=4 ft=perl6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters