fix irc module to work with py3 (#42267)

* fix irc module to work with py3

fixes #42256
pull/42576/merge
Brian Coca 6 years ago committed by GitHub
parent 4a7940c562
commit 1c08eb8b27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- made irc module python3 compatible https://github.com/ansible/ansible/issues/42256

@ -133,8 +133,8 @@ import ssl
import time
import traceback
from ansible.module_utils._text import to_native, to_bytes
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_native
def send_msg(msg, server='localhost', port='6667', channel=None, nick_to=None, key=None, topic=None,
@ -170,13 +170,13 @@ def send_msg(msg, server='localhost', port='6667', channel=None, nick_to=None, k
try:
styletext = stylechoices[style]
except:
except Exception:
styletext = ""
try:
colornumber = colornumbers[color]
colortext = "\x03" + colornumber
except:
except Exception:
colortext = ""
message = styletext + colortext + msg
@ -185,14 +185,15 @@ def send_msg(msg, server='localhost', port='6667', channel=None, nick_to=None, k
if use_ssl:
irc = ssl.wrap_socket(irc)
irc.connect((server, int(port)))
if passwd:
irc.send('PASS %s\r\n' % passwd)
irc.send('NICK %s\r\n' % nick)
irc.send('USER %s %s %s :ansible IRC\r\n' % (nick, nick, nick))
irc.send(to_bytes('PASS %s\r\n' % passwd))
irc.send(to_bytes('NICK %s\r\n' % nick))
irc.send(to_bytes('USER %s %s %s :ansible IRC\r\n' % (nick, nick, nick)))
motd = ''
start = time.time()
while 1:
motd += irc.recv(1024)
motd += to_native(irc.recv(1024))
# The server might send back a shorter nick than we specified (due to NICKLEN),
# so grab that and use it from now on (assuming we find the 00[1-4] response).
match = re.search(r'^:\S+ 00[1-4] (?P<nick>\S+) :', motd, flags=re.M)
@ -204,14 +205,14 @@ def send_msg(msg, server='localhost', port='6667', channel=None, nick_to=None, k
time.sleep(0.5)
if key:
irc.send('JOIN %s %s\r\n' % (channel, key))
irc.send(to_bytes('JOIN %s %s\r\n' % (channel, key)))
else:
irc.send('JOIN %s\r\n' % channel)
irc.send(to_bytes('JOIN %s\r\n' % channel))
join = ''
start = time.time()
while 1:
join += irc.recv(1024)
join += to_native(irc.recv(1024))
if re.search(r'^:\S+ 366 %s %s :' % (nick, channel), join, flags=re.M):
break
elif time.time() - start > timeout:
@ -219,18 +220,18 @@ def send_msg(msg, server='localhost', port='6667', channel=None, nick_to=None, k
time.sleep(0.5)
if topic is not None:
irc.send('TOPIC %s :%s\r\n' % (channel, topic))
irc.send(to_bytes('TOPIC %s :%s\r\n' % (channel, topic)))
time.sleep(1)
if nick_to:
for nick in nick_to:
irc.send('PRIVMSG %s :%s\r\n' % (nick, message))
irc.send(to_bytes('PRIVMSG %s :%s\r\n' % (nick, message)))
if channel:
irc.send('PRIVMSG %s :%s\r\n' % (channel, message))
irc.send(to_bytes('PRIVMSG %s :%s\r\n' % (channel, message)))
time.sleep(1)
if part:
irc.send('PART %s\r\n' % channel)
irc.send('QUIT\r\n')
irc.send(to_bytes('PART %s\r\n' % channel))
irc.send(to_bytes('QUIT\r\n'))
time.sleep(1)
irc.close()

Loading…
Cancel
Save