| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2013, Jan-Piet Mens <jpmens () gmail.com> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: irc |
| 11 |
short_description: Send a message to an IRC channel or a nick |
| 12 |
description: |
| 13 |
- Send a message to an IRC channel or a nick. This is a very simplistic implementation. |
| 14 |
extends_documentation_fragment: |
| 15 |
- community.general._attributes |
| 16 |
attributes: |
| 17 |
check_mode: |
| 18 |
support: full |
| 19 |
diff_mode: |
| 20 |
support: none |
| 21 |
options: |
| 22 |
server: |
| 23 |
type: str |
| 24 |
description: |
| 25 |
- IRC server name/address. |
| 26 |
default: localhost |
| 27 |
port: |
| 28 |
type: int |
| 29 |
description: |
| 30 |
- IRC server port number. |
| 31 |
default: 6667 |
| 32 |
nick: |
| 33 |
type: str |
| 34 |
description: |
| 35 |
- Nickname to send the message from. May be shortened, depending on server's NICKLEN setting. |
| 36 |
default: ansible |
| 37 |
msg: |
| 38 |
type: str |
| 39 |
description: |
| 40 |
- The message body. |
| 41 |
required: true |
| 42 |
topic: |
| 43 |
type: str |
| 44 |
description: |
| 45 |
- Set the channel topic. |
| 46 |
color: |
| 47 |
type: str |
| 48 |
description: |
| 49 |
- Text color for the message. |
| 50 |
default: "none" |
| 51 |
choices: |
| 52 |
- none |
| 53 |
- white |
| 54 |
- black |
| 55 |
- blue |
| 56 |
- green |
| 57 |
- red |
| 58 |
- brown |
| 59 |
- purple |
| 60 |
- orange |
| 61 |
- yellow |
| 62 |
- light_green |
| 63 |
- teal |
| 64 |
- light_cyan |
| 65 |
- light_blue |
| 66 |
- pink |
| 67 |
- gray |
| 68 |
- light_gray |
| 69 |
aliases: [colour] |
| 70 |
channel: |
| 71 |
type: str |
| 72 |
description: |
| 73 |
- Channel name. One of O(nick_to) or O(channel) needs to be set. When both are set, the message is sent to both of them. |
| 74 |
nick_to: |
| 75 |
type: list |
| 76 |
elements: str |
| 77 |
description: |
| 78 |
- A list of nicknames to send the message to. One of O(nick_to) or O(channel) needs to be set. When both are defined, the |
| 79 |
message is sent to both of them. |
| 80 |
key: |
| 81 |
type: str |
| 82 |
description: |
| 83 |
- Channel key. |
| 84 |
passwd: |
| 85 |
type: str |
| 86 |
description: |
| 87 |
- Server password. |
| 88 |
timeout: |
| 89 |
type: int |
| 90 |
description: |
| 91 |
- Timeout to use while waiting for successful registration and join messages, this is to prevent an endless loop. |
| 92 |
default: 30 |
| 93 |
use_tls: |
| 94 |
description: |
| 95 |
- Designates whether TLS/SSL should be used when connecting to the IRC server. |
| 96 |
- O(use_tls) is available since community.general 8.1.0, before the option was exclusively called O(use_ssl). The latter |
| 97 |
is now an alias of O(use_tls). |
| 98 |
- B(Note:) for security reasons, you should always set O(use_tls=true) and O(validate_certs=true) whenever possible. |
| 99 |
- The default of this option changed to V(true) in community.general 10.0.0. |
| 100 |
type: bool |
| 101 |
default: true |
| 102 |
aliases: |
| 103 |
- use_ssl |
| 104 |
part: |
| 105 |
description: |
| 106 |
- Designates whether user should part from channel after sending message or not. Useful for when using a mock bot and |
| 107 |
not wanting join/parts between messages. |
| 108 |
type: bool |
| 109 |
default: true |
| 110 |
style: |
| 111 |
type: str |
| 112 |
description: |
| 113 |
- Text style for the message. Note italic does not work on some clients. |
| 114 |
choices: ["bold", "underline", "reverse", "italic", "none"] |
| 115 |
default: none |
| 116 |
validate_certs: |
| 117 |
description: |
| 118 |
- If set to V(false), the SSL certificates are not validated. |
| 119 |
- This should always be set to V(true). Using V(false) is unsafe and should only be done if the network between between |
| 120 |
Ansible and the IRC server is known to be safe. |
| 121 |
- B(Note:) for security reasons, you should always set O(use_tls=true) and O(validate_certs=true) whenever possible. |
| 122 |
- The default of this option changed to V(true) in community.general 10.0.0. |
| 123 |
type: bool |
| 124 |
default: true |
| 125 |
version_added: 8.1.0 |
| 126 |
|
| 127 |
# informational: requirements for nodes |
| 128 |
requirements: [socket] |
| 129 |
author: |
| 130 |
- "Jan-Piet Mens (@jpmens)" |
| 131 |
- "Matt Martz (@sivel)" |
| 132 |
""" |
| 133 |
|
| 134 |
EXAMPLES = r""" |
| 135 |
- name: Send a message to an IRC channel from nick ansible |
| 136 |
community.general.irc: |
| 137 |
server: irc.example.net |
| 138 |
use_tls: true |
| 139 |
validate_certs: true |
| 140 |
channel: '#t1' |
| 141 |
msg: Hello world |
| 142 |
|
| 143 |
- name: Send a message to an IRC channel |
| 144 |
local_action: |
| 145 |
module: irc |
| 146 |
port: 6669 |
| 147 |
server: irc.example.net |
| 148 |
use_tls: true |
| 149 |
validate_certs: true |
| 150 |
channel: '#t1' |
| 151 |
msg: 'All finished at {{ ansible_date_time.iso8601 }}' |
| 152 |
color: red |
| 153 |
nick: ansibleIRC |
| 154 |
|
| 155 |
- name: Send a message to an IRC channel |
| 156 |
local_action: |
| 157 |
module: irc |
| 158 |
port: 6669 |
| 159 |
server: irc.example.net |
| 160 |
use_tls: true |
| 161 |
validate_certs: true |
| 162 |
channel: '#t1' |
| 163 |
nick_to: |
| 164 |
- nick1 |
| 165 |
- nick2 |
| 166 |
msg: 'All finished at {{ ansible_date_time.iso8601 }}' |
| 167 |
color: red |
| 168 |
nick: ansibleIRC |
| 169 |
""" |
| 170 |
|
| 171 |
# =========================================== |
| 172 |
# IRC module support methods. |
| 173 |
# |
| 174 |
|
| 175 |
import re |
| 176 |
import socket |
| 177 |
import ssl |
| 178 |
import time |
| 179 |
import traceback |
| 180 |
|
| 181 |
from ansible.module_utils.basic import AnsibleModule |
| 182 |
from ansible.module_utils.common.text.converters import to_bytes, to_native |
| 183 |
|
| 184 |
|
| 185 |
def send_msg( |
| 186 |
msg, |
| 187 |
server="localhost", |
| 188 |
port="6667", |
| 189 |
channel=None, |
| 190 |
nick_to=None, |
| 191 |
key=None, |
| 192 |
topic=None, |
| 193 |
nick="ansible", |
| 194 |
color="none", |
| 195 |
passwd=False, |
| 196 |
timeout=30, |
| 197 |
use_tls=False, |
| 198 |
validate_certs=True, |
| 199 |
part=True, |
| 200 |
style=None, |
| 201 |
): |
| 202 |
"""send message to IRC""" |
| 203 |
nick_to = [] if nick_to is None else nick_to |
| 204 |
|
| 205 |
colornumbers = { |
| 206 |
"white": "00", |
| 207 |
"black": "01", |
| 208 |
"blue": "02", |
| 209 |
"green": "03", |
| 210 |
"red": "04", |
| 211 |
"brown": "05", |
| 212 |
"purple": "06", |
| 213 |
"orange": "07", |
| 214 |
"yellow": "08", |
| 215 |
"light_green": "09", |
| 216 |
"teal": "10", |
| 217 |
"light_cyan": "11", |
| 218 |
"light_blue": "12", |
| 219 |
"pink": "13", |
| 220 |
"gray": "14", |
| 221 |
"light_gray": "15", |
| 222 |
} |
| 223 |
|
| 224 |
stylechoices = { |
| 225 |
"bold": "\x02", |
| 226 |
"underline": "\x1f", |
| 227 |
"reverse": "\x16", |
| 228 |
"italic": "\x1d", |
| 229 |
} |
| 230 |
|
| 231 |
try: |
| 232 |
styletext = stylechoices[style] |
| 233 |
except Exception: |
| 234 |
styletext = "" |
| 235 |
|
| 236 |
try: |
| 237 |
colornumber = colornumbers[color] |
| 238 |
colortext = f"\x03{colornumber}" |
| 239 |
except Exception: |
| 240 |
colortext = "" |
| 241 |
|
| 242 |
message = styletext + colortext + msg |
| 243 |
|
| 244 |
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 245 |
if use_tls: |
| 246 |
kwargs = {} |
| 247 |
if validate_certs: |
| 248 |
context = ssl.create_default_context() |
| 249 |
kwargs["server_hostname"] = server |
| 250 |
else: |
| 251 |
context = ssl.SSLContext(ssl.PROTOCOL_TLS) |
| 252 |
context.verify_mode = ssl.CERT_NONE |
| 253 |
irc = context.wrap_socket(irc, **kwargs) |
| 254 |
irc.connect((server, int(port))) |
| 255 |
|
| 256 |
if passwd: |
| 257 |
irc.send(to_bytes(f"PASS {passwd}\r\n")) |
| 258 |
irc.send(to_bytes(f"NICK {nick}\r\n")) |
| 259 |
irc.send(to_bytes(f"USER {nick} {nick} {nick} :ansible IRC\r\n")) |
| 260 |
motd = "" |
| 261 |
start = time.time() |
| 262 |
while True: |
| 263 |
motd += to_native(irc.recv(1024)) |
| 264 |
# The server might send back a shorter nick than we specified (due to NICKLEN), |
| 265 |
# so grab that and use it from now on (assuming we find the 00[1-4] response). |
| 266 |
match = re.search(r"^:\S+ 00[1-4] (?P<nick>\S+) :", motd, flags=re.M) |
| 267 |
if match: |
| 268 |
nick = match.group("nick") |
| 269 |
break |
| 270 |
elif time.time() - start > timeout: |
| 271 |
raise Exception("Timeout waiting for IRC server welcome response") |
| 272 |
time.sleep(0.5) |
| 273 |
|
| 274 |
if channel: |
| 275 |
if key: |
| 276 |
irc.send(to_bytes(f"JOIN {channel} {key}\r\n")) |
| 277 |
else: |
| 278 |
irc.send(to_bytes(f"JOIN {channel}\r\n")) |
| 279 |
|
| 280 |
join = "" |
| 281 |
start = time.time() |
| 282 |
while True: |
| 283 |
join += to_native(irc.recv(1024)) |
| 284 |
if re.search(rf"^:\S+ 366 {nick} {channel} :", join, flags=re.M | re.I): |
| 285 |
break |
| 286 |
elif time.time() - start > timeout: |
| 287 |
raise Exception("Timeout waiting for IRC JOIN response") |
| 288 |
time.sleep(0.5) |
| 289 |
|
| 290 |
if topic is not None: |
| 291 |
irc.send(to_bytes(f"TOPIC {channel} :{topic}\r\n")) |
| 292 |
time.sleep(1) |
| 293 |
|
| 294 |
if nick_to: |
| 295 |
for nick in nick_to: |
| 296 |
irc.send(to_bytes(f"PRIVMSG {nick} :{message}\r\n")) |
| 297 |
if channel: |
| 298 |
irc.send(to_bytes(f"PRIVMSG {channel} :{message}\r\n")) |
| 299 |
time.sleep(1) |
| 300 |
if part: |
| 301 |
if channel: |
| 302 |
irc.send(to_bytes(f"PART {channel}\r\n")) |
| 303 |
irc.send(to_bytes("QUIT\r\n")) |
| 304 |
time.sleep(1) |
| 305 |
irc.close() |
| 306 |
|
| 307 |
|
| 308 |
# =========================================== |
| 309 |
# Main |
| 310 |
# |
| 311 |
|
| 312 |
|
| 313 |
def main(): |
| 314 |
module = AnsibleModule( |
| 315 |
argument_spec=dict( |
| 316 |
server=dict(default="localhost"), |
| 317 |
port=dict(type="int", default=6667), |
| 318 |
nick=dict(default="ansible"), |
| 319 |
nick_to=dict(type="list", elements="str"), |
| 320 |
msg=dict(required=True), |
| 321 |
color=dict( |
| 322 |
default="none", |
| 323 |
aliases=["colour"], |
| 324 |
choices=[ |
| 325 |
"white", |
| 326 |
"black", |
| 327 |
"blue", |
| 328 |
"green", |
| 329 |
"red", |
| 330 |
"brown", |
| 331 |
"purple", |
| 332 |
"orange", |
| 333 |
"yellow", |
| 334 |
"light_green", |
| 335 |
"teal", |
| 336 |
"light_cyan", |
| 337 |
"light_blue", |
| 338 |
"pink", |
| 339 |
"gray", |
| 340 |
"light_gray", |
| 341 |
"none", |
| 342 |
], |
| 343 |
), |
| 344 |
style=dict(default="none", choices=["underline", "reverse", "bold", "italic", "none"]), |
| 345 |
channel=dict(), |
| 346 |
key=dict(no_log=True), |
| 347 |
topic=dict(), |
| 348 |
passwd=dict(no_log=True), |
| 349 |
timeout=dict(type="int", default=30), |
| 350 |
part=dict(type="bool", default=True), |
| 351 |
use_tls=dict(type="bool", default=True, aliases=["use_ssl"]), |
| 352 |
validate_certs=dict(type="bool", default=True), |
| 353 |
), |
| 354 |
supports_check_mode=True, |
| 355 |
required_one_of=[["channel", "nick_to"]], |
| 356 |
) |
| 357 |
|
| 358 |
server = module.params["server"] |
| 359 |
port = module.params["port"] |
| 360 |
nick = module.params["nick"] |
| 361 |
nick_to = module.params["nick_to"] |
| 362 |
msg = module.params["msg"] |
| 363 |
color = module.params["color"] |
| 364 |
channel = module.params["channel"] |
| 365 |
topic = module.params["topic"] |
| 366 |
if topic and not channel: |
| 367 |
module.fail_json(msg="When topic is specified, a channel is required.") |
| 368 |
key = module.params["key"] |
| 369 |
passwd = module.params["passwd"] |
| 370 |
timeout = module.params["timeout"] |
| 371 |
use_tls = module.params["use_tls"] |
| 372 |
part = module.params["part"] |
| 373 |
style = module.params["style"] |
| 374 |
validate_certs = module.params["validate_certs"] |
| 375 |
|
| 376 |
try: |
| 377 |
send_msg( |
| 378 |
msg, |
| 379 |
server, |
| 380 |
port, |
| 381 |
channel, |
| 382 |
nick_to, |
| 383 |
key, |
| 384 |
topic, |
| 385 |
nick, |
| 386 |
color, |
| 387 |
passwd, |
| 388 |
timeout, |
| 389 |
use_tls, |
| 390 |
validate_certs, |
| 391 |
part, |
| 392 |
style, |
| 393 |
) |
| 394 |
except Exception as e: |
| 395 |
module.fail_json(msg=f"unable to send to IRC: {e}", exception=traceback.format_exc()) |
| 396 |
|
| 397 |
module.exit_json(changed=False, channel=channel, nick=nick, msg=msg) |
| 398 |
|
| 399 |
|
| 400 |
if __name__ == "__main__": |
| 401 |
main() |