Don't send JOIN and PART commands if channel is not set (#62334)

Currently, the irc module is sending JOIN and PART commands even if the channel
key is not set in the irc module. This causes the module to send JOIN and PART
commands to the #None channel. This module can send PRIVMSGs to either a
channel or a list of nicks, so sending JOIN and PART doesn't make sense in
cases where channel is undefined. The same goes for the TOPIC command, which is
used to set a topic for channels.

This patch ensures that the JOIN, PART and TOPIC commands are only sent if the
channel is defined.
pull/67442/head
Ronnie P. Thomas 5 years ago committed by GitHub
parent cc86748109
commit 807f4ea658
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -17,9 +17,9 @@ DOCUMENTATION = '''
--- ---
module: irc module: irc
version_added: "1.2" version_added: "1.2"
short_description: Send a message to an IRC channel short_description: Send a message to an IRC channel or a nick
description: description:
- Send a message to an IRC channel. This is a very simplistic implementation. - Send a message to an IRC channel or a nick. This is a very simplistic implementation.
options: options:
server: server:
description: description:
@ -204,24 +204,25 @@ def send_msg(msg, server='localhost', port='6667', channel=None, nick_to=None, k
raise Exception('Timeout waiting for IRC server welcome response') raise Exception('Timeout waiting for IRC server welcome response')
time.sleep(0.5) time.sleep(0.5)
if key: if channel:
irc.send(to_bytes('JOIN %s %s\r\n' % (channel, key))) if key:
else: irc.send(to_bytes('JOIN %s %s\r\n' % (channel, key)))
irc.send(to_bytes('JOIN %s\r\n' % channel)) else:
irc.send(to_bytes('JOIN %s\r\n' % channel))
join = ''
start = time.time() join = ''
while 1: start = time.time()
join += to_native(irc.recv(1024)) while 1:
if re.search(r'^:\S+ 366 %s %s :' % (nick, channel), join, flags=re.M | re.I): join += to_native(irc.recv(1024))
break if re.search(r'^:\S+ 366 %s %s :' % (nick, channel), join, flags=re.M | re.I):
elif time.time() - start > timeout: break
raise Exception('Timeout waiting for IRC JOIN response') elif time.time() - start > timeout:
time.sleep(0.5) raise Exception('Timeout waiting for IRC JOIN response')
time.sleep(0.5)
if topic is not None:
irc.send(to_bytes('TOPIC %s :%s\r\n' % (channel, topic))) if topic is not None:
time.sleep(1) irc.send(to_bytes('TOPIC %s :%s\r\n' % (channel, topic)))
time.sleep(1)
if nick_to: if nick_to:
for nick in nick_to: for nick in nick_to:
@ -230,7 +231,8 @@ def send_msg(msg, server='localhost', port='6667', channel=None, nick_to=None, k
irc.send(to_bytes('PRIVMSG %s :%s\r\n' % (channel, message))) irc.send(to_bytes('PRIVMSG %s :%s\r\n' % (channel, message)))
time.sleep(1) time.sleep(1)
if part: if part:
irc.send(to_bytes('PART %s\r\n' % channel)) if channel:
irc.send(to_bytes('PART %s\r\n' % channel))
irc.send(to_bytes('QUIT\r\n')) irc.send(to_bytes('QUIT\r\n'))
time.sleep(1) time.sleep(1)
irc.close() irc.close()

Loading…
Cancel
Save