2013-12-28 09:58:20 -08:00
|
|
|
#!/usr/bin/env python
|
2014-10-02 16:53:54 -07:00
|
|
|
"""
|
|
|
|
.. module:: Services
|
2015-11-01 18:03:11 -08:00
|
|
|
:synopsis: Provides the ability to configure a nickname, password, channel auto-join
|
2014-10-02 16:53:54 -07:00
|
|
|
|
|
|
|
.. moduleauthor:: Dave Pedu <dave@davepedu.com>
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2017-11-27 18:58:20 -08:00
|
|
|
from pyircbot.modulebase import ModuleBase, hook
|
2013-12-28 09:58:20 -08:00
|
|
|
from time import sleep
|
|
|
|
|
2017-01-01 14:59:01 -08:00
|
|
|
|
2013-12-28 09:58:20 -08:00
|
|
|
class Services(ModuleBase):
|
2015-11-01 18:03:11 -08:00
|
|
|
def __init__(self, bot, moduleName):
|
|
|
|
ModuleBase.__init__(self, bot, moduleName)
|
2017-12-04 23:55:30 -08:00
|
|
|
self.current_preferred_nick = 0
|
|
|
|
self.current_nick = None
|
|
|
|
self.current_channels = []
|
2015-11-01 18:03:11 -08:00
|
|
|
self.do_ghost = False
|
2017-12-04 23:55:30 -08:00
|
|
|
self.services = ["services"]
|
2017-01-01 14:01:23 -08:00
|
|
|
|
2017-11-27 18:58:20 -08:00
|
|
|
@hook("_CONNECT")
|
2017-12-04 23:55:30 -08:00
|
|
|
def _doConnect(self, msg, cmd):
|
2015-11-01 18:03:11 -08:00
|
|
|
"""Hook for when the IRC conneciton is opened"""
|
2017-12-04 23:55:30 -08:00
|
|
|
self.current_preferred_nick = 0
|
|
|
|
self.bot.act_NICK(self.config["user"]["nick"][self.current_preferred_nick])
|
2017-01-01 14:59:01 -08:00
|
|
|
self.bot.act_USER(self.config["user"]["username"], self.config["user"]["hostname"],
|
|
|
|
self.config["user"]["realname"])
|
2017-01-01 14:01:23 -08:00
|
|
|
|
2017-11-27 18:58:20 -08:00
|
|
|
@hook("433")
|
2017-12-04 23:55:30 -08:00
|
|
|
def _nickTaken(self, msg, cmd):
|
2015-11-01 18:03:11 -08:00
|
|
|
"""Hook that responds to 433, meaning our nick is taken"""
|
|
|
|
if self.config["ident"]["ghost"]:
|
|
|
|
self.do_ghost = True
|
2017-12-04 23:55:30 -08:00
|
|
|
self.current_preferred_nick += 1
|
|
|
|
if self.current_preferred_nick >= len(self.config["user"]["nick"]):
|
2015-11-01 18:03:11 -08:00
|
|
|
self.log.critical("Ran out of usernames while selecting backup username!")
|
|
|
|
return
|
2017-12-04 23:55:30 -08:00
|
|
|
self.bot.act_NICK(self.config["user"]["nick"][self.current_preferred_nick])
|
2013-12-28 09:58:20 -08:00
|
|
|
|
2017-11-27 18:58:20 -08:00
|
|
|
@hook("001")
|
2017-12-04 23:55:30 -08:00
|
|
|
def _initservices(self, msg, cmd):
|
2015-11-01 18:03:11 -08:00
|
|
|
"""Hook that sets our initial nickname"""
|
|
|
|
if self.do_ghost:
|
2017-01-01 14:59:01 -08:00
|
|
|
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"]})
|
2015-11-01 18:03:11 -08:00
|
|
|
sleep(2)
|
2017-12-04 23:55:30 -08:00
|
|
|
self.bot.act_NICK(self.config["user"]["nick"][self.current_preferred_nick])
|
|
|
|
self.current_nick = self.config["user"]["nick"][self.current_preferred_nick]
|
|
|
|
self._do_initservices()
|
2017-01-01 14:01:23 -08:00
|
|
|
|
2017-11-27 18:58:20 -08:00
|
|
|
@hook("INVITE")
|
2017-12-04 23:55:30 -08:00
|
|
|
def _invited(self, msg, cmd):
|
2015-11-01 18:03:11 -08:00
|
|
|
"""Hook responding to INVITE channel invitations"""
|
2017-11-27 18:58:20 -08:00
|
|
|
if msg.trailing.lower() in self.config["privatechannels"]["list"]:
|
|
|
|
self.log.info("Invited to %s, joining" % msg.trailing)
|
|
|
|
self.bot.act_JOIN(msg.trailing)
|
2017-01-01 14:01:23 -08:00
|
|
|
|
2017-12-04 23:55:30 -08:00
|
|
|
def _do_initservices(self):
|
2015-11-01 18:03:11 -08:00
|
|
|
"""Identify with nickserv and join startup channels"""
|
|
|
|
" id to nickserv "
|
|
|
|
if self.config["ident"]["enable"]:
|
2017-01-01 14:59:01 -08:00
|
|
|
self.bot.act_PRIVMSG(self.config["ident"]["to"], self.config["ident"]["command"] %
|
|
|
|
{"password": self.config["user"]["password"]})
|
2017-01-01 14:01:23 -08:00
|
|
|
|
2015-11-01 18:03:11 -08:00
|
|
|
" join plain channels "
|
|
|
|
for channel in self.config["channels"]:
|
|
|
|
self.log.info("Joining %s" % channel)
|
|
|
|
self.bot.act_JOIN(channel)
|
2017-01-01 14:01:23 -08:00
|
|
|
|
2015-11-01 18:03:11 -08:00
|
|
|
" request invite for private message channels "
|
|
|
|
for channel in self.config["privatechannels"]["list"]:
|
|
|
|
self.log.info("Requesting invite to %s" % channel)
|
2017-01-01 14:59:01 -08:00
|
|
|
self.bot.act_PRIVMSG(self.config["privatechannels"]["to"], self.config["privatechannels"]["command"] %
|
|
|
|
{"channel": channel})
|
2017-01-01 14:01:23 -08:00
|
|
|
|
2017-12-04 23:55:30 -08:00
|
|
|
@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):
|
2018-01-16 17:05:19 -08:00
|
|
|
channel = msg.args[0] if msg.command == "PART" else msg.trailing
|
|
|
|
(self.current_channels.append if msg.command == "JOIN" else self.current_channels.remove)(channel)
|
2017-12-04 23:55:30 -08:00
|
|
|
|
|
|
|
def nick(self):
|
|
|
|
return self.current_nick
|
|
|
|
|
|
|
|
def channels(self):
|
|
|
|
return self.current_channels
|
|
|
|
|