summaryrefslogtreecommitdiffhomepage
path: root/libs/apprise/plugins/NotifyGrowl.py
blob: 446ad660e68fd5ed617ebc7590ecba6449f3043e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# -*- coding: utf-8 -*-
#
# Copyright (C) 2020 Chris Caron <lead2gold@gmail.com>
# All rights reserved.
#
# This code is licensed under the MIT License.
#
# 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 :
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# 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.

from .NotifyBase import NotifyBase
from ..URLBase import PrivacyMode
from ..common import NotifyImageSize
from ..common import NotifyType
from ..utils import parse_bool
from ..AppriseLocale import gettext_lazy as _

# Default our global support flag
NOTIFY_GROWL_SUPPORT_ENABLED = False

try:
    import gntp.notifier

    # We're good to go!
    NOTIFY_GROWL_SUPPORT_ENABLED = True

except ImportError:
    # No problem; we just simply can't support this plugin until
    # gntp is installed
    pass


# Priorities
class GrowlPriority(object):
    LOW = -2
    MODERATE = -1
    NORMAL = 0
    HIGH = 1
    EMERGENCY = 2


GROWL_PRIORITIES = (
    GrowlPriority.LOW,
    GrowlPriority.MODERATE,
    GrowlPriority.NORMAL,
    GrowlPriority.HIGH,
    GrowlPriority.EMERGENCY,
)


class NotifyGrowl(NotifyBase):
    """
    A wrapper to Growl Notifications

    """
    # Set our global enabled flag
    enabled = NOTIFY_GROWL_SUPPORT_ENABLED

    requirements = {
        # Define our required packaging in order to work
        'packages_required': 'gntp'
    }

    # The default descriptive name associated with the Notification
    service_name = 'Growl'

    # The services URL
    service_url = 'http://growl.info/'

    # The default protocol
    protocol = 'growl'

    # A URL that takes you to the setup/help of the specific protocol
    setup_url = 'https://github.com/caronc/apprise/wiki/Notify_growl'

    # Allows the user to specify the NotifyImageSize object
    image_size = NotifyImageSize.XY_72

    # Disable throttle rate for Growl requests since they are normally
    # local anyway
    request_rate_per_sec = 0

    # Limit results to just the first 10 line otherwise there is just to much
    # content to display
    body_max_line_count = 2

    # Default Growl Port
    default_port = 23053

    # The Growl notification type used
    growl_notification_type = "New Messages"

    # Define object templates
    templates = (
        '{schema}://{host}',
        '{schema}://{host}:{port}',
        '{schema}://{password}@{host}',
        '{schema}://{password}@{host}:{port}',
    )

    # Define our template tokens
    template_tokens = dict(NotifyBase.template_tokens, **{
        'host': {
            'name': _('Hostname'),
            'type': 'string',
            'required': True,
        },
        'port': {
            'name': _('Port'),
            'type': 'int',
            'min': 1,
            'max': 65535,
        },
        'password': {
            'name': _('Password'),
            'type': 'string',
            'private': True,
        },
    })

    # Define our template arguments
    template_args = dict(NotifyBase.template_args, **{
        'priority': {
            'name': _('Priority'),
            'type': 'choice:int',
            'values': GROWL_PRIORITIES,
            'default': GrowlPriority.NORMAL,
        },
        'version': {
            'name': _('Version'),
            'type': 'choice:int',
            'values': (1, 2),
            'default': 2,
        },
        'image': {
            'name': _('Include Image'),
            'type': 'bool',
            'default': True,
            'map_to': 'include_image',
        },
        'sticky': {
            'name': _('Sticky'),
            'type': 'bool',
            'default': True,
            'map_to': 'sticky',
        },
    })

    def __init__(self, priority=None, version=2, include_image=True,
                 sticky=False, **kwargs):
        """
        Initialize Growl Object
        """
        super(NotifyGrowl, self).__init__(**kwargs)

        if not self.port:
            self.port = self.default_port

        # The Priority of the message
        if priority not in GROWL_PRIORITIES:
            self.priority = GrowlPriority.NORMAL

        else:
            self.priority = priority

        # Our Registered object
        self.growl = None

        # Sticky flag
        self.sticky = sticky

        # Store Version
        self.version = version

        # Track whether or not we want to send an image with our notification
        # or not.
        self.include_image = include_image

        return

    def register(self):
        """
        Registers with the Growl server
        """
        payload = {
            'applicationName': self.app_id,
            'notifications': [self.growl_notification_type, ],
            'defaultNotifications': [self.growl_notification_type, ],
            'hostname': self.host,
            'port': self.port,
        }

        if self.password is not None:
            payload['password'] = self.password

        self.logger.debug('Growl Registration Payload: %s' % str(payload))
        self.growl = gntp.notifier.GrowlNotifier(**payload)

        try:
            self.growl.register()

        except gntp.errors.NetworkError:
            msg = 'A network error error occurred registering ' \
                  'with Growl at {}.'.format(self.host)
            self.logger.warning(msg)
            return False

        except gntp.errors.ParseError:
            msg = 'A parsing error error occurred registering ' \
                  'with Growl at {}.'.format(self.host)
            self.logger.warning(msg)
            return False

        except gntp.errors.AuthError:
            msg = 'An authentication error error occurred registering ' \
                  'with Growl at {}.'.format(self.host)
            self.logger.warning(msg)
            return False

        except gntp.errors.UnsupportedError:
            msg = 'An unsupported error occurred registering with ' \
                  'Growl at {}.'.format(self.host)
            self.logger.warning(msg)
            return False

        self.logger.debug(
            'Growl server registration completed successfully.'
        )

        # Return our state
        return True

    def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
        """
        Perform Growl Notification
        """
        # Register ourselves with the server if we haven't done so already
        if not self.growl and not self.register():
            # We failed to register
            return False

        icon = None
        if self.version >= 2:
            # URL Based
            icon = None if not self.include_image \
                else self.image_url(notify_type)

        else:
            # Raw
            icon = None if not self.include_image \
                else self.image_raw(notify_type)

        payload = {
            'noteType': self.growl_notification_type,
            'title': title,
            'description': body,
            'icon': icon is not None,
            'sticky': self.sticky,
            'priority': self.priority,
        }
        self.logger.debug('Growl Payload: %s' % str(payload))

        # Update icon of payload to be raw data; this is intentionally done
        # here after we spit the debug message above (so we don't try to
        # print the binary contents of an image
        payload['icon'] = icon

        # Always call throttle before any remote server i/o is made
        self.throttle()

        try:
            # Perform notification
            response = self.growl.notify(**payload)
            if not isinstance(response, bool):
                self.logger.warning(
                    'Growl notification failed to send with response: %s' %
                    str(response),
                )

            else:
                self.logger.info('Sent Growl notification.')

        except gntp.errors.BaseError as e:
            # Since Growl servers listen for UDP broadcasts, it's possible
            # that you will never get to this part of the code since there is
            # no acknowledgement as to whether it accepted what was sent to it
            # or not.

            # However, if the host/server is unavailable, you will get to this
            # point of the code.
            self.logger.warning(
                'A Connection error occurred sending Growl '
                'notification to %s.' % self.host)
            self.logger.debug('Growl Exception: %s' % str(e))

            # Return; we're done
            return False

        return True

    def url(self, privacy=False, *args, **kwargs):
        """
        Returns the URL built dynamically based on specified arguments.
        """

        _map = {
            GrowlPriority.LOW: 'low',
            GrowlPriority.MODERATE: 'moderate',
            GrowlPriority.NORMAL: 'normal',
            GrowlPriority.HIGH: 'high',
            GrowlPriority.EMERGENCY: 'emergency',
        }

        # Define any URL parameters
        params = {
            'image': 'yes' if self.include_image else 'no',
            'sticky': 'yes' if self.sticky else 'no',
            'priority':
                _map[GrowlPriority.NORMAL] if self.priority not in _map
                else _map[self.priority],
            'version': self.version,
        }

        # Extend our parameters
        params.update(self.url_parameters(privacy=privacy, *args, **kwargs))

        auth = ''
        if self.user:
            # The growl password is stored in the user field
            auth = '{password}@'.format(
                password=self.pprint(
                    self.user, privacy, mode=PrivacyMode.Secret, safe=''),
            )

        return '{schema}://{auth}{hostname}{port}/?{params}'.format(
            schema=self.secure_protocol if self.secure else self.protocol,
            auth=auth,
            # never encode hostname since we're expecting it to be a valid one
            hostname=self.host,
            port='' if self.port is None or self.port == self.default_port
                 else ':{}'.format(self.port),
            params=NotifyGrowl.urlencode(params),
        )

    @staticmethod
    def parse_url(url):
        """
        Parses the URL and returns enough arguments that can allow
        us to re-instantiate this object.

        """
        results = NotifyBase.parse_url(url)
        if not results:
            # We're done early as we couldn't load the results
            return results

        version = None
        if 'version' in results['qsd'] and len(results['qsd']['version']):
            # Allow the user to specify the version of the protocol to use.
            try:
                version = int(
                    NotifyGrowl.unquote(
                        results['qsd']['version']).strip().split('.')[0])

            except (AttributeError, IndexError, TypeError, ValueError):
                NotifyGrowl.logger.warning(
                    'An invalid Growl version of "%s" was specified and will '
                    'be ignored.' % results['qsd']['version']
                )
                pass

        if 'priority' in results['qsd'] and len(results['qsd']['priority']):
            _map = {
                # Letter Assignments
                'l': GrowlPriority.LOW,
                'm': GrowlPriority.MODERATE,
                'n': GrowlPriority.NORMAL,
                'h': GrowlPriority.HIGH,
                'e': GrowlPriority.EMERGENCY,
                'lo': GrowlPriority.LOW,
                'me': GrowlPriority.MODERATE,
                'no': GrowlPriority.NORMAL,
                'hi': GrowlPriority.HIGH,
                'em': GrowlPriority.EMERGENCY,
                # Support 3rd Party Documented Scale
                '-2': GrowlPriority.LOW,
                '-1': GrowlPriority.MODERATE,
                '0': GrowlPriority.NORMAL,
                '1': GrowlPriority.HIGH,
                '2': GrowlPriority.EMERGENCY,
            }
            try:
                results['priority'] = \
                    _map[results['qsd']['priority'][0:2].lower()]

            except KeyError:
                # No priority was set
                pass

        # Because of the URL formatting, the password is actually where the
        # username field is. For this reason, we just preform this small hack
        # to make it (the URL) conform correctly. The following strips out the
        # existing password entry (if exists) so that it can be swapped with
        # the new one we specify.
        if results.get('password', None) is None:
            results['password'] = results.get('user', None)

        # Include images with our message
        results['include_image'] = \
            parse_bool(results['qsd'].get('image',
                       NotifyGrowl.template_args['image']['default']))

        # Include images with our message
        results['sticky'] = \
            parse_bool(results['qsd'].get('sticky',
                       NotifyGrowl.template_args['sticky']['default']))

        # Set our version
        if version:
            results['version'] = version

        return results