# This script will identify your bot once received the notice that nick is 
# owned by someone else from NickServ.
#
# We can also use this script if the bot's need to be an op, unban & invite.
# To do this, set your channel like the given example below:
#
# .chanset #lamestchan need-op chanserv #lamestchan op
# .chanset #lamestchan need-invite chanserv #lamestchan invite
# .chanset #lamestchan need-unban chanserv #lamestchan unban
# Note: Don't set your channel like that if you don't have access on the channel
#
# (C) 2002 - SparkCom, Spark Computer Technology
# Any suggestion or comment email me at sparkcom@sparkcom.eu.org


# [0/1] To let us know that bot is unidentified, then send ident request.
set is_identified 0

# [0/1] To show that identifying is in progress to stop multiple request. 
# it will help to prevent flooding to NickServ
set identifying 0

# If in case that NickServ didn't reply for a certain period of time we will
# resend ident requests and continue until the bot identified
# (must be in minutes).
set nickserv_time 10

# The bot's registered nick to NickServ also the bot's nick.
set eggnick "nickname"

# The bot's nick password to NickServ
set nickpass "password"

# [0/1] To let us know if ChanServ is responding or not, to wait for the
# action before sending another request. 
set chanserv_success 0

# Duration in minutes before turning chanserv_success to 1 thus enabling us
# to send another request.
set chanserv_time 10

############################################################################
# PLEASE DO NOT EDIT ANYTHING BELOW UNLESS YOU KNOW WHAT YOU ARE DOING !!! #
############################## Script Begin ################################

# Now, we recieved the notice. We will turn off the is_identified to
# wait the "Password accepted" reply before sending a service requests
# to ChanServ.
proc nickserv { nick uhost hand chan text } {
	global nickpass botnick eggnick is_identified identifying chanserv_success nickserv_time
	if { $identifying == 0 } {
		set is_identified 0
		set chanserv_success 0
		if { $botnick == $eggnick } {
			set identifying 1
			timer $nickserv_time nickserv_reset
			putserv "PRIVMSG NickServ :identify $nickpass"
		}
	}
} 

# Now the bot received the notice that password is accepted. 
# Right here, we will turn off identifying variable as a preparation for
# the next ident request and since we are now identified we will turn the
# memory variable is_identified and chanserv_success to on.
proc pass_accepted { nick uhost hand chan text } {
global is_identified identifying chanserv_success
	set nickservTimerID [timerexists nickserv_reset]
	set identifying 0
	set is_identified 1
	set chanserv_success 1
	if { $nickservTimerID != "" } {	
		killtimer $nickservTimerID
	}
}

# Reset the variables to an initial value after timer expires.
proc nickserv_reset {} {
	global is_identified identifying chanserv_success
	set identifying 0
	set is_identified 0
	set chanserv_success 0
	nickserv "" "" "" "" ""
}

# Send op, unban & invite request to ChanServ.
proc chanserv {where what} {
	global botnick is_identified chanserv_success chanserv_time 
	if { ($is_identified == 1) && ($chanserv_success == 1) } {
		set chanserv_success 0
		timer $chanserv_time chanserv_reset
		putserv "PRIVMSG ChanServ :$what $where $botnick"
	} 
} 

# This procedure will verify if ChanServ is responding to the service requests.
# It will help if ChanServ is lagged, thus preventing multiple requests.
proc mode_proc {nick uhost hand chan mc {victim ""}} {
	global botnick chanserv_success
	if {(($nick == "ChanServ") && ($victim == $botnick) && (($mc == "+o") || ($mc == "-b" )))}  {
		set chanservTimerID [timerexists chanserv_reset]
		set chanserv_success 1
		if { $chanservTimerID != "" } {	
			killtimer $chanservTimerID
		}
	}
}

# This procedure will call after the chanserv_timer expires
proc chanserv_reset {} {
	global chanserv_success
	set chanserv_success 1
}

# The following are those tcl binds for this script. 
bind notc - "*This nick is owned by someone else*" nickserv
bind notc - "*Password accepted*" pass_accepted
bind mode - * mode_proc

putlog " * Loaded [file tail [info script]] successfully."

# Script Written by Rey C. Geroleo - Jan. 07, 2002

