mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #22824 from favadi/xmpp-multi
Add xmpp.send_msg_multi state/module
This commit is contained in:
commit
3e21e43915
2 changed files with 147 additions and 7 deletions
|
@ -4,7 +4,10 @@ Module for Sending Messages via XMPP (a.k.a. Jabber)
|
|||
|
||||
.. versionadded:: 2014.1.0
|
||||
|
||||
:depends: - sleekxmpp python module
|
||||
:depends: - sleekxmpp>=1.3.1
|
||||
- pyasn1
|
||||
- pyasn1-modules
|
||||
- dnspython
|
||||
:configuration: This module can be used by either passing a jid and password
|
||||
directly to send_message, or by specifying the name of a configuration
|
||||
profile in the minion config, minion pillar, or master config.
|
||||
|
@ -35,9 +38,12 @@ Module for Sending Messages via XMPP (a.k.a. Jabber)
|
|||
# Import Python Libs
|
||||
from __future__ import absolute_import
|
||||
|
||||
import logging
|
||||
|
||||
HAS_LIBS = False
|
||||
try:
|
||||
from sleekxmpp import ClientXMPP as _ClientXMPP
|
||||
from sleekxmpp.exceptions import XMPPError
|
||||
HAS_LIBS = True
|
||||
except ImportError:
|
||||
class _ClientXMPP(object):
|
||||
|
@ -45,9 +51,12 @@ except ImportError:
|
|||
Fake class in order not to raise errors
|
||||
'''
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
__virtualname__ = 'xmpp'
|
||||
|
||||
MUC_DEPRECATED = "Use of send mask waiters is deprecated."
|
||||
|
||||
|
||||
def __virtual__():
|
||||
'''
|
||||
|
@ -58,6 +67,11 @@ def __virtual__():
|
|||
return False
|
||||
|
||||
|
||||
class SleekXMPPMUC(logging.Filter):
|
||||
def filter(self, record):
|
||||
return not record.getMessage() == MUC_DEPRECATED
|
||||
|
||||
|
||||
class SendMsgBot(_ClientXMPP):
|
||||
|
||||
def __init__(self, jid, password, recipient, msg): # pylint: disable=E1002
|
||||
|
@ -65,17 +79,41 @@ class SendMsgBot(_ClientXMPP):
|
|||
# disable call
|
||||
super(SendMsgBot, self).__init__(jid, password)
|
||||
|
||||
self.recipient = recipient
|
||||
self.recipients = [] if recipient is None else [recipient]
|
||||
self.rooms = []
|
||||
|
||||
self.msg = msg
|
||||
|
||||
self.add_event_handler('session_start', self.start)
|
||||
|
||||
@classmethod
|
||||
def create_multi(cls, jid, password, msg, recipients=None, rooms=None,
|
||||
nick="SaltStack Bot"):
|
||||
'''
|
||||
Alternate constructor that accept multiple recipients and rooms
|
||||
'''
|
||||
obj = SendMsgBot(jid, password, None, msg)
|
||||
obj.recipients = [] if recipients is None else recipients
|
||||
obj.rooms = [] if rooms is None else rooms
|
||||
obj.nick = nick
|
||||
return obj
|
||||
|
||||
def start(self, event):
|
||||
self.send_presence()
|
||||
self.get_roster()
|
||||
|
||||
self.send_message(mto=self.recipient,
|
||||
mbody=self.msg,
|
||||
mtype='chat')
|
||||
for recipient in self.recipients:
|
||||
self.send_message(mto=recipient,
|
||||
mbody=self.msg,
|
||||
mtype='chat')
|
||||
|
||||
for room in self.rooms:
|
||||
self.plugin['xep_0045'].joinMUC(room,
|
||||
self.nick,
|
||||
wait=True)
|
||||
self.send_message(mto=room,
|
||||
mbody=self.msg,
|
||||
mtype='groupchat')
|
||||
|
||||
self.disconnect(wait=True)
|
||||
|
||||
|
@ -104,3 +142,52 @@ def send_msg(recipient, message, jid=None, password=None, profile=None):
|
|||
xmpp.process(block=True)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def send_msg_multi(message,
|
||||
recipients=None,
|
||||
rooms=None,
|
||||
jid=None,
|
||||
password=None,
|
||||
nick="SaltStack Bot",
|
||||
profile=None):
|
||||
'''
|
||||
Send a message to an XMPP recipient, support send message to
|
||||
multiple recipients or chat room.
|
||||
|
||||
CLI Examples::
|
||||
|
||||
xmpp.send_msg recipients=['admins@xmpp.example.com'] \
|
||||
rooms=['secret@conference.xmpp.example.com'] \
|
||||
'This is a salt module test' \
|
||||
profile='my-xmpp-account'
|
||||
xmpp.send_msg recipients=['admins@xmpp.example.com'] \
|
||||
rooms=['secret@conference.xmpp.example.com'] \
|
||||
'This is a salt module test' \
|
||||
jid='myuser@xmpp.example.com/salt' password='verybadpass'
|
||||
|
||||
'''
|
||||
|
||||
# Remove: [WARNING ] Use of send mask waiters is deprecated.
|
||||
for handler in logging.root.handlers:
|
||||
handler.addFilter(SleekXMPPMUC())
|
||||
|
||||
if profile:
|
||||
creds = __salt__['config.option'](profile)
|
||||
jid = creds.get('xmpp.jid')
|
||||
password = creds.get('xmpp.password')
|
||||
|
||||
xmpp = SendMsgBot.create_multi(
|
||||
jid, password, message, recipients=recipients, rooms=rooms)
|
||||
|
||||
if rooms:
|
||||
xmpp.register_plugin('xep_0045') # MUC plugin
|
||||
if xmpp.connect():
|
||||
try:
|
||||
xmpp.process(block=True)
|
||||
return True
|
||||
except XMPPError as err:
|
||||
log.error("Could not send message, error: %s", err)
|
||||
else:
|
||||
log.error("Could not connect to XMPP server")
|
||||
return False
|
||||
|
|
|
@ -50,11 +50,64 @@ def send_msg(name, recipient, profile):
|
|||
name,
|
||||
)
|
||||
return ret
|
||||
__salt__['xmpp.send_msg'](
|
||||
__salt__['xmpp.send_msg_multi'](
|
||||
message=name,
|
||||
recipient=recipient,
|
||||
recipients=[recipient],
|
||||
profile=profile,
|
||||
)
|
||||
ret['result'] = True
|
||||
ret['comment'] = 'Sent message to {0}: {1}'.format(recipient, name)
|
||||
return ret
|
||||
|
||||
|
||||
def send_msg_multi(name,
|
||||
profile,
|
||||
recipients=None,
|
||||
rooms=None):
|
||||
'''
|
||||
Send a message to an list of recipients or rooms
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
server-warning-message:
|
||||
xmpp.send_msg:
|
||||
- name: 'This is a server warning message'
|
||||
- profile: my-xmpp-account
|
||||
- recipients:
|
||||
- admins@xmpp.example.com/salt
|
||||
- rooms:
|
||||
- qa@conference.xmpp.example.com
|
||||
|
||||
name
|
||||
The message to send to the XMPP user
|
||||
'''
|
||||
ret = {'name': name,
|
||||
'changes': {},
|
||||
'result': None,
|
||||
'comment': ''}
|
||||
|
||||
if recipients is None and rooms is None:
|
||||
ret['comment'] = "Recipients and rooms are empty, no need to send"
|
||||
return ret
|
||||
|
||||
comment = ''
|
||||
if recipients:
|
||||
comment += ' users {0}'.format(recipients)
|
||||
if rooms:
|
||||
comment += ' rooms {0}'.format(rooms)
|
||||
comment += ', message: {0}'.format(name)
|
||||
|
||||
if __opts__['test']:
|
||||
ret['comment'] = 'Need to send' + comment
|
||||
return ret
|
||||
|
||||
__salt__['xmpp.send_msg_multi'](
|
||||
message=name,
|
||||
recipients=recipients,
|
||||
rooms=rooms,
|
||||
profile=profile,
|
||||
)
|
||||
ret['result'] = True
|
||||
ret['comment'] = 'Sent message to' + comment
|
||||
|
||||
return ret
|
||||
|
|
Loading…
Add table
Reference in a new issue