__LICENSE__ = '''
Inactive Tabs 1.0 - XChat plugin to show offline dialog tabs
Copyright (C) 2008  TerrorBite

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 2 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.
'''

__module_name__ = "Inactive Tabs"
__module_version__ = "1.0"
__module_description__ = "Marks dialogs whose users are offline."

import xchat

dialogs = {}

def on_ison(word, word_eol, userdata):
	namelist = word_eol[3][1:].split()
	for item in dialogs[xchat.get_prefs('id')]:
		tabname = item.channel if item.channel in namelist else "(%s)" % item.channel
		item.context.command('SETTAB ' + tabname)
	return xchat.EAT_XCHAT

def nickname_check(userdata):
	global dialogs
	dialogs = {}
	netbuffers = dict((x.id, x) for x in xchat.get_list('channels') if x.type==1)
	
	for n in netbuffers.keys():
		dialogs[n] = filter(lambda(x): x.type==3 and x.id==n, xchat.get_list('channels'))
		if not dialogs[n]: continue
		names = list(x.channel for x in dialogs[n])
		netbuffers[n].context.command('ISON ' + ' '.join(names))
	return True

xchat.hook_server('303', on_ison)
xchat.hook_timer(30000, nickname_check)
nickname_check(None)
