From 6f78d6e52b874f4787a9f3f1752012df8257e355 Mon Sep 17 00:00:00 2001 From: Clark Boylan Date: Fri, 28 May 2021 12:38:02 -0700 Subject: [PATCH] Support normal auth in gerritbot This is a partial revert of 57a669d35dc70d21be0b799e893535fc26d95c6d which added SASL auth support but in the process removed normal auth. In order to support OFTC which doesn't currentl do SASL we add back in normal auth support while keeping SASL for OFTC should they add the functionality. Change-Id: I191ced962e91226d196e6fae728484d8cd7233e2 --- doc/source/installation.rst | 4 +- gerritbot/bot.py | 100 ++++++++++++++++++++++++++++-------- 2 files changed, 82 insertions(+), 22 deletions(-) diff --git a/doc/source/installation.rst b/doc/source/installation.rst index 2cbc2da..74535c2 100644 --- a/doc/source/installation.rst +++ b/doc/source/installation.rst @@ -15,8 +15,10 @@ when starting the bot. It should look like:: [ircbot] nick=NICKNAME pass=PASSWORD - server=irc.freenode.net + use_sasl=False + server=irc.oftc.net port=6697 + server_password=SERVERPASS channel_config=/path/to/yaml/config (See below) [gerrit] diff --git a/gerritbot/bot.py b/gerritbot/bot.py index 4ab7712..796377d 100755 --- a/gerritbot/bot.py +++ b/gerritbot/bot.py @@ -46,8 +46,10 @@ except Exception: [ircbot] nick=NICKNAME pass=PASSWORD -server=irc.freenode.net +use_sasl=True or False (Defaults to False) +server=irc.oftc.net port=6697 +server_password=SERVERPASS channel_config=/path/to/yaml/config pid=/path/to/pid_file use_mqtt=True @@ -98,21 +100,7 @@ class Channel(object): self.last_used = time.time() -class GerritBot(SASL, SSL, irc.bot.SingleServerIRCBot): - def __init__(self, channels, nickname, password, server, port=6697): - super(GerritBot, self).__init__( - server_list=[(server, port)], - nickname=nickname, - realname=nickname, - ident_password=password) - self.all_channels = {} - for name in channels: - self.all_channels[name] = Channel(name) - self.joined_channels = {} - self.nickname = nickname - self.password = password - self.log = logging.getLogger('gerritbot') - +class BaseGerritBot(SSL, irc.bot.SingleServerIRCBot): def send(self, channel_name, msg): self.log.info('Sending "%s" to %s' % (msg, channel_name)) if channel_name not in self.joined_channels: @@ -148,6 +136,58 @@ class GerritBot(SASL, SSL, irc.bot.SingleServerIRCBot): self.connection.reconnect() +class NoSASLGerritBot(BaseGerritBot): + def __init__(self, channels, nickname, password, server, port=6697, + server_password=None): + super(NoSASLGerritBot, self).__init__( + server_list=[(server, port, server_password)], + nickname=nickname, + realname=nickname) + self.all_channels = {} + for name in channels: + self.all_channels[name] = Channel(name) + self.joined_channels = {} + self.nickname = nickname + self.password = password + self.log = logging.getLogger('gerritbot') + + def on_nicknameinuse(self, connection, event): + self.log.info('Nick previously in use, recovering.') + connection.nick(connection.get_nickname() + "_") + connection.privmsg("nickserv", "identify %s " % self.password) + connection.privmsg("nickserv", "ghost %s %s" % (self.nickname, + self.password)) + connection.privmsg("nickserv", "release %s %s" % (self.nickname, + self.password)) + time.sleep(1) + connection.nick(self.nickname) + self.log.info('Nick previously in use, recovered.') + + def on_welcome(self, connection, event): + self.log.info('Identifying with IRC server.') + connection.privmsg("nickserv", "identify %s " % self.password) + self.log.info('Identified with IRC server.') + self.joined_channels = {} + + +class SASLGerritBot(SASL, BaseGerritBot): + def __init__(self, channels, nickname, password, server, port=6697, + server_password=None): + super(SASLGerritBot, self).__init__( + channels=channels, + server_list=[(server, port)], + nickname=nickname, + realname=nickname, + ident_password=password) + self.all_channels = {} + for name in channels: + self.all_channels[name] = Channel(name) + self.joined_channels = {} + self.nickname = nickname + self.password = password + self.log = logging.getLogger('gerritbot') + + class Gerrit(threading.Thread): def __init__(self, ircbot, channel_config, server, username, port=29418, keyfile=None): @@ -455,11 +495,29 @@ def _main(config): log.exception("Syntax error in chanel config file") raise - bot = GerritBot(channel_config.channels, - config.get('ircbot', 'nick'), - config.get('ircbot', 'pass'), - config.get('ircbot', 'server'), - config.getint('ircbot', 'port')) + if config.has_option('ircbot', 'use_sasl'): + use_sasl = config.getboolean('ircbot', 'use_sasl') + else: + use_sasl = False + if config.has_option('ircbot', 'server_password'): + server_password = config.get('ircbot', 'server_password') + else: + server_password = None + if use_sasl: + bot = SASLGerritBot(channel_config.channels, + config.get('ircbot', 'nick'), + config.get('ircbot', 'pass'), + config.get('ircbot', 'server'), + config.getint('ircbot', 'port'), + server_password) + else: + bot = NoSASLGerritBot(channel_config.channels, + config.get('ircbot', 'nick'), + config.get('ircbot', 'pass'), + config.get('ircbot', 'server'), + config.getint('ircbot', 'port'), + server_password) + if config.has_option('ircbot', 'use_mqtt'): use_mqtt = config.getboolean('ircbot', 'use_mqtt') else: