Identify with SASL

Identify through SASL using the convenient ib3 mixins, and
get rid of a bunch of special-case code in the process.

This helps when dealing with channels set to require identified
users, as otherwise channel joins will race NickServ's processing of
the identify message and some channels will end up not serviced by
the bot (an alternative would be to delay joining channels until the
identify success is confirmed, but the implementation for that looks
like it would be at least as complex).

Change-Id: I1a01eed8102b59818df247a93fbe4bf50148e76a
Co-Authored-By: Thierry Carrez <thierry@openstack.org>
Depends-On: https://review.openstack.org/608313
This commit is contained in:
Jeremy Stanley 2018-10-05 17:07:41 +00:00
parent f32d742eff
commit 57a669d35d
3 changed files with 15 additions and 41 deletions

View File

@ -16,9 +16,7 @@ when starting the bot. It should look like::
nick=NICKNAME nick=NICKNAME
pass=PASSWORD pass=PASSWORD
server=irc.freenode.net server=irc.freenode.net
port=6667 port=6697
force_ssl=True or False (Defaults to False)
server_password=SERVERPASS
channel_config=/path/to/yaml/config (See below) channel_config=/path/to/yaml/config (See below)
[gerrit] [gerrit]

View File

@ -21,9 +21,7 @@
nick=NICKNAME nick=NICKNAME
pass=PASSWORD pass=PASSWORD
server=irc.freenode.net server=irc.freenode.net
port=6667 port=6697
force_ssl=false
server_password=SERVERPASS
channel_config=/path/to/yaml/config channel_config=/path/to/yaml/config
pid=/path/to/pid_file pid=/path/to/pid_file
use_mqtt=True use_mqtt=True
@ -57,12 +55,13 @@ openstack-dev:
import ConfigParser import ConfigParser
import daemon import daemon
from ib3.auth import SASL
from ib3.connection import SSL
import irc.bot import irc.bot
import json import json
import logging.config import logging.config
import os import os
import re import re
import ssl
import sys import sys
import threading import threading
import time import time
@ -98,17 +97,14 @@ class Channel(object):
self.last_used = time.time() self.last_used = time.time()
class GerritBot(irc.bot.SingleServerIRCBot): class GerritBot(SASL, SSL, irc.bot.SingleServerIRCBot):
def __init__(self, channels, nickname, password, server, port=6667, def __init__(self, channels, nickname, password, server, port=6697):
force_ssl=False, server_password=None): super(GerritBot, self).__init__(
if force_ssl or port == 6697: server_list=[(server, port)],
factory = irc.connection.Factory(wrapper=ssl.wrap_socket) nickname=nickname,
super(GerritBot, self).__init__([(server, port, server_password)], realname=nickname,
nickname, nickname, ident_password=password,
connect_factory=factory) channels=channels)
else:
super(GerritBot, self).__init__([(server, port, server_password)],
nickname, nickname)
self.all_channels = {} self.all_channels = {}
for name in channels: for name in channels:
self.all_channels[name] = Channel(name) self.all_channels[name] = Channel(name)
@ -117,24 +113,6 @@ class GerritBot(irc.bot.SingleServerIRCBot):
self.password = password self.password = password
self.log = logging.getLogger('gerritbot') 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 = {}
def send(self, channel_name, msg): def send(self, channel_name, msg):
self.log.info('Sending "%s" to %s' % (msg, channel_name)) self.log.info('Sending "%s" to %s' % (msg, channel_name))
if channel_name not in self.joined_channels: if channel_name not in self.joined_channels:
@ -468,9 +446,7 @@ def _main(config):
config.get('ircbot', 'nick'), config.get('ircbot', 'nick'),
config.get('ircbot', 'pass'), config.get('ircbot', 'pass'),
config.get('ircbot', 'server'), config.get('ircbot', 'server'),
config.getint('ircbot', 'port'), config.getint('ircbot', 'port'))
config.getboolean('ircbot', 'force_ssl'),
config.get('ircbot', 'server_password'))
if config.has_option('ircbot', 'use_mqtt'): if config.has_option('ircbot', 'use_mqtt'):
use_mqtt = config.getboolean('ircbot', 'use_mqtt') use_mqtt = config.getboolean('ircbot', 'use_mqtt')
else: else:
@ -499,8 +475,7 @@ def main():
print("Usage: %s CONFIGFILE" % sys.argv[0]) print("Usage: %s CONFIGFILE" % sys.argv[0])
sys.exit(1) sys.exit(1)
config = ConfigParser.ConfigParser({'force_ssl': 'false', config = ConfigParser.ConfigParser()
'server_password': None})
config.read(sys.argv[1]) config.read(sys.argv[1])
pid_path = "" pid_path = ""

View File

@ -5,3 +5,4 @@ irc
pyyaml pyyaml
python-daemon python-daemon
paho-mqtt>=1.2 paho-mqtt>=1.2
ib3