Utilisateur:FoeNyx/osdmsg.py

Un article de Wikipédia, l'encyclopédie libre.

#-*- coding: utf-8 -*-
# filename: osdmsg.py

__module_name__ = "osdmsg" 
__module_version__ = "0.0.2" 
__module_description__ = "Show an osd message when someone use your nickname"
__module_author__ = "[[w:fr:Utilisateur:FoeNyx]]"

## Depends :
## * Linux (not tested under windows)
## * xosd  [http://www.ignavus.net/software.html]
## * pyosd [http://repose.cx/pyosd/]
## * python 2.3
## an helvetica font 100

import string
import re
import urllib
import time

import xchat
import pyosd
import unicodedata #python 2.3

class Messages:
        
        def __init__(self):
                self.msg = []
                self.enabled = True
                
                self.display = pyosd.osd(lines=1)
                self.display.set_pos(pyosd.POS_TOP)
                self.display.set_colour("#AAFF55")
                self.display.set_offset(3)
                self.display.set_shadow_offset(1)
                self.display.set_timeout(10)
                try:
                        self.display.set_font("-*-helvetica-*-r-*-*-*-100-*-*-*-*-*-*")                       
                except:
                        pass
                
        def enqueue(self,str):
                if self.enabled :
                        self.msg.append(str)

        def process(self):
                if self.enabled and len(self.msg) > 0:

                        msgtrq = self.msg[0]
                        if len(msgtrq) > 120:
                                msgtrq = msgtrq[:120] + " ..."
                                
                        self.display.display("",line=0)
                        self.display.display(msgtrq, line=0)
                        
                        if len(self.msg) > 1:
                                self.msg = self.msg[1:]
                        else:
                                self.msg = []                           


def getEncoding(channel):
        "Get the encoding of a channel (default utf-8)"
        
        channel_encoding= {"#fr.wikipedia" : "utf-8",
                           "#linuxfr" : "latin-1"
                           }
        
        if channel in channel_encoding.keys():
                return channel_encoding[channel]
        return "utf-8"

def _uni2asciimap(charname):
        "equivalent for some unicode char"
        
        uni2asciimap = {"LATIN SMALL LETTER AE" : "ae",
                        "LATIN SMALL LIGATURE OE" : "oe",
                        "LATIN CAPITAL LETTER AE" : "AE",
                        "LATIN CAPITAL LIGATURE OE" : "OE",
                        "LEFT-POINTING DOUBLE ANGLE QUOTATION MARK" : '"',
                        "RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK" : '"',                 
                        "RIGHTWARDS ARROW" : "->",
                        "LEFTWARDS ARROW" : "<-",
                        "FRACTION SLASH" : "/",
                        "COPYRIGHT SIGN" : "(c)",
                        "?" : "?"
                        }
        if charname in uni2asciimap.keys():
                return uni2asciimap[charname]
        return "{%s}" % charname

def normalize(enc, text):
        "unicode to ascii (for osd) but &aegrave; will be rendered as 'a' not '?'"
        
        text = unicode(text, enc ,'replace')    
        text = unicodedata.normalize('NFKD', text) # inflected letter -> letter + inflection -> letter
        rendu = ""
        for c in text:
                x = ord(c)
                if x > 31 and x < 127:
                        #Ascii
                        rendu += c
                elif x >= 0x02B0 and x <= 0x036F: 
                        #Combining, diacritic
                        pass
                else:
                        rendu += _uni2asciimap(unicodedata.name(c,"?"))
                
        return rendu    
                
m = Messages()
def monitor(word, word_eol, userdata):
        channel = xchat.get_info("channel")
        text = normalize(getEncoding(channel), word[1])                 
        m.enqueue(" " * 5 + "[%s] %s> %s" % (channel, word[0], text))
        m.process()
        return xchat.EAT_NONE

EVENTS = []
#EVENTS.append(("Channel Message", 1))
#EVENTS.append(("Your Message", 1))
EVENTS.append(("Channel Msg Hilight", 1))
EVENTS.append(("Private Message", 1))
EVENTS.append(("Private Message to Dialog", 1))

def unload(userdata):
        print "Plugin " + __module_name__ + " " + __module_version__ + " unloaded."

for event in EVENTS:
        xchat.hook_print(event[0], monitor, event)
xchat.hook_unload(unload)
print "Plugin " + __module_name__ + " " + __module_version__ + " loaded."

def osd_disable(word, word_eol, userdata):
        m.enabled = False
        print "On Screen Display script disabled."
        return xchat.EAT_ALL

def osd_enable(word, word_eol, userdata):
        m.enabled = True
        print "On Screen Display script enabled."
        return xchat.EAT_ALL

xchat.hook_command("ENABLEOSD", osd_enable, help="/ENABLEOSD Enable the on screen display script.")
xchat.hook_command("DISABLEOSD", osd_disable, help="/DISABLEOSD Disable the on screen display script.") 
print "Plugin %s Commands : %s" %( __module_name__, "ENABLEOSD DISABLEOSD" )

#ChangeLog:

# 2004-09-06, v0.0.2 (Foenyx)
# unicode to ascii
# commands to enable/disable osd

# 2004-08-01, v0.0.1 (Foenyx)
# display msg to me on screen