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
|
|
|
|
|
|
|
import re
|
2019-06-07 11:16:07 +00:00
|
|
|
import six
|
2018-09-17 00:27:00 +00:00
|
|
|
import requests
|
|
|
|
|
|
|
|
from .NotifyBase import NotifyBase
|
2019-06-07 11:16:07 +00:00
|
|
|
from ..common import NotifyType
|
|
|
|
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 _
|
2020-03-25 01:14:36 +00:00
|
|
|
from ..attachment.AttachBase import AttachBase
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
# Flag used as a placeholder to sending to all devices
|
|
|
|
PUSHOVER_SEND_TO_ALL = 'ALL_DEVICES'
|
|
|
|
|
2019-11-25 01:15:30 +00:00
|
|
|
# Used to detect a Device
|
2018-09-17 00:27:00 +00:00
|
|
|
VALIDATE_DEVICE = re.compile(r'^[a-z0-9_]{1,25}$', re.I)
|
|
|
|
|
|
|
|
|
|
|
|
# Priorities
|
|
|
|
class PushoverPriority(object):
|
|
|
|
LOW = -2
|
|
|
|
MODERATE = -1
|
|
|
|
NORMAL = 0
|
|
|
|
HIGH = 1
|
|
|
|
EMERGENCY = 2
|
|
|
|
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Sounds
|
|
|
|
class PushoverSound(object):
|
|
|
|
PUSHOVER = 'pushover'
|
|
|
|
BIKE = 'bike'
|
|
|
|
BUGLE = 'bugle'
|
|
|
|
CASHREGISTER = 'cashregister'
|
|
|
|
CLASSICAL = 'classical'
|
|
|
|
COSMIC = 'cosmic'
|
|
|
|
FALLING = 'falling'
|
|
|
|
GAMELAN = 'gamelan'
|
|
|
|
INCOMING = 'incoming'
|
|
|
|
INTERMISSION = 'intermission'
|
|
|
|
MAGIC = 'magic'
|
|
|
|
MECHANICAL = 'mechanical'
|
|
|
|
PIANOBAR = 'pianobar'
|
|
|
|
SIREN = 'siren'
|
|
|
|
SPACEALARM = 'spacealarm'
|
|
|
|
TUGBOAT = 'tugboat'
|
|
|
|
ALIEN = 'alien'
|
|
|
|
CLIMB = 'climb'
|
|
|
|
PERSISTENT = 'persistent'
|
|
|
|
ECHO = 'echo'
|
|
|
|
UPDOWN = 'updown'
|
|
|
|
NONE = 'none'
|
|
|
|
|
|
|
|
|
|
|
|
PUSHOVER_SOUNDS = (
|
|
|
|
PushoverSound.PUSHOVER,
|
|
|
|
PushoverSound.BIKE,
|
|
|
|
PushoverSound.BUGLE,
|
|
|
|
PushoverSound.CASHREGISTER,
|
|
|
|
PushoverSound.CLASSICAL,
|
|
|
|
PushoverSound.COSMIC,
|
|
|
|
PushoverSound.FALLING,
|
|
|
|
PushoverSound.GAMELAN,
|
|
|
|
PushoverSound.INCOMING,
|
|
|
|
PushoverSound.INTERMISSION,
|
|
|
|
PushoverSound.MAGIC,
|
|
|
|
PushoverSound.MECHANICAL,
|
|
|
|
PushoverSound.PIANOBAR,
|
|
|
|
PushoverSound.SIREN,
|
|
|
|
PushoverSound.SPACEALARM,
|
|
|
|
PushoverSound.TUGBOAT,
|
|
|
|
PushoverSound.ALIEN,
|
|
|
|
PushoverSound.CLIMB,
|
|
|
|
PushoverSound.PERSISTENT,
|
|
|
|
PushoverSound.ECHO,
|
|
|
|
PushoverSound.UPDOWN,
|
|
|
|
PushoverSound.NONE,
|
|
|
|
)
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
PUSHOVER_PRIORITIES = (
|
|
|
|
PushoverPriority.LOW,
|
|
|
|
PushoverPriority.MODERATE,
|
|
|
|
PushoverPriority.NORMAL,
|
|
|
|
PushoverPriority.HIGH,
|
|
|
|
PushoverPriority.EMERGENCY,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Extend HTTP Error Messages
|
2019-06-07 11:16:07 +00:00
|
|
|
PUSHOVER_HTTP_ERROR_MAP = {
|
2018-09-17 00:27:00 +00:00
|
|
|
401: 'Unauthorized - Invalid Token.',
|
2019-06-07 11:16:07 +00:00
|
|
|
}
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NotifyPushover(NotifyBase):
|
|
|
|
"""
|
|
|
|
A wrapper for Pushover Notifications
|
|
|
|
"""
|
|
|
|
|
2018-10-22 16:39:11 +00:00
|
|
|
# The default descriptive name associated with the Notification
|
|
|
|
service_name = 'Pushover'
|
|
|
|
|
|
|
|
# The services URL
|
|
|
|
service_url = 'https://pushover.net/'
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
# All pushover requests are secure
|
|
|
|
secure_protocol = 'pover'
|
|
|
|
|
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_pushover'
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
# Pushover uses the http protocol with JSON requests
|
|
|
|
notify_url = 'https://api.pushover.net/1/messages.json'
|
|
|
|
|
|
|
|
# The maximum allowable characters allowed in the body per message
|
|
|
|
body_maxlen = 512
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Default Pushover sound
|
|
|
|
default_pushover_sound = PushoverSound.PUSHOVER
|
|
|
|
|
2020-03-25 01:14:36 +00:00
|
|
|
# 2.5MB is the maximum supported image filesize as per documentation
|
|
|
|
# here: https://pushover.net/api#attachments (Dec 26th, 2019)
|
|
|
|
attach_max_size_bytes = 2621440
|
|
|
|
|
|
|
|
# The regular expression of the current attachment supported mime types
|
|
|
|
# At this time it is only images
|
|
|
|
attach_supported_mime_type = r'^image/.*'
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Define object templates
|
|
|
|
templates = (
|
|
|
|
'{schema}://{user_key}@{token}',
|
|
|
|
'{schema}://{user_key}@{token}/{targets}',
|
|
|
|
)
|
|
|
|
|
|
|
|
# Define our template tokens
|
|
|
|
template_tokens = dict(NotifyBase.template_tokens, **{
|
|
|
|
'user_key': {
|
|
|
|
'name': _('User Key'),
|
|
|
|
'type': 'string',
|
|
|
|
'private': True,
|
|
|
|
'required': True,
|
2019-11-25 01:15:30 +00:00
|
|
|
'regex': (r'^[a-z0-9]{30}$', 'i'),
|
2019-06-07 11:16:07 +00:00
|
|
|
},
|
|
|
|
'token': {
|
|
|
|
'name': _('Access Token'),
|
|
|
|
'type': 'string',
|
|
|
|
'private': True,
|
|
|
|
'required': True,
|
2019-11-25 01:15:30 +00:00
|
|
|
'regex': (r'^[a-z0-9]{30}$', 'i'),
|
2019-06-07 11:16:07 +00:00
|
|
|
},
|
|
|
|
'target_device': {
|
|
|
|
'name': _('Target Device'),
|
|
|
|
'type': 'string',
|
2019-11-25 01:15:30 +00:00
|
|
|
'regex': (r'^[a-z0-9_]{1,25}$', 'i'),
|
2019-06-07 11:16:07 +00:00
|
|
|
'map_to': 'targets',
|
|
|
|
},
|
|
|
|
'targets': {
|
|
|
|
'name': _('Targets'),
|
|
|
|
'type': 'list:string',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
# Define our template arguments
|
|
|
|
template_args = dict(NotifyBase.template_args, **{
|
|
|
|
'priority': {
|
|
|
|
'name': _('Priority'),
|
|
|
|
'type': 'choice:int',
|
|
|
|
'values': PUSHOVER_PRIORITIES,
|
|
|
|
'default': PushoverPriority.NORMAL,
|
|
|
|
},
|
|
|
|
'sound': {
|
|
|
|
'name': _('Sound'),
|
|
|
|
'type': 'string',
|
2019-11-25 01:15:30 +00:00
|
|
|
'regex': (r'^[a-z]{1,12}$', 'i'),
|
2019-06-07 11:16:07 +00:00
|
|
|
'default': PushoverSound.PUSHOVER,
|
|
|
|
},
|
2019-08-14 16:46:21 +00:00
|
|
|
'retry': {
|
|
|
|
'name': _('Retry'),
|
|
|
|
'type': 'int',
|
|
|
|
'min': 30,
|
|
|
|
'default': 900, # 15 minutes
|
|
|
|
},
|
|
|
|
'expire': {
|
|
|
|
'name': _('Expire'),
|
|
|
|
'type': 'int',
|
|
|
|
'min': 0,
|
|
|
|
'max': 10800,
|
|
|
|
'default': 3600, # 1 hour
|
|
|
|
},
|
2019-06-07 11:16:07 +00:00
|
|
|
'to': {
|
|
|
|
'alias_of': 'targets',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2019-11-25 01:15:30 +00:00
|
|
|
def __init__(self, user_key, token, targets=None, priority=None,
|
|
|
|
sound=None, retry=None, expire=None, **kwargs):
|
2018-09-17 00:27:00 +00:00
|
|
|
"""
|
|
|
|
Initialize Pushover Object
|
|
|
|
"""
|
|
|
|
super(NotifyPushover, self).__init__(**kwargs)
|
|
|
|
|
2019-11-25 01:15:30 +00:00
|
|
|
# Access Token (associated with project)
|
|
|
|
self.token = validate_regex(
|
|
|
|
token, *self.template_tokens['token']['regex'])
|
|
|
|
if not self.token:
|
|
|
|
msg = 'An invalid Pushover Access Token ' \
|
|
|
|
'({}) was specified.'.format(token)
|
2019-06-07 11:16:07 +00:00
|
|
|
self.logger.warning(msg)
|
|
|
|
raise TypeError(msg)
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2019-11-25 01:15:30 +00:00
|
|
|
# User Key (associated with project)
|
|
|
|
self.user_key = validate_regex(
|
|
|
|
user_key, *self.template_tokens['user_key']['regex'])
|
|
|
|
if not self.user_key:
|
|
|
|
msg = 'An invalid Pushover User Key ' \
|
|
|
|
'({}) was specified.'.format(user_key)
|
2019-06-07 11:16:07 +00:00
|
|
|
self.logger.warning(msg)
|
|
|
|
raise TypeError(msg)
|
|
|
|
|
|
|
|
self.targets = parse_list(targets)
|
|
|
|
if len(self.targets) == 0:
|
|
|
|
self.targets = (PUSHOVER_SEND_TO_ALL, )
|
|
|
|
|
|
|
|
# Setup our sound
|
|
|
|
self.sound = NotifyPushover.default_pushover_sound \
|
|
|
|
if not isinstance(sound, six.string_types) else sound.lower()
|
|
|
|
if self.sound and self.sound not in PUSHOVER_SOUNDS:
|
|
|
|
msg = 'The sound specified ({}) is invalid.'.format(sound)
|
|
|
|
self.logger.warning(msg)
|
|
|
|
raise TypeError(msg)
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
# The Priority of the message
|
|
|
|
if priority not in PUSHOVER_PRIORITIES:
|
2019-11-25 01:15:30 +00:00
|
|
|
self.priority = self.template_args['priority']['default']
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
self.priority = priority
|
|
|
|
|
2019-08-14 16:46:21 +00:00
|
|
|
# The following are for emergency alerts
|
|
|
|
if self.priority == PushoverPriority.EMERGENCY:
|
|
|
|
|
|
|
|
# How often to resend notification, in seconds
|
2019-11-25 01:15:30 +00:00
|
|
|
self.retry = self.template_args['retry']['default']
|
2019-08-14 16:46:21 +00:00
|
|
|
try:
|
|
|
|
self.retry = int(retry)
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
# Do nothing
|
|
|
|
pass
|
|
|
|
|
|
|
|
# How often to resend notification, in seconds
|
2019-11-25 01:15:30 +00:00
|
|
|
self.expire = self.template_args['expire']['default']
|
2019-08-14 16:46:21 +00:00
|
|
|
try:
|
|
|
|
self.expire = int(expire)
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
# Do nothing
|
|
|
|
pass
|
|
|
|
|
|
|
|
if self.retry < 30:
|
2019-11-25 01:15:30 +00:00
|
|
|
msg = 'Pushover retry must be at least 30 seconds.'
|
2019-08-14 16:46:21 +00:00
|
|
|
self.logger.warning(msg)
|
|
|
|
raise TypeError(msg)
|
2019-11-25 01:15:30 +00:00
|
|
|
|
2019-08-14 16:46:21 +00:00
|
|
|
if self.expire < 0 or self.expire > 10800:
|
2019-11-25 01:15:30 +00:00
|
|
|
msg = 'Pushover expire must reside in the range of ' \
|
|
|
|
'0 to 10800 seconds.'
|
2019-08-14 16:46:21 +00:00
|
|
|
self.logger.warning(msg)
|
|
|
|
raise TypeError(msg)
|
2019-11-25 01:15:30 +00:00
|
|
|
return
|
2019-06-07 11:16:07 +00:00
|
|
|
|
2020-03-25 01:14:36 +00:00
|
|
|
def send(self, body, title='', notify_type=NotifyType.INFO, attach=None,
|
|
|
|
**kwargs):
|
2018-09-17 00:27:00 +00:00
|
|
|
"""
|
|
|
|
Perform Pushover Notification
|
|
|
|
"""
|
|
|
|
|
|
|
|
# error tracking (used for function return)
|
|
|
|
has_error = False
|
|
|
|
|
|
|
|
# Create a copy of the devices list
|
2019-06-07 11:16:07 +00:00
|
|
|
devices = list(self.targets)
|
2018-09-17 00:27:00 +00:00
|
|
|
while len(devices):
|
|
|
|
device = devices.pop(0)
|
|
|
|
|
|
|
|
if VALIDATE_DEVICE.match(device) is None:
|
|
|
|
self.logger.warning(
|
|
|
|
'The device specified (%s) is invalid.' % device,
|
|
|
|
)
|
2019-06-07 11:16:07 +00:00
|
|
|
|
|
|
|
# Mark our failure
|
2018-09-17 00:27:00 +00:00
|
|
|
has_error = True
|
|
|
|
continue
|
|
|
|
|
|
|
|
# prepare JSON Object
|
|
|
|
payload = {
|
|
|
|
'token': self.token,
|
2019-11-25 01:15:30 +00:00
|
|
|
'user': self.user_key,
|
2018-09-17 00:27:00 +00:00
|
|
|
'priority': str(self.priority),
|
2020-03-25 01:14:36 +00:00
|
|
|
'title': title if title else self.app_desc,
|
2018-09-17 00:27:00 +00:00
|
|
|
'message': body,
|
|
|
|
'device': device,
|
2019-06-07 11:16:07 +00:00
|
|
|
'sound': self.sound,
|
2018-09-17 00:27:00 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 16:46:21 +00:00
|
|
|
if self.priority == PushoverPriority.EMERGENCY:
|
|
|
|
payload.update({'retry': self.retry, 'expire': self.expire})
|
|
|
|
|
2020-03-25 01:14:36 +00:00
|
|
|
if attach:
|
|
|
|
# Create a copy of our payload
|
|
|
|
_payload = payload.copy()
|
|
|
|
|
|
|
|
# Send with attachments
|
|
|
|
for attachment in attach:
|
|
|
|
# Simple send
|
|
|
|
if not self._send(_payload, attachment):
|
|
|
|
# Mark our failure
|
|
|
|
has_error = True
|
|
|
|
# clean exit from our attachment loop
|
|
|
|
break
|
|
|
|
|
|
|
|
# To handle multiple attachments, clean up our message
|
|
|
|
_payload['title'] = '...'
|
|
|
|
_payload['message'] = attachment.name
|
|
|
|
# No need to alarm for each consecutive attachment uploaded
|
|
|
|
# afterwards
|
|
|
|
_payload['sound'] = PushoverSound.NONE
|
|
|
|
|
|
|
|
else:
|
|
|
|
# Simple send
|
|
|
|
if not self._send(payload):
|
2019-06-07 11:16:07 +00:00
|
|
|
# Mark our failure
|
2018-09-17 00:27:00 +00:00
|
|
|
has_error = True
|
|
|
|
|
2020-03-25 01:14:36 +00:00
|
|
|
return not has_error
|
|
|
|
|
|
|
|
def _send(self, payload, attach=None):
|
|
|
|
"""
|
|
|
|
Wrapper to the requests (post) object
|
|
|
|
"""
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2020-03-25 01:14:36 +00:00
|
|
|
if isinstance(attach, AttachBase):
|
|
|
|
# Perform some simple error checking
|
|
|
|
if not attach:
|
|
|
|
# We could not access the attachment
|
|
|
|
self.logger.error(
|
|
|
|
'Could not access attachment {}.'.format(
|
|
|
|
attach.url(privacy=True)))
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Perform some basic checks as we want to gracefully skip
|
|
|
|
# over unsupported mime types.
|
|
|
|
if not re.match(
|
|
|
|
self.attach_supported_mime_type,
|
|
|
|
attach.mimetype,
|
|
|
|
re.I):
|
|
|
|
# No problem; we just don't support this attachment
|
|
|
|
# type; gracefully move along
|
|
|
|
self.logger.debug(
|
|
|
|
'Ignored unsupported Pushover attachment ({}): {}'
|
|
|
|
.format(
|
|
|
|
attach.mimetype,
|
|
|
|
attach.url(privacy=True)))
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
# If we get here, we're dealing with a supported image.
|
|
|
|
# Verify that the filesize is okay though.
|
|
|
|
file_size = len(attach)
|
|
|
|
if not (file_size > 0
|
|
|
|
and file_size <= self.attach_max_size_bytes):
|
|
|
|
|
|
|
|
# File size is no good
|
2018-09-17 00:27:00 +00:00
|
|
|
self.logger.warning(
|
2020-03-25 01:14:36 +00:00
|
|
|
'Pushover attachment size ({}B) exceeds limit: {}'
|
|
|
|
.format(file_size, attach.url(privacy=True)))
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2020-03-25 01:14:36 +00:00
|
|
|
return False
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2020-03-25 01:14:36 +00:00
|
|
|
self.logger.debug(
|
|
|
|
'Posting Pushover attachment {}'.format(
|
|
|
|
attach.url(privacy=True)))
|
|
|
|
|
|
|
|
# Default Header
|
|
|
|
headers = {
|
|
|
|
'User-Agent': self.app_id,
|
|
|
|
}
|
|
|
|
|
|
|
|
# Authentication
|
|
|
|
auth = (self.token, '')
|
|
|
|
|
|
|
|
# Some default values for our request object to which we'll update
|
|
|
|
# depending on what our payload is
|
|
|
|
files = None
|
|
|
|
|
|
|
|
self.logger.debug('Pushover POST URL: %s (cert_verify=%r)' % (
|
|
|
|
self.notify_url, self.verify_certificate,
|
|
|
|
))
|
|
|
|
self.logger.debug('Pushover Payload: %s' % str(payload))
|
|
|
|
|
|
|
|
# Always call throttle before any remote server i/o is made
|
|
|
|
self.throttle()
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Open our attachment path if required:
|
|
|
|
if attach:
|
|
|
|
files = {'attachment': (attach.name, open(attach.path, 'rb'))}
|
|
|
|
|
|
|
|
r = requests.post(
|
|
|
|
self.notify_url,
|
|
|
|
data=payload,
|
|
|
|
headers=headers,
|
|
|
|
files=files,
|
|
|
|
auth=auth,
|
|
|
|
verify=self.verify_certificate,
|
2020-09-14 12:24:26 +00:00
|
|
|
timeout=self.request_timeout,
|
2020-03-25 01:14:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if r.status_code != requests.codes.ok:
|
|
|
|
# We had a problem
|
|
|
|
status_str = \
|
|
|
|
NotifyPushover.http_response_code_lookup(
|
|
|
|
r.status_code, PUSHOVER_HTTP_ERROR_MAP)
|
|
|
|
|
|
|
|
self.logger.warning(
|
|
|
|
'Failed to send Pushover notification to {}: '
|
|
|
|
'{}{}error={}.'.format(
|
|
|
|
payload['device'],
|
|
|
|
status_str,
|
|
|
|
', ' if status_str else '',
|
|
|
|
r.status_code))
|
|
|
|
|
|
|
|
self.logger.debug(
|
|
|
|
'Response Details:\r\n{}'.format(r.content))
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.logger.info(
|
|
|
|
'Sent Pushover notification to %s.' % payload['device'])
|
|
|
|
|
|
|
|
except requests.RequestException as e:
|
|
|
|
self.logger.warning(
|
2020-09-14 12:24:26 +00:00
|
|
|
'A Connection error occurred sending Pushover:%s ' % (
|
2020-03-25 01:14:36 +00:00
|
|
|
payload['device']) + 'notification.'
|
|
|
|
)
|
|
|
|
self.logger.debug('Socket Exception: %s' % str(e))
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
except (OSError, IOError) as e:
|
|
|
|
self.logger.warning(
|
2020-09-14 12:24:26 +00:00
|
|
|
'An I/O error occurred while reading {}.'.format(
|
2020-03-25 01:14:36 +00:00
|
|
|
attach.name if attach else 'attachment'))
|
|
|
|
self.logger.debug('I/O Exception: %s' % str(e))
|
|
|
|
return False
|
|
|
|
|
|
|
|
finally:
|
|
|
|
# Close our file (if it's open) stored in the second element
|
|
|
|
# of our files tuple (index 1)
|
|
|
|
if files:
|
|
|
|
files['attachment'][1].close()
|
|
|
|
|
|
|
|
return True
|
2018-09-17 00:27:00 +00:00
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
_map = {
|
|
|
|
PushoverPriority.LOW: 'low',
|
|
|
|
PushoverPriority.MODERATE: 'moderate',
|
|
|
|
PushoverPriority.NORMAL: 'normal',
|
|
|
|
PushoverPriority.HIGH: 'high',
|
|
|
|
PushoverPriority.EMERGENCY: 'emergency',
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:24:26 +00:00
|
|
|
# Define any URL parameters
|
|
|
|
params = {
|
2019-06-07 11:16:07 +00:00
|
|
|
'priority':
|
2019-11-25 01:15:30 +00:00
|
|
|
_map[self.template_args['priority']['default']]
|
|
|
|
if self.priority not in _map else _map[self.priority],
|
2019-06-07 11:16:07 +00:00
|
|
|
}
|
2020-09-14 12:24:26 +00:00
|
|
|
|
2019-08-14 16:46:21 +00:00
|
|
|
# Only add expire and retry for emergency messages,
|
|
|
|
# pushover ignores for all other priorities
|
|
|
|
if self.priority == PushoverPriority.EMERGENCY:
|
2020-09-14 12:24:26 +00:00
|
|
|
params.update({'expire': self.expire, 'retry': self.retry})
|
|
|
|
|
|
|
|
# Extend our parameters
|
|
|
|
params.update(self.url_parameters(privacy=privacy, *args, **kwargs))
|
2019-06-07 11:16:07 +00:00
|
|
|
|
|
|
|
# Escape our devices
|
|
|
|
devices = '/'.join([NotifyPushover.quote(x, safe='')
|
|
|
|
for x in self.targets])
|
|
|
|
|
|
|
|
if devices == PUSHOVER_SEND_TO_ALL:
|
|
|
|
# keyword is reserved for internal usage only; it's safe to remove
|
|
|
|
# it from the devices list
|
|
|
|
devices = ''
|
|
|
|
|
2020-09-14 12:24:26 +00:00
|
|
|
return '{schema}://{user_key}@{token}/{devices}/?{params}'.format(
|
2019-06-07 11:16:07 +00:00
|
|
|
schema=self.secure_protocol,
|
2019-11-25 01:15:30 +00:00
|
|
|
user_key=self.pprint(self.user_key, privacy, safe=''),
|
|
|
|
token=self.pprint(self.token, privacy, safe=''),
|
2019-06-07 11:16:07 +00:00
|
|
|
devices=devices,
|
2020-09-14 12:24:26 +00:00
|
|
|
params=NotifyPushover.urlencode(params))
|
2019-06-07 11:16:07 +00:00
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
@staticmethod
|
|
|
|
def parse_url(url):
|
|
|
|
"""
|
|
|
|
Parses the URL and returns enough arguments that can allow
|
2020-09-14 12:24:26 +00:00
|
|
|
us to re-instantiate this object.
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
"""
|
2020-09-14 12:24:26 +00:00
|
|
|
results = NotifyBase.parse_url(url, verify_host=False)
|
2018-09-17 00:27:00 +00:00
|
|
|
if not results:
|
|
|
|
# We're done early as we couldn't load the results
|
|
|
|
return results
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Set our priority
|
2018-09-17 00:27:00 +00:00
|
|
|
if 'priority' in results['qsd'] and len(results['qsd']['priority']):
|
|
|
|
_map = {
|
|
|
|
'l': PushoverPriority.LOW,
|
|
|
|
'm': PushoverPriority.MODERATE,
|
|
|
|
'n': PushoverPriority.NORMAL,
|
|
|
|
'h': PushoverPriority.HIGH,
|
|
|
|
'e': PushoverPriority.EMERGENCY,
|
|
|
|
}
|
|
|
|
try:
|
|
|
|
results['priority'] = \
|
|
|
|
_map[results['qsd']['priority'][0].lower()]
|
|
|
|
|
|
|
|
except KeyError:
|
|
|
|
# No priority was set
|
|
|
|
pass
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Retrieve all of our targets
|
|
|
|
results['targets'] = NotifyPushover.split_path(results['fullpath'])
|
|
|
|
|
2019-11-25 01:15:30 +00:00
|
|
|
# User Key is retrieved from the user
|
|
|
|
results['user_key'] = NotifyPushover.unquote(results['user'])
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Get the sound
|
|
|
|
if 'sound' in results['qsd'] and len(results['qsd']['sound']):
|
|
|
|
results['sound'] = \
|
|
|
|
NotifyPushover.unquote(results['qsd']['sound'])
|
|
|
|
|
2019-08-14 16:46:21 +00:00
|
|
|
# Get expire and retry
|
|
|
|
if 'expire' in results['qsd'] and len(results['qsd']['expire']):
|
|
|
|
results['expire'] = results['qsd']['expire']
|
|
|
|
if 'retry' in results['qsd'] and len(results['qsd']['retry']):
|
|
|
|
results['retry'] = results['qsd']['retry']
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# The 'to' makes it easier to use yaml configuration
|
|
|
|
if 'to' in results['qsd'] and len(results['qsd']['to']):
|
|
|
|
results['targets'] += \
|
|
|
|
NotifyPushover.parse_list(results['qsd']['to'])
|
|
|
|
|
|
|
|
# Token
|
|
|
|
results['token'] = NotifyPushover.unquote(results['host'])
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
return results
|