#by wiebe @ QuakeNet



#help asc pub
bind pubm fvlomn|fvlomn "% ${botnet-nick} help asc" c_asc:help:pub

proc c_asc:help:pub { nick uhost handle chan text } {
  c_asc:pub $nick $uhost $handle $chan ""
  putloglev c $chan "[lindex [split $text] 1]: $nick $uhost $handle $chan [join [lrange [split $text] 2 end]]"
}



#help asc msg
bind msgm fvlomn|fvlomn "help asc" c_asc:help:msg

proc c_asc:help:msg { nick uhost handle text } {
  c_asc:msg $nick $uhost $handle ""
  putcmdlog "($nick!$uhost) !$handle! $text"
}



#asc pub
bind pubm fvlomn|fvlomn "% ${botnet-nick} asc" c_asc:pub
bind pubm fvlomn|fvlomn "% ${botnet-nick} asc *" c_asc:pub

proc c_asc:pub { nick uhost handle chan text } {
  set ascs [join [lrange [split $text] 2 11]]
  set result ""
  foreach asc [split $ascs] {
    set r [c_asc:asc2chr $asc]
    if { [string equal $r ""] } { set r invalid }
    lappend result "  $asc = $r  "
  }
  if { [string equal $result ""] } {
    lappend output "Usage: asc <char1> \[<char2> .. <char10>\] (special entries are: \$null ctcp bold color tab lf cr crlf enter end reverse underline space)"
    if { [catch {set x [cnotice $nick $output]} error] || !$x } {
      foreach t $output { puthelp "NOTICE $nick :$t" }
    }
  } else {
    puthelp "PRIVMSG $chan :ASC for: [join $result]"
  }
  putloglev c $chan "[lindex [split $text] 1]: $nick $uhost $handle $chan [join [lrange [split $text] 2 end]]" 
}



#asc msg
bind msg fvlomn|fvlomn asc c_asc:msg

proc c_asc:msg { nick uhost handle text } {
  set ascs [join [lrange [split $text] 0 9]]
  set result ""
  foreach asc [split $ascs] {
    set r [c_asc:asc2chr $asc]
    if { [string equal $r ""] } { set r invalid }
    lappend result "  $asc = $r  "
  }
  if { [string equal $result ""] } {
    lappend output "Usage: asc <char1> \[<char2> .. <char10>\] (special entries are: \$null ctcp bold color tab lf cr crlf enter end reverse underline space)"
  } else {
    lappend output "ASC for: [join $result]"
  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}



#asc dcc
bind dcc -|- asc c_asc:dcc

proc c_asc:dcc { handle idx text } {
  set ascs [join [lrange [split $text] 0 9]]
  set result ""
  foreach asc [split $ascs] {
    set r [c_asc:asc2chr $asc]
    if { [string equal $r ""] } { set r invalid }
    lappend result "  $asc = $r  "
  }
  if { [string equal $result ""] } {
    putidx $idx "Usage: asc <char1> \[<char2> .. <char10>\] (special entries are: \$null ctcp bold color tab lf cr crlf enter end reverse underline space)"
  } else {
    putidx $idx "ASC for: [join $result]"
  }
  return 1
}



#proc to go from asc to chr
proc c_asc:asc2chr { asc } {
  if { [string equal $asc ""] } { return }
  set _asc(\$null) "0"
  set _asc(ctcp) "1"
  set _asc(bold) "2"
  set _asc(color) "3"
  set _asc(tab) "9"
  set _asc(lf) "10"
  set _asc(cr) "13"
  set _asc(crlf) "13 and 10"
  set _asc(enter) "13 and 10"
  set _asc(end) "15"
  set _asc(reverse) "22"
  set _asc(underline) "31"
  set _asc(space) "32 or 160 for hard space"
  if { [string length $asc] > "1" && ![info exists _asc($asc)] } { return }
  if { [info exists _asc($asc)] } { set result $_asc($asc) } else { set result [scan $asc %c] }
  return $result
}

