2013-07-17 22:37:04 +02:00
|
|
|
#!/usr/bin/python
|
2013-05-06 01:34:06 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-06-01 21:27:55 +02:00
|
|
|
#
|
|
|
|
# (c) 2015, Brian Coca <bcoca@ansible.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
|
2013-05-06 01:34:06 +02:00
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
2013-05-11 22:36:37 +02:00
|
|
|
version_added: "1.2"
|
2013-05-06 01:34:06 +02:00
|
|
|
module: jabber
|
|
|
|
short_description: Send a message to jabber user or chat room
|
|
|
|
description:
|
|
|
|
- Send a message to jabber
|
|
|
|
options:
|
|
|
|
user:
|
|
|
|
description:
|
2015-07-21 18:58:18 +02:00
|
|
|
- User as which to connect
|
2013-05-06 01:34:06 +02:00
|
|
|
required: true
|
|
|
|
password:
|
|
|
|
description:
|
2015-07-21 18:58:18 +02:00
|
|
|
- password for user to connect
|
2013-05-06 01:34:06 +02:00
|
|
|
required: true
|
|
|
|
to:
|
|
|
|
description:
|
2015-07-21 18:58:18 +02:00
|
|
|
- user ID or name of the room, when using room use a slash to indicate your nick.
|
2013-05-06 01:34:06 +02:00
|
|
|
required: true
|
|
|
|
msg:
|
|
|
|
description:
|
|
|
|
- The message body.
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
host:
|
|
|
|
description:
|
2015-07-21 18:58:18 +02:00
|
|
|
- host to connect, overrides user info
|
2013-05-06 01:34:06 +02:00
|
|
|
required: false
|
|
|
|
port:
|
|
|
|
description:
|
2015-07-21 18:58:18 +02:00
|
|
|
- port to connect to, overrides default
|
2013-05-06 01:34:06 +02:00
|
|
|
required: false
|
|
|
|
default: 5222
|
|
|
|
encoding:
|
|
|
|
description:
|
2015-07-21 18:58:18 +02:00
|
|
|
- message encoding
|
2013-05-06 01:34:06 +02:00
|
|
|
required: false
|
|
|
|
|
|
|
|
# informational: requirements for nodes
|
|
|
|
requirements: [ xmpp ]
|
2015-05-14 04:09:07 +02:00
|
|
|
author: "Brian Coca (@bcoca)"
|
2013-05-06 01:34:06 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2013-05-11 22:23:56 +02:00
|
|
|
# send a message to a user
|
2013-06-17 08:53:46 +02:00
|
|
|
- jabber: user=mybot@example.net
|
|
|
|
password=secret
|
|
|
|
to=friend@example.net
|
|
|
|
msg="Ansible task finished"
|
2013-05-06 04:22:07 +02:00
|
|
|
|
2013-05-11 22:23:56 +02:00
|
|
|
# send a message to a room
|
2013-06-17 08:53:46 +02:00
|
|
|
- jabber: user=mybot@example.net
|
|
|
|
password=secret
|
|
|
|
to=mychaps@conference.example.net/ansiblebot
|
|
|
|
msg="Ansible task finished"
|
2013-05-06 01:34:06 +02:00
|
|
|
|
2013-05-11 22:23:56 +02:00
|
|
|
# send a message, specifying the host and port
|
2013-06-17 08:53:46 +02:00
|
|
|
- jabber user=mybot@example.net
|
|
|
|
host=talk.example.net
|
|
|
|
port=5223
|
|
|
|
password=secret
|
|
|
|
to=mychaps@example.net
|
|
|
|
msg="Ansible task finished"
|
2013-05-06 01:34:06 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
2013-05-06 04:22:07 +02:00
|
|
|
import re
|
2013-05-06 01:34:06 +02:00
|
|
|
import time
|
|
|
|
|
|
|
|
HAS_XMPP = True
|
|
|
|
try:
|
|
|
|
import xmpp
|
|
|
|
except ImportError:
|
|
|
|
HAS_XMPP = False
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
user=dict(required=True),
|
|
|
|
password=dict(required=True),
|
|
|
|
to=dict(required=True),
|
|
|
|
msg=dict(required=True),
|
|
|
|
host=dict(required=False),
|
|
|
|
port=dict(required=False,default=5222),
|
|
|
|
encoding=dict(required=False),
|
|
|
|
),
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
|
|
|
if not HAS_XMPP:
|
|
|
|
module.fail_json(msg="xmpp is not installed")
|
|
|
|
|
|
|
|
jid = xmpp.JID(module.params['user'])
|
|
|
|
user = jid.getNode()
|
|
|
|
server = jid.getDomain()
|
|
|
|
port = module.params['port']
|
2013-05-06 04:22:07 +02:00
|
|
|
password = module.params['password']
|
2013-07-24 22:28:21 +02:00
|
|
|
try:
|
|
|
|
to, nick = module.params['to'].split('/', 1)
|
|
|
|
except ValueError:
|
|
|
|
to, nick = module.params['to'], None
|
2013-05-06 04:22:07 +02:00
|
|
|
|
2013-05-06 01:34:06 +02:00
|
|
|
if module.params['host']:
|
|
|
|
host = module.params['host']
|
|
|
|
else:
|
|
|
|
host = server
|
|
|
|
if module.params['encoding']:
|
|
|
|
xmpp.simplexml.ENCODING = params['encoding']
|
|
|
|
|
2013-05-06 04:22:07 +02:00
|
|
|
msg = xmpp.protocol.Message(body=module.params['msg'])
|
|
|
|
|
2013-05-06 01:34:06 +02:00
|
|
|
try:
|
|
|
|
conn=xmpp.Client(server)
|
|
|
|
if not conn.connect(server=(host,port)):
|
|
|
|
module.fail_json(rc=1, msg='Failed to connect to server: %s' % (server))
|
|
|
|
if not conn.auth(user,password,'Ansible'):
|
|
|
|
module.fail_json(rc=1, msg='Failed to authorize %s on: %s' % (user,server))
|
|
|
|
# some old servers require this, also the sleep following send
|
|
|
|
conn.sendInitPresence(requestRoster=0)
|
2013-05-06 04:22:07 +02:00
|
|
|
|
|
|
|
if nick: # sending to room instead of user, need to join
|
|
|
|
msg.setType('groupchat')
|
|
|
|
msg.setTag('x', namespace='http://jabber.org/protocol/muc#user')
|
|
|
|
conn.send(xmpp.Presence(to=module.params['to']))
|
|
|
|
time.sleep(1)
|
2013-07-24 22:28:21 +02:00
|
|
|
else:
|
|
|
|
msg.setType('chat')
|
2013-05-06 04:22:07 +02:00
|
|
|
|
|
|
|
msg.setTo(to)
|
2013-05-06 01:34:06 +02:00
|
|
|
if not module.check_mode:
|
2013-05-06 04:22:07 +02:00
|
|
|
conn.send(msg)
|
2013-05-06 01:34:06 +02:00
|
|
|
time.sleep(1)
|
|
|
|
conn.disconnect()
|
|
|
|
except Exception, e:
|
|
|
|
module.fail_json(msg="unable to send msg: %s" % e)
|
|
|
|
|
2013-05-15 15:59:14 +02:00
|
|
|
module.exit_json(changed=False, to=to, user=user, msg=msg.getBody())
|
2013-05-06 01:34:06 +02:00
|
|
|
|
2013-12-02 21:13:49 +01:00
|
|
|
# import module snippets
|
2013-12-02 21:11:23 +01:00
|
|
|
from ansible.module_utils.basic import *
|
2013-05-06 01:34:06 +02:00
|
|
|
main()
|