####################################################################
##                                                                ##
##                 pwf.tcl for eggdrop 1.6.x.                     ##
##                       By kalhimeo                              ##
##                                                                ##
##      (c)2002 kalhimeo - released under the BSD License         ##
##                                                                ##
##           check the last version of the script :               ##
##            http://sourceforge.net/projects/pwf/                ##
##                                                                ##
##             version 1.3 (stable) - 1st Juny 2002               ##
##                                                                ##
####################################################################
##                                                                ##
##                                                                ##
##  INTRODUCTION :                                                ##
##                                                                ##
##  This script is fully open-source, you are free to use,        ##
##  modify or improve it. The only things i ask you to keep are   ##
##  the reference to the #pwf channel and to the creator in       ##
##  respect for him.                                              ##
##                                                                ##
##  If you add big new features and agree to publish your work,   ##
##  you can mail me at kalhimeo@users.sourceforge.net to add      ##
##  these in the next release ( with reference to you nick for    ##
##  sure ).                                                       ##
##                                                                ##
##                                                                ##
##  INSTALLATION :                                                ##
##                                                                ##
##  This pickup script may work with every game without needing   ##
##  to modify the whole code. Just edit the six variables below   ##
##  and launch it with your favorite eggdrop.                     ##
##                                                                ##
##  You can then add the flag +G to your game-admins ( they       ##
##  can launch pickups ) and then teach these little padawan      ##
##  how to use this script :p                                     ##
##                                                                ##
##                                                                ##
##  FEATURES :                                                    ##
##                                                                ##
##  - Easy pickup management on your chan.                        ##
##  - Fully customisable for game , bot replies , etc.            ##
##  - Support nick tracking for player who rename/quit/part/kick. ##
##  - Authentication system for Game-Admins to increase security. ##
##  - Possibility to change game type/player number/map on the    ##
##    fly during the pickup organisation.                         ##
##  - Players who subscribe the match can vote for a map if the   ##
##    Game-Admin didn t set one yet.                              ##
##  - IP and password are send in private message to each player  ##
##    to prevent non-subscribed players to join the server.       ##
##  - Classic !add / !remove/ !teams / !help commands.            ##
##  - !ip command reply the ip of the game.                       ##
##  - Admins can !enable or !disable their chan for pickups.      ##
##  - Possibility for masters ( +m ) to add Game-Admins easyly    ##
##    with !addadmin ( or remove with !remaddmin ).               ##
##  - Game is ended if : chan is disable by a master, game admin  ##
##    who launced it quit chan or if he decide to stop it with    ##
##    the endgame command.                                        ##
##  - Built in permaban command !permban for Game-Admins.         ##
##  - Multi language support.                                     ##
##                                                                ##
##                                                                ##
##  COMMANDS :                                                    ##
##                                                                ##
##  I've included many control commands in this script. You       ##
##  won't probably use all of these.                              ##
##  Some commands have to be executed in the channel ( ie: most   ##
##  of the public commands ) and some others in private talk      ##
##  with the bot.                                                 ##
##                                                                ##
##  Public        = all users                                     ##
##  Game-Admins   = users with +G flag                            ##
##  Server-Holder = Game-Admin who have launched the current game ##
##  Masters       = users with +mG flag                           ##
##                                                                ##
##  To list the basic commands you can also use :                 ##
##            /msg botnick help    or    !help                    ##
##                                                                ##
##                                                                ##
##  Public commands ( channel ) :                                 ##
##    !add (or !addme)       => add yourself to the game          ##
##    !remove (or !removeme) => remove yourself...                ##
##    !votemap <map>         => vote for a map                    ##
##    !teams (or !team)      => list the team                     ##
##    !ip                    => get the ip of the next game       ##
##    !help                  => replies the commands available    ##
##                                                                ##
##  Public commands ( private ) :                                 ##
##    help                   => replies the commands available    ##
##                                                                ##
##  Game-Admins commands ( channel ) :                            ##
##    !enable                => enable the bot for pickup         ##
##    !disable               => shut-up bot !                     ##
##                                                                ##
##  Game-Admins commands ( private ) :                            ##
##    admin <pass>           => auth yourself                     ##
##    addgame <ip pass vip>  => start new game (vip= *see below)  ##
##    info <ok>              => confirm game informations         ##
##    permban <mask>         => ban this pickup lamer for life    ##
##    permbaninfo <ok>       => confirm permban info              ##
##                                                                ##
##  Server-Holder commands ( channel ) :                          ##
##    !addmap <map>          => set the map and enforce it        ##
##    !addtype <type>        => type of the game ( ie : CTF )     ##
##    !playernumber <number> => number of players (pair number)   ##
##                                                                ##
##  Server-Holder commands ( private ) :                          ##
##    endgame                => end current game                  ##
##                                                                ##
##  Master commands ( channel ) :                                 ##
##    !addadmin <nick>       => add Game-Admin rights             ##
##    !remadmin <nick>       => remove Game-Admin rights...       ##
##                                                                ##
##  Master commands ( private ) :                                 ##
##    enable <chan>          => same as !enable for Game-Admin    ##
##    disable <chan>         => same as !disable ...              ##
##                                                                ##
##     * you can add "VIP" string after the <password> to         ##
##       launch a VIP game ( only voiced and op users can         ##
##       use public commands )                                    ##
##                                                                ##
##                                                                ##
##                                                                ##
##  HISTORY :                                                     ##
##                                                                ##
##  1.3 :                                                         ##
##                                                                ##
##  - Add public !help command                                    ##
##  - Change some part of this header                             ##
##                                                                ##
##  1.2 :                                                         ##
##                                                                ##
##  - Flags for Masters is now +mG instead of +oG                 ##
##  - Permban is now accessible for Game-Admins                   ##
##  - When a Master disable a chan and a pickup is launching,     ##
##    the bot automaticly stop the pickup                         ##
##  - When a Game-Admin try to !disable a chan and a pickup is    ##
##    launching, the bot refuse to disable                        ##
##  - Bug when rehash and lost of authentication fixed            ##
##  - Some very very minor bugs fixed                             ##
##                                                                ##
##  1.1 :                                                         ##
##                                                                ##
##  - Multi-language support ( if you translate the script in     ##
##    a new language and agree to publish your work, you can      ##
##    mail me at kalhimeo@users.sourceforge.net, THX )            ##
##  - Easy to edit/remove some bot's reply or coulours            ##           
##  - Some minor bug fixes                                        ##
##                                                                ##
##                                                                ##
##                                                                ##
##                                                                ##
##  THANKS TO :                                                   ##
##                                                                ##
##  http://www.eggdrop-fr.org ( i learned TCL there :p )          ##
##  Lucas Nussbaum for the fastop script (= many code i'm using ) ##
##  k-rist for topicme script ( i'm also using some functions )   ##
##                                                                ##
####################################################################
##                                                                ##
##                 EDIT THE VARIABLES BELOW                       ##
##                                                                ##
####################################################################


## Set the channel where the bot has to do the pickups ( only 1 channel ).
set pwf_pickupchan "#pwf"

## Set the welcome msg sends to all users joining the channel ( you can remove the reference to #pwf in this message of course ).
set pwf_welcomemsg "New: PWF gather is now open source !! Looking for Help ? !help !! More info about PWF ? => /join #pwf"

## Set the default number of players for this kind of pickup. Only set pair numbers.
set pwf_defaultplayer 10

## Set the map available for votes ( ie : here is a sample with classic counter-strike maps )
set pwf_maps "de_dust de_dust2 de_aztec de_prodigy de_nuke de_inferno de_cbble de_train as_oilrig as_tundra cs_747 cs_assault cs_backalley cs_estate cs_italy cs_militia cs_office cs_siege"

## Set the the string that will be added after the topic if there isn't any pickup ( set pwf_nogametopic "" if you don't want any msg ).
set pwf_nogametopic "1-7|1- 3Pickup: No games currently 1-7|1-"

# Language the script will use for all messages : 
# 1 = English
# 2 = French
set pwf_language 1

####################################################################
##                                                                ##
##                 DO NOT EDIT THE LINES BELOW                    ##
##                                                                ##
####################################################################


bind pub - !addme pwf_addme
bind pub - !add pwf_addme
bind pub - !remove pwf_remove
bind pub - !removeme pwf_remove
bind pub - !addmap pwf_addmap
bind pub - !addtype pwf_addtype
bind pub - !playernumber pwf_playernumber
bind pub - !votemap pwf_votemap
bind pub - !ip pwf_ip
bind pub - !teams pwf_teams
bind pub - !team pwf_teams
bind pub - !help pwf_help
bind pub - !addadmin pwf_addadmin
bind pub - !remadmin pwf_removeadmin
bind pub G|G !enable pwf_enable
bind pub G|G !disable pwf_disable
bind msg - help pwf_msghelp
bind msg - admin pwf_auth
bind msg G|G addgame pwf_addgame
bind msg G|G endgame pwf_remgame
bind msg G|G info pwf_info
bind msg G|G permban pwf_permban
bind msg G|G permbaninfo pwf_permbaninfo
bind msg m|m enable pwf_msgenable
bind msg m|m disable pwf_msgdisable
bind join - * pwf_msgonjoin
bind nick - * pwf_nickchange
bind part - * pwf_playerquit
bind kick - * pwf_playerkicked
bind sign - * pwf_playersign
bind topc - * pwf_topicchange
utimer 10 pwf_remoldflag

set pwf_shutupchan 0
set pwf_type ""
set pwf_maxpl $pwf_defaultplayer
set pwf_voters [list ]
set pwf_baneduser ""
set pwf_quality 0
set pwf_serverholder ""
set pwf_active 0
set pwf_playernumber 0
set pwf_serverip ""
set pwf_serverpassword ""
set pwf_teamaserver [list ]
set pwf_teambserver [list ]
set pwf_maplock 0
set pwf_mapslength [llength $pwf_maps]
set pwf_timeoutdelay 30


if ![info exists pwf_antirehash] {
  timer 10 pwf_checkactive
  timer 2 pwf_tchk
  set pwf_antirehash 1
}


proc pwf_msgonjoin { nickname user handle channel } {
  global pwf_welcomemsg pwf_pickupchan botnick
  if { $channel == $pwf_pickupchan } {
    putserv "NOTICE $nickname :[pwf_txt welcome] $nickname . $pwf_welcomemsg"
  }
}


proc pwf_topicchange {nick uhost handle channel topic} {
  global botnick pwf_pickupchan
  if { ( $nick != $botnick ) && ( $channel == $pwf_pickupchan ) } {
    set thetopic [topic $channel]
    if {$thetopic == ""} {
     utimer 2 "pwf_waittopic $channel"
    } else {
     pwf_newtopic $channel $thetopic
    }
  }
  return 1
}


proc pwf_waittopic {channel} {
  set channelfile topic$channel
  if {[pwf_checkfile $channelfile "r"]} {
    set fileio [open $channelfile "r"]
    set restoredtopic [gets $fileio]
    close $fileio
    if {$restoredtopic != ""} {
      putserv "TOPIC $channel :$restoredtopic"
      pwf_newtopic $channel $restoredtopic
      return 1
    }
  }
}


proc pwf_newtopic {channel topic} {
  set channelfile topic$channel
  if {[file exists $channelfile]} {
    if {![pwf_checkfile $channelfile "w"]} {
	putlog "[pwf_txt misc1] $channelfile"
	return 0
    }
  }
  set fileio [open $channelfile "w"]
  puts $fileio $topic
  flush $fileio
  close $fileio
  return 1
}


proc pwf_checkfile {file rw} {
  if {[file exists $file] && [file isfile $file]} {
    if {$rw == "r"} {
      if {[file readable $file]} {
        return 1
      } else {
        return 0
      }
    } elseif {$rw == "w"} {
      if {[file writable $file]} {
        return 1
      } else {
        return 0
      }
    } else {
      return 1
    }
  }
  return 0
}


proc pwf_setnogame {} {
  global pwf_pickupchan pwf_shutupchan pwf_nogametopic
  set channel $pwf_pickupchan
  if { $pwf_shutupchan == 0 } {
    set channelfile topic$channel
    if {[pwf_checkfile $channelfile "r"]} {
      set fileio [open $channelfile "r"]
      set restoredtopic [gets $fileio]
      close $fileio
      if {$restoredtopic != ""} {
        set keyword [string range $pwf_nogametopic 0 5]
        if { [string match "*$keyword*" $restoredtopic]} {
     	    putserv "TOPIC $channel :$restoredtopic"
        } else {
          putserv "TOPIC $channel :$restoredtopic $pwf_nogametopic"
        }
      }
    }
  }
}


proc pwf_starttopic {} {
  global pwf_teamatopic pwf_teambtopic pwf_maptopic pwf_maxpl pwf_playernumber pwf_serverholder pwf_quality pwf_pickupchan pwf_shutupchan pwf_type
  pwf_buildteams
  if { $pwf_quality == 0 } {
    if { $pwf_shutupchan == 0 } {
      if { $pwf_type == "" } {
        putserv "TOPIC $pwf_pickupchan :[pwf_txt topic1] $pwf_serverholder [pwf_txt topic2] $pwf_maptopic"
      } else {
        putserv "TOPIC $pwf_pickupchan :[pwf_txt topic1] $pwf_serverholder [pwf_txt topic4] $pwf_type [pwf_txt topic2] $pwf_maptopic"
      }
    }
  } else {
    if { $pwf_shutupchan == 0 } {
      if { $pwf_type == "" } {
        putserv "TOPIC $pwf_pickupchan :[pwf_txt topic1] $pwf_serverholder [pwf_txt topic5] [pwf_txt topic2] $pwf_maptopic"
      } else {
        putserv "TOPIC $pwf_pickupchan :[pwf_txt topic1] $pwf_serverholder [pwf_txt topic5] [pwf_txt topic4] $pwf_type [pwf_txt topic2] $pwf_maptopic"
      }
    }
  }
}


proc pwf_settopic {} {
  global pwf_teamatopic pwf_teambtopic pwf_maxpl pwf_playernumber pwf_serverholder pwf_pickupchan pwf_shutupchan
  pwf_buildteams
  if { $pwf_shutupchan == 0 } {
      putserv "PRIVMSG $pwf_pickupchan :[pwf_txt teams1] $pwf_playernumber/$pwf_maxpl [pwf_txt separator] $pwf_teamatopic [pwf_txt separator] $pwf_teambtopic"
  }
}


proc pwf_addme { nick uhost handle channel team } {
  global pwf_teamaserver pwf_teambserver pwf_playernumber pwf_maxpl pwf_pickupchan pwf_active pwf_quality pwf_shutupchan
  if { $channel == $pwf_pickupchan } { 
    if { $pwf_shutupchan == 0 } {
      if { $pwf_active == 1 } {
        set team [string tolower $team]
        if { $team == "" } {
          set numberteama [lsearch -exact $pwf_teamaserver $nick ]
          set numberteamb [lsearch -exact $pwf_teambserver $nick ]
          set ispresent [expr $numberteama + $numberteamb]
          if { $ispresent == -2 } {
            if { [llength $pwf_teambserver] < [llength $pwf_teamaserver] } {
              set team b
            } else {
              set team a
            }
          } else {
            putserv "NOTICE $nick :[pwf_txt addme1]"
            return 0
          }
        }
        if { $pwf_quality == 1 } {
          if { ( [isvoice $nick $channel] ) || ( [isop $nick $channel] ) } {
            if { ( [lsearch -exact $team a ] >-1 ) && ( [lsearch -exact $pwf_teamaserver $nick ] <0 ) && ( [llength $pwf_teamaserver] < [expr $pwf_maxpl/2] ) } {
              set pwf_teamaserver [linsert $pwf_teamaserver end $nick ]
              if {[lsearch -exact $pwf_teambserver $nick ] >-1 } {
                set playerindex [lsearch -exact $pwf_teambserver $nick ]
                set pwf_teambserver [lreplace $pwf_teambserver $playerindex $playerindex ]
              }
              set pwf_playernumber [expr [llength $pwf_teamaserver]+[llength $pwf_teambserver]]
              pwf_settopic
            } elseif { ( [lsearch -exact $team b] >-1 ) && ( [lsearch -exact $pwf_teambserver $nick ] <0 ) && ( [llength $pwf_teambserver] < [expr $pwf_maxpl/2] ) } {
              set pwf_teambserver [linsert $pwf_teambserver end $nick ]
              if {[lsearch -exact $pwf_teamaserver $nick ] >-1 } {
                set playerindex [lsearch -exact $pwf_teamaserver $nick ]
                set pwf_teamaserver [lreplace $pwf_teamaserver $playerindex $playerindex ]
              }
              set pwf_playernumber [expr [llength $pwf_teamaserver]+[llength $pwf_teambserver]]
              pwf_settopic
            } else {
              putserv "NOTICE $nick :[pwf_txt addme1]"
            }
            if { $pwf_playernumber == $pwf_maxpl } {
              pwf_launchgame
              set pwf_quality 0
            }
          } else {
            putserv "NOTICE $nick :[pwf_txt addme2]"
          }
        } elseif { $pwf_quality == 0 } {
          if { ( [lsearch -exact $team a ] >-1 ) && ( [lsearch -exact $pwf_teamaserver $nick ] <0 ) && ( [llength $pwf_teamaserver] < [expr $pwf_maxpl/2] ) } {
            set pwf_teamaserver [linsert $pwf_teamaserver end $nick ]
            if {[lsearch -exact $pwf_teambserver $nick ] >-1 } {
              set playerindex [lsearch -exact $pwf_teambserver $nick ]
              set pwf_teambserver [lreplace $pwf_teambserver $playerindex $playerindex ]
            }
            set pwf_playernumber [expr [llength $pwf_teamaserver]+[llength $pwf_teambserver]]
            pwf_settopic
          } elseif { ( [lsearch -exact $team b] >-1 ) && ( [lsearch -exact $pwf_teambserver $nick ] <0 ) && ( [llength $pwf_teambserver] < [expr $pwf_maxpl/2] ) } {
            set pwf_teambserver [linsert $pwf_teambserver end $nick ]
            if {[lsearch -exact $pwf_teamaserver $nick ] >-1 } {
              set playerindex [lsearch -exact $pwf_teamaserver $nick ]
              set pwf_teamaserver [lreplace $pwf_teamaserver $playerindex $playerindex ]
            }
            set pwf_playernumber [expr [llength $pwf_teamaserver]+[llength $pwf_teambserver]]
            pwf_settopic
          } else {
            putserv "NOTICE $nick :[pwf_txt addme1]"
          }
          if { $pwf_playernumber == $pwf_maxpl } {
            pwf_launchgame
          }
        }
      } else {
        putserv "NOTICE $nick :[pwf_txt addme3]"
      }
    } else {
      putserv "NOTICE $nick :[pwf_txt addme4]"
    }
  }
}


proc pwf_remove { nick uhost handle channel arg} {
  global pwf_teamaserver pwf_teambserver pwf_playernumber pwf_active pwf_pickupchan pwf_shutupchan
  if { $channel == $pwf_pickupchan } {
    if { $pwf_shutupchan == 0 } {
      if { $pwf_active == 1 } {
        if { [lsearch -exact $pwf_teamaserver $nick ] >-1 } {
          set playerindex [lsearch -exact $pwf_teamaserver $nick ]
          set pwf_teamaserver [lreplace $pwf_teamaserver $playerindex $playerindex ]
          set pwf_playernumber [expr [llength $pwf_teamaserver]+[llength $pwf_teambserver]]
          pwf_settopic
        } elseif { [lsearch -exact $pwf_teambserver $nick ] >-1 } {
          set playerindex [lsearch -exact $pwf_teambserver $nick ]
          set pwf_teambserver [lreplace $pwf_teambserver $playerindex $playerindex ]
          set pwf_playernumber [expr [llength $pwf_teamaserver]+[llength $pwf_teambserver]]
          pwf_settopic
        } else {
          putserv "NOTICE $nick :[pwf_txt addme5]"
        }
      } else {
        putserv "NOTICE $nick :[pwf_txt addme3]"
      }
    } else {
      putserv "NOTICE $nick :[pwf_txt addme4]"
    }
  }
}


proc pwf_votemap { nick uhost handle channel votemap } {
  global pwf_teamaserver pwf_teambserver pwf_active pwf_maplock pwf_voters pwf_maps pwf_maptopic pwf_mapslength pwf_pickupchan pwf_shutupchan
  if { $channel == $pwf_pickupchan } {
    if { $pwf_shutupchan == 0 } {
      if { $pwf_active == 1 } {
        set numberteama [lsearch -exact $pwf_teamaserver $nick ]
        set numberteamb [lsearch -exact $pwf_teambserver $nick ]
        set ispresent [expr $numberteama + $numberteamb]
        if { $ispresent > -2 } {
          if { $pwf_maplock == 0 } {
            if { [lsearch -exact $pwf_voters $nick] == -1 } {
              set votemap [string tolower $votemap]
              set mapindex [lsearch $pwf_maps $votemap]
              if { $mapindex > -1 } {
                set pwf_voters [linsert $pwf_voters end $nick]
                set pwf_voters [linsert $pwf_voters end $mapindex]
                putserv "NOTICE $nick :[pwf_txt vote1]"
              } else {
                putserv "NOTICE $nick :[pwf_txt vote2]"
              }
            } else {
              set mapindex [lsearch $pwf_maps $votemap]
              if { $mapindex > -1 } {
                set voterindex [lsearch -exact $pwf_voters $nick]
                set voterindex [incr voterindex]
                set pwf_voters [lreplace $pwf_voters $voterindex $voterindex $mapindex]
                putserv "NOTICE $nick :[pwf_txt vote1]"
              } else {
                putserv "NOTICE $nick :[pwf_txt vote2]"
              }
            }
            set previousmap $pwf_maptopic
            set voters2 $pwf_voters
            set voters2length [llength $voters2]
            set mapranking [list ]
            set maps2 [list ]
            for {set x 0} { $x < $pwf_mapslength } {incr x} {
              set maps2 [linsert $maps2 0 0]
            }
            for {set x 1} { $x < $voters2length } {incr x 2} {
              set votedmap [lindex $voters2 $x]
              set numbervote [lindex $maps2 $votedmap]
              incr numbervote
              set maps2 [lreplace $maps2 $votedmap $votedmap $numbervote]
            }
            for {set x 0} { $x < $pwf_mapslength } {incr x} {
              set themap [list [lindex $maps2 $x] [lindex $pwf_maps $x]]
              set mapranking [linsert $mapranking end $themap]
            }
            set mapranking [lsort -decreasing -index 0 $mapranking]
            set mapwin [list [lindex $mapranking 0 ]]
            set endindex [expr [string length $mapwin ] - 2 ]
            set pwf_maptopic [string range $mapwin 3 $endindex]
            if { ( $pwf_maptopic == $votemap ) && ( $pwf_maptopic != $previousmap ) } {
              putserv "PRIVMSG $channel :[pwf_txt vote3] $pwf_maptopic"
              pwf_starttopic
            }
          } else {
            putserv "NOTICE $nick :[pwf_txt vote4]"
          }
        } else {
          putserv "NOTICE $nick :[pwf_txt vote5]"
        }
      } else {
        putserv "NOTICE $nick :[pwf_txt addme3]"
      }
    } else {
      putserv "NOTICE $nick :[pwf_txt addme4]"
    }
  }
}


proc pwf_ip { nick uhost handle channel arg } {
  global pwf_active pwf_pickupchan pwf_serverip pwf_shutupchan
  if { $channel == $pwf_pickupchan } {
    if { $pwf_shutupchan == 0 } {
      if { $pwf_active == 1 } {
        putserv "PRIVMSG $channel :[pwf_txt ip1] $pwf_serverip [pwf_txt ip2]"
      } else {
        putserv "NOTICE $nick :[pwf_txt addme3]"
      }
    } else {
      putserv "NOTICE $nick :[pwf_txt addme4]"
    }
  }
}


proc pwf_teams { nick uhost handle channel arg } {
  global pwf_active pwf_pickupchan pwf_teamatopic pwf_teambtopic pwf_shutupchan
    if { $channel == $pwf_pickupchan } {
      if { $pwf_shutupchan == 0 } {
        if { $pwf_active == 1 } {
          putserv "PRIVMSG $channel : $pwf_teamatopic [pwf_txt separator] $pwf_teambtopic"
        } else {
          putserv "NOTICE $nick :[pwf_txt addme3]"
        }
      } else {
        putserv "NOTICE $nick :[pwf_txt addme4]"
      }
   }
}


proc pwf_addmap { nick uhost handle channel map } {
  global pwf_maptopic pwf_active pwf_pickupchan pwf_maplock pwf_serverholder pwf_shutupchan
  if { $channel == $pwf_pickupchan } {
    if { $pwf_shutupchan == 0 } {
      if { $pwf_active == 1 } {
        if { $nick == $pwf_serverholder } {
          if { $map != "" } {
            set pwf_maptopic $map
            pwf_starttopic
            set pwf_maplock 1
          } else {
            putserv "NOTICE $nick :[pwf_txt admin1]"
          }
        } else {
          putserv "NOTICE $nick :[pwf_txt admin2]"
        }
      } else {
        putserv "NOTICE $nick :[pwf_txt addme3]"
      }
    } else {
      putserv "NOTICE $nick :[pwf_txt addme4]"
    }
  }
}


proc pwf_addtype { nick uhost handle channel arg } {
  global pwf_type pwf_active pwf_pickupchan pwf_serverholder pwf_shutupchan
  if { $channel == $pwf_pickupchan } {
    if { $pwf_shutupchan == 0 } {
      if { $pwf_active == 1 } {
        if { $nick == $pwf_serverholder } {
          set pwf_type $arg
          pwf_starttopic
        } else {
          putserv "NOTICE $nick :[pwf_txt admin2]"
        }
      } else {
        putserv "NOTICE $nick :[pwf_txt addme3]"
      }
    } else {
      putserv "NOTICE $nick :[pwf_txt addme4]"
    }
  }
}


proc pwf_playernumber { nick uhost handle channel arg } {
  global pwf_maxpl pwf_active pwf_pickupchan pwf_serverholder pwf_shutupchan
  if { $channel == $pwf_pickupchan } {
    if { $pwf_shutupchan == 0 } {
      if { $pwf_active == 1 } {
        if { $nick == $pwf_serverholder } {
          if { $arg != "" } {
            if { [expr $arg % 2 ] == "0" } {
              set pwf_maxpl $arg
              pwf_starttopic
              pwf_settopic
            } else {
              putserv "NOTICE $nick :[pwf_txt admin4]"
            }
          } else {
            putserv "NOTICE $nick :[pwf_txt admin3]"
          }
        } else {
          putserv "NOTICE $nick :[pwf_txt admin2]"
        }
      } else {
        putserv "NOTICE $nick :[pwf_txt addme3]"
      }
    } else {
      putserv "NOTICE $nick :[pwf_txt addme4]"
    }
  }
}


proc pwf_addgame { nick uhost handle arg } {
  global pwf_active pwf_pickupchan pwf_shutupchan pwf_serverip pwf_serverpassword pwf_serverholder pwf_quality botnick
  if { [matchattr $handle H] } {
    if { $pwf_shutupchan == 0 } {
      if { ( $pwf_active == 0 ) } {
        set pwf_serverip [lindex $arg 0]
        set pwf_serverpassword [lindex $arg 1]
        set gamequality [lindex $arg 2]
        set qualitytxt "vip"
        if { ( $pwf_serverpassword != "" ) && ( $pwf_serverip != "" ) && ( $pwf_serverpassword != $qualitytxt ) } {
          if { $gamequality == $qualitytxt } {
            if { $pwf_quality == 0 } {
              set pwf_quality 1
              set pwf_serverholder $nick
              putserv "PRIVMSG $pwf_serverholder :[pwf_txt addgame1] $pwf_serverip [pwf_txt addgame2] $pwf_serverpassword [pwf_txt addgame3] [pwf_txt addgame4]"
            } else {
              putserv "PRIVMSG $pwf_serverholder :[pwf_txt addgame5]"
              set pwf_serverip ""
              set pwf_serverpassword ""
            }
          } else {
            set pwf_serverholder $nick
            putserv "PRIVMSG $pwf_serverholder :[pwf_txt addgame1] $pwf_serverip [pwf_txt addgame2] $pwf_serverpassword [pwf_txt addgame4]"
          }
        } else {
          putserv "PRIVMSG $nick :[pwf_txt addgame8]"
          set pwf_serverip ""
          set pwf_serverpassword ""
        }
      } else {
        putserv "PRIVMSG $nick :[pwf_txt addgame6] $pwf_serverholder [pwf_txt addgame7]"
      }
    } else {
      putserv "NOTICE $nick :[pwf_txt addgame10] $pwf_pickupchan"
    }
  } else {
    putserv "NOTICE $nick :[pwf_txt noauth]"
  }
}


proc pwf_info { nick uhost handle arg } {
  global pwf_active pwf_pickupchan pwf_serverip pwf_serverpassword pwf_serverholder pwf_pickupchan pwf_quality botnick
  if { ( $nick == $pwf_serverholder ) && ( $pwf_active == 0 )} {
    if { $arg == "ok" } {
      putserv "PRIVMSG $nick :[pwf_txt addgame9]"
      set pwf_active 1
      pwf_starttopic
    } else {
      set pwf_serverpassword ""
      set pwf_serverip ""
      set pwf_serverholder ""
      putserv "PRIVMSG $nick :[pwf_txt addgame0]"
    }
  }
}


proc pwf_help { nick uhost handle channel arg } {
   global botnick pwf_pickupchan
   if { $channel == $pwf_pickupchan } {
     putserv "NOTICE $nick :[pwf_txt help1]"
     putserv "NOTICE $nick :[pwf_txt help2]"
     putserv "NOTICE $nick :[pwf_txt help3]"
     putserv "NOTICE $nick :[pwf_txt help4]"
     putserv "NOTICE $nick :[pwf_txt help5]"
     putserv "NOTICE $nick :[pwf_txt help6] #pwf ( Quakenet ) - http://sourceforge.net/projects/pwf/ - PWF by kalhimeo"
   }
}


proc pwf_msghelp { nick uhost handle arg } {
   global botnick
   putserv "NOTICE $nick :[pwf_txt help1]"
   putserv "NOTICE $nick :[pwf_txt help2]"
   putserv "NOTICE $nick :[pwf_txt help3]"
   putserv "NOTICE $nick :[pwf_txt help4]"
   putserv "NOTICE $nick :[pwf_txt help5]"
   putserv "NOTICE $nick :[pwf_txt help6] #pwf ( Quakenet ) - http://sourceforge.net/projects/pwf/ - PWF by kalhimeo"
}


proc pwf_checkactive {} {
   global pwf_active pwf_serverpassword pwf_serverip pwf_serverholder pwf_baneduser
   timer 10 pwf_checkactive
   if { ( $pwf_active == 0 ) && ( $pwf_serverholder != "" ) } {
     set pwf_serverpassword ""
     set pwf_serverip ""
     set pwf_serverholder ""
   }
   set pwf_baneduser ""
}


proc pwf_remgame { nick uhost handle arg } {
  global pwf_active pwf_quality pwf_maptopic pwf_playernumber pwf_serverip pwf_teamaserver pwf_teambserver pwf_serverpassword pwf_pickupchan pwf_maplock pwf_serverholder botnick pwf_voters pwf_type pwf_maxpl pwf_defaultplayer
  if { [matchattr $handle H] } {
    if { $pwf_active == 1 } {
      if { $nick == $pwf_serverholder } {
        putserv "PRIVMSG $pwf_pickupchan : [pwf_txt cancel1] $pwf_serverholder [pwf_txt cancel2]"
        set pwf_active 0
        set pwf_serverip ""
        set pwf_serverpassword ""
        set pwf_playernumber 0
        set pwf_teamaserver [list ]
        set pwf_teambserver [list ]
        set pwf_maplock 0
        set pwf_serverholder 0
        set pwf_maptopic "Map not defined yet"
        set pwf_voters [list ]
        pwf_setnogame
        set pwf_quality 0
        set pwf_type ""
        set pwf_maxpl $pwf_defaultplayer
      } else {
        putserv "NOTICE $nick :[pwf_txt cancel5]"
      }
    } else {
      putserv "NOTICE $nick :[pwf_txt cancel4]"
    }  
  } else {
    putserv "NOTICE $nick :[pwf_txt noauth]"
  }
}


proc pwf_nickchange { nick uhost handle channel newnick } {
   global pwf_teamaserver pwf_teambserver pwf_serverholder pwf_shutupchan pwf_authnick pwf_pickupchan
   if { $channel == $pwf_pickupchan } {
     if { $pwf_shutupchan == 0 } {
       if {[lsearch -exact $pwf_teamaserver $nick ] >-1 } {
         set playerindex [lsearch -exact $pwf_teamaserver $nick ]
         set pwf_teamaserver [lreplace $pwf_teamaserver $playerindex $playerindex $newnick]
       } elseif {[lsearch -exact $pwf_teambserver $nick ] >-1 } {
         set playerindex [lsearch -exact $pwf_teambserver $nick ]
         set pwf_teambserver [lreplace $pwf_teambserver $playerindex $playerindex $newnick]
       }
       if { $nick == $pwf_serverholder } {
         set pwf_serverholder $newnick
       }
     }
     if [info exists pwf_authnick($nick)] {
       set pwf_authnick($newnick) $pwf_authnick($nick)
       unset pwf_authnick($nick)
     }
   }
}



proc pwf_playerquit { nick uhost handle channel arg } {
  global pwf_teamaserver pwf_teambserver pwf_playernumber pwf_pickupchan pwf_serverholder pwf_shutupchan pwf_authnick
  if { $channel == $pwf_pickupchan } {
    if { $pwf_shutupchan == 0 } {
      if {[lsearch -exact $pwf_teamaserver $nick ] >-1 } {
        set playerindex [lsearch -exact $pwf_teamaserver $nick ]
        set pwf_teamaserver [lreplace $pwf_teamaserver $playerindex $playerindex ]
        set pwf_playernumber [expr [llength $pwf_teamaserver]+[llength $pwf_teambserver]]
        pwf_settopic
      } elseif {[lsearch -exact $pwf_teambserver $nick ] >-1 } {
        set playerindex [lsearch -exact $pwf_teambserver $nick ]
        set pwf_teambserver [lreplace $pwf_teambserver $playerindex $playerindex ]
        set pwf_playernumber [expr [llength $pwf_teamaserver]+[llength $pwf_teambserver]]
        pwf_settopic
      }
      if { $nick == $pwf_serverholder } {
        if { $pwf_shutupchan == 0 } { 
          putserv "PRIVMSG $pwf_pickupchan :[pwf_txt cancel1] $pwf_serverholder [pwf_txt cancel3]"
        }
        pwf_endgame
      }
    }
    if ![info exists pwf_authnick($nick)] {
      return 0
    }
    if ![matchattr $pwf_authnick($nick) H] {
      return 0
    }
    chattr $pwf_authnick($nick) -H
    unset pwf_authnick($nick)
    putserv "NOTICE $nick :[pwf_txt deauthpart]"
    return 0
  }
}


proc pwf_playerkicked { kickernick uhost hand channel nick reason } {
  global pwf_teamaserver pwf_teambserver pwf_playernumber pwf_pickupchan pwf_serverholder pwf_shutupchan pwf_authnick
  if { $channel == $pwf_pickupchan } {
    if { $pwf_shutupchan == 0 } {
      if {[lsearch -exact $pwf_teamaserver $nick ] >-1 } {
        set playerindex [lsearch -exact $pwf_teamaserver $nick ]
        set pwf_teamaserver [lreplace $pwf_teamaserver $playerindex $playerindex ]
        set pwf_playernumber [expr [llength $pwf_teamaserver]+[llength $pwf_teambserver]]
        pwf_settopic
      } elseif {[lsearch -exact $pwf_teambserver $nick ] >-1 } {
        set playerindex [lsearch -exact $pwf_teambserver $nick ]
        set pwf_teambserver [lreplace $pwf_teambserver $playerindex $playerindex ]
        set pwf_playernumber [expr [llength $pwf_teamaserver]+[llength $pwf_teambserver]]
        pwf_settopic
      }
      if { $nick == $pwf_serverholder } {
        if { $pwf_shutupchan == 0 } { 
          putserv "PRIVMSG $pwf_pickupchan :[pwf_txt cancel1] $pwf_serverholder [pwf_txt cancel3]"
        }
        pwf_endgame
      }
    }
    if ![info exists pwf_authnick($nick)] {
      return 0
    }
    if ![matchattr $pwf_authnick($nick) H] {
      return 0
    }
    chattr $pwf_authnick($nick) -H
    unset pwf_authnick($nick)
    putserv "NOTICE $nick :[pwf_txt deauthkick]"
    return 0
  }
}


proc pwf_playersign {nick uhost hand channel rest} { 
  global pwf_teamaserver pwf_teambserver pwf_playernumber pwf_pickupchan pwf_serverholder pwf_shutupchan pwf_authnick
  if { $channel == $pwf_pickupchan } {
    if { $pwf_shutupchan == 0 } {
      if {[lsearch -exact $pwf_teamaserver $nick ] >-1 } {
        set playerindex [lsearch -exact $pwf_teamaserver $nick ]
        set pwf_teamaserver [lreplace $pwf_teamaserver $playerindex $playerindex ]
        set pwf_playernumber [expr [llength $pwf_teamaserver]+[llength $pwf_teambserver]]
        pwf_settopic
      } elseif {[lsearch -exact $pwf_teambserver $nick ] >-1 } {
        set playerindex [lsearch -exact $pwf_teambserver $nick ]
        set pwf_teambserver [lreplace $pwf_teambserver $playerindex $playerindex ]
        set pwf_playernumber [expr [llength $pwf_teamaserver]+[llength $pwf_teambserver]]
        pwf_settopic
      }
      if { $nick == $pwf_serverholder } {
        if { $pwf_shutupchan == 0 } { 
          putserv "PRIVMSG $pwf_pickupchan :[pwf_txt cancel1] $pwf_serverholder [pwf_txt cancel3]"
        }
        pwf_endgame
      }
    }
    if ![info exists pwf_authnick($nick)] {
      return 0
    }
    if ![matchattr $pwf_authnick($nick) H] {
      return 0
    }
    chattr $pwf_authnick($nick) -H
    unset pwf_authnick($nick)
  }
}


proc pwf_endgame {} {
  global pwf_active pwf_maptopic pwf_playernumber pwf_serverip pwf_teamaserver pwf_teambserver pwf_serverpassword pwf_pickupchan pwf_maplock pwf_serverholder pwf_voters pwf_type pwf_maxpl pwf_defaultplayer
  set pwf_active 0
  set pwf_serverip ""
  set pwf_serverpassword ""
  set pwf_playernumber 0
  set pwf_teamaserver [list ]
  set pwf_teambserver [list ]
  set pwf_maplock 0
  set pwf_serverholder 0
  set pwf_maptopic "Map not defined yet"
  set pwf_voters [list ]
  set pwf_type ""
  set pwf_maxpl $pwf_defaultplayer
  pwf_setnogame
}


proc pwf_buildteams {} {
  global pwf_teamaserver pwf_teambserver pwf_teamatopic pwf_teambtopic pwf_maxpl
  set lengthteama [llength $pwf_teamaserver]
  set lengthteamb [llength $pwf_teambserver]
  set x 0
  set y 0
  set pwf_teamatopic [pwf_txt defteama]
  set pwf_teambtopic [pwf_txt defteamb]
  while {$x<$lengthteama} {
    set pwf_teamatopic "$pwf_teamatopic [lindex $pwf_teamaserver $x] -"
    incr x
  }
  while {$x<[expr $pwf_maxpl/2]} {
    set pwf_teamatopic "$pwf_teamatopic ? -"
    incr x
  }
  while {$y<$lengthteamb} {
    set pwf_teambtopic "$pwf_teambtopic [lindex $pwf_teambserver $y] -"
    incr y
  }
    while {$y<[expr $pwf_maxpl/2]} {
    set pwf_teambtopic "$pwf_teambtopic ? -"
    incr y
  }
  set endindexa [expr [string length $pwf_teamatopic ] - 2 ]
  set pwf_teamatopic [string range $pwf_teamatopic 0 $endindexa]
  set endindexb [expr [string length $pwf_teambtopic ] - 2 ]
  set pwf_teambtopic [string range $pwf_teambtopic 0 $endindexb]
}


proc pwf_launchgame {} {
  global pwf_serverip pwf_serverpassword pwf_maxpl pwf_active pwf_teamaserver pwf_teambserver pwf_serverholder pwf_pickupchan pwf_shutupchan
  set pwf_active 0
  set limit [expr $pwf_maxpl / 2 ]
  if { $pwf_shutupchan == 0 } {
    putserv "TOPIC $pwf_pickupchan :[pwf_txt start1]"
  }
  set msgteama "[pwf_txt addgame1] $pwf_serverip [pwf_txt addgame2] $pwf_serverpassword [pwf_txt start2] $pwf_serverholder [pwf_txt start4]"
  set msgteamb "[pwf_txt addgame1] $pwf_serverip [pwf_txt addgame2] $pwf_serverpassword [pwf_txt start3] $pwf_serverholder [pwf_txt start4]"
  for {set x 0} { $x < $limit } {incr x} {
    putserv "PRIVMSG [lindex $pwf_teamaserver $x] :$msgteama"
    putserv "PRIVMSG [lindex $pwf_teambserver $x] :$msgteamb"
  }      
  pwf_endgame
}


proc pwf_permban { nick uhost handle arg } {
  global pwf_baneduser botnick
  if { [matchattr $handle H] } { 
    set pwf_baneduser $arg
    putserv "PRIVMSG $nick :[pwf_txt banconf1] $pwf_baneduser [pwf_txt banconf2]"
  } else {
    putserv "NOTICE $nick :[pwf_txt noauth]"
  }
}


proc pwf_permbaninfo { nick uhost handle arg } {
  global pwf_pickupchan pwf_baneduser botnick
  set ok ok
  if { $pwf_baneduser != "" } {
    if { $arg == $ok } {
      set reason [pwf_txt banconf5]
      newchanban $pwf_pickupchan $pwf_baneduser $handle $reason 0
      putserv "PRIVMSG $nick :[pwf_txt banconf3]"
      set pwf_baneduser ""
    } else {
      set pwf_baneduser ""
      putserv "PRIVMSG $nick :[pwf_txt banconf4]"
    }
  }
}


proc pwf_enable { nick uhost handle channel arg } {
  global pwf_pickupchan pwf_shutupchan botnick
  if { $channel == $pwf_pickupchan } {
    if { [matchattr $handle H] } {
      if { $pwf_shutupchan == 1 } {
        set pwf_shutupchan 0
        putserv "PRIVMSG $channel :[pwf_txt enable1]"
      } else {
        putserv "NOTICE $nick :[pwf_txt enable2]"
      }
    } else {
      putserv "NOTICE $nick :[pwf_txt noauth]"
    }
  }
}


proc pwf_disable { nick uhost handle channel arg } {
  global pwf_pickupchan pwf_shutupchan pwf_active botnick
  if { $channel == $pwf_pickupchan } {
    if { [matchattr $handle H ] } {
      if { $pwf_shutupchan == 0 } {
        if { $pwf_active == 0 } {
          set pwf_shutupchan 1
          putserv "PRIVMSG $channel :[pwf_txt disable1]"
        } elseif { [matchattr $handle m|m ] } {
          pwf_endgame
          putserv "PRIVMSG $channel :[pwf_txt disable1]"
          set pwf_shutupchan 1
        } else {
          putserv "NOTICE $nick :[pwf_txt disable4]"
        }
      } else {
        putserv "NOTICE $nick :[pwf_txt disable2]"
      }
    } else {
      putserv "NOTICE $nick :[pwf_txt noauth]"
    }
  }
}


proc pwf_msgenable { nick uhost handle arg } {
  global pwf_pickupchan pwf_shutupchan botnick
  if { [matchattr $handle H] } {
    set channel $arg
    if {[string index $channel 0] != "#"} {
      set channel "#$channel"
    }
    if { ( $pwf_shutupchan == 1 ) && ( $pwf_pickupchan == $channel ) } {
      set pwf_shutupchan 0
      putserv "PRIVMSG $channel :[pwf_txt enable1]"
    } else {
      putserv "PRIVMSG $nick :[pwf_txt enable3]"
    }
  } else {
    putserv "NOTICE $nick :[pwf_txt noauth]"
  }
}


proc pwf_msgdisable { nick uhost handle arg } {
  global pwf_pickupchan pwf_shutupchan pwf_active botnick
  if { [matchattr $handle H] } {
    set channel $arg
    if {[string index $channel 0] != "#"} {
      set channel "#$channel"
    }
    if { ( $pwf_shutupchan == 0 ) && ( $pwf_pickupchan == $channel ) } {
      putserv "PRIVMSG $channel :[pwf_txt disable1]"
      if { $pwf_active == 1 } {
        pwf_endgame
      }
      set pwf_shutupchan 1
    } else {
      putserv "PRIVMSG $nick :[pwf_txt disable3]"
    }
  } else {
    putserv "NOTICE $nick :[pwf_txt noauth]"
  }
}


proc pwf_addadmin {nick uhost hand channel rest} {
  global botnick pwf_pickupchan
  if { $channel == $pwf_pickupchan } {
    set victime [lindex $rest 0]
    if { [matchattr $hand H] } {
      if [matchattr $hand m|m $channel] {
        if [onchan $victime $channel] {
          set victhand [nick2hand $victime]
          if [string compare $victhand "*"] {
            if [matchattr $victhand G|G $channel] {
              putserv "NOTICE $nick :[pwf_txt right1]"
            } else {
              chattr $victhand +|GY $channel
              putserv "NOTICE $nick :[pwf_txt right2] $victime"
              putserv "NOTICE $victime :[pwf_txt right3] $channel [pwf_txt right4a]"
              return 1
            }
          } else {
            adduser $victime [maskhost [getchanhost $victime $channel]]
            chattr [nick2hand $victime] +|GY $channel
            putserv "NOTICE $nick :[pwf_txt right2] $victime"
            putserv "NOTICE $victime :[pwf_txt right3] $channel [pwf_txt right4b]"
            return 1
          }
        } else {
          putserv "NOTICE $nick :[pwf_txt right5]"
        }
      } else {
        putserv "NOTICE $nick :[pwf_txt right6]"
      }
    } else {
      putserv "NOTICE $nick :[pwf_txt noauth]"
    }
  }
}


proc pwf_removeadmin {nick uhost hand channel rest} {
  global botnick pwf_pickupchan
  if { $channel == $pwf_pickupchan } {
    set victime [lindex $rest 0]
    if { [matchattr $hand H] } { 
      if [matchattr $hand m|m $channel] {
        if [onchan $victime $channel] {
          set victhand [nick2hand $victime]
          if [matchattr $victhand &G $channel] {
            chattr $victhand |-GYZ $channel
            putserv "NOTICE $nick :[pwf_txt right7] $victime"
            putserv "NOTICE $victime :[pwf_txt right8]"
            return 1
          } else {
            putserv "NOTICE $nick :[pwf_txt right9]"
          }
        } else {
          if [validuser $victime] {
            if [matchattr $victime &|G $channel] {
              chattr $victime |-GYZ $channel
              putserv "NOTICE $nick :[pwf_txt right7] $victime"
              return 1
            } else {
              putserv "NOTICE $nick :[pwf_txt right9]"
            }
          } else {
            putserv "NOTICE $nick :[pwf_txt right9]"
          }
        }
      } else {
        putserv "NOTICE $nick :[pwf_txt right0]"
      }
    } else {
      putserv "NOTICE $nick :[pwf_txt noauth]"
    }
  }
}


proc pwf_remoldflag {} {
  global pwf_pickupchan pwf_authnick
  foreach pwf_handlecheck [userlist H] {
    set pwf_nickcheck [hand2nick $pwf_handlecheck $pwf_pickupchan]
    if { ( $pwf_nickcheck == "" ) || ( ![info exists pwf_authnick($pwf_nickcheck)] ) } {
      chattr $pwf_handlecheck -H
      if { [info exists pwf_authnick($pwf_nickcheck)] } {
        unset pwf_authnick($pwf_nickcheck)
      }
    }
  }
}


proc pwf_isauth { nick chan } {
  global pwf_authnick
  if ![info exists pwf_authnick($nick)] { return "" }
    if [matchattr $pwf_authnick($nick) G|G $chan] {
      if [matchattr $pwf_authnick($nick) H] {
        return $pwf_authnick($nick)
      }
    }
  return ""
}


proc pwf_auth {nick uhost hand rest} {
  global pwf_authnick botnick pwf_pickupchan
  set handle hand
  set pw [lindex $rest 0]
  set givenick [lindex $rest 1]
  if [string length $givenick] {
    set hand $givenick
  } else {
    if ![string compare $hand "*"] {
      putserv "NOTICE $nick :[pwf_txt auth1]"
      return 0
    }
  }
  if [lsearch -exact [userlist G|G] $hand]==-1 {
    putserv "NOTICE $nick :[pwf_txt auth2]"
    return 0
  }
  set ok 0
  if [onchan $nick $pwf_pickupchan] {
     set ok 1
  }
  if !$ok {
    putserv "NOTICE $nick :[pwf_txt auth3] $pwf_pickupchan [pwf_txt auth4]"
    return 0
  }
  if [info exists pwf_authnick($nick)] {
    putserv "NOTICE $nick :[pwf_txt auth5] $pwf_authnick($nick)."
    return 0
  }
  if ![string length $pw] {
    putserv "NOTICE $nick :[pwf_txt auth6]"
    return 0
  }
  if [passwdok $hand ""] {
    putserv "NOTICE $nick :[pwf_txt auth7]"
    return 0
  }
  if [passwdok $hand $pw] {
    chattr $hand +H
    set pwf_authnick($nick) $hand
    putserv "NOTICE $nick :[pwf_txt auth8]"
    putcmdlog "($nick!$uhost) !$hand! admin ..."
  } else {
    putserv "NOTICE $nick :[pwf_txt auth9]"
    putcmdlog "($nick!$uhost) !$hand! FAILED admin"
  }
}

proc pwf_tchk { } {
  global pwf_timeoutdelay pwf_authnick pwf_pickupchan
  timer 2 pwf_tchk
  set inactnicks ""
  set actnicks ""
  set chan $pwf_pickupchan
  foreach nick [chanlist $chan] {
    if [string length [pwf_isauth $nick $chan]] {
      set inactindex [lsearch -exact $inactnicks $nick]
      set actindex [lsearch -exact $actnicks $nick]
      if [getchanidle $nick $chan]>$pwf_timeoutdelay {
        if ($inactindex==-1)&&($actindex==-1) {
          lappend inactnicks $nick
        }
      } else {
        if ($actindex!=-1) {
          lappend actnicks $nick
        }
        set inactnicks [lreplace $inactnicks $inactindex $inactindex]
      }
    }
  }
  foreach nick $inactnicks {
    chattr $pwf_authnick($nick) -H
    unset pwf_authnick($nick)
    putserv "NOTICE $nick :[pwf_txt auth0]"
  }
}


proc pwf_txt { txt } {
  global pwf_reply botnick pwf_maptopic
  if { [info exists pwf_maptopic] } {
    return $pwf_reply($txt)
  } else {
    return $pwf_reply(wait)
  }
}

## You can easyly translate and edit all the msg the bot reply here ( utimer is used to wait for the value of $botnick , do not use variable wich are not glabal )  

set pwf_reply(wait) "Please wait during language initialisation. Try again in some seconds ( 60 secs max )"

utimer 60 {
if $pwf_language==1 {
set pwf_reply(addme1) "4Invalid team 14( try a team using !addme a or b )"
set pwf_reply(addme2) "14Sorry but the current game is a ** VIP ** match, only voiced users and opers can add themselves."
set pwf_reply(addme3) "4There is no match for the moment."
set pwf_reply(addme4) "14Match are disable on this chan."
set pwf_reply(addme5) "4I can't find you in any team."
set pwf_reply(vote1) "Vote Recorded"
set pwf_reply(vote2) "4This map is not in my database."
set pwf_reply(vote3) "7The most voted map is now"
set pwf_reply(vote4) "4Map has been set by the pickup admin, votes are disabled."
set pwf_reply(vote5) "4You have to be in a valid team to vote for a map."
set pwf_reply(ip1) "5The next match will start on"
set pwf_reply(ip2) "5Check your ping..."
set pwf_reply(admin1) "4Ambiguous command, use !addmap <map>"
set pwf_reply(admin2) "4You are not the administror of this match."
set pwf_reply(admin3) "4Ambiguous number, use !playernumber <number>"
set pwf_reply(admin4) "4Invalid player number. Please enter a pair number."
set pwf_reply(addgame1) "14IP :"
set pwf_reply(addgame2) "- Password :"
set pwf_reply(addgame3) "- ** VIP **"
set pwf_reply(addgame4) "- If these informations are correct, use /msg $botnick info ok , if not use /msg $botnick info wrong"
set pwf_reply(addgame5) "5The last match was a 14** VIP ** game, you can't launch an other one now. Please use addgame again."
set pwf_reply(addgame6) "14A pickup is currently running on the channel and is managed by"
set pwf_reply(addgame7) "14, try again it will be finished."
set pwf_reply(addgame8) "14You have to enter both : <ip:port> AND <password> . Please use addgame command again."
set pwf_reply(addgame9) "14Ok, thank you for confirming, your game is now launched"
set pwf_reply(addgame0) "14Your server info are now deleted, please use the /msg $botnick addgame <ip:port> <password> command again"
set pwf_reply(addgame10) "14You can't laucnh a new match now because pickups have been disabled on"
set pwf_reply(noauth) "You are not authenticated, use /msg $botnick admin <password>"
set pwf_reply(help1) "Public Channel commands are : !add <team> - !remove - !votemap <map> - !ip - !teams"
set pwf_reply(help2) "Admins Channel commands are : !addmap <map> - !addtype <type> - !playernumber <number> - !enable - !disable"
set pwf_reply(help3) "Admins Private commands are : addgame <ip:port> <password> - info <ok> - endgame - permban <mask> - permbaninfo <ok>"
set pwf_reply(help4) "Admins don't forget to authenticate yourself before using commands by typing /msg $botnick admin <password>"
set pwf_reply(help5) "Don't be lame and enjoy your pickup."
set pwf_reply(help6) "More info about this script :"
set pwf_reply(cancel1) "4The server admin ("
set pwf_reply(cancel2) ") canceled the game :("
set pwf_reply(cancel3) ") left the channel, the game is canceled :("
set pwf_reply(cancel4) "14You try to cancel a game but no games are launched."
set pwf_reply(cancel5) "14You are not the Server-Holder of the current game, you can't end it."
set pwf_reply(deauthpart) "Auto-deauthentication after a channel part."
set pwf_reply(deauthkick) "Auto-deauthentication after a channel kick."
set pwf_reply(start1) "4Match Starting, please wait during password forwarding... ( if you subscribed this match, check your private messages )"
set pwf_reply(start2) "14- You are in team 4A14 ( Start 12CT14 ) - Your admin is4"
set pwf_reply(start3) "14- You are in team 4B14 ( Start 4Terro14 ) - Your admin is4"
set pwf_reply(start4) "14- GO GO GO connect to the server NOW"
set pwf_reply(banconf1) "14Ban this mask for life :"
set pwf_reply(banconf2) "? ( use /msg $botnick permbaninfo ok to confirm )"
set pwf_reply(banconf3) "14Ok , user is now banned :/"
set pwf_reply(banconf4) "14Ban canceled, use /msg $botnick permban again."
set pwf_reply(banconf5) "pickup-lamer successfully removed"
set pwf_reply(enable1) "4Pickup are now enbale on this channel."
set pwf_reply(enable2) "14This channel is already enabled."
set pwf_reply(enable3) "4This channel is not in my channel database or this chan is already enabled."
set pwf_reply(disable1) "4Pickup are now disabled on this channel."
set pwf_reply(disable2) "14This chan is already disabled."
set pwf_reply(disable3) "4This channel is not in my channel database or this chan is already disabled."
set pwf_reply(disable4) "14You cannot disable a chan where a pickup is launching if you are not a channel master."
set pwf_reply(right1) "14The user you are trying to add a Game-Admin acces already have one."
set pwf_reply(right2) "14You just added a Game-Admin access to :"
set pwf_reply(right3) "4Congratulations, you now have the Game-Admin access on"
set pwf_reply(right4a) "!14 You can authenticate yourself using: /msg $botnick admin <password> . Help available on /msg $botnick help"
set pwf_reply(right4b) "!14 You should specify a password immediatly using: /msg $botnick pass <password> . You can then authenticate yourself using: /msg $botnick admin <password> . Help available on /msg $botnick help"
set pwf_reply(right5) "14The user you are trying to add a Game-Admin acces is not on the channel for the moment."
set pwf_reply(right6) "14You are authenticated, but you don't have the required rights to add a new Game-Admin access."
set pwf_reply(right7) "14You have removed Game-Admin access to"
set pwf_reply(right8) "4You Game-Admin access has been removed. 14You won't be able to use commands anymore on"
set pwf_reply(right9) "14This user dont't have Game-Admin access."
set pwf_reply(right0) "14You are authenticated, but you don't have the rights to remove a Game-Admin access."
set pwf_reply(auth1) "14You aren't recognized by the bot. Maybe you changed your ident@host. Please contact an operator."
set pwf_reply(auth2) "14You are recognized, but you don't have the Game-Admin access. Please contact an operator."
set pwf_reply(auth3) "14You have to be on"
set pwf_reply(auth4) "to authenticate yourself."
set pwf_reply(auth5) "14You are already authenticated as a Game-Admin with the nick"
set pwf_reply(auth6) "14Syntax : /msg $botnick admin <password>"
set pwf_reply(auth7) "4You don't have any password set.14 Type /msg $botnick pass <password>"
set pwf_reply(auth8) "14You are now authenticated as a Game-Admin."
set pwf_reply(auth9) "4Authentication failed ... logged !"
set pwf_reply(auth0) "14Your Game-Admin authentication has been removed automatcly because of your idleness."
set pwf_reply(rehash) "4database has been restarted, sorry for the disturbance."
set pwf_reply(welcome) "Welcome"
set pwf_reply(topic1) "7Ready for next match 1-2 Admin is"
set pwf_reply(topic2) "1-3"
set pwf_reply(topic4) "1-10 Game Type :"
set pwf_reply(topic5) "1- 14** VIP GAME **"
set pwf_reply(teams1) "2 Currently :3"
set pwf_reply(separator) "1-"
set pwf_reply(defteama) "5Team A : 2"
set pwf_reply(defteamb) "5Team B : 2"
set pwf_reply(defteama2) "5Team A :2 ? - ? - ? - ? - ? -"
set pwf_reply(defteamb2) "5Team B :2 ? - ? - ? - ? - ?"
set pwf_reply(defmap) "Map not defined yet"
set pwf_reply(misc1) "topicmem : could not edit the topic file of"
} elseif $pwf_language==2 {
set pwf_reply(addme1) "4Team invalide 14( essayez une team avec !addme a ou b )"
set pwf_reply(addme2) "14Désoler mais le jeu actuel est un match ** VIP ** , seuls les utilisateurs voice ou op peuvent s'ajouter."
set pwf_reply(addme3) "4Aucun pickup pour l'instant."
set pwf_reply(addme4) "14Les pickup sont actuellement désactivés sur ce channel."
set pwf_reply(addme5) "4Je ne vous ai trouvé dans aucune team."
set pwf_reply(vote1) "Vote enrégistré"
set pwf_reply(vote2) "4Cette map n'est pas dans ma base de donnée."
set pwf_reply(vote3) "7La map la plus sollicitée est maintenant"
set pwf_reply(vote4) "4La map a été choisie par l'administeur de ce pickup, les votes sont désactivés."
set pwf_reply(vote5) "4Vous devez être dans une team pour pouvoir voter."
set pwf_reply(ip1) "5Le prochain match se jouera sur"
set pwf_reply(ip2) "5Testez votre ping..."
set pwf_reply(admin1) "4Commande ambiguë, utilisez !addmap <map>"
set pwf_reply(admin2) "4Vous n'êtes pas l'administrateur de ce pickup."
set pwf_reply(admin3) "4Commande ambiguë, utlisez !pwf_playernumber <nombre>"
set pwf_reply(admin4) "4Nombre de joueurs invalide. N'utilisez que des nombres pairs svp"
set pwf_reply(addgame1) "14IP :"
set pwf_reply(addgame2) "- Mot-de-Passe :"
set pwf_reply(addgame3) "- ** VIP **"
set pwf_reply(addgame4) "- Si ces informations sont correctent, utilisez /msg $botnick info ok , sinon utilisez /msg $botnick info erreure"
set pwf_reply(addgame5) "5Le dernier pickup était déja un match 14** VIP **, vous ne pouvez pas en lancer un nouveau maintenant. Veuillez utilisez addgame à nouveau."
set pwf_reply(addgame6) "14Un match est déjà en cour sur le channel et est administrer par"
set pwf_reply(addgame7) "14, réessayez quand celui ci sera termniné."
set pwf_reply(addgame8) "14Commande ambiguë, vous devez entrer <ip:port> ET <mot-de-passe> . Veuillez utilisez addgame à nouveau."
set pwf_reply(addgame9) "14Ok, merci d'avoir confirmé, votre match est lancé."
set pwf_reply(addgame0) "14Les informations que vous avez entré ont été effacées, utilisez la commande /msg $botnick addgame <ip:port> <mot-de-passe> à nouveau."
set pwf_reply(addgame10) "14Vous ne pouvez pas lancer de nouveau match pour le moment car les pickup ont été désactivés sur"
set pwf_reply(noauth) "14Vous n'êtes pas authentifié, utilisez /msg $botnick admin <mot-de-passe>"
set pwf_reply(help1) "Commandes Publiques sur le channel  : !add <team> - !remove - !votemap <map> - !ip - !teams"
set pwf_reply(help2) "Commandes des Admins sur le channel : !addmap <map> - !addtype <type> - !playernumber <nombre> - !enable - !disable"
set pwf_reply(help3) "Commandes des Admins en privé       : addgame <ip:port> <password> - info <ok> - endgame - permban <mask> - permbaninfo <ok>"
set pwf_reply(help4) "Pour les Admins, n'oubliez pas de vous authentifier pour utiliser les commandes en tapant /msg $botnick admin <mot-de-passe>"
set pwf_reply(help5) "Respectez les autres joueurs et amusez vous bien."
set pwf_reply(help6) "Pour plus d'informations à propos de ce script :"
set pwf_reply(cancel1) "4L'administrateur du match ("
set pwf_reply(cancel2) ") a annulé le pickup :("
set pwf_reply(cancel3) ") a quitté le channel, le match est donc annulé :("
set pwf_reply(cancel4) "14Vous essayez d'annuler un match en cour mais il n'y en a aucun."
set pwf_reply(cancel5) "14Vous n'êtes pas le Server-Holder de ce match, vous ne pouvez pas annuler le match."
set pwf_reply(deauthpart) "14Désauthentification automatique après avoir quitter le channel."
set pwf_reply(deauthkick) "14Désauthentification automatique après avoir été kicker."
set pwf_reply(start1) "4Le match va maintenant commencer, veillez patienter pendant l'envoi du mot-de-passe... ( si vous êtes inscris pour ce match, vérifiez vos messages privés )"
set pwf_reply(start2) "14- Vous êtes dans l'équipe 4A14 ( Débutez 12CT14 ) - Votre admin est4"
set pwf_reply(start3) "14- Vous êtes dans l'équipe 4B14 ( Débutez 4Terro14 ) - Votre admin est4"
set pwf_reply(start4) "14- GO GO GO connectez le serveur maintenant."
set pwf_reply(banconf1) "14Bannir ce mask de façon permanente :"
set pwf_reply(banconf2) "? ( utilisez /msg $botnick permbaninfo ok pour confirmer )"
set pwf_reply(banconf3) "14Ok, l'utilisateur est bannis de façon permanente :/"
set pwf_reply(banconf4) "14Ban annulé, utilisez /msg $botnick permban à nouveau."
set pwf_reply(banconf5) "pickup-lamer successfully removed"
set pwf_reply(enable1) "4Les pickup sont maintenant actifs sur ce channel."
set pwf_reply(enable2) "14Ce channel est déjà activé."
set pwf_reply(enable3) "4Ce channel n'est pas dans ma base de donnée ou ce channel est déjà activé."
set pwf_reply(disable1) "4Les pickup sont maintenant désactivés sur ce channel."
set pwf_reply(disable2) "14Ce channel est déjà désactivé."
set pwf_reply(disable3) "4Ce channel n'est pas dans ma base de donnée ou ce channel est déjà désactivé."
set pwf_reply(disable4) "14Vous ne pouvez pas désactiver ce channel si vous n'êtes pas master de celui-ci."
set pwf_reply(right1) "14L'utilisateur pour lequel vous essayez d'ajouter l'accès Game-Admin l'a déjà."
set pwf_reply(right2) "14Vous avez donné l'accès Game-Admin à"
set pwf_reply(right3) "4Félicitation, vous êtes maintenant un Game-Admin sur"
set pwf_reply(right4a) "!14 Vous pouvez vous authentifier en utilisant: /msg $botnick admin <mot-de-passe> . Si vous avez besoin d'aide tapez /msg $botnick help"
set pwf_reply(right4b) "!14 Vous devriez définir votre mot de passe en utilisant: /msg $botnick pass <mot-de-passe> . Vous pourrez ensuite vous authentifier avec: /msg $botnick admin <mot-de-passe> . Si vous avez besoin d'aide tapez /msg $botnick help"
set pwf_reply(right5) "14L'utilisateur pour lequel vous tentez d'ajouter un accès Game-Admin n'est pas sur le channel pour l'instant."
set pwf_reply(right6) "14Vous êtes authentifié mais vous n'avez pas les droits pour ajouter un nouvel accès Game-Admin."
set pwf_reply(right7) "14Vous avez effacer l'acces Game-Admin de"
set pwf_reply(right8) "4Votre accès Game-Admin vous a été supprimer, vous ne pouvez plus utilisés les commandes d'administration sur"
set pwf_reply(right9) "14Cet utilisateur n'a pas d'accès Game-Admin."
set pwf_reply(right0) "14Vous êtes authentifié mais vous n'avez pas les droits pour supprimer un accès Game-Admin."
set pwf_reply(auth1) "14Vous n'êtes pas reconnu, peut-être avez vous modifié votre ident@host. Veuillez contacter un opérateur."
set pwf_reply(auth2) "14Vous êtes reconnu, mais vous n'avez pas d'accès Game-Admin. Veuillez contacter un opérateur."
set pwf_reply(auth3) "14Vous devez etre présent sur"
set pwf_reply(auth4) "pour vous authentifier."
set pwf_reply(auth5) "14Vous êtes déjà authentifié en tant que Game-Admin avec le nick"
set pwf_reply(auth6) "14Syntaxe : /msg $botnick admin <mot-de-passe>"
set pwf_reply(auth7) "4Vous n'avez encore aucun mot de passe défini.14 Tapez /msg $botnick pass <mot-de-passe>"
set pwf_reply(auth8) "14Vous êtes maintenant authentifié en tant que Game-Admin."
set pwf_reply(auth9) "4L'authentification a échouer ... essais logger !"
set pwf_reply(auth0) "14Votre authentification vous a été automatiquement retirée suite à une absence prolongée."
set pwf_reply(rehash) "4La base de donnée a été remise a zéro, veuillez nous excuser."
set pwf_reply(welcome) "Bienvenue"
set pwf_reply(topic1) "7Prêt pour le prochain match 1-2 Admin :"
set pwf_reply(topic2) "1-3"
set pwf_reply(topic4) "1-10 Type de match :"
set pwf_reply(topic5) "1- 14** MATCH VIP **"
set pwf_reply(teams1) "2 Actuellement :3"
set pwf_reply(separator) "1-"
set pwf_reply(defteama) "5Team A : 2"
set pwf_reply(defteamb) "5Team B : 2"
set pwf_reply(defteama2) "5Team A :2 ? - ? - ? - ? - ? -"
set pwf_reply(defteamb2) "5Team B :2 ? - ? - ? - ? - ?"
set pwf_reply(defmap) "Map non désignée pour l'instant"
set pwf_reply(misc1) "topicmem : impossible d'editer le fichier topic de"
} else {
  putlog "Error with language in pwf.tcl , please verify your config"
}
  set pwf_teamatopic [pwf_txt defteama2]
  set pwf_teambtopic [pwf_txt defteamb2]
  set pwf_maptopic [pwf_txt defmap]
}

if [info exists pwf_rehashmsg] {
  putserv "PRIVMSG $pwf_pickupchan :[pwf_txt rehash]"
} else {
  set pwf_rehashmsg 1
}

pwf_setnogame

putlog "Play With Friends by kalhimeo has successfully loaded"