Browse Source

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
changes/14/608314/1
Jeremy Stanley 8 months ago
parent
commit
57a669d35d
3 changed files with 15 additions and 41 deletions
  1. 1
    3
      doc/source/installation.rst
  2. 13
    38
      gerritbot/bot.py
  3. 1
    0
      requirements.txt

+ 1
- 3
doc/source/installation.rst View File

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

+ 13
- 38
gerritbot/bot.py View File

@@ -21,9 +21,7 @@
21 21
 nick=NICKNAME
22 22
 pass=PASSWORD
23 23
 server=irc.freenode.net
24
-port=6667
25
-force_ssl=false
26
-server_password=SERVERPASS
24
+port=6697
27 25
 channel_config=/path/to/yaml/config
28 26
 pid=/path/to/pid_file
29 27
 use_mqtt=True
@@ -57,12 +55,13 @@ openstack-dev:
57 55
 
58 56
 import ConfigParser
59 57
 import daemon
58
+from ib3.auth import SASL
59
+from ib3.connection import SSL
60 60
 import irc.bot
61 61
 import json
62 62
 import logging.config
63 63
 import os
64 64
 import re
65
-import ssl
66 65
 import sys
67 66
 import threading
68 67
 import time
@@ -98,17 +97,14 @@ class Channel(object):
98 97
         self.last_used = time.time()
99 98
 
100 99
 
101
-class GerritBot(irc.bot.SingleServerIRCBot):
102
-    def __init__(self, channels, nickname, password, server, port=6667,
103
-                 force_ssl=False, server_password=None):
104
-        if force_ssl or port == 6697:
105
-            factory = irc.connection.Factory(wrapper=ssl.wrap_socket)
106
-            super(GerritBot, self).__init__([(server, port, server_password)],
107
-                                            nickname, nickname,
108
-                                            connect_factory=factory)
109
-        else:
110
-            super(GerritBot, self).__init__([(server, port, server_password)],
111
-                                            nickname, nickname)
100
+class GerritBot(SASL, SSL, irc.bot.SingleServerIRCBot):
101
+    def __init__(self, channels, nickname, password, server, port=6697):
102
+        super(GerritBot, self).__init__(
103
+            server_list=[(server, port)],
104
+            nickname=nickname,
105
+            realname=nickname,
106
+            ident_password=password,
107
+            channels=channels)
112 108
         self.all_channels = {}
113 109
         for name in channels:
114 110
             self.all_channels[name] = Channel(name)
@@ -117,24 +113,6 @@ class GerritBot(irc.bot.SingleServerIRCBot):
117 113
         self.password = password
118 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 116
     def send(self, channel_name, msg):
139 117
         self.log.info('Sending "%s" to %s' % (msg, channel_name))
140 118
         if channel_name not in self.joined_channels:
@@ -468,9 +446,7 @@ def _main(config):
468 446
                     config.get('ircbot', 'nick'),
469 447
                     config.get('ircbot', 'pass'),
470 448
                     config.get('ircbot', 'server'),
471
-                    config.getint('ircbot', 'port'),
472
-                    config.getboolean('ircbot', 'force_ssl'),
473
-                    config.get('ircbot', 'server_password'))
449
+                    config.getint('ircbot', 'port'))
474 450
     if config.has_option('ircbot', 'use_mqtt'):
475 451
         use_mqtt = config.getboolean('ircbot', 'use_mqtt')
476 452
     else:
@@ -499,8 +475,7 @@ def main():
499 475
         print("Usage: %s CONFIGFILE" % sys.argv[0])
500 476
         sys.exit(1)
501 477
 
502
-    config = ConfigParser.ConfigParser({'force_ssl': 'false',
503
-                                        'server_password': None})
478
+    config = ConfigParser.ConfigParser()
504 479
     config.read(sys.argv[1])
505 480
 
506 481
     pid_path = ""

+ 1
- 0
requirements.txt View File

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

Loading…
Cancel
Save