jabber 3.69 KB
Newer Older
1
#!/usr/bin/python
2 3 4 5
# -*- coding: utf-8 -*-

DOCUMENTATION = '''
---
6
version_added: "1.2"
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
module: jabber
short_description: Send a message to jabber user or chat room
description:
   - Send a message to jabber
options:
  user:
    description:
      User as which to connect
    required: true
  password:
    description:
      password for user to connect
    required: true
  to:
    description:
22
      user ID or name of the room, when using room use a slash to indicate your nick.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    required: true
  msg:
    description:
      - The message body.
    required: true
    default: null
  host:
    description:
      host to connect, overrides user info
    required: false
  port:
    description:
      port to connect to, overrides default
    required: false
    default: 5222
  encoding:
    description:
      message encoding
    required: false

# informational: requirements for nodes
requirements: [ xmpp ]
author: Brian Coca
'''

EXAMPLES = '''
49
# send a message to a user
50 51 52 53
- jabber: user=mybot@example.net
          password=secret
          to=friend@example.net
          msg="Ansible task finished"
54

55
# send a message to a room
56 57 58 59
- jabber: user=mybot@example.net
          password=secret
          to=mychaps@conference.example.net/ansiblebot
          msg="Ansible task finished"
60

61
# send a message, specifying the host and port
62 63 64 65 66 67
- jabber user=mybot@example.net
         host=talk.example.net
         port=5223
         password=secret
         to=mychaps@example.net
         msg="Ansible task finished"
68 69 70
'''

import os
71
import re
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
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']
102
    password = module.params['password']
103 104 105 106
    try:
        to, nick = module.params['to'].split('/', 1)
    except ValueError:
        to, nick = module.params['to'], None
107

108 109 110 111 112 113 114
    if module.params['host']:
        host = module.params['host']
    else:
        host = server
    if module.params['encoding']:
        xmpp.simplexml.ENCODING = params['encoding']

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

117 118 119 120 121 122 123 124
    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)
125 126 127 128 129 130

        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)
131 132
        else:
            msg.setType('chat')
133 134

        msg.setTo(to)
135
        if not module.check_mode:
136
            conn.send(msg)
137 138 139 140 141
        time.sleep(1)
        conn.disconnect()
    except Exception, e:
        module.fail_json(msg="unable to send msg: %s" % e)

142
    module.exit_json(changed=False, to=to, user=user, msg=msg.getBody())
143

144
# import module snippets
145
from ansible.module_utils.basic import *
146
main()