2014-03-10 05:18:05 +00:00
|
|
|
# Author: Nic Wolfe <nic@wolfeden.ca>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# This file is part of SickGear.
|
2014-03-10 05:18:05 +00:00
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# SickGear is free software: you can redistribute it and/or modify
|
2014-03-10 05:18:05 +00:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# SickGear is distributed in the hope that it will be useful,
|
2014-03-10 05:18:05 +00:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2014-11-12 16:43:14 +00:00
|
|
|
# along with SickGear. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-06-09 11:13:00 +00:00
|
|
|
from __future__ import print_function
|
2014-03-10 05:18:05 +00:00
|
|
|
import socket
|
2017-10-17 15:43:28 +00:00
|
|
|
import urllib
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
import sickbeard
|
|
|
|
from sickbeard.exceptions import ex
|
2017-10-17 15:43:28 +00:00
|
|
|
from sickbeard.notifiers.generic import Notifier, notify_strings
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
from lib.growl import gntp
|
|
|
|
|
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
class GrowlNotifier(Notifier):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super(GrowlNotifier, self).__init__()
|
|
|
|
|
|
|
|
def _send_growl_msg(self, options, message=None):
|
|
|
|
|
|
|
|
# Send Notification
|
2014-03-10 05:18:05 +00:00
|
|
|
notice = gntp.GNTPNotice()
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
# Required
|
2014-03-25 05:57:24 +00:00
|
|
|
notice.add_header('Application-Name', options['app'])
|
|
|
|
notice.add_header('Notification-Name', options['name'])
|
|
|
|
notice.add_header('Notification-Title', options['title'])
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
if options['password']:
|
|
|
|
notice.set_password(options['password'])
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
# Optional
|
2014-03-10 05:18:05 +00:00
|
|
|
if options['sticky']:
|
2014-03-25 05:57:24 +00:00
|
|
|
notice.add_header('Notification-Sticky', options['sticky'])
|
2014-03-10 05:18:05 +00:00
|
|
|
if options['priority']:
|
2014-03-25 05:57:24 +00:00
|
|
|
notice.add_header('Notification-Priority', options['priority'])
|
2014-03-10 05:18:05 +00:00
|
|
|
if options['icon']:
|
2018-03-23 22:29:48 +00:00
|
|
|
notice.add_header('Notification-Icon', self._sg_logo_url)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
if message:
|
2014-03-25 05:57:24 +00:00
|
|
|
notice.add_header('Notification-Text', message)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
response = self._send(options['host'], options['port'], notice.encode(), options['debug'])
|
2017-10-17 15:43:28 +00:00
|
|
|
if isinstance(response, gntp.GNTPOK):
|
|
|
|
return True
|
2014-03-10 05:18:05 +00:00
|
|
|
return False
|
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
@staticmethod
|
|
|
|
def _send(host, port, data, debug=False):
|
|
|
|
|
|
|
|
if debug:
|
|
|
|
print('<Sending>\n', data, '\n</Sending>')
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2014-03-25 05:57:24 +00:00
|
|
|
s.connect((host, port))
|
2014-03-10 05:18:05 +00:00
|
|
|
s.send(data)
|
|
|
|
response = gntp.parse_gntp(s.recv(1024))
|
|
|
|
s.close()
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
if debug:
|
|
|
|
print('<Recieved>\n', response, '\n</Recieved>')
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
return response
|
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
def _send_registration(self, host=None, password=None):
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
host_parts = self._choose(host, sickbeard.GROWL_HOST).split(':')
|
|
|
|
port = 23053 if (2 != len(host_parts) or '' == host_parts[1]) else int(host_parts[1])
|
|
|
|
password = self._choose(password, sickbeard.GROWL_PASSWORD)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
opts = dict(app='SickGear', host=host_parts[0], port=port, password=password, debug=False)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
# Send Registration
|
|
|
|
register = gntp.GNTPRegister()
|
|
|
|
register.add_header('Application-Name', opts['app'])
|
|
|
|
register.add_header('Application-Icon', self._sg_logo_url)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
register.add_notification('Test', True)
|
|
|
|
register.add_notification(notify_strings['snatch'], True)
|
|
|
|
register.add_notification(notify_strings['download'], True)
|
|
|
|
register.add_notification(notify_strings['git_updated'], True)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
if opts['password']:
|
|
|
|
register.set_password(opts['password'])
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
try:
|
|
|
|
return self._send(opts['host'], opts['port'], register.encode(), opts['debug'])
|
|
|
|
except Exception as e:
|
|
|
|
self._log_warning(u'Unable to send growl to %s:%s - %s' % (opts['host'], opts['port'], ex(e)))
|
|
|
|
return False
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
def _notify(self, title, body, name=None, host=None, password=None, **kwargs):
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
name = name or title or 'SickGear Notification'
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
host_parts = self._choose(host, sickbeard.GROWL_HOST).split(':')
|
|
|
|
port = (int(host_parts[1]), 23053)[len(host_parts) != 2 or '' == host_parts[1]]
|
|
|
|
growl_hosts = [(host_parts[0], port)]
|
|
|
|
password = self._choose(password, sickbeard.GROWL_PASSWORD)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
opts = dict(title=title, name=name, app='SickGear', sticky=None, priority=None,
|
|
|
|
password=password, icon=True, debug=False)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
for pc in growl_hosts:
|
2014-03-10 05:18:05 +00:00
|
|
|
opts['host'] = pc[0]
|
|
|
|
opts['port'] = pc[1]
|
|
|
|
try:
|
2017-10-17 15:43:28 +00:00
|
|
|
if self._send_growl_msg(opts, body):
|
2014-03-10 05:18:05 +00:00
|
|
|
return True
|
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
if self._send_registration(host, password):
|
|
|
|
return self._send_growl_msg(opts, body)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
except Exception as e:
|
|
|
|
self._log_warning(u'Unable to send growl to %s:%s - %s' % (opts['host'], opts['port'], ex(e)))
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
return False
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
def test_notify(self, host, password):
|
|
|
|
self._testing = True
|
|
|
|
self._send_registration(host, password)
|
|
|
|
return ('Success, registered and tested', 'Failed registration and testing')[
|
2018-03-23 22:29:48 +00:00
|
|
|
'sent' not in super(GrowlNotifier, self).test_notify(name='Test', host=host, password=password)] + \
|
2017-10-17 15:43:28 +00:00
|
|
|
(urllib.unquote_plus(host) + ' with password: ' + password, '')[password in (None, '')]
|
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-05-29 00:30:38 +00:00
|
|
|
notifier = GrowlNotifier
|