From 1c08eb8b2753f4451c08d63a9d70fd966046da2b Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Tue, 10 Jul 2018 09:42:14 -0400 Subject: [PATCH] fix irc module to work with py3 (#42267) * fix irc module to work with py3 fixes #42256 --- changelogs/fragments/irc_py3_compat.yaml | 2 ++ lib/ansible/modules/notification/irc.py | 31 ++++++++++++------------ 2 files changed, 18 insertions(+), 15 deletions(-) create mode 100644 changelogs/fragments/irc_py3_compat.yaml diff --git a/changelogs/fragments/irc_py3_compat.yaml b/changelogs/fragments/irc_py3_compat.yaml new file mode 100644 index 0000000000..69fa37d67d --- /dev/null +++ b/changelogs/fragments/irc_py3_compat.yaml @@ -0,0 +1,2 @@ +bugfixes: + - made irc module python3 compatible https://github.com/ansible/ansible/issues/42256 diff --git a/lib/ansible/modules/notification/irc.py b/lib/ansible/modules/notification/irc.py index 7786dde375..3172c25a20 100644 --- a/lib/ansible/modules/notification/irc.py +++ b/lib/ansible/modules/notification/irc.py @@ -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\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()