Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed to send to conference rooms correctly #2849

Merged
merged 1 commit into from
May 6, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 22 additions & 7 deletions library/notification/jabber
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ options:
required: true
to:
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
msg:
description:
Expand All @@ -45,14 +45,18 @@ author: Brian Coca
'''

EXAMPLES = '''
- description: message to jabber user/room
code: jabber: user=mybot@chatserver.tld password=secret to=mychaps@chatserver.tld msg="Ansible task finished"
- description: message to jabber user
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
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 re
import time

HAS_XMPP = True
Expand Down Expand Up @@ -83,32 +87,43 @@ def main():
user = jid.getNode()
server = jid.getDomain()
port = module.params['port']
password = module.params['password']
to, nick = re.split( r'/', module.params['to'])

if module.params['host']:
host = module.params['host']
else:
host = server
password = module.params['password']
if module.params['encoding']:
xmpp.simplexml.ENCODING = params['encoding']

msg = xmpp.protocol.Message(body=module.params['msg'])

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)

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:
conn.send(xmpp.Message(module.params['to'], module.params['msg']))
conn.send(msg)
time.sleep(1)
conn.disconnect()
except Exception, e:
module.fail_json(msg="unable to send msg: %s" % e)

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
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
Expand Down