Handling authentication on IRC with a python bot

So I have an IRC bot and I’m trying to implement elevated commands for operators+. Currently the bot checks that the command is coming from a list of pre-configured operator names but this is pretty brittle.

I did try some other things, using the jaraco’s irc library:
I’ve tried other things like running /nickserv info username and trying to parse the private notice that’s returned but that’s always

class Bot(irc.bot.SingleServerIRCBot):
    def __init__(self, nickname: str, server: str, port: int) -> None:
        factory = irc.connection.Factory(wrapper=ssl.SSLContext().wrap_socket)
        irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname, connect_factory=factory)
        self.register_handlers()
        self.privnotices = []

    def register_handlers(self) -> None:
        self.reactor.add_global_handler("privnotice", self.on_privnotice)


    def on_priv_notice(self, server: irc.client.ServerConnection, event: irc.client.Event) -> None:
       # try to capture it
        self.privnotices.extend(event.arguments)

    def on_pubmsg(self, server: irc.client.ServerConnection, event: irc.client.Event) -> None:
       target = ... 
        sever.send_items("NICKSERV", "INFO", target) # Replies with a priv notice
        # I want to confirm this is coming from an authenticated user so that they cannot use an elevated command just by changing their nick to that of an operator
       # If i try to access self.privnotices here, it will not have the reponse of the above command

I’m not dead set on this one way of authentication or this library, this is what I first discovered pretty much. If there’s a correct/better way to handle gated commands, I’m all ears.

My goal is essentially having some gated commands for operators that cannot be used by malicious users.

With that library, channel objects should have an is_oper() method you can pass a nick into and get back a boolean result. Some colleagues and I use it in one of our bots:

1 Like

That did the trick! thank you!