summaryrefslogtreecommitdiff
path: root/plugins/user_extras.py
blob: ad49a14214cdc10e2bcf5990b657fce2cb04adaa (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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
"""
This plugin enables rich presence events, such as mood, activity, gaming or tune.

.. versionadded:: 0.14
    This plugin was previously provided in the poezio core features.

Command
-------
.. glossary::

    /activity
        **Usage:** ``/activity [<general> [specific] [comment]]``

        Send your current activity to your contacts (use the completion to cycle
        through all the general and specific possible activities).

        Nothing means "stop broadcasting an activity".

    /mood
        **Usage:** ``/mood [<mood> [comment]]``
        Send your current mood to your contacts (use the completion to cycle
        through all the possible moods).

        Nothing means "stop broadcasting a mood".

    /gaming
        **Usage:** ``/gaming [<game name> [server address]]``

        Send your current gaming activity to your contacts.

        Nothing means "stop broadcasting a gaming activity".


Configuration
-------------

.. glossary::

    display_gaming_notifications

        **Default value:** ``true``

        If set to true, notifications about the games your contacts are playing
        will be displayed in the info buffer as 'Gaming' messages.

    display_tune_notifications

        **Default value:** ``true``

        If set to true, notifications about the music your contacts listen to
        will be displayed in the info buffer as 'Tune' messages.

    display_mood_notifications

        **Default value:** ``true``

        If set to true, notifications about the mood of your contacts
        will be displayed in the info buffer as 'Mood' messages.

    display_activity_notifications

        **Default value:** ``true``

        If set to true, notifications about the current activity of your contacts
        will be displayed in the info buffer as 'Activity' messages.

    enable_user_activity

        **Default value:** ``true``

        Set this to ``false`` if you don’t want to receive the activity of your contacts.

    enable_user_gaming

        **Default value:** ``true``

        Set this to ``false`` if you don’t want to receive the gaming activity of your contacts.

    enable_user_mood

        **Default value:** ``true``

        Set this to ``false`` if you don’t want to receive the mood of your contacts.

    enable_user_tune

        **Default value:** ``true``

        If this is set to ``false``, you will no longer be subscribed to tune events,
        and the :term:`display_tune_notifications` option will be ignored.


"""
from asyncio import (
    ensure_future,
    gather,
)
from functools import reduce
from typing import Dict

from slixmpp import InvalidJID, JID, Message
from poezio.decorators import command_args_parser
from poezio.plugin import BasePlugin
from poezio.roster import roster
from poezio.contact import Contact, Resource
from poezio.core.structs import Completion
from poezio import common
from poezio import tabs


class Plugin(BasePlugin):

    default_config = {
        'user_extras': {
            'display_gaming_notifications': True,
            'display_mood_notifications': True,
            'display_activity_notifications': True,
            'display_tune_notifications': True,
            'enable_user_activity': True,
            'enable_user_gaming': True,
            'enable_user_mood': True,
            'enable_user_tune': True,
        }
    }

    def init(self):
        for plugin in {'xep_0196', 'xep_0108', 'xep_0107', 'xep_0118'}:
            self.core.xmpp.register_plugin(plugin)
        self.api.add_command(
            'activity',
            self.command_activity,
            usage='[<general> [specific] [text]]',
            help='Send your current activity to your contacts '
            '(use the completion). Nothing means '
            '"stop broadcasting an activity".',
            short='Send your activity.',
            completion=self.comp_activity
        )
        self.api.add_command(
            'mood',
            self.command_mood,
            usage='[<mood> [text]]',
            help='Send your current mood to your contacts '
            '(use the completion). Nothing means '
            '"stop broadcasting a mood".',
            short='Send your mood.',
            completion=self.comp_mood,
        )
        self.api.add_command(
            'gaming',
            self.command_gaming,
            usage='[<game name> [server address]]',
            help='Send your current gaming activity to '
            'your contacts. Nothing means "stop '
            'broadcasting a gaming activity".',
            short='Send your gaming activity.',
            completion=None
        )
        handlers = [
            ('user_mood_publish', self.on_mood_event),
            ('user_tune_publish', self.on_tune_event),
            ('user_gaming_publish', self.on_gaming_event),
            ('user_activity_publish', self.on_activity_event),
        ]
        for name, handler in handlers:
            self.core.xmpp.add_event_handler(name, handler)

    def cleanup(self):
        handlers = [
            ('user_mood_publish', self.on_mood_event),
            ('user_tune_publish', self.on_tune_event),
            ('user_gaming_publish', self.on_gaming_event),
            ('user_activity_publish', self.on_activity_event),
        ]
        for name, handler in handlers:
            self.core.xmpp.del_event_handler(name, handler)
        ensure_future(self._stop())

    async def _stop(self):
        await gather(
            self.core.xmpp.plugin['xep_0108'].stop(),
            self.core.xmpp.plugin['xep_0107'].stop(),
            self.core.xmpp.plugin['xep_0196'].stop(),
        )


    @command_args_parser.quoted(0, 2)
    async def command_mood(self, args):
        """
        /mood [<mood> [text]]
        """
        if not args:
            return await self.core.xmpp.plugin['xep_0107'].stop()
        mood = args[0]
        if mood not in MOODS:
            return self.core.information(
                '%s is not a correct value for a mood.' % mood, 'Error')
        if len(args) == 2:
            text = args[1]
        else:
            text = None
        await self.core.xmpp.plugin['xep_0107'].publish_mood(
            mood, text
        )

    @command_args_parser.quoted(0, 3)
    async def command_activity(self, args):
        """
        /activity [<general> [specific] [text]]
        """
        length = len(args)
        if not length:
            return await self.core.xmpp.plugin['xep_0108'].stop()

        general = args[0]
        if general not in ACTIVITIES:
            return self.api.information(
                '%s is not a correct value for an activity' % general, 'Error')
        specific = None
        text = None
        if length == 2:
            if args[1] in ACTIVITIES[general]:
                specific = args[1]
            else:
                text = args[1]
        elif length == 3:
            specific = args[1]
            text = args[2]
        if specific and specific not in ACTIVITIES[general]:
            return self.core.information(
                '%s is not a correct value '
                'for an activity' % specific, 'Error')
        await self.core.xmpp.plugin['xep_0108'].publish_activity(
            general, specific, text
        )

    @command_args_parser.quoted(0, 2)
    async def command_gaming(self, args):
        """
        /gaming [<game name> [server address]]
        """
        if not args:
            return await self.core.xmpp.plugin['xep_0196'].stop()

        name = args[0]
        if len(args) > 1:
            address = args[1]
        else:
            address = None
        return await self.core.xmpp.plugin['xep_0196'].publish_gaming(
            name=name, server_address=address
        )

    def comp_activity(self, the_input):
        """Completion for /activity"""
        n = the_input.get_argument_position(quoted=True)
        args = common.shell_split(the_input.text)
        if n == 1:
            return Completion(
                the_input.new_completion,
                sorted(ACTIVITIES.keys()),
                n,
                quotify=True)
        elif n == 2:
            if args[1] in ACTIVITIES:
                l = list(ACTIVITIES[args[1]])
                l.remove('category')
                l.sort()
                return Completion(the_input.new_completion, l, n, quotify=True)

    def comp_mood(self, the_input):
        """Completion for /mood"""
        n = the_input.get_argument_position(quoted=True)
        if n == 1:
            return Completion(
                the_input.new_completion,
                sorted(MOODS.keys()),
                1,
                quotify=True)

    def on_gaming_event(self, message: Message):
        """
        Called when a pep notification for user gaming
        is received
        """
        contact = roster[message['from'].bare]
        if not contact:
            return
        item = message['pubsub_event']['items']['item']
        old_gaming = contact.rich_presence['gaming']
        xml_node = item.xml.find('{urn:xmpp:gaming:0}game')
        # list(xml_node) checks whether there are children or not.
        if xml_node is not None and list(xml_node):
            item = item['gaming']
            # only name and server_address are used for now
            contact.rich_presence['gaming'] = {
                'character_name': item['character_name'],
                'character_profile': item['character_profile'],
                'name': item['name'],
                'level': item['level'],
                'uri': item['uri'],
                'server_name': item['server_name'],
                'server_address': item['server_address'],
            }
        else:
            contact.rich_presence['gaming'] = {}

        if old_gaming != contact.rich_presence['gaming'] and self.config.get(
                'display_gaming_notifications'):
            if contact.rich_presence['gaming']:
                self.core.information(
                    '%s is playing %s' % (contact.bare_jid,
                                          common.format_gaming_string(
                                              contact.rich_presence['gaming'])), 'Gaming')
            else:
                self.core.information(contact.bare_jid + ' stopped playing.',
                                      'Gaming')

    def on_mood_event(self, message: Message):
        """
        Called when a pep notification for a user mood
        is received.
        """
        contact = roster[message['from'].bare]
        if not contact:
            return
        item = message['pubsub_event']['items']['item']
        old_mood = contact.rich_presence.get('mood')
        plugin = item.get_plugin('mood', check=True)
        if plugin:
            mood = item['mood']['value']
        else:
            mood = ''
        if mood:
            mood = MOODS.get(mood, mood)
            text = item['mood']['text']
            if text:
                mood = '%s (%s)' % (mood, text)
            contact.rich_presence['mood'] = mood
        else:
            contact.rich_presence['mood'] = ''

        if old_mood != contact.rich_presence['mood'] and self.config.get(
                'display_mood_notifications'):
            if contact.rich_presence['mood']:
                self.core.information(
                    'Mood from ' + contact.bare_jid + ': ' + contact.rich_presence['mood'],
                    'Mood')
            else:
                self.core.information(
                    contact.bare_jid + ' stopped having their mood.', 'Mood')

    def on_activity_event(self, message: Message):
        """
        Called when a pep notification for a user activity
        is received.
        """
        contact = roster[message['from'].bare]
        if not contact:
            return
        item = message['pubsub_event']['items']['item']
        old_activity = contact.rich_presence['activity']
        xml_node = item.xml.find('{http://jabber.org/protocol/activity}activity')
        # list(xml_node) checks whether there are children or not.
        if xml_node is not None and list(xml_node):
            try:
                activity = item['activity']['value']
            except ValueError:
                return
            if activity[0]:
                general = ACTIVITIES.get(activity[0])
                if general is None:
                    return
                s = general['category']
                if activity[1]:
                    s = s + '/' + general.get(activity[1], 'other')
                text = item['activity']['text']
                if text:
                    s = '%s (%s)' % (s, text)
                contact.rich_presence['activity'] = s
            else:
                contact.rich_presence['activity'] = ''
        else:
            contact.rich_presence['activity'] = ''

        if old_activity != contact.rich_presence['activity'] and self.config.get(
                'display_activity_notifications'):
            if contact.rich_presence['activity']:
                self.core.information(
                    'Activity from ' + contact.bare_jid + ': ' +
                    contact.rich_presence['activity'], 'Activity')
            else:
                self.core.information(
                    contact.bare_jid + ' stopped doing their activity.',
                    'Activity')

    def on_tune_event(self, message: Message):
        """
        Called when a pep notification for a user tune
        is received
        """
        contact = roster[message['from'].bare]
        if not contact:
            return
        roster.modified()
        item = message['pubsub_event']['items']['item']
        old_tune = contact.rich_presence['tune']
        xml_node = item.xml.find('{http://jabber.org/protocol/tune}tune')
        # list(xml_node) checks whether there are children or not.
        if xml_node is not None and list(xml_node):
            item = item['tune']
            contact.rich_presence['tune'] = {
                'artist': item['artist'],
                'length': item['length'],
                'rating': item['rating'],
                'source': item['source'],
                'title': item['title'],
                'track': item['track'],
                'uri': item['uri']
            }
        else:
            contact.rich_presence['tune'] = {}

        if old_tune != contact.rich_presence['tune'] and self.config.get(
                'display_tune_notifications'):
            if contact.rich_presence['tune']:
                self.core.information(
                    'Tune from ' + message['from'].bare + ': ' +
                    common.format_tune_string(contact.rich_presence['tune']), 'Tune')
            else:
                self.core.information(
                    contact.bare_jid + ' stopped listening to music.', 'Tune')


# Collection of mappings for PEP moods/activities
# extracted directly from the XEP

MOODS: Dict[str, str] = {
    'afraid': 'Afraid',
    'amazed': 'Amazed',
    'angry': 'Angry',
    'amorous': 'Amorous',
    'annoyed': 'Annoyed',
    'anxious': 'Anxious',
    'aroused': 'Aroused',
    'ashamed': 'Ashamed',
    'bored': 'Bored',
    'brave': 'Brave',
    'calm': 'Calm',
    'cautious': 'Cautious',
    'cold': 'Cold',
    'confident': 'Confident',
    'confused': 'Confused',
    'contemplative': 'Contemplative',
    'contented': 'Contented',
    'cranky': 'Cranky',
    'crazy': 'Crazy',
    'creative': 'Creative',
    'curious': 'Curious',
    'dejected': 'Dejected',
    'depressed': 'Depressed',
    'disappointed': 'Disappointed',
    'disgusted': 'Disgusted',
    'dismayed': 'Dismayed',
    'distracted': 'Distracted',
    'embarrassed': 'Embarrassed',
    'envious': 'Envious',
    'excited': 'Excited',
    'flirtatious': 'Flirtatious',
    'frustrated': 'Frustrated',
    'grumpy': 'Grumpy',
    'guilty': 'Guilty',
    'happy': 'Happy',
    'hopeful': 'Hopeful',
    'hot': 'Hot',
    'humbled': 'Humbled',
    'humiliated': 'Humiliated',
    'hungry': 'Hungry',
    'hurt': 'Hurt',
    'impressed': 'Impressed',
    'in_awe': 'In awe',
    'in_love': 'In love',
    'indignant': 'Indignant',
    'interested': 'Interested',
    'intoxicated': 'Intoxicated',
    'invincible': 'Invincible',
    'jealous': 'Jealous',
    'lonely': 'Lonely',
    'lucky': 'Lucky',
    'mean': 'Mean',
    'moody': 'Moody',
    'nervous': 'Nervous',
    'neutral': 'Neutral',
    'offended': 'Offended',
    'outraged': 'Outraged',
    'playful': 'Playful',
    'proud': 'Proud',
    'relaxed': 'Relaxed',
    'relieved': 'Relieved',
    'remorseful': 'Remorseful',
    'restless': 'Restless',
    'sad': 'Sad',
    'sarcastic': 'Sarcastic',
    'serious': 'Serious',
    'shocked': 'Shocked',
    'shy': 'Shy',
    'sick': 'Sick',
    'sleepy': 'Sleepy',
    'spontaneous': 'Spontaneous',
    'stressed': 'Stressed',
    'strong': 'Strong',
    'surprised': 'Surprised',
    'thankful': 'Thankful',
    'thirsty': 'Thirsty',
    'tired': 'Tired',
    'undefined': 'Undefined',
    'weak': 'Weak',
    'worried': 'Worried'
}

ACTIVITIES: Dict[str, Dict[str, str]] = {
    'doing_chores': {
        'category': 'Doing_chores',
        'buying_groceries': 'Buying groceries',
        'cleaning': 'Cleaning',
        'cooking': 'Cooking',
        'doing_maintenance': 'Doing maintenance',
        'doing_the_dishes': 'Doing the dishes',
        'doing_the_laundry': 'Doing the laundry',
        'gardening': 'Gardening',
        'running_an_errand': 'Running an errand',
        'walking_the_dog': 'Walking the dog',
        'other': 'Other',
    },
    'drinking': {
        'category': 'Drinking',
        'having_a_beer': 'Having a beer',
        'having_coffee': 'Having coffee',
        'having_tea': 'Having tea',
        'other': 'Other',
    },
    'eating': {
        'category': 'Eating',
        'having_breakfast': 'Having breakfast',
        'having_a_snack': 'Having a snack',
        'having_dinner': 'Having dinner',
        'having_lunch': 'Having lunch',
        'other': 'Other',
    },
    'exercising': {
        'category': 'Exercising',
        'cycling': 'Cycling',
        'dancing': 'Dancing',
        'hiking': 'Hiking',
        'jogging': 'Jogging',
        'playing_sports': 'Playing sports',
        'running': 'Running',
        'skiing': 'Skiing',
        'swimming': 'Swimming',
        'working_out': 'Working out',
        'other': 'Other',
    },
    'grooming': {
        'category': 'Grooming',
        'at_the_spa': 'At the spa',
        'brushing_teeth': 'Brushing teeth',
        'getting_a_haircut': 'Getting a haircut',
        'shaving': 'Shaving',
        'taking_a_bath': 'Taking a bath',
        'taking_a_shower': 'Taking a shower',
        'other': 'Other',
    },
    'having_appointment': {
        'category': 'Having appointment',
        'other': 'Other',
    },
    'inactive': {
        'category': 'Inactive',
        'day_off': 'Day_off',
        'hanging_out': 'Hanging out',
        'hiding': 'Hiding',
        'on_vacation': 'On vacation',
        'praying': 'Praying',
        'scheduled_holiday': 'Scheduled holiday',
        'sleeping': 'Sleeping',
        'thinking': 'Thinking',
        'other': 'Other',
    },
    'relaxing': {
        'category': 'Relaxing',
        'fishing': 'Fishing',
        'gaming': 'Gaming',
        'going_out': 'Going out',
        'partying': 'Partying',
        'reading': 'Reading',
        'rehearsing': 'Rehearsing',
        'shopping': 'Shopping',
        'smoking': 'Smoking',
        'socializing': 'Socializing',
        'sunbathing': 'Sunbathing',
        'watching_a_movie': 'Watching a movie',
        'watching_tv': 'Watching tv',
        'other': 'Other',
    },
    'talking': {
        'category': 'Talking',
        'in_real_life': 'In real life',
        'on_the_phone': 'On the phone',
        'on_video_phone': 'On video phone',
        'other': 'Other',
    },
    'traveling': {
        'category': 'Traveling',
        'commuting': 'Commuting',
        'driving': 'Driving',
        'in_a_car': 'In a car',
        'on_a_bus': 'On a bus',
        'on_a_plane': 'On a plane',
        'on_a_train': 'On a train',
        'on_a_trip': 'On a trip',
        'walking': 'Walking',
        'cycling': 'Cycling',
        'other': 'Other',
    },
    'undefined': {
        'category': 'Undefined',
        'other': 'Other',
    },
    'working': {
        'category': 'Working',
        'coding': 'Coding',
        'in_a_meeting': 'In a meeting',
        'writing': 'Writing',
        'studying': 'Studying',
        'other': 'Other',
    }
}