####################################################
# by wiebe @ QuakeNet
#
# script provides access to ISUPPORT info given by the server on connect (numeric 005)
#
# usage: isupport [<setting>]
# returns -1 if settings does not exist
#
####################################################

####################################################
# numeric: 005    RPL_ISUPPORT
# format: <source> 005 <target> <list_of_features> :are supported by this server
# info: returned when connecting and when using the VERSION command
# info: features are either a word describing the feature eg: 'SILENCE', or a word describing the feature and an equals and a list of parameters. eg: SILENCE=15 (says that we support silence, and we support up to 15 of them per user), or FOO=12,3 (says we support FOO with parameters 12 and 3)
# example: irc.quakenet.org 005 woobie WHOX WALLCHOPS WALLVOICES USERIP CPRIVMSG CNOTICE SILENCE=15 MODES=6 MAXCHANNELS=20 MAXBANS=45 NICKLEN=15 TOPICLEN=250 AWAYLEN=160 KICKLEN=250 :are supported by this server
# example: irc.quakenet.org 005 woobie CHANTYPES=#& PREFIX=(ov)@+ CHANMODES=b,k,l,imnpstrDdcCNu CASEMAPPING=rfc1459 NETWORK=QuakeNet :are supported by this server
#
####################################################


####################################################
# isupport:raw
#
# delete info when the bot connects, numeric 001
# save info when the bot receives numeric 005
#
####################################################
bind raw -|- "001" isupport:raw; bind raw -|- "005" isupport:raw
proc isupport:raw { s n t } {
  global isupport max-bans modes-per-line use-354 rfc-compliant
  if {$n == 001} {
    if {[info exists isupport]} { unset isupport }
  } elseif {$n == 005} {
    set t [lrange [split $t] 1 end-5]
    foreach u $t {
      #if {![string equal [lsearch -exact ":are supported by this server" $u] -1]} { continue }
      set s [string toupper [lindex [split $u =] 0]]
      set v [join [lrange [split $u =] 1 end] =]
      if {$v == ""} { set v "yes" }
      set isupport($s) $v
# set some settings
      if {$s == "MAXBANS"} { set max-bans $v }
      if {$s == "MODES"} { set modes-per-line $v }
      if {$s == "WHOX"} { set use-354 1 }
      if {"$s $v" == "CASEMAPPING RFC1459"} { set rfc-compliant 1 }
    }
  }
}


####################################################
# isupport
#
# returns -1 if the requested setting does not exist
# without type, all settings are returned
####################################################
proc isupport { {type ""} } {
  global isupport; set t [string toupper $type]
  if {![info exists isupport]} { return -1
  } elseif {$t == ""} { return [array names isupport]
  } elseif {[info exists isupport($t)]} { return $isupport($t)
  } else { return -1 }
}


####################################################
# info for script.tcl
####################################################
set ::scriptdb(isupport) {
  "collects info from the ISUPPORT (005 numerics) from the IRC server and shares this with scripts, informing them of the IRC server's capabilities."
}

