#by wiebe @ QuakeNet
#add dcc cmd?



#lusers pub
#bind pub -|- !lusers lusers:pub

proc lusers:pub { nick uhost handle chan text } {
  global lusers
  set lusers(on) "1"
  set lusers(show,$chan) "1"
  puthelp "LUSERS"
  return 1
}



#lusers dcc
bind dcc -|- lusers lusers:dcc

proc lusers:dcc { handle idx text } {
  global lusers
  set lusers(on) "1"
  set lusers(show,$idx) "1"
  puthelp "LUSERS"
  return 1
}



#raw binds
bind raw -|- "251" lusers:raw
bind raw -|- "252" lusers:raw
bind raw -|- "253" lusers:raw
bind raw -|- "254" lusers:raw
bind raw -|- "255" lusers:raw
bind raw -|- "NOTICE" lusers:raw

proc lusers:raw { from numeric text } {
  global lusers
  if { [info exists lusers(on)] } {
  if { ![string equal $lusers(on) 1] } { return 0 }
    if { ![string equal [lsearch "251 252 253 254 255" $numeric] -1] } {
      set text [join [lrange [split $text] 1 end] ]
      if { [info exists lusers(info)] && [string equal $numeric 251] } {
        unset lusers(info)
      }
      if { ![info exists lusers(info)] } { set lusers(info) "LUSERS:" }
      if { [string equal $numeric 251] } {
#There are 89 users and 201315 invisible on 37 servers
        set text2 "[join [lrange [split [string trimleft $text :]] 0 1]]"
        set text2 "$text2 [lusers:comma [lindex [split $text] 2]]"
        set text2 "$text2 [join [lrange [split [string trimleft $text :]] 3 4]]"
        set text2 "$text2 [lusers:comma [lindex [split $text] 5]]"
        set text2 "$text2 [lindex [split $text] 6]"
        set text2 "$text2 ([lusers:comma [expr [lindex [split $text] 2] + [lindex [split $text] 5]]] total)"
        set text2 "$text2 [join [lrange [split $text] 7 end]]."
        set text $text2
      } elseif { [string equal $numeric 252] } {
#75 :operator(s) online
        set text2 "[lusers:comma [lindex [split $text] 0]]"
        set text2 "$text2 [string trimleft [join [lrange [split $text] 1 end]] :]."
        set text $text2
      } elseif { [string equal $numeric 253] } {
#1 :unknown connection(s)
        set text2 "[lusers:comma [lindex [split $text] 0]]"
        set text2 "$text2 [string trimleft [join [lrange [split $text] 1 end]] :]."
        set text $text2
      } elseif { [string equal $numeric 254] } {
#182942 :channels formed
        set text2 "[lusers:comma [lindex [split $text] 0]]"
        set text2 "$text2 [string trimleft [join [lrange [split $text] 1 end]] :]."
        set text $text2
      } elseif { [string equal $numeric 255] } {
#I have 25 clients and 1 servers
        set text2 "My server has"
        set text2 "$text2 [lusers:comma [lindex [split $text] 2]]"
        set text2 "$text2 [join [lrange [split $text] 3 4]]"
        set text2 "$text2 [lusers:comma [lindex [split $text] 5]]"
        set text2 "$text2 [join [lrange [split $text] 6 end]]."
        set text $text2
      }
      set lusers(info) "$lusers(info) $text"
    } elseif { [string equal $numeric NOTICE] } {
#Highest connection count: 35 (34 clients)
      set text "[join [lrange [split $text] 1 end]]"
      set text "[string trimleft $text :]."
      set text2 "[join [lrange [split $text] 0 2]]"
      set text2 "$text2 [lusers:comma [lindex [split $text] 3]]"
      set text2 "$text2 ([lusers:comma [string trimleft [lindex [split $text] 4] (]]"
      set text2 "$text2 [join [lrange [split $text] 5 end]]"
      set text $text2
      set lusers(info) "$lusers(info) $text"
      foreach name [array names lusers] {
        if { [string match show,* $name] } {
          set target [lindex [split $name ,] 1]
          if { [validchan $target] && [botonchan $target] } {
            puthelp "PRIVMSG $target :$lusers(info)"
          } elseif { [valididx $target] } {
            putidx $target "$lusers(info)"
          }
          unset lusers($name)
        }
      }
      unset lusers(info)
      unset lusers(on)
    }
  }
  return 0
}



#connect
bind evnt -|- ini-server lusers:connect

proc lusers:connect { type } {
  global lusers
  if { [info exists lusers] } {
    unset lusers
  }
}



#join
bind join -|- * lusers:join

proc lusers:join { nick uhost handle chan } {
  global lusers
  if { [isbotnick $nick] && [info exists lusers($chan)] } {
    unset lusers($chan)
  }
}



#convert a number to 123,456,789 etc
proc lusers:comma { number } {
  set x 2
  set y 0
  set r ""
  while { ![string equal [string range $number end-$x end-$y] ""] } {
    set r [linsert $r 0 [string range $number end-$x end-$y]]
    incr x 3
    incr y 3
  }
  set number [join $r ,]
  return $number
}

