###
#
# queue.tcl by faile - (modified by jake0)
#


# the (F) Bot for windows :)
#You will need to modify the settings below

# NEED TO EDIT #


# nickname you want bot to have
set nickname "F"

# Oper Username
set opusername "jake0"

# Oper Password
set oppassword "leetpass"

# set your services username 
set username "jake0"

# set your services password
set password "omfgleet"

# what do you want the ident of your bot to have?
set fident "F"

# what do you want the host of your bot to have? 
set fhost "feds.yournetwork.org"

# what is the ident + host for your services? (i.e Q@CServe.quakenet.org)
set servicehost "Q@CServe.jake0.org.uk"

# what word do we use after msging the ident + host for your services? (i.e AUTH LOGIN)
set loginword "AUTH"

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

bind evnt - init-server evnt:init_server

proc evnt:init_server {type} {
  global opusername oppassword fident fhost nickname servicehost loginword username password 
set nick "$nickname"
putquick "oper $opusername $oppassword"
putserv "sethost $fident $fhost"
puthelp "nick $nickname"
puthelp "mode $nickname +X"
putserv "privmsg $servicehost :$loginword $username $password"
}



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

# the channel you wish to have the queue on, default #fedsqueue
set queue_chan "#feds"

# how long people should be allowed to idle in SECONDS, default 300
# to disable this, set the time to -1
set queue_idle_seconds 300

# how long to ban someone who was kicked for idle in MINUTES, default 5
set queue_idle_ban_minutes 5

# if all the ops are idle (havent SAID anything) should we automatically
# turn off the queue if it's left on? default 1200
# to disable, set to -1
set queue_idle_remove_queue_seconds 10000000


bind join * "$queue_chan *" queue_join

bind kick * "$queue_chan *" queue_kick

bind mode * "$queue_chan +v" queue_mode_voice

bind mode * "$queue_chan +o" queue_mode_op

bind nick * "$queue_chan *" queue_nick

bind part * "$queue_chan *" queue_part

bind pubm * "$queue_chan !done *" queue_pubm_done

bind pubm * "$queue_chan !next*" queue_pubm_next

bind pubm * "$queue_chan !queue" queue_pubm_queue

bind pubm * "$queue_chan !requeue" queue_pubm_requeue

bind pubm * "$queue_chan *" queue_pubm

bind sign * "$queue_chan *" queue_sign

bind time * * queue_time


proc queue_join { nick mask hand chan } {
  global queue_idle queue_queue

  if {[isbotnick $nick]} {
    array unset queue_idle
    set queue_queue ""
  } else {
    lappend queue_queue $nick
    set queue_idle($nick) [clock seconds]
  }
}

proc queue_kick { nick mask hand chan target reason } {
  queue_idle_remove $target
  queue_queue_remove $target
}

proc queue_mode_op { nick mask hand chan mode victim } {
  queue_queue_remove $victim
}

proc queue_mode_voice { nick mask hand chan mode victim } {
  queue_queue_remove $victim
  if {[string first "m" [getchanmode $chan]] > 0} {
    set queue_idle($victim) [clock seconds]
  }
}

proc queue_nick { nick mask hand chan newnick } {
  queue_idle_rename $nick $newnick
  queue_queue_rename $nick $newnick
}

proc queue_part { nick mask hand chan msg } {
  queue_idle_remove $nick
  queue_queue_remove $nick
}

proc queue_pubm_done { nick mask hand chan text } {
  if {[botisop $chan] && [isop $nick $chan]} {
    foreach i [lrange $text 1 [llength $text]] {
      if {[isvoice $i $chan]} {
        putserv "privmsg $chan :$i you will now be devoiced, i hope your problem / question was solved sucessfully."
        pushmode $chan -v $i
      }
    }
  }
}

proc queue_pubm_next { nick mask hand chan text } {
  global queue_queue

  if {[botisop $chan] && [isop $nick $chan]} {
    if {[lindex $text 1] == ""} {
      if {[lindex $queue_queue 0] != ""} {
        putserv "privmsg $chan :[lindex $queue_queue 0] You will now be voiced, Please have your question ready."
        pushmode $chan +v [lindex $queue_queue 0]
        set queue_queue [lreplace $queue_queue 0 0]
      }
    } else {
      if {[lindex $text 1] < 13} {
        set i 0
        while {$i < [lindex $text 1] && [lindex $queue_queue 0] != ""} {
          putserv "privmsg $chan :[lindex $queue_queue 0] You will now be voiced, Please have your question ready."
          pushmode $chan +v [lindex $queue_queue 0]
          set queue_queue [lreplace $queue_queue 0 0]
          incr i
        }
      } else {
        foreach i [lrange $text 1 [llength $text]] {
          if {![isvoice $i $chan]} {
            putserv "privmsg $chan :[lindex $queue_queue 0] You will now be voiced, Please have your question ready."
            pushmode $chan +v $i
          }
        }
      }
    }
  }
}

proc queue_pubm_queue { nick mask hand chan text } {
global queue
  if {[botisop $chan] && [isop $nick $chan]} {
    if {[string first "m" [getchanmode $chan]] > 0} {
      pushmode $chan -m
      foreach i [chanlist $chan] {
        set queue_idle($i) [clock seconds]
         set queue "off" 
         putserv "privmsg $chan :The Queue is now $queue"
        if {![isop $i $chan] && [isvoice $i $chan]} {
          putserv "privmsg $chan :$i The queue has been turned off and you will now be devoiced."
          pushmode $chan -v $i
        }
      }
    } else {
      pushmode $chan +m
      set queue "on"
      putserv "privmsg $chan :The Queue is now $queue"
    }
  }
}

proc queue_pubm_requeue { nick mask hand chan text } {
  global queue_idle queue_queue

  if {[botisop $chan] && [isop $nick $chan]} {
    array unset queue_idle
    set queue_queue ""
    putserv "privmsg $chan :creating a new queue..."
    foreach i [chanlist $chan] {
      set queue_idle($i) [clock seconds]
      if {![isop $i $chan] && ![isvoice $i $chan]} {
        lappend queue_queue $i
        
      }
    }
  }
}

proc queue_pubm { nick host hand chan text } {
  global queue_idle queue_idle_op

  set queue_idle($nick) [clock seconds]
  if {[isop $nick $chan]} {
    set queue_idle_op [clock seconds]
  }
}

proc queue_sign { nick mask hand chan reason } {
  queue_idle_remove $nick
  queue_queue_remove $nick
}

proc queue_time { minute hour day month year } {
  global botnick queue_chan queue_idle queue_idle_ban_minutes queue_idle_op queue_idle_seconds queue_idle_remove_queue_seconds

  if {[botisop $queue_chan]} {
    if {[string first "m" [getchanmode $queue_chan]] > 0} {
      if {[expr [clock seconds] - $queue_idle_op] > $queue_idle_remove_queue_seconds} {
        queue_pubm_queue $botnick none none $queue_chan none
      } elseif {$queue_idle_seconds > -1} {
        foreach i [array names queue_idle] {
          if {![isop $i $queue_chan] && [isvoice $i $queue_chan] && [expr [clock seconds] - $queue_idle($i)] > $queue_idle_seconds} {
            newchanban $queue_chan *!*[getchanhost $i $queue_chan] F.tcl "idle" $queue_idle_ban_minutes
            putkick $queue_chan $i "You Are NOT Allowed To Idle"
          }
        }
      }
    } elseif {$queue_idle_seconds > -1} {
      foreach i [array names queue_idle] {
        if {![isop $i $queue_chan] && [expr [clock seconds] - $queue_idle($i)] > $queue_idle_seconds} {
          newchanban $queue_chan *!*[getchanhost $i $queue_chan] F.tcl "idle" $queue_idle_ban_minutes
          putkick $queue_chan $i "You Are NOT Allowed To Idle"
        }
      }
    }
  }
}


proc queue_idle_rename { i j } {
  global queue_idle

  set queue_idle($j) queue_idle($i)
  array unset queue_idle [queue_tcl_sucks $i]
}

proc queue_idle_remove { i } {
  global queue_idle

  array unset queue_idle [queue_tcl_sucks $i]
}


proc queue_queue_rename { i j } {
  global queue_queue

  set queue_queue [lreplace $queue_queue [lsearch $queue_queue [queue_tcl_sucks $i]] [lsearch $queue_queue [queue_tcl_sucks $i]] $j]
}

proc queue_queue_remove { i } {
  global queue_queue

  set queue_queue [lreplace $queue_queue [lsearch $queue_queue [queue_tcl_sucks $i]] [lsearch $queue_queue [queue_tcl_sucks $i]]]
}


# hi slug :)
proc queue_tcl_sucks { i } {
  regsub -all -- \\\\ $i \\\\\\\\ i
  regsub -all -- \\\[ $i \\\\\[ i
  regsub -all -- \\\] $i \\\\\] i
  regsub -all -- \\\} $i \\\\\} i
  regsub -all -- \\\{ $i \\\\\{ i
  regsub -all -- \\\" $i \\\\\" i
  return $i
}

## greet ( onjoin )

bind join - * joinmsg

proc joinmsg {nick uhost hand chan} {
global queue
 if {$queue == "on"} {
 putserv "privmsg $nick : #feds is using a queue system. You will soon be voiced to ask your question, please have it ready."
}
}