set alias(/slap) { plineact {slaps $arg with a large trout} }
set alias(gg) { plinemsg {good game $arg} }
set alias(/quit) { catch {disconnect} }
set alias(/rehash) { catch {rehash} }
set alias(/errorinfo) { alias:errorinfo }
set alias(/eval) { exec:tcl $arg }
set alias(/start) { startalias $arg } ;# See Below for startalias procedure
set alias(/install) { installscript $arg } ;# See Below for installscript procedure
set alias(/team) { teamalias {$arg} } ;# See Below for teamalias procedure
set alias(/ppm) { ppmalias {$arg} } ;# See Below for ppmalias procedure
set alias(/unteam) { unteam } ;# See below for unteam procedure
set alias(/dns) { lookup $arg } ;# See Below for lookup procedure

addbind pmsgout aliases

proc aliases {text} {
  global alias
  set cmd [lindex [split $text] 0]
  set arg [join [lrange [split $text] 1 end]]
  if {[info exists alias($cmd)]} {
    eval [string trim [subst $alias($cmd)]]
    return 1
  }
  return 0
}

proc startalias {{count 2}} {
  if {[inprogress]} { return }
  if {$count > 0} {
    if {$count > 10} {
      plinelog "Count too high. Defaulting to 10 Seconds."
      set count 10
    }
    plinemsg "$count"
    incr count -1
    after 1000 startalias $count
    return
  }
  if {[isserver]} {
    set mynick [setservernick]
  } else {
    set mynick [setnick]
  }
  if {[isop $mynick]} {
    startgame
  } else {
    plinemsg "/start"
  }
}

proc teamalias {team} {
  if {$team == ""} { 
    plinelog "Usage: /team <team>" 
    return
  } 
  if {[catch {setteam $team} err]} { 
    plinelog "Error: $err" 
  } 
}

proc ppmalias {slot} {
  if {($slot == "") || (![string is integer $slot]) || ($slot < 1) || ($slot > 6)} {
    plinelog "Usage: /ppm <slot>"
    return
  }
  plinelog "PPM for player [slot2nick $slot]: [ppm $slot]"
}

proc exec:tcl {args} {
  set arg [join $args]
  if {[catch {set res [eval [list uplevel #0 $arg]]} err]} {
    plinelog "Error: $err"
  } else {
    plinelog "$res"
  }
}

proc alias:errorinfo {} {
  global errorInfo
  plinelog "$errorInfo"
}

proc lookup {iphost} {
  set res [dns $iphost]
  if {($res == "") || ($res == $iphost)} {
    plinelog "Unable to resolve $iphost"
  } else {
    plinelog "Resolved $iphost to $res"
  }
}

proc installscript {script} {
  if {![file exists Scripts/$script]} {
    plinelog "$script was not found in the Scripts directory."
    plinelog "Please place $script in the Scripts directory, and try again."
    return
  }
  set outfile [open "Scripts/startup.tcl" a+]
  puts $outfile "source Scripts/$script"
  close $outfile
  plinelog "$script has been installed. Please use /rehash to reload scripts."
}

proc gg {} {
  set infile [open "quotes.txt" r]
  set data [split [read $infile] \n]
  close $infile
  set a [expr {round(rand() * [llength $data])}]
  set line [lindex $data $a]
  plinemsg "$line"
}

proc unteam {} {
  if {[catch {setteam ""} err]} {
    plinelog "Error: $err"
  }
}

describe "Demonstrates how to create simple alias commands from the text typed by the user in the partyline."

plinelog "aliases.tcl v1.8 by strikelight now loaded"
