__module_name__ = "freenode"
__module_version__ = "0.2" 
__module_description__ = "Freenode ident + auto-join script"
######################################################################
#                                                                    #
# This script is intended to join your channels upon connect to      #
# freenode only after NickServ sets your cloak, to make your cloak   #
# visible in all channels.                                           #
# Garf: extended to non-cloacked users for +r channels
#                                                                    #
# Usage: 1. set the JOIN_CHANNELS = to a comma separated list o      #
#           channels, removing any channels set in your xchat config #
#        2. set the PASSWORD = to your password.                     #
#        3. Open the edit mode for the Freenode Server in your       #
#           serverlist, and enter this as your connect command:      #
#                py load /home/user/.xchat/freenode.py               #
######################################################################
#
#  Set your nickserv password here
#
PASSWORD = 'xxxxxx'

#
#  Set your join list here
#
JOIN_CHANNELS = '#foobar2000,#hydrogenaudio'
INVITE_CHANNELS = '#somechannel'

######################################################################
#                                                                    #
#              YOU DO NOT HAVE TO EDIT BELOW THIS LINE               #
#                                                                    #
######################################################################
import xchat

def notice_check(word, word_eol, userdata):
    if (word[0] == ':NickServ!NickServ@services.'):
        if (word[3] == ':+Password'):
            if (word[4] == 'accepted'):
                joinchannels()
                invitechannels()
    return xchat.EAT_NONE
  
def joinchannels(*args):
    global JOIN_CHANNELS
    for chan in JOIN_CHANNELS.split(','):
        chan = chan.strip()
        if not chan.startswith('#'):
            chan = '#' + chan
        xchat.command('join %s' % chan)

def invitechannels(*args):
    global INVITE_CHANNELS
    for chan in INVITE_CHANNELS.split(','):
        chan = chan.strip()
        if not chan.startswith('#'):
            chan = '#' + chan
        xchat.command('chanserv invite %s' % chan)
        xchat.command('join %s' % chan)
            
xchat.hook_server('NOTICE', notice_check)
xchat.command('nickserv identify %s' % PASSWORD)
