nick and channel tracking in services module

This commit is contained in:
dave 2017-12-04 23:55:30 -08:00
parent 6be2d92bac
commit a6fab20270
2 changed files with 46 additions and 14 deletions

View File

@ -8,6 +8,7 @@ Services enables the bot to:
- Ghost users using it's nick - Ghost users using it's nick
- Request invites & join private channels - Request invites & join private channels
Config Config
------ ------
@ -47,7 +48,7 @@ Config
.. cmdoption:: user.nick .. cmdoption:: user.nick
A list of nicks, the first being the preferred nick and the others being A list of nicks, the first being the preferred nick and the others being
fallbacks if the primary nick is taken. fallbacks if the primary nick is taken.
.. cmdoption:: user.password .. cmdoption:: user.password
@ -78,7 +79,7 @@ Config
.. cmdoption:: ident.command .. cmdoption:: ident.command
String formatted command to be sent for identification. Available tokens: String formatted command to be sent for identification. Available tokens:
- password - password
.. cmdoption:: ident.ghost .. cmdoption:: ident.ghost
@ -111,6 +112,17 @@ Config
List of channels to request an invite to join on startup List of channels to request an invite to join on startup
Service
-------
The ``service`` service provides information about the state of IRC. Available service methods:
.. cmdoption:: nick()
Returns the current nick of the bot
Class Reference Class Reference
--------------- ---------------

View File

@ -14,45 +14,50 @@ from time import sleep
class Services(ModuleBase): class Services(ModuleBase):
def __init__(self, bot, moduleName): def __init__(self, bot, moduleName):
ModuleBase.__init__(self, bot, moduleName) ModuleBase.__init__(self, bot, moduleName)
self.current_nick = 0 self.current_preferred_nick = 0
self.current_nick = None
self.current_channels = []
self.do_ghost = False self.do_ghost = False
self.services = ["services"]
@hook("_CONNECT") @hook("_CONNECT")
def doConnect(self, msg, cmd): def _doConnect(self, msg, cmd):
"""Hook for when the IRC conneciton is opened""" """Hook for when the IRC conneciton is opened"""
self.bot.act_NICK(self.config["user"]["nick"][0]) self.current_preferred_nick = 0
self.bot.act_NICK(self.config["user"]["nick"][self.current_preferred_nick])
self.bot.act_USER(self.config["user"]["username"], self.config["user"]["hostname"], self.bot.act_USER(self.config["user"]["username"], self.config["user"]["hostname"],
self.config["user"]["realname"]) self.config["user"]["realname"])
@hook("433") @hook("433")
def nickTaken(self, msg, cmd): def _nickTaken(self, msg, cmd):
"""Hook that responds to 433, meaning our nick is taken""" """Hook that responds to 433, meaning our nick is taken"""
if self.config["ident"]["ghost"]: if self.config["ident"]["ghost"]:
self.do_ghost = True self.do_ghost = True
self.current_nick += 1 self.current_preferred_nick += 1
if self.current_nick >= len(self.config["user"]["nick"]): if self.current_preferred_nick >= len(self.config["user"]["nick"]):
self.log.critical("Ran out of usernames while selecting backup username!") self.log.critical("Ran out of usernames while selecting backup username!")
return return
self.bot.act_NICK(self.config["user"]["nick"][self.current_nick]) self.bot.act_NICK(self.config["user"]["nick"][self.current_preferred_nick])
@hook("001") @hook("001")
def initservices(self, msg, cmd): def _initservices(self, msg, cmd):
"""Hook that sets our initial nickname""" """Hook that sets our initial nickname"""
if self.do_ghost: if self.do_ghost:
self.bot.act_PRIVMSG(self.config["ident"]["ghost_to"], self.config["ident"]["ghost_cmd"] % self.bot.act_PRIVMSG(self.config["ident"]["ghost_to"], self.config["ident"]["ghost_cmd"] %
{"nick": self.config["user"]["nick"][0], "password": self.config["user"]["password"]}) {"nick": self.config["user"]["nick"][0], "password": self.config["user"]["password"]})
sleep(2) sleep(2)
self.bot.act_NICK(self.config["user"]["nick"][0]) self.bot.act_NICK(self.config["user"]["nick"][self.current_preferred_nick])
self.do_initservices() self.current_nick = self.config["user"]["nick"][self.current_preferred_nick]
self._do_initservices()
@hook("INVITE") @hook("INVITE")
def invited(self, msg, cmd): def _invited(self, msg, cmd):
"""Hook responding to INVITE channel invitations""" """Hook responding to INVITE channel invitations"""
if msg.trailing.lower() in self.config["privatechannels"]["list"]: if msg.trailing.lower() in self.config["privatechannels"]["list"]:
self.log.info("Invited to %s, joining" % msg.trailing) self.log.info("Invited to %s, joining" % msg.trailing)
self.bot.act_JOIN(msg.trailing) self.bot.act_JOIN(msg.trailing)
def do_initservices(self): def _do_initservices(self):
"""Identify with nickserv and join startup channels""" """Identify with nickserv and join startup channels"""
" id to nickserv " " id to nickserv "
if self.config["ident"]["enable"]: if self.config["ident"]["enable"]:
@ -70,3 +75,18 @@ class Services(ModuleBase):
self.bot.act_PRIVMSG(self.config["privatechannels"]["to"], self.config["privatechannels"]["command"] % self.bot.act_PRIVMSG(self.config["privatechannels"]["to"], self.config["privatechannels"]["command"] %
{"channel": channel}) {"channel": channel})
@hook("NICK")
def _changed_nick(self, msg, cmd):
if msg.prefix.nick == self.current_nick:
self.current_nick = msg.trailing
@hook("JOIN", "PART")
def _joinpart(self, msg, cmd):
(self.current_channels.append if msg.command == "JOIN" else self.current_channels.remove)(msg.args[0])
def nick(self):
return self.current_nick
def channels(self):
return self.current_channels