# Begin - Utility, NickServ Auto-Identify. (utl_nickserv.tcl)
#	Designed & Written by TCP-IP (Vicky@Vic.ky), © April 1999
#	Developed by Ninja_baby (Jaysee@Jaysee.Tv), © July 2000
#	Modified by nukie (nukie@front.ru), © Feb 2004

# This script was made by TCP-IP in middle of 1999, Ninja_baby develop this script since he passes this script
# to her in middle of 2000. I made few changes, add some features, and fixed some bugs that remained in
# this script.

# Changes: added function to purge memos when receiving memo notice from memoserv

# This script will help your bot doing self-identify to the NickServ when it ask to.
# This script has timer to set frequent time for your bot ot do identify.
# added new features for you to set this bot to identify for other nickname(s) beside its own nickname.
# Also you can setup different IPs for NickServ(s) in case you jump your bot to different IRC network.
# You can either tell your bot to identify itself by doing /msg <botnick> nickident or doing `nickident
# in channel. ;)
# You need to set few variable(s) below in order get maximum performance of this script. Enjoy ;)

# Set this one as the current network NickServ nick. I took example from DALnet NickServ nick.
set nicksnick "NickServ@services.dal.net"

# Set this one as the current network ChanServ nick. I took example from DALnet ChanServ nick.
set chanserv "ChanServ@services.dal.net"

# Set this one as the current network MemoServ nick. I took example from DALnet MemoServ nick.
set memoserv "MemoServ@services.dal.net"

# This are for network compatibility, eq. 3 IRC network has same NickServ's nickname. But they've
# different IPs since they're different network, you can either set this as an addition.
# You can set this as many as you want. Just be careful and prevent someone from cheating your bot hehe ;)
set ident_ips {
  "@services.dal.net"
}

# This is your bot nick password. Don't forget to set this ;) (moved to config file)
#set botnick_pass "pentagram"

# Set this as default frequent time for the bot to identify itself to the NickServ.
# Careful don't set this too low coz you don't want your bot to flood the NickServ won't you? ;p
# Set this one in minute(s) format
set botauto 15

# Set this one to "1" if you like to bot to identify other nickname(s) beside its own nick.
# Otherwise leave it to "0" if you like to bot to identify itself only.
set ident_owner 0

# Set this as default frequent time for the bot to identify other nickname(s) to the NickServ.
# Careful don't set this too low coz you don't want your bot to flood the NickServ won't you? ;p
# Set this one also in minute(s) format
set ownerauto 30

# If you set "ident_owner" to "1", you need to set this as well. Put any nickname(s) you wish here-
# in the list. You can set as many as you wish, the bot will identify within time frequent. NOT
# all phrase at one time. Don't worry ;)
set ownernpass {
  "ownernick:ownernickpass"
}

# Set this as your Public (channel) command character to tell the bot to identify itself.
# For example: if you set this to "!" means you must type !nickident in channel to tell your bot to identify itself.
set IDPRM "`"

# This is for your benefit hehe ;), you can either set your own LOGO here, your logo will appear
# when the bot notice you, or when it makes msgs/notices/kicks or scripts loading. So smile and set this
# variable as you wish ;), you can either set this to "" to leave it blank.
set utlnkvlg "\[4x]:"

######### Please do not edit anything below unless you know what you are doing ;) #########

#let's start identifying to the server once connected and remove modes "sw".
set init-server {
   putserv "PRIVMSG $nicksnick :identify $botnick_pass"
   putserv "MODE $botnick -sw"
}

proc identifynow {nick uhost hand text} {
	global botnick nicksnick ident_ips botauto botnick_pass ident_owner ownerauto ownernpass utlnkvlg
	foreach ident_host [string tolower $ident_ips] {
		if {[string match *[string tolower $ident_host]* [string tolower $uhost]]} {
			if {$botnick_pass == ""} {putlog "$utlnkvlg nick password for $botnick hasn't been set yet." ; return 0}
			identifybot $botnick
			if {$botauto > 0} {rstbottimer}
			if {$ident_owner} {
				foreach ownernpas1 $ownernpass {
					if {$ownernpas1 == ""} {putlog "$utlnkvlg nickname or nick password for owner hasn't been set yet." ; return 0}
				}
				identifyown $botnick
				if {$ownerauto > 0} {rstowntimer}
			}
		}
	} ; return 0
}

proc identifybot {botnick} {
	global botauto botnick_pass nicksnick utlnkvlg
	if {[string match "@" $nicksnick]} {set lnicksnick [lindex [split $nicksnick @] 0]} else {set lnicksnick $nicksnick}
	putquick "PRIVMSG $nicksnick :identify $botnick $botnick_pass"
	putlog "$utlnkvlg identifying $botnick to $lnicksnick."
	rstbottimer ; return 1
}

proc identifyown {botnick} {
	global ownerauto ownernpass nicksnick utlnkvlg
	if {[string match "@" $nicksnick]} {set lnicksnick [lindex [split $nicksnick @] 0]} else {set lnicksnick $nicksnick}
	set ownidcount 0
	foreach ownernpas1 $ownernpass {
		set ownidcount1 [expr $ownidcount + 4]
		set ownernick [lindex [split $ownernpas1 :] 0] ; set ownerpass [lindex [split $ownernpas1 :] 1]
		utimer $ownidcount1 "idownmsg $nicksnick $ownernick $ownerpass" ; set ownidcount $ownidcount1
	} ; unset ownidcount ; unset ownidcount1
	rstowntimer ; return 1
}

proc idownmsg {nservnick owncrnick owncrpass} {
	global botnick utlnkvlg
	putquick "PRIVMSG $nservnick :identify $owncrnick $owncrpass"
	putlog "$utlnkvlg identifying $owncrnick to $nservnick." ; return 1
}

proc rm_memos { nick uhost hand args } {
   global botnick memoserv utlnkvlg
     putlog "$utlnkvlg there's memo from $memoserv for $botnick"
     putserv "PRIVMSG $memoserv :DEL ALL"
     putlog "$utlnkvlg removing memos..."
}

proc rstbottimer {} {
	global botnick botauto utlnkvlg
	foreach bottimer [timers] {if {[string match "identifybot $botnick" [lindex $bottimer 1]]} {killtimer [lindex $bottimer 2]}}
	timer $botauto "identifybot $botnick"
	putlog "$utlnkvlg activating auto-identify timer for $botnick." ; return 1
}

proc rstowntimer {} {
	global botnick ownerauto utlnkvlg
	foreach owntimer [timers] {if {[string match "identifyown $botnick" [lindex $owntimer 1]]} {killtimer [lindex $owntimer 2]}}
	timer $ownerauto "identifyown $botnick"
	putlog "$utlnkvlg activating auto-identify timer for other nick(s)." ; return 1
}

proc msgidentbot {nick uhost hand rest} {
	global botnick botauto botnick_pass nicksnick utlnkvlg
	if {![matchattr $hand m]} {return 0}
	if {$botnick_pass == ""} {putlog "$utlnkvlg nick password for $botnick hasn't been set yet." ; return 0}
	if {[string match "@" $nicksnick]} {set lnicksnick [lindex [split $nicksnick @] 0]} else {set lnicksnick $nicksnick}
	if {[string match "#*" [lindex $rest 0]]} {set idtarget [lindex $rest 0] ; set idmode "PRIVMSG"} else {set idtarget $nick ; set idmode "NOTICE"}
	putquick "$idmode $idtarget :$utlnkvlg identifying $botnick to $lnicksnick."
	putcmdlog "$utlnkvlg <<$nick>> !$hand! identify to $lnicksnick."
	identifybot $botnick
}

proc op_chanserv { nick uhost hand args } {
   global botnick chanserv channame utlnkvlg
     putlog "$utlnkvlg notice from $chanserv"
     foreach channame [channels] {
     putserv "PRIVMSG $chanserv :OP $channame $botnick" }
     putlog "$utlnkvlg trying to gain ops in $channame from $chanserv"
}

proc dccidentbot {nick hand rest} {global IDPRM botnick utlnkvlg ; identifybot $botnick}
proc pubidentbot {nick uhost hand chan rest} {global IDPRM botnick utlnkvlg ; msgidentbot $nick $uhost $hand $chan}

#we may need to know when to identify again, so this takes care of that.
bind notc - "*This nick is owned by * Please choose*" identifynow
bind notc - "*This * registered * protected*" identifynow

#detecting and removing new memos/news from memoserv
bind notc - "*You have*" rm_memos
bind notc - "*New memo from *" rm_memos

bind dcc - nickident dccidentbot
bind msg - nickident msgidentbot
bind pub - ${IDPRM}nickident pubidentbot

putlog "${utlnkvlg} Utility, NickServ Auto-Identify + Memo Purge. Loaded."

# End of - Utility, NickServ Auto-Identify + Memo Purge. (utl_nickserv.tcl)