summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremy Stanley <fungi@yuggoth.org>2018-10-05 17:07:41 +0000
committerJeremy Stanley <fungi@yuggoth.org>2018-10-05 17:13:59 +0000
commit57a669d35dc70d21be0b799e893535fc26d95c6d (patch)
treed31092b1b616a43f0f2074115ef474fd0b67a821
parentf32d742effb74d8e5f81673247411189aa7308e2 (diff)
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
Notes
Notes (review): Code-Review+2: Clark Boylan <cboylan@sapwetik.org> Code-Review+1: Alex Schultz <aschultz@redhat.com> Code-Review+2: Paul Belanger <pabelanger@redhat.com> Workflow+1: Paul Belanger <pabelanger@redhat.com> Workflow+1: Jeremy Stanley <fungi@yuggoth.org> Verified+2: Zuul Submitted-by: Zuul Submitted-at: Thu, 11 Oct 2018 20:39:54 +0000 Reviewed-on: https://review.openstack.org/608314 Project: openstack-infra/gerritbot Branch: refs/heads/master
-rw-r--r--doc/source/installation.rst4
-rwxr-xr-xgerritbot/bot.py51
-rw-r--r--requirements.txt1
3 files changed, 15 insertions, 41 deletions
diff --git a/doc/source/installation.rst b/doc/source/installation.rst
index 1c4c5d7..2cbc2da 100644
--- a/doc/source/installation.rst
+++ b/doc/source/installation.rst
@@ -16,9 +16,7 @@ when starting the bot. It should look like::
16 nick=NICKNAME 16 nick=NICKNAME
17 pass=PASSWORD 17 pass=PASSWORD
18 server=irc.freenode.net 18 server=irc.freenode.net
19 port=6667 19 port=6697
20 force_ssl=True or False (Defaults to False)
21 server_password=SERVERPASS
22 channel_config=/path/to/yaml/config (See below) 20 channel_config=/path/to/yaml/config (See below)
23 21
24 [gerrit] 22 [gerrit]
diff --git a/gerritbot/bot.py b/gerritbot/bot.py
index 8a60c71..d6cc226 100755
--- a/gerritbot/bot.py
+++ b/gerritbot/bot.py
@@ -21,9 +21,7 @@
21nick=NICKNAME 21nick=NICKNAME
22pass=PASSWORD 22pass=PASSWORD
23server=irc.freenode.net 23server=irc.freenode.net
24port=6667 24port=6697
25force_ssl=false
26server_password=SERVERPASS
27channel_config=/path/to/yaml/config 25channel_config=/path/to/yaml/config
28pid=/path/to/pid_file 26pid=/path/to/pid_file
29use_mqtt=True 27use_mqtt=True
@@ -57,12 +55,13 @@ openstack-dev:
57 55
58import ConfigParser 56import ConfigParser
59import daemon 57import daemon
58from ib3.auth import SASL
59from ib3.connection import SSL
60import irc.bot 60import irc.bot
61import json 61import json
62import logging.config 62import logging.config
63import os 63import os
64import re 64import re
65import ssl
66import sys 65import sys
67import threading 66import threading
68import time 67import time
@@ -98,17 +97,14 @@ class Channel(object):
98 self.last_used = time.time() 97 self.last_used = time.time()
99 98
100 99
101class GerritBot(irc.bot.SingleServerIRCBot): 100class GerritBot(SASL, SSL, irc.bot.SingleServerIRCBot):
102 def __init__(self, channels, nickname, password, server, port=6667, 101 def __init__(self, channels, nickname, password, server, port=6697):
103 force_ssl=False, server_password=None): 102 super(GerritBot, self).__init__(
104 if force_ssl or port == 6697: 103 server_list=[(server, port)],
105 factory = irc.connection.Factory(wrapper=ssl.wrap_socket) 104 nickname=nickname,
106 super(GerritBot, self).__init__([(server, port, server_password)], 105 realname=nickname,
107 nickname, nickname, 106 ident_password=password,
108 connect_factory=factory) 107 channels=channels)
109 else:
110 super(GerritBot, self).__init__([(server, port, server_password)],
111 nickname, nickname)
112 self.all_channels = {} 108 self.all_channels = {}
113 for name in channels: 109 for name in channels:
114 self.all_channels[name] = Channel(name) 110 self.all_channels[name] = Channel(name)
@@ -117,24 +113,6 @@ class GerritBot(irc.bot.SingleServerIRCBot):
117 self.password = password 113 self.password = password
118 self.log = logging.getLogger('gerritbot') 114 self.log = logging.getLogger('gerritbot')
119 115
120 def on_nicknameinuse(self, connection, event):
121 self.log.info('Nick previously in use, recovering.')
122 connection.nick(connection.get_nickname() + "_")
123 connection.privmsg("nickserv", "identify %s " % self.password)
124 connection.privmsg("nickserv", "ghost %s %s" % (self.nickname,
125 self.password))
126 connection.privmsg("nickserv", "release %s %s" % (self.nickname,
127 self.password))
128 time.sleep(1)
129 connection.nick(self.nickname)
130 self.log.info('Nick previously in use, recovered.')
131
132 def on_welcome(self, connection, event):
133 self.log.info('Identifying with IRC server.')
134 connection.privmsg("nickserv", "identify %s " % self.password)
135 self.log.info('Identified with IRC server.')
136 self.joined_channels = {}
137
138 def send(self, channel_name, msg): 116 def send(self, channel_name, msg):
139 self.log.info('Sending "%s" to %s' % (msg, channel_name)) 117 self.log.info('Sending "%s" to %s' % (msg, channel_name))
140 if channel_name not in self.joined_channels: 118 if channel_name not in self.joined_channels:
@@ -468,9 +446,7 @@ def _main(config):
468 config.get('ircbot', 'nick'), 446 config.get('ircbot', 'nick'),
469 config.get('ircbot', 'pass'), 447 config.get('ircbot', 'pass'),
470 config.get('ircbot', 'server'), 448 config.get('ircbot', 'server'),
471 config.getint('ircbot', 'port'), 449 config.getint('ircbot', 'port'))
472 config.getboolean('ircbot', 'force_ssl'),
473 config.get('ircbot', 'server_password'))
474 if config.has_option('ircbot', 'use_mqtt'): 450 if config.has_option('ircbot', 'use_mqtt'):
475 use_mqtt = config.getboolean('ircbot', 'use_mqtt') 451 use_mqtt = config.getboolean('ircbot', 'use_mqtt')
476 else: 452 else:
@@ -499,8 +475,7 @@ def main():
499 print("Usage: %s CONFIGFILE" % sys.argv[0]) 475 print("Usage: %s CONFIGFILE" % sys.argv[0])
500 sys.exit(1) 476 sys.exit(1)
501 477
502 config = ConfigParser.ConfigParser({'force_ssl': 'false', 478 config = ConfigParser.ConfigParser()
503 'server_password': None})
504 config.read(sys.argv[1]) 479 config.read(sys.argv[1])
505 480
506 pid_path = "" 481 pid_path = ""
diff --git a/requirements.txt b/requirements.txt
index 9c5c1d9..bde41b8 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -5,3 +5,4 @@ irc
5pyyaml 5pyyaml
6python-daemon 6python-daemon
7paho-mqtt>=1.2 7paho-mqtt>=1.2
8ib3