Skip to content

Commit

Permalink
Merge pull request #9 from ekilah/addChannelKeySupport
Browse files Browse the repository at this point in the history
  • Loading branch information
cvium authored Jan 21, 2023
2 parents 2b6ea0f + f03e26f commit 8a11c82
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions irc_bot/simple_irc_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,14 @@ def __init__(self, config):
self.servers = config['servers']
self.port = config['port']
self.channels = {}
self.channel_keys = {}
for channel in config['channels']:
self.add_irc_channel(channel)
parts = channel.split(' ', 1)
if len(parts) > 1:
self.add_irc_channel(parts[0])
self.add_irc_channel_key(parts[0], parts[1])
else:
self.add_irc_channel(channel)
self.nickname = config.get('nickname', 'SimpleIRCBot-%s' % uuid.uuid4())
self.invite_nickname = config.get('invite_nickname')
self.invite_message = config.get('invite_message')
Expand Down Expand Up @@ -106,6 +112,14 @@ def add_irc_channel(self, name, status=None):
return
self.channels[name] = status

# some IRC channels have keys (channel passwords, +k mode)
# you can provide one separated by a space after the channel name if necessary, and it will be used when joining the channel
def add_irc_channel_key(self, name, key):
name = name.lower() # doing this lower casing because other things here do it, it isn't relevant to keys specifically
if name in self.channel_keys:
return
self.channel_keys[name] = key

def handle_expt_event(self):
error = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
if error == errno.ECONNREFUSED:
Expand Down Expand Up @@ -208,8 +222,13 @@ def join(self, channels, delay=None):
channel = channel.lower()
if channel in self.channels and self.channels[channel] in illegal_statuses:
continue
log.info('Joining channel: %s', channel)
self.write('JOIN %s' % channel)

if channel in self.channel_keys:
log.info('Joining channel with key: %s', channel)
self.write('JOIN %s %s' % (channel, self.channel_keys[channel]))
else:
log.info('Joining channel: %s', channel)
self.write('JOIN %s' % channel)

def found_terminator(self):
lines = self._process_message(self.buffer)
Expand Down

0 comments on commit 8a11c82

Please sign in to comment.