### StarChat ChanServ Login Script Version 1.0
### By Apparition <apparition@b5sunrise.com> and Psi_Cop <psicop@psicorps.com>
###
### Written for and tested on Eggdrop version 1.1.5
###
### Specifically intended for the StarChat IRC Network Channel Services,
### but the script can easily be modified for a different IRC network.
###
### Based on "Q/NS Login script" by Psi_Cop <psicop@psicorps.com>
### Which was based on "W Login script" by Java <java@kuwait.kern.com>


#Channels the bot will be in and has access to ChanServ
set CS_channels "#Robotech #Starhaven7"

#Bot's NickServ password
set NS_password "Password"

#Minutes between checking if the bot is opped
set CS_timer 5

#####################################################################

proc CS_login {} {
  global CS_channels NS_password CS_timer _CS_timer botnick server
  unset _CS_timer
  set CS_name [string tolower $server]
  if {$CS_channels == "" || $NS_password == ""} {
    putlog "ChanServ Login Error: No channels defined."
    return 0
  } elseif {![string match "*starchat*" $CS_name]} {
    putlog "ChanServ Login Error: Apparently I'm not on a StarChat server."
    return 0
  } else {
    set logchans [llength $CS_channels]
    set authchk "no"
    for {set loop 0} {$loop < $logchans} {incr loop} {
      if {[validchan [lindex $CS_channels $loop]]} {
        if {![botisop [lindex $CS_channels $loop]]} {
          if {$authchk == "no"} {
            putserv "PRIVMSG NickServ :IDENTIFY $NS_password"
            putlog "\[ChanServ Login\] I am IDENTIFYing myself with NickServ"
			set authchk "yes"
          }
          puthelp "PRIVMSG ChanServ :OP [lindex $CS_channels $loop]"
          putlog "\[ChanServ Login\] I am asking ChanServ to op me in channel [lindex $CS_channels $loop]"
        }
      } else {putlog "ChanServ Login Error: No channel record for [lindex $CS_channels $loop]"}
    }
    set _CS_timer [timer $CS_timer CS_login]
  }
}


if {[info exists _CS_timer]} {killtimer $_CS_timer}
set _CS_timer [timer $CS_timer CS_login]


putlog "*** Loaded: ChanServ Login 1.0 for $botnick"
