PyXChatNotify

Description

PyXChatNotify allows you to register different kinds of notifications when using XChat, like a libnotify popup using DBus.

As example I've implemented the DBus libnotify. The source can be found in my git repository PyXChatNotify.git, or you can download/copy the source directly from this page. But remember: I'm maybe not able to keep this page up-to-date. To be sure, to have the latest version of this plugin I recommend to get a git clone.

Installation

At first be sure you have XChat2 and DBus installed on your system, than copy the .py-files into your ~/.xchat2 or the XChat2 plugin directory. XChat2 should load them automatically.

Sourcecode

Important Notice

After implementing a dynamic module loader PyXChatNotify will load any available Listener_<modulename>.py files in ~/.xchat2/PyXChatListeners. There is a nameing convention for listeners, the filename is identical with the class name in that module.

The XChat2 configuration directory will be read from xchat directly, so you don't need to worry about, when your XChat saves it's files else where, as long as you copy this plugin into this directory.

Plugin

Filename: PyXChatNotify.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# XChat Notification Delivery Plugin
# Copyright (C) 2008 Julian Knauer <jpk@goatpr0n.de>
#
# This program is free software: you can redistribute it and/or modify
# 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.
#
# This program is distributed in the hope that it will be useful,
# 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
# along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.html
#
# $Id: $
 
__module_name__ = "PyXChatNotify"
__module_version__ = "0.1.1"
__module_description__ = "XChat Notification Plugin"
__module_author__ = "Julian Knauer <jpk@goatpr0n.de>"
 
try:
    import xchat
    do_load = True
except ImportError:
    do_load = False
 
 
# Observer Pattern, just to be sure we get all Listeners
class Subject:
    def __init__(self):
        self._observers = []
 
    def attach(self, observer):
        if not observer in self._observers:
            self._observers.append(observer)
            return True
 
    def notify(self, msg, modifier=None):
        for observer in self._observers:
            if modifier != observer:
                observer.update(self, msg)
 
 
class Notifier(Subject):
    def __init__(self, name=''):
        Subject.__init__(self)
        self.name = name
 
    def send_message_to_listener(self, word, word_eol, userdata):
        # append text event type and send message to listeners
        word.append(userdata[0])
        self.notify(word)
 
 
def importName(modulename, name):
    try:
        module = __import__(modulename, globals(), locals(), [name])
    except ImportError:
        return None
    return getattr(module, name)
 
 
def load_listener_modules(subject):
    moddir = 'PyXChatListeners'
    xchatdir = xchat.get_info('xchatdir') + '/' + moddir
    sys.path.append(xchatdir)
 
    for item in os.listdir(xchatdir):
        if os.path.isfile(xchatdir + '/' + item):
            if item.startswith('Listener_') and item.endswith('.py'):
                name = item[:-3]
 
                mod = importName(name, name)
                if not mod:
                    continue
 
                if notifier.attach(mod()):
 
 
                    print '[ ] PyXChatNotify::%s registered' % name
 
 
if do_load:
    print '%s v%s is loading...' % (__module_name__, __module_version__)
    # Attempt to initialize module
    EVENTS = [
            ('Channel Action Hilight', 1),
            ('Channel Msg Hilight', 1)
    ]
 
    # Naming gives me the option of adding more different events and
    # observers to organize them later.
    notifier = Notifier('Hilight')
    load_listener_modules(notifier)
 
    # Set event hooks
    for event in EVENTS:
        xchat.hook_print(event[0], notifier.send_message_to_listener, event)
 
    print '%s v%s loaded' % (__module_name__, __module_version__)
else:
    # Wrong environment, module cannot be loaded
    print 'Unable to load %s v%s\n' \
            'May be this module has not been started by XChat?' \
            % (__module_name__, __module_version__)

Listener #1: DBus libnotify

Here is the example for the DBus libnotify listener.

Filename: Listener_libnotify.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Listeners for XChat Notification Delivery Plugin
# Copyright (C) 2008 Julian Knauer <jpk@goatpr0n.de>
#
# This program is free software: you can redistribute it and/or modify
# 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.
#
# This program is distributed in the hope that it will be useful,
# 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
# along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.html
#
# $Id: $
 
__module_name__ = "Listener_libnotify"
__module_version__ = "0.1"
__module_description__ = "XChat hilight notification through libnotify"
__module_author__ = "Julian Knauer <jpk@goatpr0n.de>"
 
 
class Listener_libnotify(object):
    def __init__(self):
        try:
            import dbus
 
            self._bus = dbus.SessionBus()
            libnotify_obj = self._bus.get_object('org.freedesktop.Notifications',
                    '/org/freedesktop/Notifications')
            self._libnotify = dbus.Interface(libnotify_obj, 'org.freedesktop.Notifications')
        except ImportError:
            print 'DBus python module not installed. Not loading listener.'
            return None
 
        self._libnotify.Notify('DBus Test', 0, 'gtk-connect', 'PyXChatNotify',
                '<b>PyXChatNotify::libnotify initialized.</b>', [], {}, 2500)
 
    def update(self, subject, msg):
        self._libnotify.Notify('PyXChatNotify', 0, '', '%s from %s' % (msg[2], msg[0]), msg[1],
                [], {}, 9000)

Listener #2: ThinkLight

This listener let your ThinkLight of your ThinkPad blink, on hilight.

Filename: Listener_ThinkLight.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Listeners for XChat Notification Delivery Plugin
# Copyright (C) 2008 Julian Knauer <jpk@goatpr0n.de>
#
# This program is free software: you can redistribute it and/or modify
# 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.
#
# This program is distributed in the hope that it will be useful,
# 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
# along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.html
#
# $Id: $
 
__module_name__ = "Listener_ThinkLight"
__module_version__ = "0.1"
__module_description__ = "ThinkLight Blink!"
__module_author__ = "Julian Knauer <jpk@goatpr0n.de>"
 
 
class Listener_ThinkLight(object):
    _thinklight = '/proc/acpi/ibm/light'
    _thinklight_states = ['on', 'off']
 
    def __init__(self):
        from time import sleep
        self._sleep = sleep
 
    def update(self, subject, msg):
        fp = open(self._thinklight, 'w')
        try:
            for i in xrange(4):
                fp.write(self._thinklight_states[i % 2])
                fp.flush()
                self._sleep(0.300)
 
        finally:
            fp.close()

Listener #sample: Dev-Stub

This piece of code is ment to be your start of your very own notification listener. Please feel free to email me your Listener, so I can publish them here.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Listeners for XChat Notification Delivery Plugin
# Copyright (C) 2008 Julian Knauer <jpk@goatpr0n.de>
#
# This program is free software: you can redistribute it and/or modify
# 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.
#
# This program is distributed in the hope that it will be useful,
# 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
# along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.html
#
# $Id: $
 
__module_name__ = "Listener_RENAME"
__module_version__ = "<version_number>"
__module_description__ = "PyXChat Notification Listener"
__module_author__ = "<author>"
 
 
class Listener_RENAME(object):
    def update(self, subject, msg):
        # This is so far only a stub, content will follow soon.
        pass

Download

Discussion

Enter your comment
If you can't read the letters on the image, download this .wav file to get them read to you.
 
 
projects/pyxchatnotify.txt · Last modified: 2009/01/21 00:55 by jpk
Recent changes RSS feed Creative Commons License Valid XHTML 1.0 Valid CSS Driven by DokuWiki
[unknown button type]