| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) 2015, Brian Coca <[email protected]> |
| 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: jabber |
| 11 |
short_description: Send a message to jabber user or chat room |
| 12 |
description: |
| 13 |
- Send a message to jabber. |
| 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 |
user: |
| 23 |
type: str |
| 24 |
description: |
| 25 |
- User as which to connect. |
| 26 |
required: true |
| 27 |
password: |
| 28 |
type: str |
| 29 |
description: |
| 30 |
- Password for user to connect. |
| 31 |
required: true |
| 32 |
to: |
| 33 |
type: str |
| 34 |
description: |
| 35 |
- User ID or name of the room, when using room use a slash to indicate your nick. |
| 36 |
required: true |
| 37 |
msg: |
| 38 |
type: str |
| 39 |
description: |
| 40 |
- The message body. |
| 41 |
required: true |
| 42 |
host: |
| 43 |
type: str |
| 44 |
description: |
| 45 |
- Host to connect, overrides user info. |
| 46 |
port: |
| 47 |
type: int |
| 48 |
description: |
| 49 |
- Port to connect to, overrides default. |
| 50 |
default: 5222 |
| 51 |
encoding: |
| 52 |
type: str |
| 53 |
description: |
| 54 |
- Message encoding. |
| 55 |
requirements: |
| 56 |
- python xmpp (xmpppy) |
| 57 |
author: "Brian Coca (@bcoca)" |
| 58 |
""" |
| 59 |
|
| 60 |
EXAMPLES = r""" |
| 61 |
- name: Send a message to a user |
| 62 |
community.general.jabber: |
| 63 |
user: [email protected] |
| 64 |
password: secret |
| 65 |
to: [email protected] |
| 66 |
msg: Ansible task finished |
| 67 |
|
| 68 |
- name: Send a message to a room |
| 69 |
community.general.jabber: |
| 70 |
user: [email protected] |
| 71 |
password: secret |
| 72 |
to: [email protected]/ansiblebot |
| 73 |
msg: Ansible task finished |
| 74 |
|
| 75 |
- name: Send a message, specifying the host and port |
| 76 |
community.general.jabber: |
| 77 |
user: [email protected] |
| 78 |
host: talk.example.net |
| 79 |
port: 5223 |
| 80 |
password: secret |
| 81 |
to: [email protected] |
| 82 |
msg: Ansible task finished |
| 83 |
""" |
| 84 |
|
| 85 |
import time |
| 86 |
import traceback |
| 87 |
|
| 88 |
HAS_XMPP = True |
| 89 |
XMPP_IMP_ERR = None |
| 90 |
try: |
| 91 |
import xmpp |
| 92 |
except ImportError: |
| 93 |
XMPP_IMP_ERR = traceback.format_exc() |
| 94 |
HAS_XMPP = False |
| 95 |
|
| 96 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 97 |
|
| 98 |
|
| 99 |
def main(): |
| 100 |
module = AnsibleModule( |
| 101 |
argument_spec=dict( |
| 102 |
user=dict(required=True), |
| 103 |
password=dict(required=True, no_log=True), |
| 104 |
to=dict(required=True), |
| 105 |
msg=dict(required=True), |
| 106 |
host=dict(), |
| 107 |
port=dict(default=5222, type="int"), |
| 108 |
encoding=dict(), |
| 109 |
), |
| 110 |
supports_check_mode=True, |
| 111 |
) |
| 112 |
|
| 113 |
if not HAS_XMPP: |
| 114 |
module.fail_json(msg=missing_required_lib("xmpppy"), exception=XMPP_IMP_ERR) |
| 115 |
|
| 116 |
jid = xmpp.JID(module.params["user"]) |
| 117 |
user = jid.getNode() |
| 118 |
server = jid.getDomain() |
| 119 |
port = module.params["port"] |
| 120 |
password = module.params["password"] |
| 121 |
try: |
| 122 |
to, nick = module.params["to"].split("/", 1) |
| 123 |
except ValueError: |
| 124 |
to, nick = module.params["to"], None |
| 125 |
|
| 126 |
if module.params["host"]: |
| 127 |
host = module.params["host"] |
| 128 |
else: |
| 129 |
host = server |
| 130 |
if module.params["encoding"]: |
| 131 |
xmpp.simplexml.ENCODING = module.params["encoding"] |
| 132 |
|
| 133 |
msg = xmpp.protocol.Message(body=module.params["msg"]) |
| 134 |
|
| 135 |
try: |
| 136 |
conn = xmpp.Client(server, debug=[]) |
| 137 |
if not conn.connect(server=(host, port)): |
| 138 |
module.fail_json(rc=1, msg=f"Failed to connect to server: {server}") |
| 139 |
if not conn.auth(user, password, "Ansible"): |
| 140 |
module.fail_json(rc=1, msg=f"Failed to authorize {user} on: {server}") |
| 141 |
# some old servers require this, also the sleep following send |
| 142 |
conn.sendInitPresence(requestRoster=0) |
| 143 |
|
| 144 |
if nick: # sending to room instead of user, need to join |
| 145 |
msg.setType("groupchat") |
| 146 |
msg.setTag("x", namespace="http://jabber.org/protocol/muc#user") |
| 147 |
join = xmpp.Presence(to=module.params["to"]) |
| 148 |
join.setTag("x", namespace="http://jabber.org/protocol/muc") |
| 149 |
conn.send(join) |
| 150 |
time.sleep(1) |
| 151 |
else: |
| 152 |
msg.setType("chat") |
| 153 |
|
| 154 |
msg.setTo(to) |
| 155 |
if not module.check_mode: |
| 156 |
conn.send(msg) |
| 157 |
time.sleep(1) |
| 158 |
conn.disconnect() |
| 159 |
except Exception as e: |
| 160 |
module.fail_json(msg=f"unable to send msg: {e}", exception=traceback.format_exc()) |
| 161 |
|
| 162 |
module.exit_json(changed=False, to=to, user=user, msg=msg.getBody()) |
| 163 |
|
| 164 |
|
| 165 |
if __name__ == "__main__": |
| 166 |
main() |