####################################################
# by wiebe @ QuakeNet
#
# script provides asc command to convert a character to its ascii value
# users with +f and higher (chan or global) can use the command
# on the partyline any user can
#
#   in channel:   botnet-nick asc <char> [<char> .. <char>]
#   in private:   /msg bot asc <char> [<char> .. <char>]
#   on partyline: asc <char> [<char> .. <char>]
#
# channel flags
# +unknown  allows anyone on the channel to use the asc command
#
# script can use: irc.tcl matchignore.tcl script.tcl
#
####################################################

setudef flag unknown

####################################################
# asc:help:pubm
####################################################
bind pubm -|- "% ${::botnet-nick} help asc" asc:help:pubm
proc asc:help:pubm { n u h c t } {
  if {![validchan $c]} { return 0 }
  if {[matchattr $h bkZ|k $c]} { return 0 }
  if {![matchattr $h fvlomn|fvlomn $c] && ![asc:unknown $n $u $c]} { return 0 }
  lappend o "asc: usage asc <char> \[<char> .. <char>\]"
  lappend o "asc: shows the ascii number of the given characters, special entries are: ack bell bold bs can color cr crlf ctcp dc1 dc2 dc3 dc4 dle em end enq enter eot esc etb etx ff fs gs hardspace lf nak null reverse rs si so soh space stx sub syn tab underline us vt"
  asc:cnotice $n $o
  putloglev c $c "help: $n $u $h $c asc"
  return 1
}


####################################################
# asc:help:msgm
####################################################
bind msgm fvlomn|fvlomn "help asc" asc:help:msgm
proc asc:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "asc: usage asc <char> \[<char> .. <char>\]"
  lappend o "asc: shows the ascii number of the given characters, special entries are: ack bell bold bs can color cr crlf ctcp dc1 dc2 dc3 dc4 dle em end enq enter eot esc etb etx ff fs gs hardspace lf nak null reverse rs si so soh space stx sub syn tab underline us vt"
  asc:cnotice $n $o
  putcmdlog "($n!$u) !$h! help asc"
  return 1
}


####################################################
# asc:pubm
####################################################
bind pubm -|- "% ${::botnet-nick} asc" asc:pubm
bind pubm -|- "% ${::botnet-nick} asc *" asc:pubm
proc asc:pubm { n u h c t } {
  if {![validchan $c]} { return 0 }
  if {[matchattr $h bkZ|k $c]} { return 0 }
  if {![matchattr $h fvlomn|fvlomn $c] && ![asc:unknown $n $u $c]} { return 0 }
  set t [join [lrange [split $t] 2 end]]
  if {$t == ""} {
    lappend o "asc: usage asc <char> \[<char> .. <char>\]"
  } else {
    set o [asc:a2c $t]; set m [lindex [split [getchanmode $c]] 0]
    if {![string match *m* $m] || [botisop $c] || [botishalfop $c] || [botisvoice $c]} {
      if {[string match *c* $m] } { set o [stripcodes bcru $o] }
      asc:privmsg $c $o
      set o ""
    }
  }
  asc:cnotice $n $o
  putloglev c $c "asc: $n $u $h $c $t"
  return 1
}


####################################################
# asc:unknown
####################################################
proc asc:unknown { n u c } {
  if {![botisop $c] && ![botishalfop $c]} { return 0 }
  if {![channel get $c unknown]} { return 0 }
  if {[info procs matchignore] != "" && [matchignore $n!$u]} { return 0 }
  if {[matchban $n!$u $c]} { return 0 }
  if {[isvoice $n $c] || [ishalfop $n $c] || [isop $n $c]} { return 1 }
  if {[expr [clock seconds] - [getchanjoin $n $c]] < 30} { return 0 }
  return 1
}


####################################################
# asc:msg
####################################################
bind msg fvlomn|fvlomn asc asc:msg
proc asc:msg { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  if {$t == ""} { lappend o "asc: usage asc <char> \[<char> .. <char>\]"
  } else { set o [asc:a2c $t] }
  asc:cnotice $n $o
  return 1
}


####################################################
# asc:dcc
####################################################
bind dcc -|- asc asc:dcc
proc asc:dcc { h i t } {
  if {![valididx $i]} { return 0 }
  if {$t == ""} {
    lappend o "asc: usage asc <char> \[<char> .. <char>\]"
    lappend o "asc: shows the ascii number of the given characters, special entries are: ack bell bold bs can color cr crlf ctcp dc1 dc2 dc3 dc4 dle em end enq enter eot esc etb etx ff fs gs hardspace lf nak null reverse rs si so soh space stx sub syn tab underline us vt"
  } else { set o [asc:a2c $t] }
  foreach l $o { putidx $i $l }
  return 1
}


####################################################
# asc:a2c
####################################################
proc asc:a2c { t } {
  lappend o "asc:"; set a(null) "0"; set a(soh) "start of heading 1"; set a(ctcp) "1"
  set a(stx) "start of text 2"; set a(bold) "2"; set a(etx) "end of text 3"; set a(color) "3"
  set a(eot) "end of transmission 4"; set a(enq) "enquiry 5"; set a(ack) "acknowledge 6"
  set a(bell) "7"; set a(bs) "backspace 8"; set a(tab) "horizontal tab 9"
  set a(lf) "NL line feed, new line 10"; set a(vt) "vertical tab 11"
  set a(ff) "NP form feed, new page 12"; set a(cr) "carriage return 13"
  set a(crlf) "crlf is cr ($a(cr)) & lf ($a(lf))"; set a(enter) "$a(crlf)"; set a(so) "shift out 14"
  set a(si) "shift in 15"; set a(end) "15"; set a(dle) "data link escape 16"
  set a(dc1) "device control 1 17"; set a(dc2) "device control 2 18"
  set a(dc3) "device control 3 19"; set a(dc4) "device control 4 20"
  set a(nak) "negative acknowledge 21"; set a(syn) "synchronous idle 22"
  set a(reverse) "22"; set a(etb) "end of trans. block 23"; set a(can) "cancel 24"
  set a(em) "end of medium 25"; set a(sub) "substitute 26"; set a(esc) "escape 27"
  set a(fs) "file separator 28"; set a(gs) "group separator 29"; set a(rs) "record separator 30"
  set a(us) "unit separator 31"; set a(underline) "31"; set a(space) "32 or 160 for hard space"
  set a(hardspace) "160"
  foreach c [split $t] {
    if {[info exists a([string tolower $c])]} { set c [list $c] } else { set c [split $c ""] }
    foreach d $c {
      if {[string length [join $o "\017   "]] > 300} { break; break }
      if {[info exists a([string tolower $d])]} { set r "$d=$a([string tolower $d])" } else { set r "$d=[scan $d %c]" }
      #if {[lsearch -exact $o $r] == -1} { lappend o $r }
      lappend o $r
    }
  }
  return [list [join $o "\017   "]]
}


####################################################
# asc:cnotice
####################################################
proc asc:cnotice { n o } {
  if {[info procs cnotice] == ""} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "asc: " } }
}


####################################################
# asc:privmsg
####################################################
proc asc:privmsg { c o } {
  if {[info procs privmsg] == ""} { foreach l $o { puthelp "PRIVMSG $c :$l" }
  } else { foreach l $o { privmsg $c $l puthelp "asc: " } }
}


####################################################
# set info for script.tcl
####################################################
set ::scriptdb(asc) {
  "Provides asc command for converting characters to their ascii value.  Adds chan flag +unknown (allows unknown users to use the command on the channel)."
}

