2018-09-17 00:27:00 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2019-06-07 11:16:07 +00:00
|
|
|
# Copyright (C) 2019 Chris Caron <lead2gold@gmail.com>
|
|
|
|
# All rights reserved.
|
2018-09-17 00:27:00 +00:00
|
|
|
#
|
2019-06-07 11:16:07 +00:00
|
|
|
# This code is licensed under the MIT License.
|
2018-09-17 00:27:00 +00:00
|
|
|
#
|
2019-06-07 11:16:07 +00:00
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files(the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions :
|
2018-09-17 00:27:00 +00:00
|
|
|
#
|
2019-06-07 11:16:07 +00:00
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
2018-09-17 00:27:00 +00:00
|
|
|
#
|
2019-06-07 11:16:07 +00:00
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
# THE SOFTWARE.
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
import six
|
2018-09-17 00:27:00 +00:00
|
|
|
import requests
|
|
|
|
from json import dumps
|
|
|
|
|
|
|
|
from .NotifyBase import NotifyBase
|
|
|
|
from ..common import NotifyImageSize
|
2019-06-07 11:16:07 +00:00
|
|
|
from ..common import NotifyType
|
|
|
|
from ..utils import parse_bool
|
|
|
|
from ..utils import parse_list
|
2019-11-25 01:15:30 +00:00
|
|
|
from ..utils import validate_regex
|
2019-06-07 11:16:07 +00:00
|
|
|
from ..AppriseLocale import gettext_lazy as _
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
# Some Reference Locations:
|
|
|
|
# - https://docs.mattermost.com/developer/webhooks-incoming.html
|
|
|
|
# - https://docs.mattermost.com/administration/config-settings.html
|
|
|
|
|
|
|
|
|
|
|
|
class NotifyMatterMost(NotifyBase):
|
|
|
|
"""
|
|
|
|
A wrapper for MatterMost Notifications
|
|
|
|
"""
|
|
|
|
|
2018-10-22 16:39:11 +00:00
|
|
|
# The default descriptive name associated with the Notification
|
|
|
|
service_name = 'MatterMost'
|
|
|
|
|
|
|
|
# The services URL
|
|
|
|
service_url = 'https://mattermost.com/'
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
# The default protocol
|
|
|
|
protocol = 'mmost'
|
|
|
|
|
|
|
|
# The default secure protocol
|
|
|
|
secure_protocol = 'mmosts'
|
|
|
|
|
2018-10-22 16:39:11 +00:00
|
|
|
# A URL that takes you to the setup/help of the specific protocol
|
|
|
|
setup_url = 'https://github.com/caronc/apprise/wiki/Notify_mattermost'
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
# The default Mattermost port
|
|
|
|
default_port = 8065
|
|
|
|
|
|
|
|
# Allows the user to specify the NotifyImageSize object
|
|
|
|
image_size = NotifyImageSize.XY_72
|
|
|
|
|
|
|
|
# The maximum allowable characters allowed in the body per message
|
|
|
|
body_maxlen = 4000
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Mattermost does not have a title
|
|
|
|
title_maxlen = 0
|
|
|
|
|
|
|
|
# Define object templates
|
|
|
|
templates = (
|
|
|
|
'{schema}://{host}/{authtoken}',
|
|
|
|
'{schema}://{host}/{authtoken}:{port}',
|
|
|
|
'{schema}://{botname}@{host}/{authtoken}',
|
|
|
|
'{schema}://{botname}@{host}:{port}/{authtoken}',
|
|
|
|
'{schema}://{host}/{fullpath}/{authtoken}',
|
|
|
|
'{schema}://{host}/{fullpath}{authtoken}:{port}',
|
|
|
|
'{schema}://{botname}@{host}/{fullpath}/{authtoken}',
|
|
|
|
'{schema}://{botname}@{host}:{port}/{fullpath}/{authtoken}',
|
|
|
|
)
|
|
|
|
|
|
|
|
# Define our template tokens
|
|
|
|
template_tokens = dict(NotifyBase.template_tokens, **{
|
|
|
|
'host': {
|
|
|
|
'name': _('Hostname'),
|
|
|
|
'type': 'string',
|
|
|
|
'required': True,
|
|
|
|
},
|
|
|
|
'authtoken': {
|
|
|
|
'name': _('Access Key'),
|
|
|
|
'type': 'string',
|
2019-11-25 01:15:30 +00:00
|
|
|
'regex': (r'^[a-z0-9]{24,32}$', 'i'),
|
2019-06-07 11:16:07 +00:00
|
|
|
'private': True,
|
|
|
|
'required': True,
|
|
|
|
},
|
|
|
|
'fullpath': {
|
|
|
|
'name': _('Path'),
|
|
|
|
'type': 'string',
|
|
|
|
},
|
|
|
|
'botname': {
|
|
|
|
'name': _('Bot Name'),
|
|
|
|
'type': 'string',
|
|
|
|
'map_to': 'user',
|
|
|
|
},
|
|
|
|
'port': {
|
|
|
|
'name': _('Port'),
|
|
|
|
'type': 'int',
|
|
|
|
'min': 1,
|
|
|
|
'max': 65535,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
# Define our template arguments
|
|
|
|
template_args = dict(NotifyBase.template_args, **{
|
|
|
|
'channels': {
|
|
|
|
'name': _('Channels'),
|
|
|
|
'type': 'list:string',
|
|
|
|
},
|
|
|
|
'image': {
|
|
|
|
'name': _('Include Image'),
|
|
|
|
'type': 'bool',
|
|
|
|
'default': True,
|
|
|
|
'map_to': 'include_image',
|
|
|
|
},
|
|
|
|
'to': {
|
|
|
|
'alias_of': 'channels',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
def __init__(self, authtoken, fullpath=None, channels=None,
|
|
|
|
include_image=False, **kwargs):
|
2018-09-17 00:27:00 +00:00
|
|
|
"""
|
|
|
|
Initialize MatterMost Object
|
|
|
|
"""
|
|
|
|
super(NotifyMatterMost, self).__init__(**kwargs)
|
|
|
|
|
|
|
|
if self.secure:
|
|
|
|
self.schema = 'https'
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.schema = 'http'
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# our full path
|
|
|
|
self.fullpath = '' if not isinstance(
|
|
|
|
fullpath, six.string_types) else fullpath.strip()
|
|
|
|
|
2019-11-25 01:15:30 +00:00
|
|
|
# Authorization Token (associated with project)
|
|
|
|
self.authtoken = validate_regex(
|
|
|
|
authtoken, *self.template_tokens['authtoken']['regex'])
|
|
|
|
if not self.authtoken:
|
|
|
|
msg = 'An invalid MatterMost Authorization Token ' \
|
|
|
|
'({}) was specified.'.format(authtoken)
|
2019-06-07 11:16:07 +00:00
|
|
|
self.logger.warning(msg)
|
|
|
|
raise TypeError(msg)
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Optional Channels
|
|
|
|
self.channels = parse_list(channels)
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
if not self.port:
|
|
|
|
self.port = self.default_port
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Place a thumbnail image inline with the message body
|
|
|
|
self.include_image = include_image
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
return
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
|
2018-09-17 00:27:00 +00:00
|
|
|
"""
|
|
|
|
Perform MatterMost Notification
|
|
|
|
"""
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Create a copy of our channels, otherwise place a dummy entry
|
|
|
|
channels = list(self.channels) if self.channels else [None, ]
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
headers = {
|
|
|
|
'User-Agent': self.app_id,
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
}
|
|
|
|
|
|
|
|
# prepare JSON Object
|
|
|
|
payload = {
|
2019-06-07 11:16:07 +00:00
|
|
|
'text': body,
|
|
|
|
'icon_url': None,
|
2018-09-17 00:27:00 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Acquire our image url if configured to do so
|
|
|
|
image_url = None if not self.include_image \
|
|
|
|
else self.image_url(notify_type)
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
if image_url:
|
|
|
|
# Set our image configuration if told to do so
|
|
|
|
payload['icon_url'] = image_url
|
|
|
|
|
|
|
|
# Set our user
|
|
|
|
payload['username'] = self.user if self.user else self.app_id
|
|
|
|
|
|
|
|
# For error tracking
|
|
|
|
has_error = False
|
|
|
|
|
|
|
|
while len(channels):
|
|
|
|
# Pop a channel off of the list
|
|
|
|
channel = channels.pop(0)
|
|
|
|
|
|
|
|
if channel:
|
|
|
|
payload['channel'] = channel
|
|
|
|
|
|
|
|
url = '{}://{}:{}{}/hooks/{}'.format(
|
|
|
|
self.schema, self.host, self.port, self.fullpath,
|
|
|
|
self.authtoken)
|
|
|
|
|
|
|
|
self.logger.debug('MatterMost POST URL: %s (cert_verify=%r)' % (
|
|
|
|
url, self.verify_certificate,
|
|
|
|
))
|
|
|
|
self.logger.debug('MatterMost Payload: %s' % str(payload))
|
|
|
|
|
|
|
|
# Always call throttle before any remote server i/o is made
|
|
|
|
self.throttle()
|
|
|
|
|
|
|
|
try:
|
|
|
|
r = requests.post(
|
|
|
|
url,
|
|
|
|
data=dumps(payload),
|
|
|
|
headers=headers,
|
|
|
|
verify=self.verify_certificate,
|
|
|
|
)
|
|
|
|
|
|
|
|
if r.status_code != requests.codes.ok:
|
|
|
|
# We had a problem
|
|
|
|
status_str = \
|
|
|
|
NotifyMatterMost.http_response_code_lookup(
|
|
|
|
r.status_code)
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
self.logger.warning(
|
2019-06-07 11:16:07 +00:00
|
|
|
'Failed to send MatterMost notification{}: '
|
|
|
|
'{}{}error={}.'.format(
|
|
|
|
'' if not channel
|
|
|
|
else ' to channel {}'.format(channel),
|
|
|
|
status_str,
|
|
|
|
', ' if status_str else '',
|
2018-09-17 00:27:00 +00:00
|
|
|
r.status_code))
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
self.logger.debug(
|
|
|
|
'Response Details:\r\n{}'.format(r.content))
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Flag our error
|
|
|
|
has_error = True
|
|
|
|
continue
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.logger.info(
|
|
|
|
'Sent MatterMost notification{}.'.format(
|
|
|
|
'' if not channel
|
|
|
|
else ' to channel {}'.format(channel)))
|
|
|
|
|
|
|
|
except requests.RequestException as e:
|
|
|
|
self.logger.warning(
|
|
|
|
'A Connection error occured sending MatterMost '
|
|
|
|
'notification{}.'.format(
|
|
|
|
'' if not channel
|
|
|
|
else ' to channel {}'.format(channel)))
|
|
|
|
self.logger.debug('Socket Exception: %s' % str(e))
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Flag our error
|
|
|
|
has_error = True
|
|
|
|
continue
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Return our overall status
|
|
|
|
return not has_error
|
|
|
|
|
2019-11-25 01:15:30 +00:00
|
|
|
def url(self, privacy=False, *args, **kwargs):
|
2019-06-07 11:16:07 +00:00
|
|
|
"""
|
|
|
|
Returns the URL built dynamically based on specified arguments.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Define any arguments set
|
|
|
|
args = {
|
|
|
|
'format': self.notify_format,
|
|
|
|
'overflow': self.overflow_mode,
|
|
|
|
'image': 'yes' if self.include_image else 'no',
|
|
|
|
'verify': 'yes' if self.verify_certificate else 'no',
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.channels:
|
|
|
|
# historically the value only accepted one channel and is
|
|
|
|
# therefore identified as 'channel'. Channels have always been
|
|
|
|
# optional, so that is why this setting is nested in an if block
|
|
|
|
args['channel'] = ','.join(self.channels)
|
|
|
|
|
|
|
|
default_port = 443 if self.secure else self.default_port
|
|
|
|
default_schema = self.secure_protocol if self.secure else self.protocol
|
|
|
|
|
2019-11-25 01:15:30 +00:00
|
|
|
# Determine if there is a botname present
|
|
|
|
botname = ''
|
|
|
|
if self.user:
|
|
|
|
botname = '{botname}@'.format(
|
|
|
|
botname=NotifyMatterMost.quote(self.user, safe=''),
|
|
|
|
)
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
return \
|
2019-11-25 01:15:30 +00:00
|
|
|
'{schema}://{botname}{hostname}{port}{fullpath}{authtoken}' \
|
|
|
|
'/?{args}'.format(
|
2019-06-07 11:16:07 +00:00
|
|
|
schema=default_schema,
|
2019-11-25 01:15:30 +00:00
|
|
|
botname=botname,
|
2019-06-07 11:16:07 +00:00
|
|
|
hostname=NotifyMatterMost.quote(self.host, safe=''),
|
|
|
|
port='' if not self.port or self.port == default_port
|
|
|
|
else ':{}'.format(self.port),
|
|
|
|
fullpath='/' if not self.fullpath else '{}/'.format(
|
|
|
|
NotifyMatterMost.quote(self.fullpath, safe='/')),
|
2019-11-25 01:15:30 +00:00
|
|
|
authtoken=self.pprint(self.authtoken, privacy, safe=''),
|
2019-06-07 11:16:07 +00:00
|
|
|
args=NotifyMatterMost.urlencode(args),
|
|
|
|
)
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def parse_url(url):
|
|
|
|
"""
|
|
|
|
Parses the URL and returns enough arguments that can allow
|
|
|
|
us to substantiate this object.
|
|
|
|
|
|
|
|
"""
|
|
|
|
results = NotifyBase.parse_url(url)
|
|
|
|
|
|
|
|
if not results:
|
|
|
|
# We're done early as we couldn't load the results
|
|
|
|
return results
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Acquire our tokens; the last one will always be our authtoken
|
|
|
|
# all entries before it will be our path
|
|
|
|
tokens = NotifyMatterMost.split_path(results['fullpath'])
|
|
|
|
|
|
|
|
results['authtoken'] = None if not tokens else tokens.pop()
|
|
|
|
|
|
|
|
# Store our path
|
|
|
|
results['fullpath'] = '' if not tokens \
|
|
|
|
else '/{}'.format('/'.join(tokens))
|
|
|
|
|
|
|
|
# Define our optional list of channels to notify
|
|
|
|
results['channels'] = list()
|
|
|
|
|
|
|
|
# Support both 'to' (for yaml configuration) and channel=
|
|
|
|
if 'to' in results['qsd'] and len(results['qsd']['to']):
|
|
|
|
# Allow the user to specify the channel to post to
|
|
|
|
results['channels'].append(
|
|
|
|
NotifyMatterMost.parse_list(results['qsd']['to']))
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
if 'channel' in results['qsd'] and len(results['qsd']['channel']):
|
|
|
|
# Allow the user to specify the channel to post to
|
2019-06-07 11:16:07 +00:00
|
|
|
results['channels'].append(
|
|
|
|
NotifyMatterMost.parse_list(results['qsd']['channel']))
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Image manipulation
|
|
|
|
results['include_image'] = \
|
|
|
|
parse_bool(results['qsd'].get('image', False))
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
return results
|