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 click
|
|
|
|
import logging
|
2019-06-07 11:16:07 +00:00
|
|
|
import platform
|
2018-09-17 00:27:00 +00:00
|
|
|
import sys
|
2019-06-07 11:16:07 +00:00
|
|
|
from os.path import isfile
|
|
|
|
from os.path import expanduser
|
|
|
|
from os.path import expandvars
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
from . import NotifyType
|
2020-09-14 12:24:26 +00:00
|
|
|
from . import NotifyFormat
|
2018-09-17 00:27:00 +00:00
|
|
|
from . import Apprise
|
|
|
|
from . import AppriseAsset
|
2019-06-07 11:16:07 +00:00
|
|
|
from . import AppriseConfig
|
|
|
|
from .utils import parse_list
|
|
|
|
from .common import NOTIFY_TYPES
|
2020-09-14 12:24:26 +00:00
|
|
|
from .common import NOTIFY_FORMATS
|
2019-06-07 11:16:07 +00:00
|
|
|
from .logger import logger
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
from . import __title__
|
|
|
|
from . import __version__
|
|
|
|
from . import __license__
|
|
|
|
from . import __copywrite__
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2020-09-14 12:24:26 +00:00
|
|
|
# By default we allow looking 1 level down recursivly in Apprise configuration
|
|
|
|
# files.
|
|
|
|
DEFAULT_RECURSION_DEPTH = 1
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
# Defines our click context settings adding -h to the additional options that
|
|
|
|
# can be specified to get the help menu to come up
|
|
|
|
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Define our default configuration we use if nothing is otherwise specified
|
|
|
|
DEFAULT_SEARCH_PATHS = (
|
|
|
|
'~/.apprise',
|
|
|
|
'~/.apprise.yml',
|
|
|
|
'~/.config/apprise',
|
|
|
|
'~/.config/apprise.yml',
|
|
|
|
)
|
|
|
|
|
|
|
|
# Detect Windows
|
|
|
|
if platform.system() == 'Windows':
|
|
|
|
# Default Search Path for Windows Users
|
|
|
|
DEFAULT_SEARCH_PATHS = (
|
|
|
|
expandvars('%APPDATA%/Apprise/apprise'),
|
|
|
|
expandvars('%APPDATA%/Apprise/apprise.yml'),
|
|
|
|
expandvars('%LOCALAPPDATA%/Apprise/apprise'),
|
|
|
|
expandvars('%LOCALAPPDATA%/Apprise/apprise.yml'),
|
|
|
|
)
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
def print_help_msg(command):
|
|
|
|
"""
|
|
|
|
Prints help message when -h or --help is specified.
|
|
|
|
|
|
|
|
"""
|
|
|
|
with click.Context(command) as ctx:
|
|
|
|
click.echo(command.get_help(ctx))
|
|
|
|
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
def print_version_msg():
|
|
|
|
"""
|
|
|
|
Prints version message when -V or --version is specified.
|
|
|
|
|
|
|
|
"""
|
|
|
|
result = list()
|
|
|
|
result.append('{} v{}'.format(__title__, __version__))
|
|
|
|
result.append(__copywrite__)
|
|
|
|
result.append(
|
|
|
|
'This code is licensed under the {} License.'.format(__license__))
|
|
|
|
click.echo('\n'.join(result))
|
|
|
|
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
@click.command(context_settings=CONTEXT_SETTINGS)
|
|
|
|
@click.option('--body', '-b', default=None, type=str,
|
2019-06-07 11:16:07 +00:00
|
|
|
help='Specify the message body. If no body is specified then '
|
|
|
|
'content is read from <stdin>.')
|
|
|
|
@click.option('--title', '-t', default=None, type=str,
|
|
|
|
help='Specify the message title. This field is complete '
|
|
|
|
'optional.')
|
|
|
|
@click.option('--config', '-c', default=None, type=str, multiple=True,
|
|
|
|
metavar='CONFIG_URL',
|
|
|
|
help='Specify one or more configuration locations.')
|
2019-11-25 01:15:30 +00:00
|
|
|
@click.option('--attach', '-a', default=None, type=str, multiple=True,
|
|
|
|
metavar='ATTACHMENT_URL',
|
2020-09-14 12:24:26 +00:00
|
|
|
help='Specify one or more attachment.')
|
2018-09-17 00:27:00 +00:00
|
|
|
@click.option('--notification-type', '-n', default=NotifyType.INFO, type=str,
|
2019-06-07 11:16:07 +00:00
|
|
|
metavar='TYPE',
|
2020-09-14 12:24:26 +00:00
|
|
|
help='Specify the message type (default={}). '
|
|
|
|
'Possible values are "{}", and "{}".'.format(
|
|
|
|
NotifyType.INFO, '", "'.join(NOTIFY_TYPES[:-1]),
|
|
|
|
NOTIFY_TYPES[-1]))
|
|
|
|
@click.option('--input-format', '-i', default=NotifyFormat.TEXT, type=str,
|
|
|
|
metavar='FORMAT',
|
|
|
|
help='Specify the message input format (default={}). '
|
|
|
|
'Possible values are "{}", and "{}".'.format(
|
|
|
|
NotifyFormat.TEXT, '", "'.join(NOTIFY_FORMATS[:-1]),
|
|
|
|
NOTIFY_FORMATS[-1]))
|
2019-06-07 11:16:07 +00:00
|
|
|
@click.option('--theme', '-T', default='default', type=str, metavar='THEME',
|
2018-09-17 00:27:00 +00:00
|
|
|
help='Specify the default theme.')
|
2019-06-07 11:16:07 +00:00
|
|
|
@click.option('--tag', '-g', default=None, type=str, multiple=True,
|
|
|
|
metavar='TAG', help='Specify one or more tags to filter '
|
|
|
|
'which services to notify. Use multiple --tag (-g) entries to '
|
|
|
|
'"OR" the tags together and comma separated to "AND" them. '
|
|
|
|
'If no tags are specified then all services are notified.')
|
2020-09-14 12:24:26 +00:00
|
|
|
@click.option('--disable-async', '-Da', is_flag=True,
|
|
|
|
help='Send all notifications sequentially')
|
2019-11-25 01:15:30 +00:00
|
|
|
@click.option('--dry-run', '-d', is_flag=True,
|
|
|
|
help='Perform a trial run but only prints the notification '
|
|
|
|
'services to-be triggered to stdout. Notifications are never '
|
|
|
|
'sent using this mode.')
|
2020-09-14 12:24:26 +00:00
|
|
|
@click.option('--recursion-depth', '-R', default=DEFAULT_RECURSION_DEPTH,
|
|
|
|
type=int,
|
|
|
|
help='The number of recursive import entries that can be '
|
|
|
|
'loaded from within Apprise configuration. By default '
|
|
|
|
'this is set to {}.'.format(DEFAULT_RECURSION_DEPTH))
|
2020-03-25 01:14:36 +00:00
|
|
|
@click.option('--verbose', '-v', count=True,
|
|
|
|
help='Makes the operation more talkative. Use multiple v to '
|
|
|
|
'increase the verbosity. I.e.: -vvvv')
|
2020-09-14 12:24:26 +00:00
|
|
|
@click.option('--debug', '-D', is_flag=True, help='Debug mode')
|
2019-11-25 01:15:30 +00:00
|
|
|
@click.option('--version', '-V', is_flag=True,
|
2019-06-07 11:16:07 +00:00
|
|
|
help='Display the apprise version and exit.')
|
2018-09-17 00:27:00 +00:00
|
|
|
@click.argument('urls', nargs=-1,
|
|
|
|
metavar='SERVER_URL [SERVER_URL2 [SERVER_URL3]]',)
|
2019-11-25 01:15:30 +00:00
|
|
|
def main(body, title, config, attach, urls, notification_type, theme, tag,
|
2020-09-14 12:24:26 +00:00
|
|
|
input_format, dry_run, recursion_depth, verbose, disable_async,
|
|
|
|
debug, version):
|
2018-09-17 00:27:00 +00:00
|
|
|
"""
|
|
|
|
Send a notification to all of the specified servers identified by their
|
|
|
|
URLs the content provided within the title, body and notification-type.
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
For a list of all of the supported services and information on how to
|
|
|
|
use them, check out at https://github.com/caronc/apprise
|
2018-09-17 00:27:00 +00:00
|
|
|
"""
|
|
|
|
# Note: Click ignores the return values of functions it wraps, If you
|
|
|
|
# want to return a specific error code, you must call sys.exit()
|
|
|
|
# as you will see below.
|
|
|
|
|
2020-09-14 12:24:26 +00:00
|
|
|
debug = True if debug else False
|
|
|
|
if debug:
|
|
|
|
# Verbosity must be a minimum of 3
|
|
|
|
verbose = 3 if verbose < 3 else verbose
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
# Logging
|
|
|
|
ch = logging.StreamHandler(sys.stdout)
|
2019-06-07 11:16:07 +00:00
|
|
|
if verbose > 3:
|
|
|
|
# -vvvv: Most Verbose Debug Logging
|
|
|
|
logger.setLevel(logging.TRACE)
|
|
|
|
|
|
|
|
elif verbose > 2:
|
|
|
|
# -vvv: Debug Logging
|
2018-09-17 00:27:00 +00:00
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
elif verbose > 1:
|
|
|
|
# -vv: INFO Messages
|
2018-09-17 00:27:00 +00:00
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
elif verbose > 0:
|
|
|
|
# -v: WARNING Messages
|
|
|
|
logger.setLevel(logging.WARNING)
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
else:
|
2019-06-07 11:16:07 +00:00
|
|
|
# No verbosity means we display ERRORS only AND any deprecation
|
|
|
|
# warnings
|
2018-09-17 00:27:00 +00:00
|
|
|
logger.setLevel(logging.ERROR)
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Format our logger
|
2018-09-17 00:27:00 +00:00
|
|
|
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
ch.setFormatter(formatter)
|
|
|
|
logger.addHandler(ch)
|
|
|
|
|
2020-09-14 12:24:26 +00:00
|
|
|
# Update our asyncio logger
|
|
|
|
asyncio_logger = logging.getLogger('asyncio')
|
|
|
|
for handler in logger.handlers:
|
|
|
|
asyncio_logger.addHandler(handler)
|
|
|
|
asyncio_logger.setLevel(logger.level)
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
if version:
|
|
|
|
print_version_msg()
|
|
|
|
sys.exit(0)
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2020-09-14 12:24:26 +00:00
|
|
|
# Simple Error Checking
|
|
|
|
notification_type = notification_type.strip().lower()
|
|
|
|
if notification_type not in NOTIFY_TYPES:
|
|
|
|
logger.error(
|
|
|
|
'The --notification-type (-n) value of {} is not supported.'
|
|
|
|
.format(notification_type))
|
|
|
|
# 2 is the same exit code returned by Click if there is a parameter
|
|
|
|
# issue. For consistency, we also return a 2
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
input_format = input_format.strip().lower()
|
|
|
|
if input_format not in NOTIFY_FORMATS:
|
|
|
|
logger.error(
|
|
|
|
'The --input-format (-i) value of {} is not supported.'
|
|
|
|
.format(input_format))
|
|
|
|
# 2 is the same exit code returned by Click if there is a parameter
|
|
|
|
# issue. For consistency, we also return a 2
|
|
|
|
sys.exit(2)
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
# Prepare our asset
|
2020-09-14 12:24:26 +00:00
|
|
|
asset = AppriseAsset(
|
|
|
|
body_format=input_format,
|
|
|
|
theme=theme,
|
|
|
|
# Async mode is only used for Python v3+ and allows a user to send
|
|
|
|
# all of their notifications asyncronously. This was made an option
|
|
|
|
# incase there are problems in the future where it's better that
|
|
|
|
# everything run sequentially/syncronously instead.
|
|
|
|
async_mode=disable_async is not True,
|
|
|
|
)
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2020-09-14 12:24:26 +00:00
|
|
|
# Create our Apprise object
|
|
|
|
a = Apprise(asset=asset, debug=debug)
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Load our configuration if no URLs or specified configuration was
|
|
|
|
# identified on the command line
|
|
|
|
a.add(AppriseConfig(
|
|
|
|
paths=[f for f in DEFAULT_SEARCH_PATHS if isfile(expanduser(f))]
|
2020-09-14 12:24:26 +00:00
|
|
|
if not (config or urls) else config,
|
|
|
|
asset=asset, recursion=recursion_depth))
|
2019-06-07 11:16:07 +00:00
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
# Load our inventory up
|
|
|
|
for url in urls:
|
|
|
|
a.add(url)
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
if len(a) == 0:
|
|
|
|
logger.error(
|
|
|
|
'You must specify at least one server URL or populated '
|
|
|
|
'configuration file.')
|
|
|
|
print_help_msg(main)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# each --tag entry comprises of a comma separated 'and' list
|
|
|
|
# we or each of of the --tag and sets specified.
|
|
|
|
tags = None if not tag else [parse_list(t) for t in tag]
|
|
|
|
|
2019-11-25 01:15:30 +00:00
|
|
|
if not dry_run:
|
|
|
|
if body is None:
|
|
|
|
logger.trace('No --body (-b) specified; reading from stdin')
|
|
|
|
# if no body was specified, then read from STDIN
|
|
|
|
body = click.get_text_stream('stdin').read()
|
|
|
|
|
|
|
|
# now print it out
|
|
|
|
result = a.notify(
|
|
|
|
body=body, title=title, notify_type=notification_type, tag=tags,
|
|
|
|
attach=attach)
|
|
|
|
else:
|
|
|
|
# Number of rows to assume in the terminal. In future, maybe this can
|
|
|
|
# be detected and made dynamic. The actual row count is 80, but 5
|
|
|
|
# characters are already reserved for the counter on the left
|
|
|
|
rows = 75
|
|
|
|
|
|
|
|
# Initialize our URL response; This is populated within the for/loop
|
|
|
|
# below; but plays a factor at the end when we need to determine if
|
|
|
|
# we iterated at least once in the loop.
|
|
|
|
url = None
|
|
|
|
|
|
|
|
for idx, server in enumerate(a.find(tag=tags)):
|
|
|
|
url = server.url(privacy=True)
|
|
|
|
click.echo("{: 3d}. {}".format(
|
|
|
|
idx + 1,
|
|
|
|
url if len(url) <= rows else '{}...'.format(url[:rows - 3])))
|
|
|
|
if server.tags:
|
|
|
|
click.echo("{} - {}".format(' ' * 5, ', '.join(server.tags)))
|
|
|
|
|
|
|
|
# Initialize a default response of nothing matched, otherwise
|
|
|
|
# if we matched at least one entry, we can return True
|
|
|
|
result = None if url is None else True
|
|
|
|
|
|
|
|
if result is None:
|
|
|
|
# There were no notifications set. This is a result of just having
|
|
|
|
# empty configuration files and/or being to restrictive when filtering
|
|
|
|
# by specific tag(s)
|
2020-09-14 12:24:26 +00:00
|
|
|
|
|
|
|
# Exit code 3 is used since Click uses exit code 2 if there is an
|
|
|
|
# error with the parameters specified
|
|
|
|
sys.exit(3)
|
2019-11-25 01:15:30 +00:00
|
|
|
|
|
|
|
elif result is False:
|
|
|
|
# At least 1 notification service failed to send
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# else: We're good!
|
|
|
|
sys.exit(0)
|