Skip to content

Instantly share code, notes, and snippets.

@gandaro
Last active December 17, 2015 01:09
Show Gist options
  • Save gandaro/5526210 to your computer and use it in GitHub Desktop.
Save gandaro/5526210 to your computer and use it in GitHub Desktop.
# coding: utf-8
# monitor - XChat plugin for the IRCv3.2 command MONITOR
# Copyright (C) 2013 Jakob Kramer <jakob.kramer@gmx.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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.
# TODO: split targets into multiple messages
import os.path
import xchat
__module_name__ = 'monitor'
__module_version__ = '0.2α'
__module_description__ = "Add support for IRCv3.2's MONITOR command"
REPLIES = {
730: 'online',
731: 'offline'
}
HELP = '/MONITOR ( {C,L,S} | {+,-} TARGETS )'
EMPTY_SET = frozenset([''])
CONFIG_FILENAME = os.path.join(xchat.get_info('configdir'), 'monitor_targets.conf')
# XXX: global variable
userlist = set()
def monitor_callback(word, word_eol, userdata):
# targets: ':target[,target2]*'
targets = word[-1][1:].split(',')
reply = int(word[1])
for user in targets:
xchat.prnt('%s is %s (%s)' % (user.split('!')[0], REPLIES[reply], word[0][1:]))
xchat.command('GUI FLASH')
return xchat.EAT_XCHAT
def unload_callback(userdata):
xchat.command('QUOTE MONITOR C')
def monitor_command(word, word_eol, userdata):
global userlist
if len(word) == 3 and word[1] in ('+', '-'):
targets = set(word[2].split(','))
if word[1] == '+':
userlist |= targets
elif word[1] == '-':
userlist -= targets
xchat.command('QUOTE MONITOR %s %s' % (word[1], ','.join(targets)))
with open(CONFIG_FILENAME, 'w') as f:
f.write(','.join(userlist))
elif len(word) == 2 and word[1] in ('C', 'L', 'S'):
if word[1] == 'C':
userlist = set()
with open(CONFIG_FILENAME, 'w') as f:
pass
xchat.command('QUOTE MONITOR %s' % word[1])
else:
xchat.prnt(HELP)
return xchat.EAT_ALL
if __name__ == '__main__':
try:
with open(CONFIG_FILENAME) as f:
userlist = set(f.read().split(',')) - EMPTY_SET
except IOError:
pass
if userlist:
xchat.command('QUOTE MONITOR + %s' % ','.join(userlist))
for reply in REPLIES:
xchat.hook_server(str(reply), monitor_callback)
xchat.hook_unload(unload_callback)
xchat.hook_command('MONITOR', monitor_command, help=HELP)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment