fixed to send to conference rooms correctly

Signed-off-by: Brian Coca <briancoca+dev@gmail.com>
This commit is contained in:
Brian Coca 2013-05-05 22:22:07 -04:00
parent 7d47d80255
commit 413fb87113

View file

@ -18,7 +18,7 @@ options:
required: true required: true
to: to:
description: description:
user ID or name of the room. user ID or name of the room, when using room use a slash to indicate your nick.
required: true required: true
msg: msg:
description: description:
@ -45,14 +45,18 @@ author: Brian Coca
''' '''
EXAMPLES = ''' EXAMPLES = '''
- description: message to jabber user/room - description: message to jabber user
code: jabber: user=mybot@chatserver.tld password=secret to=mychaps@chatserver.tld msg="Ansible task finished" code: jabber: user=mybot@chatserver.tld password=secret to=friend@chatserver.tld msg="Ansible task finished"
- description: message to jabber room
code: jabber: user=mybot@chatserver.tld password=secret to=mychaps@conference.chatserver.tld/ansiblebot msg="Ansible task finished"
- description: message specifying host and port - description: message specifying host and port
code: jabber user=mybot@chatserver.tld host=talk.chatserver.tld port=5223 password=secret to=mychaps@chatserver.tld msg="Ansible task finished" code: jabber user=mybot@chatserver.tld host=talk.chatserver.tld port=5223 password=secret to=mychaps@chatserver.tld msg="Ansible task finished"
''' '''
import os import os
import re
import time import time
HAS_XMPP = True HAS_XMPP = True
@ -83,32 +87,43 @@ def main():
user = jid.getNode() user = jid.getNode()
server = jid.getDomain() server = jid.getDomain()
port = module.params['port'] port = module.params['port']
password = module.params['password']
to, nick = re.split( r'/', module.params['to'])
if module.params['host']: if module.params['host']:
host = module.params['host'] host = module.params['host']
else: else:
host = server host = server
password = module.params['password']
if module.params['encoding']: if module.params['encoding']:
xmpp.simplexml.ENCODING = params['encoding'] xmpp.simplexml.ENCODING = params['encoding']
msg = xmpp.protocol.Message(body=module.params['msg'])
try: try:
conn=xmpp.Client(server) conn=xmpp.Client(server)
if not conn.connect(server=(host,port)): if not conn.connect(server=(host,port)):
module.fail_json(rc=1, msg='Failed to connect to server: %s' % (server)) module.fail_json(rc=1, msg='Failed to connect to server: %s' % (server))
if not conn.auth(user,password,'Ansible'): if not conn.auth(user,password,'Ansible'):
module.fail_json(rc=1, msg='Failed to authorize %s on: %s' % (user,server)) module.fail_json(rc=1, msg='Failed to authorize %s on: %s' % (user,server))
# some old servers require this, also the sleep following send # some old servers require this, also the sleep following send
conn.sendInitPresence(requestRoster=0) conn.sendInitPresence(requestRoster=0)
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)
msg.setTo(to)
if not module.check_mode: if not module.check_mode:
conn.send(xmpp.Message(module.params['to'], module.params['msg'])) conn.send(msg)
time.sleep(1) time.sleep(1)
conn.disconnect() conn.disconnect()
except Exception, e: except Exception, e:
module.fail_json(msg="unable to send msg: %s" % e) module.fail_json(msg="unable to send msg: %s" % e)
changed = True changed = True
module.exit_json(changed=changed, to=module.params['to'], user=module.params['user'], msg=module.params['msg']) module.exit_json(changed=changed, to=to, user=user, msg=msg.getBody())
# this is magic, see lib/ansible/module_common.py # this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>> #<<INCLUDE_ANSIBLE_MODULE_COMMON>>