mcabber/contrib/events/mcnotify-osx.py
author Mikael Berthe <mikael@lilotux.net>
Tue, 30 May 2023 16:37:46 +0200
changeset 2366 480585fec48a
parent 2062 163fb7b56e26
permissions -rwxr-xr-x
Update spell checking section in the configuration file
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2062
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
     1
#!/usr/bin/env python
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
     3
# 
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
     4
# Copyright (C) 2013 Sharoon Thomas <sharoon.thomas@openlabs.co.in>
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
     5
#
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
     6
# This script is provided under the terms of the GNU General Public License,
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
     7
# see the file COPYING in the root mcabber source directory.
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
     8
#
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
     9
#
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    10
# This script displays notifications using the new notification center
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    11
# introduced in OSX 10.8.
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    12
import sys
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    13
import os
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    14
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    15
try:
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    16
    from pync import Notifier
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    17
except ImportError, exc:
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    18
    print "TIP: Use 'pip install pync'"
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    19
    raise exc
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    20
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    21
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    22
def handle_msg(type_, source, filename=None):
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    23
    """
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    24
    Handle a message
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    25
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    26
    :param type_: IN, OUT or MUC (Multi User Chat)
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    27
    :type type_: string
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    28
    :param source: Jabber ID or Nickname, or Room ID for MUC
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    29
    :param filename: Filename of message body if event_log_files was set
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    30
    """
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    31
    if type_ == "IN":
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    32
        if filename:
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    33
            Notifier.notify(
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    34
                open(filename).read(),
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    35
                title=source, group='mcabber',
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    36
            )
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    37
        else:
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    38
            Notifier.notify(
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    39
                "Sent you a message",
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    40
                title=source, group='mcabber',
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    41
            )
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    42
    if filename and os.path.exists(filename):
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    43
        os.remove(filename)
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    44
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    45
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    46
def parse(event, *args):
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    47
    """
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    48
    Parses the arguments received and calls the appropriate function
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    49
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    50
    MSG IN jabber@id [file] (when receiving a message)
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    51
    MSG OUT jabber@id       (when sending a message)
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    52
    MSG MUC room_id [file]  (when receiving a MUC message)
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    53
    STATUS X jabber@id      (new buddy status is X)
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    54
    UNREAD "N x y z"        (number of unread buddy buffers)
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    55
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    56
    :param event: Type of event "MSG", "STATUS", "UNREAD"
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    57
    :param args: tuple of arguments
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    58
    """
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    59
    if event == "MSG":
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    60
        handle_msg(*args)
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    61
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    62
if __name__ == "__main__":
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    63
    parse(*sys.argv[1:])
163fb7b56e26 Add python based events command script which uses osx 10.8 notification center
Sharoon Thomas <sharoon.thomas@openlabs.co.in>
parents:
diff changeset
    64