####################################################
# by wiebe @ QuakeNet
#
####################################################

setudef flag unknown

####################################################
# chr:help:pubm
####################################################
bind pubm -|- "% ${botnet-nick} help chr" chr:help:pubm
proc chr:help:pubm { n u h c t } {
  if {![validchan $c]} { return 0 }
  if {[matchattr $h bkZ]} { return 0 }
  if {![matchattr $h fvlomn|fvlomn $c]} {
    if {![channel get $c unknown]} { return 0 }
    if {![botisop $c] && ![botishalfop $c]} { return 0 }
    if {![string equal [info procs matchignore] ""] && [matchignore $n!$u]} { return 0 }
    if {[matchban $n!$u $c]} { return 0 }
    if {[expr [clock seconds] - [getchanjoin $n $c]] < 30 && ![isvoice $n $c] && ![ishalfop $n $c] && ![isop $n $c]} { return 0 }
  }
  lappend o "chr: usage chr <number> \[<number2> .. <number20>\]"
  lappend o "chr: shows the character with the given ascii number"
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "chr: " } }
  putloglev c $c "help: $n $u $h $c chr"
  return 1
}


####################################################
# chr:help:msg
####################################################
bind msgm fvlomn|fvlomn "help chr" chr:help:msgm
proc chr:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "chr: usage chr <number> \[<number2> .. <number20>\]"
  lappend o "chr: shows the character with the given ascii number"
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "chr: " } }
  putcmdlog "($n!$u) !$h! help chr"
  return 1
}


####################################################
# chr:pub
####################################################
bind pubm -|- "% ${botnet-nick} chr" chr:pubm
bind pubm -|- "% ${botnet-nick} chr *" chr:pubm
proc chr:pubm { n u h c t } {
  if {![validchan $c]} { return 0 }
  if {[matchattr $h bkZ]} { return 0 }
  if {![matchattr $h fvlomn|fvlomn $c]} {
    if {![channel get $c unknown]} { return 0 }
    if {![botisop $c] && ![botishalfop $c]} { return 0 }
    if {![string equal [info procs matchignore] ""] && [matchignore $n!$u]} { return 0 }
    if {[matchban $n!$u $c]} { return 0 }
    if {[expr [clock seconds] - [getchanjoin $n $c]] < 30 && ![isvoice $n $c] && ![ishalfop $n $c] && ![isop $n $c]} { return 0 }
  }
  set t [join [lrange [split $t] 2 end]]
  if {[string equal $t ""]} {
    lappend o "chr: usage chr <number> \[<number2> .. <number20>\]"
  } else {
    set o [chr:c2a $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] }
      if {[string equal [info procs privmsg] ""]} { foreach l $o { puthelp "PRIVMSG $c :$l" }
      } else { foreach l $o { privmsg $c $l puthelp "chr: " } }
      set o ""
    }
  }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "chr: " } }
  putloglev c $c "chr: $n $u $h $c $t"
  return 1
}


####################################################
# chr:msg
####################################################
bind msg fvlomn|fvlomn chr chr:msg
proc chr:msg { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  if {[string equal $t ""]} { lappend o "chr: usage chr <number> \[<number2> .. <number20>\]"
  } else { set o [chr:c2a $t] }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "chr: " } }
  return 1
}


####################################################
# chr:dcc
####################################################
bind dcc -|- chr chr:dcc
proc chr:dcc { h i t } {
  if {[string equal $t ""]} {
    lappend o "chr: usage chr <number> \[<number2> .. <number20>\]"
    lappend o "chr: shows the character with the given ascii number"
  } else { set o [chr:c2a $t] }
  foreach l $o { putidx $i $l }
  return 1
}


####################################################
# chr:a2c
####################################################
proc chr:c2a { t } {
  set t [lrange [split $t] 0 19]; set r ""
  set a(0) "null"; set a(1) "start of heading (ctcp)"; set a(2) "start of text (bold, ctrl+b)"
  set a(3) "end of text (color, ctrl+k)"; set a(4) "end of transmission"; set a(5) "enquiry"
  set a(6) "acknowledge"; set a(7) "bell"; set a(8) "backspace"; set a(9) "horizontal tab"
  set a(10) "NL line feed, new line"; set a(11) "vertical tab"
  set a(12) "NP form feed, new page"; set a(13) "carriage return 13"; set a(14) "shift out"
  set a(15) "shift in (plain text, ctrl+o)"; set a(16) "data link escape"; set a(17) "device control 1"
  set a(18) "device control 2"; set a(19) "device control 3"; set a(20) "device control 4"
  set a(21) "negative acknowledge"; set a(22) "synchronous idle (reverse, ctrl+r)"
  set a(23) "end of trans. block"; set a(24) "cancel"; set a(25) "end of medium"
  set a(26) "substitute"; set a(27) "escape"; set a(28) "file separator"
  set a(29) "group separator"; set a(30) "record separator"
  set a(31) "unit separator (underline, ctrl+u)"; set a(32) "space"; set a(160) "hard space"
  foreach c $t {
    if {![regexp {^\d+$} $c] || $c > 255} { lappend r "$c=invalid"
    } elseif {[info exists a($c)]} { lappend r "$c=$a($c)"
    } else { lappend r "$c=[format %c $c]" }
  }
  lappend o "chr: [join $r "   "]"
  return $o
}


set scriptdb(chr) {
  "provides chr command for converting numbers into their ascii characters. +unknown (allows unknown users to use the command on the channel)."
}

