1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-02-04 07:51:50 +00:00

Reformat everything.

This commit is contained in:
Felix Fontein 2025-11-01 12:08:41 +01:00
parent 3f2213791a
commit 340ff8586d
1008 changed files with 61301 additions and 58309 deletions

View file

@ -183,36 +183,50 @@ from ansible.module_utils.common.text.converters import to_native, to_bytes
from ansible.module_utils.basic import AnsibleModule
def send_msg(msg, server='localhost', port='6667', channel=None, nick_to=None, key=None, topic=None,
nick="ansible", color='none', passwd=False, timeout=30, use_tls=False, validate_certs=True,
part=True, style=None):
'''send message to IRC'''
def send_msg(
msg,
server="localhost",
port="6667",
channel=None,
nick_to=None,
key=None,
topic=None,
nick="ansible",
color="none",
passwd=False,
timeout=30,
use_tls=False,
validate_certs=True,
part=True,
style=None,
):
"""send message to IRC"""
nick_to = [] if nick_to is None else nick_to
colornumbers = {
'white': "00",
'black': "01",
'blue': "02",
'green': "03",
'red': "04",
'brown': "05",
'purple': "06",
'orange': "07",
'yellow': "08",
'light_green': "09",
'teal': "10",
'light_cyan': "11",
'light_blue': "12",
'pink': "13",
'gray': "14",
'light_gray': "15",
"white": "00",
"black": "01",
"blue": "02",
"green": "03",
"red": "04",
"brown": "05",
"purple": "06",
"orange": "07",
"yellow": "08",
"light_green": "09",
"teal": "10",
"light_cyan": "11",
"light_blue": "12",
"pink": "13",
"gray": "14",
"light_gray": "15",
}
stylechoices = {
'bold': "\x02",
'underline': "\x1F",
'reverse': "\x16",
'italic': "\x1D",
"bold": "\x02",
"underline": "\x1f",
"reverse": "\x16",
"italic": "\x1d",
}
try:
@ -241,56 +255,57 @@ def send_msg(msg, server='localhost', port='6667', channel=None, nick_to=None, k
irc.connect((server, int(port)))
if passwd:
irc.send(to_bytes(f'PASS {passwd}\r\n'))
irc.send(to_bytes(f'NICK {nick}\r\n'))
irc.send(to_bytes(f'USER {nick} {nick} {nick} :ansible IRC\r\n'))
motd = ''
irc.send(to_bytes(f"PASS {passwd}\r\n"))
irc.send(to_bytes(f"NICK {nick}\r\n"))
irc.send(to_bytes(f"USER {nick} {nick} {nick} :ansible IRC\r\n"))
motd = ""
start = time.time()
while 1:
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)
match = re.search(r"^:\S+ 00[1-4] (?P<nick>\S+) :", motd, flags=re.M)
if match:
nick = match.group('nick')
nick = match.group("nick")
break
elif time.time() - start > timeout:
raise Exception('Timeout waiting for IRC server welcome response')
raise Exception("Timeout waiting for IRC server welcome response")
time.sleep(0.5)
if channel:
if key:
irc.send(to_bytes(f'JOIN {channel} {key}\r\n'))
irc.send(to_bytes(f"JOIN {channel} {key}\r\n"))
else:
irc.send(to_bytes(f'JOIN {channel}\r\n'))
irc.send(to_bytes(f"JOIN {channel}\r\n"))
join = ''
join = ""
start = time.time()
while 1:
join += to_native(irc.recv(1024))
if re.search(rf'^:\S+ 366 {nick} {channel} :', join, flags=re.M | re.I):
if re.search(rf"^:\S+ 366 {nick} {channel} :", join, flags=re.M | re.I):
break
elif time.time() - start > timeout:
raise Exception('Timeout waiting for IRC JOIN response')
raise Exception("Timeout waiting for IRC JOIN response")
time.sleep(0.5)
if topic is not None:
irc.send(to_bytes(f'TOPIC {channel} :{topic}\r\n'))
irc.send(to_bytes(f"TOPIC {channel} :{topic}\r\n"))
time.sleep(1)
if nick_to:
for nick in nick_to:
irc.send(to_bytes(f'PRIVMSG {nick} :{message}\r\n'))
irc.send(to_bytes(f"PRIVMSG {nick} :{message}\r\n"))
if channel:
irc.send(to_bytes(f'PRIVMSG {channel} :{message}\r\n'))
irc.send(to_bytes(f"PRIVMSG {channel} :{message}\r\n"))
time.sleep(1)
if part:
if channel:
irc.send(to_bytes(f'PART {channel}\r\n'))
irc.send(to_bytes('QUIT\r\n'))
irc.send(to_bytes(f"PART {channel}\r\n"))
irc.send(to_bytes("QUIT\r\n"))
time.sleep(1)
irc.close()
# ===========================================
# Main
#
@ -299,29 +314,46 @@ def send_msg(msg, server='localhost', port='6667', channel=None, nick_to=None, k
def main():
module = AnsibleModule(
argument_spec=dict(
server=dict(default='localhost'),
port=dict(type='int', default=6667),
nick=dict(default='ansible'),
nick_to=dict(type='list', elements='str'),
server=dict(default="localhost"),
port=dict(type="int", default=6667),
nick=dict(default="ansible"),
nick_to=dict(type="list", elements="str"),
msg=dict(required=True),
color=dict(default="none", aliases=['colour'], choices=["white", "black", "blue",
"green", "red", "brown",
"purple", "orange", "yellow",
"light_green", "teal", "light_cyan",
"light_blue", "pink", "gray",
"light_gray", "none"]),
color=dict(
default="none",
aliases=["colour"],
choices=[
"white",
"black",
"blue",
"green",
"red",
"brown",
"purple",
"orange",
"yellow",
"light_green",
"teal",
"light_cyan",
"light_blue",
"pink",
"gray",
"light_gray",
"none",
],
),
style=dict(default="none", choices=["underline", "reverse", "bold", "italic", "none"]),
channel=dict(),
key=dict(no_log=True),
topic=dict(),
passwd=dict(no_log=True),
timeout=dict(type='int', default=30),
part=dict(type='bool', default=True),
use_tls=dict(type='bool', default=True, aliases=['use_ssl']),
validate_certs=dict(type='bool', default=True),
timeout=dict(type="int", default=30),
part=dict(type="bool", default=True),
use_tls=dict(type="bool", default=True, aliases=["use_ssl"]),
validate_certs=dict(type="bool", default=True),
),
supports_check_mode=True,
required_one_of=[['channel', 'nick_to']]
required_one_of=[["channel", "nick_to"]],
)
server = module.params["server"]
@ -343,13 +375,28 @@ def main():
validate_certs = module.params["validate_certs"]
try:
send_msg(msg, server, port, channel, nick_to, key, topic, nick, color, passwd, timeout, use_tls, validate_certs, part, style)
send_msg(
msg,
server,
port,
channel,
nick_to,
key,
topic,
nick,
color,
passwd,
timeout,
use_tls,
validate_certs,
part,
style,
)
except Exception as e:
module.fail_json(msg=f"unable to send to IRC: {e}", exception=traceback.format_exc())
module.exit_json(changed=False, channel=channel, nick=nick,
msg=msg)
module.exit_json(changed=False, channel=channel, nick=nick, msg=msg)
if __name__ == '__main__':
if __name__ == "__main__":
main()