Skip to content

Commit

Permalink
feat(irc): an ignore list for IRC nicknames; mostly to ignore bots
Browse files Browse the repository at this point in the history
It is based on nicknames, not on hostnames or anything. So it is
rather sensitive to nickname changes. As such, it is meant to
ignore bots, not users.
  • Loading branch information
TrueBrain committed Oct 19, 2022
1 parent b7e6fd1 commit 4281ec5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
12 changes: 10 additions & 2 deletions dibridge/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,23 @@
@click.option("--irc-nick", help="IRC nick to use.", required=True)
@click.option("--irc-channel", help="IRC channel to relay to, without the first '#'.", required=True)
@click.option("--irc-puppet-ip-range", help="An IPv6 CIDR range to use for IRC puppets. (2001:A:B:C:D::/80)")
def main(discord_token, discord_channel_id, irc_host, irc_port, irc_nick, irc_channel, irc_puppet_ip_range):
@click.option("--irc-ignore-list", help="IRC nicknames to not relay messages for (comma separated, case-insensitive).")
def main(
discord_token, discord_channel_id, irc_host, irc_port, irc_nick, irc_channel, irc_puppet_ip_range, irc_ignore_list
):
if irc_puppet_ip_range:
irc_puppet_ip_range = ipaddress.ip_network(irc_puppet_ip_range)
if irc_puppet_ip_range.num_addresses < 2**48:
raise Exception("--irc-puppet-ip-range needs to be an IPv6 CIDR range of at least /80 or more.")

if irc_ignore_list.strip():
irc_ignore_list = [nickname.strip().lower() for nickname in irc_ignore_list.split(",") if nickname.strip()]
if not irc_ignore_list:
irc_ignore_list = None

thread_d = threading.Thread(target=discord.start, args=[discord_token, discord_channel_id])
thread_i = threading.Thread(
target=irc.start, args=[irc_host, irc_port, irc_nick, f"#{irc_channel}", irc_puppet_ip_range]
target=irc.start, args=[irc_host, irc_port, irc_nick, f"#{irc_channel}", irc_puppet_ip_range, irc_ignore_list]
)

thread_d.start()
Expand Down
9 changes: 6 additions & 3 deletions dibridge/irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


class IRCRelay(irc.client_aio.AioSimpleIRCClient):
def __init__(self, host, port, nickname, channel, puppet_ip_range):
def __init__(self, host, port, nickname, channel, puppet_ip_range, irc_ignore_list):
irc.client.SimpleIRCClient.__init__(self)

self._loop = asyncio.get_event_loop()
Expand All @@ -28,6 +28,7 @@ def __init__(self, host, port, nickname, channel, puppet_ip_range):
self._channel = channel
self._puppet_ip_range = puppet_ip_range
self._pinger_task = None
self._irc_ignore_list = irc_ignore_list

# List of users when they have last spoken.
self._users_spoken = {}
Expand Down Expand Up @@ -55,6 +56,8 @@ def on_privmsg(self, _, event):
def on_pubmsg(self, _, event):
if event.target != self._channel:
return
if event.source.nick.lower() in self._irc_ignore_list:
return
asyncio.create_task(self._relay_mesage(event.source.nick, event.arguments[0]))

def on_action(self, _, event):
Expand Down Expand Up @@ -243,10 +246,10 @@ def stop(self):
asyncio.run_coroutine_threadsafe(self._stop(), self._loop)


def start(host, port, name, channel, puppet_ip_range):
def start(host, port, name, channel, puppet_ip_range, irc_ignore_list):
asyncio.set_event_loop(asyncio.new_event_loop())

relay.IRC = IRCRelay(host, port, name, channel, puppet_ip_range)
relay.IRC = IRCRelay(host, port, name, channel, puppet_ip_range, irc_ignore_list)

log.info("Connecting to IRC ...")
asyncio.get_event_loop().run_until_complete(relay.IRC._connect())
Expand Down

0 comments on commit 4281ec5

Please sign in to comment.