Skip to content

Commit 8a11c82

Browse files
authored
Merge pull request #9 from ekilah/addChannelKeySupport
2 parents 2b6ea0f + f03e26f commit 8a11c82

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

irc_bot/simple_irc_bot.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,14 @@ def __init__(self, config):
6363
self.servers = config['servers']
6464
self.port = config['port']
6565
self.channels = {}
66+
self.channel_keys = {}
6667
for channel in config['channels']:
67-
self.add_irc_channel(channel)
68+
parts = channel.split(' ', 1)
69+
if len(parts) > 1:
70+
self.add_irc_channel(parts[0])
71+
self.add_irc_channel_key(parts[0], parts[1])
72+
else:
73+
self.add_irc_channel(channel)
6874
self.nickname = config.get('nickname', 'SimpleIRCBot-%s' % uuid.uuid4())
6975
self.invite_nickname = config.get('invite_nickname')
7076
self.invite_message = config.get('invite_message')
@@ -106,6 +112,14 @@ def add_irc_channel(self, name, status=None):
106112
return
107113
self.channels[name] = status
108114

115+
# some IRC channels have keys (channel passwords, +k mode)
116+
# you can provide one separated by a space after the channel name if necessary, and it will be used when joining the channel
117+
def add_irc_channel_key(self, name, key):
118+
name = name.lower() # doing this lower casing because other things here do it, it isn't relevant to keys specifically
119+
if name in self.channel_keys:
120+
return
121+
self.channel_keys[name] = key
122+
109123
def handle_expt_event(self):
110124
error = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
111125
if error == errno.ECONNREFUSED:
@@ -208,8 +222,13 @@ def join(self, channels, delay=None):
208222
channel = channel.lower()
209223
if channel in self.channels and self.channels[channel] in illegal_statuses:
210224
continue
211-
log.info('Joining channel: %s', channel)
212-
self.write('JOIN %s' % channel)
225+
226+
if channel in self.channel_keys:
227+
log.info('Joining channel with key: %s', channel)
228+
self.write('JOIN %s %s' % (channel, self.channel_keys[channel]))
229+
else:
230+
log.info('Joining channel: %s', channel)
231+
self.write('JOIN %s' % channel)
213232

214233
def found_terminator(self):
215234
lines = self._process_message(self.buffer)

0 commit comments

Comments
 (0)