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

# http://www.tcl.tk/man/tcl8.4/TclCmd/expr.htm

####################################################
# calc:help:pubm
####################################################
bind pubm fvlomn|fvlomn "% ${botnet-nick} help calc" calc:help:pubm
proc calc:help:pubm { n u h c t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "calc: usage calc <calculation>"
  lappend o "calc: for info on the 'expr' command in TCL, visit http://www.tcl.tk/man/tcl8.4/TclCmd/expr.htm"
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "calc: " } }
  putloglev c $c "help: $n $u $h $c calc"
  return 1
}


####################################################
# calc:help:msgm
####################################################
bind msgm fvlomn|fvlomn "help calc" calc:help:msgm
proc calc:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "calc: usage calc <calculation>"
  lappend o "calc: for info on the 'expr' command in TCL, visit http://www.tcl.tk/man/tcl8.4/TclCmd/expr.htm"
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "calc: " } }
  putcmdlog "($n!$u) !$h! help calc"
  return 1
}


####################################################
# calc:pubm
####################################################
bind pubm fvlomn|fvlomn "% ${botnet-nick} calc" calc:pubm
bind pubm fvlomn|fvlomn "% ${botnet-nick} calc *" calc:pubm
proc calc:pubm { n u h c t } {
  if {[matchattr $h bkZ]} { return 0 }
  set t [join [lrange [split $t] 2 end]]
  if {[string equal $t ""]} {
    lappend o "calc: usage calc <calculation>"
  } else {
    set o [calc:calc $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 "calc: " } }
      set o ""
    }
  }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "calc: " } }
  putloglev c $c "calc: $n $u $h $c $t"
  return 1
}


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


####################################################
# calc:dcc
####################################################
bind dcc -|- calc calc:dcc
proc calc:dcc { h i t } {
  if {[string equal $t ""]} {
    lappend o "calc: usage calc <calculation>"
    lappend o "calc: for info on the 'expr' command in TCL, visit http://www.tcl.tk/man/tcl8.4/TclCmd/expr.htm"
  } else { set o [calc:calc $t] }
  foreach l $o { putidx $i $l }
  return 1
}


####################################################
# calc:calc
####################################################
proc calc:calc { t } {
  regsub -all "pi" $t "3.1415926535897932" u
  if {[regexp {\[|\]|\$} $t]} { lappend o "calc: invalid chars"
  } elseif {[catch {set u [expr $u]} e]} { lappend o "calc: $e"
  } else { lappend o "calc: $t = $u" }
  return $o
}


set scriptdb(calc) {
  "provides calc command for calculations"
}

