# Identify script for AzzurraNET (irc.azzurra.org)
# Version 1.0.1
# Official WebSite: http://script.azzurra.org
#
# written by NERvOus (nervous@azzurra.org)
# Thanks to: TheOne, Faust, Sephiroth & the script@ml.azzurra.org 
#            development mailing-list
#
# Send bug reports to: NERvOus <nervous@azzurra.org>

# HISTORY
#
# 1.0.1: aggiunti az:lame_warn az:scriptver e ctcp/msg flood protection 
#        aggiunto controllo per permessi di lettura sul file (0600)
#        bugfix per il ctcp-version
# 1.0.0: release iniziale

# INSTALLAZIONE:
# 
# Copiate questo file (SENZA RINOMINARLO) nella directory scripts/ del 
# vostro eggdrop. Modificate il contenuto delle variabili az:nickpass e
# az:regchan rispettivamente con la password del nick del bot e con la
# lista, separata da spazi, dei canali su cui il bot e' registrato come AOP/SOP.
# Editate il file di configurazione del vostro bot (es. eggdrop.conf) e
# aggiungete come ULTIMA riga dopo tutte le altre righe che cominciano con
# source la seguente stringa:
#
# source scripts/AzzurraNET-identify-1.0.1.tcl
#
# ATTENZIONE: se non la mettete come ultima riga il vostro bot potrebbe
# essere K-LINEATO.
# Fate ripartire il bot in modo che carichi il nuovo script.


# COME FUNZIONA:
#
# Lo script controlla se il nick in uso e' quello primario impostato tramite
# la variabile botnick. Se non lo e', esegue un RECOVER seguito da un RELEASE
# dopodiche' riprende il nick primario e non appena NickServ lo richiede
# esegue l'IDENTIFY.
# Se la password impostata nella variabile $az:nickpass e' errata il bot
# muore
#
# Se il bot e' senza op su un canale compreso tra quelli listati nella
# variabile $az:regchan allora il bot chiede l'op a ChanServ. Se il bot
# non ha accesso a quel canale, muore.
# Il bot non effettuera' mai piu' di 1 richiesta di op a ChanServ ogni 5
# minuti (onde evitare flood sui servizi)
#
# Infine lo script aggiunge automaticamente delle impostazioni di
# flood-protection e channel-protection che sono CONDIZIONE NECESSARIA
# affinche' il bot possa essere autorizzato a connettersi alla rete
# AzzurraNET. Qualsiasi bot sprovvisto delle sopracitate impostazioni verra'
# immediatamente K-lineato.



# password del nick del bot
set az:nickpass "cambiami"
# lista dei canali sui quali il bot ha accesso come AOP/SOP
set az:regchan "#chan1 #chan2 #altrochan"
# avvisa se il bot e' in un canale in cui non ha accesso come AOP/SOP
# 0=no 1=si (default 1)
set az:lame_warn "1"

########################################################################
#                                                                      #
#                                                                      #
#                    NON MODIFICARE NULLA QUA SOTTO                    #
#                                                                      #
#                                                                      #
########################################################################


# init

set az:scriptver "1.0.1"
set az:nickname "$nick"
set az:csrecent 0
set az:csdelay 5
if {![string match "*AzzurraNET ID Script*" ${ctcp-version}]} {
	set ctcp-version "${ctcp-version} + AzzurraNET ID Script ${az:scriptver}"
	}

exec /bin/chmod 0600 scripts/AzzurraNET-identify-${az:scriptver}.tcl

# compatibilita' con la policy di AzzurraNET per i BOT

set net-type 5 
set ctcp-mode 2
set flood-msg 10:20
set flood-ctcp 10:20
set global-flood-chan 20:30
set global-flood-deop 2:10
set global-flood-kick 2:10
set global-flood-join 5:30
set global-flood-ctcp 10:30 
set global-idle-kick 0
set global-chanmode "" 
set global-chanset {
	-clearbans      -enforcebans
	+dynamicbans    +userbans
	-autoop         -bitch
	-protectops     -stopnethack
	-revenge        -autovoice
	-cycle		+dontkickops
	-wasoptest      -protectfriends
	-userexempts	-dynamicexempts
	-userinvites	-dynamicinvites
	+nodesynch	-inactive
}



proc az:set_need_op {nick uhost hand chan} {
	global botnick az:nickname az:regchan 
	set chan [string tolower $chan]
	channel set $chan -enforcebans -bitch -protectops -stopnethack -revenge -cycle -wasoptest -protectfriends +nodesynch
	if {([string match "* $chan *" " ${az:regchan} "])} {
		channel set $chan need-op "az:cs_ask_op $chan"
		channel set $chan need-invite "az:cs_ask_invite $chan"
		channel set $chan need-key "az:cs_ask_invite $chan"
		} else {
		channel set $chan need-op "az:lame_bot $chan" 
		channel set $chan need-invite "az:lame_bot $chan" 
		channel set $chan need-key "az:lame_bot $chan"
		}
	}

proc az:lame_bot {chan} {
	global az:lame_warn
	if { (${az:lame_warn} == 0) } {return 0}
	putlog "AzzurraNET: Non ho accesso a $chan"
	}

# add nickserv e cs come master

deluser NickServ
adduser NickServ NickServ!service@azzurra.org
chattr NickServ +m
setuser NickServ PASS [rand 9999999999999999999999999999999]
setuser NickServ COMMENT "Added by AzzurraNET ID Script"
deluser ChanServ
adduser ChanServ ChanServ!service@azzurra.org
chattr ChanServ +m
setuser ChanServ PASS [rand 9999999999999999999999999999999]
setuser ChanServ COMMENT "Added by AzzurraNET ID Script"

# procs

proc az:cs_ask_op {chan} {
	global botnick az:nickname az:csdelay az:csrecent
	if {(${az:nickname} != $botnick) ||  ${az:csrecent} } {return}
	puthelp "ChanServ OP $chan ${az:nickname}"
	putlog "AzzurraNET: Richiesta op a ChanServ su $chan"
  	set az:csrecent 1
	timer ${az:csdelay} "set az:csrecent 0"
	}

proc az:cs_ask_invite {chan} {
	global botnick az:nickname az:csdelay az:csrecent
	if {(${az:nickname} != $botnick) && ${az:csrecent} } {return 0}
	puthelp "ChanServ INVITE $chan"
	putlog "AzzurraNET: Richiesto INVITE a ChanServ per $chan"
	set az:csrecent 1
	timer ${az:csdelay} "set az:csrecent 0"
	}
	

proc az:checknick {min hour day month year} {
  global botnick az:nickname
  if {(${az:nickname} != $botnick)} {
    az:identify
  };
}

proc az:identify {} {
  global az:nickpass az:nickname nick
  putserv "nickserv identify ${az:nickname} ${az:nickpass}"
  putlog "AzzurraNET: Eseguito IDENTIFY su NickServ"
}

proc az:recovernick {nsnick uhost hand args} {
	global az:nickname az:nickpass botnick
	if {($nsnick != "NickServ") || ($uhost != "service@azzurra.org")} { 
		return 
		}
	if {(${az:nickname} != $botnick)} {
		puthelp "nickserv RECOVER ${az:nickname} ${az:nickpass}"
		utimer 5 {puthelp "nickserv RELEASE ${az:nickname} ${az:nickpass}"}
		utimer 6 {puthelp "nick ${az:nickname}"}
		putlog "AzzurraNET: Comando RECOVER usato su ${az:nickname}"
		}
	}

proc az:identifyreq {nsnick uhost hand args} {
	global az:nickname az:nickpass botnick 
	if { ${az:nickname} != $botnick } {
		return
		}
	if {($nsnick != "NickServ") || ($uhost != "service@azzurra.org")} {
		return
	        }
	az:identify
	}

proc az:wrongpass {nsnick uhost hand args} {
	global az:nickname
	if { ($nsnick != "NickServ") || ($uhost != "service@azzurra.org") } {
		return
		}
	die "AzzurraNET: La password impostata per il nick ${az:nickname} e' errata!!!"
	}

proc az:wrongchan {csnick uhost hand args} {
	global az:nickname az:regchan
	if { ($csnick != "ChanServ") || ($uhost != "service@azzurra.org") } {
		return
		}
	die "AzzurraNET: ${az:nickname} NON ha accesso a uno di questi canali: ${az:regchan}"
	}

# Bindings

bind notc - "*sicurezza*" az:recovernick
bind notc - "*IDENTIFY*" az:identifyreq
bind notc - "*password*sbagliata*" az:wrongpass
bind notc - "*Accesso negato*" az:wrongchan
bind time - "?0 % % % %" az:checknick
bind time - "?5 % % % %" az:checknick


# AzzurraNET policy
bind join - * az:set_need_op


# Credits

putlog "AzzurraNET Identify Script ver ${az:scriptver} \(irc.azzurra.org\)"
putlog "Written by NERvOus \(nervous\@azzurra.org\)"


