__module_name__ = "channel_charset" __module_version__ = "1.1" __module_description__ = "Allows you to set per-channel encoding, different from server default" __module_author__ = "Maciej Katafiasz " # CL 20060806 # - settings saved by network+channel instead of server+channel if possible # - print message when setting charset # - check whether requested encoding is available # - fixed kill command # - added filtering of /me and /topic commands import xchat import codecs help_msg = """Set charset specific to this channel, different from server charset. Usage: /ccharset [|off|kill] -- name of charset to set for this channel off -- disable previously set charset conversion kill -- disable conversion for all channels call with no arguments to see current charset """ encodings = {} class ChannelInfo: def __init__(self, encoding): codecs.lookup(encoding) self.encoding = encoding self.in_loop = False def load_settings(): global encodings try: setup = open(xchat.get_info("xchatdir") + "/ccharset1.conf", "r") except: print "CCharset: can't load settings" return for line in setup: try: chan, enc = line.split(" ", 1) encodings[chan] = ChannelInfo(enc) except: continue def write_settings(w): global encodings try: setup = open(xchat.get_info("xchatdir") + "/ccharset1.conf", "w") except: print "CCharset: can't save settings" for chan, info in encodings.items(): setup.write(chan + " " + info.encoding) def channel_id(ct): return (ct.get_info("network") or ct.get_info("server")) + "@" + ct.get_info("channel") def ccharset_set(word, word_eol, userdata): global encodings ct = xchat.get_context() key = channel_id(ct) if len(word) == 1: try: info = encodings[key] print "Charset for current channel: " + info.encoding except: print "Current channel uses server encoding" elif len(word) == 2: if word[1] == "kill": encodings = {} elif word[1] == "off": try: del(encodings[key]) print "Channel-specific encoding removed" except: print "No channel-specific encoding set" else: try: encodings[key] = ChannelInfo(word[1]) print "Charset for current channel set to: " + word[1] except LookupError, e: print e else: print help_msg return xchat.EAT_ALL def codec_cb(word, word_eol, userdata): global encodings event, send = userdata try: ct = xchat.get_context() info = encodings[channel_id(ct)] if info.in_loop: info.in_loop = False return None else: info.in_loop = True ct_encoding = ct.get_info("charset") if send: if event: text = word_eol[1] else: event = "say" text = word_eol[0] ct.command(event + " " + text.decode(ct_encoding).encode(info.encoding)) else: dec_word = [] for w in word: dec_word.append(w.decode(info.encoding).encode(ct_encoding)) ct.emit_print(event, *dec_word) except Exception, e: return None return xchat.EAT_ALL EVENTS = [ "Channel Action", "Channel Action Hilight", "Channel Message", "Channel Msg Hilight", "Channel Notice", "Generic Message", "Kick", "Killed", "Motd", "Notice", "Part with Reason", "Private Message", "Private Message to Dialog", "Quit", "Receive Wallops", "Server Notice", "Server Text", "Topic", "Topic Change", "Your Message" ] load_settings() xchat.hook_command("ccharset", ccharset_set, help=help_msg) xchat.hook_command("", codec_cb, ("", True)) xchat.hook_command("topic", codec_cb, ("topic", True)) xchat.hook_command("me", codec_cb, ("me", True)) xchat.hook_unload(write_settings) for event in EVENTS: xchat.hook_print(event, codec_cb, (event, False)) print "Plugin CCharset loaded."