2023-01-12 01:04:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-10-21 11:47:10 +00:00
|
|
|
# BSD 2-Clause License
|
2023-01-12 01:04:47 +00:00
|
|
|
#
|
2023-04-13 08:41:12 +00:00
|
|
|
# Apprise - Push Notification Library.
|
2024-06-07 18:48:09 +00:00
|
|
|
# Copyright (c) 2024, Chris Caron <lead2gold@gmail.com>
|
2023-01-12 01:04:47 +00:00
|
|
|
#
|
2023-04-13 08:41:12 +00:00
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
2023-01-12 01:04:47 +00:00
|
|
|
#
|
2023-04-13 08:41:12 +00:00
|
|
|
# 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer.
|
2023-01-12 01:04:47 +00:00
|
|
|
#
|
2023-04-13 08:41:12 +00:00
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer in the documentation
|
|
|
|
# and/or other materials provided with the distribution.
|
2023-01-12 01:04:47 +00:00
|
|
|
#
|
2023-04-13 08:41:12 +00:00
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2024-06-07 18:48:09 +00:00
|
|
|
# Free Mobile
|
|
|
|
# 1. Visit https://mobile.free.fr/
|
|
|
|
|
|
|
|
# the URL will look something like this:
|
|
|
|
# https://smsapi.free-mobile.fr/sendmsg
|
|
|
|
#
|
|
|
|
|
2023-01-12 01:04:47 +00:00
|
|
|
import requests
|
2024-06-07 18:48:09 +00:00
|
|
|
from json import dumps
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2024-06-07 18:48:09 +00:00
|
|
|
from .base import NotifyBase
|
2023-01-12 01:04:47 +00:00
|
|
|
from ..common import NotifyType
|
2024-06-07 18:48:09 +00:00
|
|
|
from ..locale import gettext_lazy as _
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
|
2024-06-07 18:48:09 +00:00
|
|
|
class NotifyFreeMobile(NotifyBase):
|
2023-01-12 01:04:47 +00:00
|
|
|
"""
|
2024-06-07 18:48:09 +00:00
|
|
|
A wrapper for Free-Mobile Notifications
|
2023-01-12 01:04:47 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
# The default descriptive name associated with the Notification
|
2024-06-07 18:48:09 +00:00
|
|
|
service_name = _('Free-Mobile')
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# The services URL
|
2024-06-07 18:48:09 +00:00
|
|
|
service_url = 'https://mobile.free.fr/'
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2024-06-07 18:48:09 +00:00
|
|
|
# The default secure protocol
|
|
|
|
secure_protocol = 'freemobile'
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# A URL that takes you to the setup/help of the specific protocol
|
2024-06-07 18:48:09 +00:00
|
|
|
setup_url = 'https://github.com/caronc/apprise/wiki/Notify_freemobile'
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2024-06-07 18:48:09 +00:00
|
|
|
# Plain Text Notification URL
|
|
|
|
notify_url = 'https://smsapi.free-mobile.fr/sendmsg'
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Define object templates
|
|
|
|
templates = (
|
2024-06-07 18:48:09 +00:00
|
|
|
'{schema}://{user}@{password}',
|
2023-01-12 01:04:47 +00:00
|
|
|
)
|
|
|
|
|
2024-06-07 18:48:09 +00:00
|
|
|
# The title is not used
|
|
|
|
title_maxlen = 0
|
|
|
|
|
|
|
|
# SMS Messages are restricted in size
|
|
|
|
body_maxlen = 160
|
|
|
|
|
|
|
|
# Define our tokens; these are the minimum tokens required required to
|
|
|
|
# be passed into this function (as arguments). The syntax appends any
|
|
|
|
# previously defined in the base package and builds onto them
|
2023-01-12 01:04:47 +00:00
|
|
|
template_tokens = dict(NotifyBase.template_tokens, **{
|
2024-06-07 18:48:09 +00:00
|
|
|
'user': {
|
|
|
|
'name': _('Username'),
|
2023-01-12 01:04:47 +00:00
|
|
|
'type': 'string',
|
|
|
|
'required': True,
|
|
|
|
},
|
2024-06-07 18:48:09 +00:00
|
|
|
'password': {
|
|
|
|
'name': _('Password'),
|
|
|
|
'type': 'string',
|
|
|
|
'private': True,
|
|
|
|
'required': True,
|
2023-01-12 01:04:47 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-06-07 18:48:09 +00:00
|
|
|
def __init__(self, **kwargs):
|
2023-01-12 01:04:47 +00:00
|
|
|
"""
|
2024-06-07 18:48:09 +00:00
|
|
|
Initialize Free Mobile Object
|
2023-01-12 01:04:47 +00:00
|
|
|
"""
|
2023-01-14 20:40:05 +00:00
|
|
|
super().__init__(**kwargs)
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2024-06-07 18:48:09 +00:00
|
|
|
if not (self.user and self.password):
|
|
|
|
msg = 'A FreeMobile user and password ' \
|
|
|
|
'combination was not provided.'
|
2023-01-14 20:40:05 +00:00
|
|
|
self.logger.warning(msg)
|
|
|
|
raise TypeError(msg)
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
2024-06-07 18:48:09 +00:00
|
|
|
def url(self, privacy=False, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Returns the URL built dynamically based on specified arguments.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Prepare our parameters
|
|
|
|
params = self.url_parameters(privacy=privacy, *args, **kwargs)
|
|
|
|
|
|
|
|
return '{schema}://{user}@{password}/?{params}'.format(
|
|
|
|
schema=self.secure_protocol,
|
|
|
|
user=self.user,
|
|
|
|
password=self.pprint(self.password, privacy, safe=''),
|
|
|
|
params=NotifyFreeMobile.urlencode(params),
|
|
|
|
)
|
|
|
|
|
2023-01-12 01:04:47 +00:00
|
|
|
def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
|
|
|
|
"""
|
2024-06-07 18:48:09 +00:00
|
|
|
Send our notification
|
2023-01-12 01:04:47 +00:00
|
|
|
"""
|
|
|
|
|
2024-06-07 18:48:09 +00:00
|
|
|
# prepare our headers
|
2023-01-12 01:04:47 +00:00
|
|
|
headers = {
|
|
|
|
'User-Agent': self.app_id,
|
2024-06-07 18:48:09 +00:00
|
|
|
'Content-Type': 'application/json',
|
2023-01-12 01:04:47 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 18:48:09 +00:00
|
|
|
# Prepare our payload
|
2023-01-12 01:04:47 +00:00
|
|
|
payload = {
|
2024-06-07 18:48:09 +00:00
|
|
|
'user': self.user,
|
|
|
|
'pass': self.password,
|
|
|
|
'msg': body
|
2023-01-12 01:04:47 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 18:48:09 +00:00
|
|
|
self.logger.debug('Free Mobile GET URL: %s (cert_verify=%r)' % (
|
|
|
|
self.notify_url, self.verify_certificate))
|
|
|
|
self.logger.debug('Free Mobile Payload: %s' % str(payload))
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Always call throttle before any remote server i/o is made
|
|
|
|
self.throttle()
|
|
|
|
|
|
|
|
try:
|
|
|
|
r = requests.post(
|
|
|
|
self.notify_url,
|
2024-06-07 18:48:09 +00:00
|
|
|
data=dumps(payload).encode('utf-8'),
|
2023-01-12 01:04:47 +00:00
|
|
|
headers=headers,
|
|
|
|
verify=self.verify_certificate,
|
2023-01-14 20:40:05 +00:00
|
|
|
timeout=self.request_timeout,
|
2023-01-12 01:04:47 +00:00
|
|
|
)
|
|
|
|
if r.status_code != requests.codes.ok:
|
|
|
|
# We had a problem
|
|
|
|
status_str = \
|
2024-06-07 18:48:09 +00:00
|
|
|
NotifyFreeMobile.http_response_code_lookup(r.status_code)
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
self.logger.warning(
|
2024-06-07 18:48:09 +00:00
|
|
|
'Failed to send Free Mobile notification: '
|
2023-01-12 01:04:47 +00:00
|
|
|
'{}{}error={}.'.format(
|
|
|
|
status_str,
|
|
|
|
', ' if status_str else '',
|
|
|
|
r.status_code))
|
|
|
|
|
|
|
|
self.logger.debug('Response Details:\r\n{}'.format(r.content))
|
|
|
|
|
|
|
|
# Return; we're done
|
|
|
|
return False
|
|
|
|
|
|
|
|
else:
|
2024-06-07 18:48:09 +00:00
|
|
|
self.logger.info('Sent Free Mobile notification.')
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
except requests.RequestException as e:
|
|
|
|
self.logger.warning(
|
2024-06-07 18:48:09 +00:00
|
|
|
'A Connection error occurred sending Free Mobile '
|
|
|
|
'notification.')
|
2023-01-12 01:04:47 +00:00
|
|
|
self.logger.debug('Socket Exception: %s' % str(e))
|
|
|
|
|
|
|
|
# Return; we're done
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def parse_url(url):
|
|
|
|
"""
|
|
|
|
Parses the URL and returns enough arguments that can allow
|
2023-01-14 20:40:05 +00:00
|
|
|
us to re-instantiate this object.
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
"""
|
2024-06-07 18:48:09 +00:00
|
|
|
|
|
|
|
# parse_url already handles getting the `user` and `password` fields
|
|
|
|
# populated.
|
2023-01-14 20:40:05 +00:00
|
|
|
results = NotifyBase.parse_url(url, verify_host=False)
|
2023-01-12 01:04:47 +00:00
|
|
|
if not results:
|
|
|
|
# We're done early as we couldn't load the results
|
|
|
|
return results
|
|
|
|
|
2024-06-07 18:48:09 +00:00
|
|
|
# The hostname can act as the password if specified and a password
|
|
|
|
# was otherwise not (specified):
|
|
|
|
if not results.get('password'):
|
|
|
|
results['password'] = NotifyFreeMobile.unquote(results['host'])
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
return results
|