#by wiebe @ QuakeNet
#http://www.tcl.tk/man/tcl8.4/TclCmd/expr.htm



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

proc c_calc:help:pub { nick uhost handle chan text } {
  lappend output "Usage: calc <calculation>"
  lappend output "For info on the 'expr' command in TCL, visit http://www.tcl.tk/man/tcl8.4/TclCmd/expr.htm"
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putloglev c $chan "[lindex [split $text] 1]: $nick $uhost $handle $chan [join [lrange [split $text] 2 end]]"
}



#help calc msg
bind msgm fvlomn|fvlomn "help calc" c_calc:help:msg

proc c_calc:help:msg { nick uhost handle text } {
  lappend output "Usage: calc <calculation>"
  lappend output "For info on the 'expr' command in TCL, visit http://www.tcl.tk/man/tcl8.4/TclCmd/expr.htm"
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putcmdlog "($nick!$uhost) !$handle! $text"
}



#calc pub
bind pubm fvlomn|fvlomn "% ${botnet-nick} calc" c_calc:pub
bind pubm fvlomn|fvlomn "% ${botnet-nick} calc *" c_calc:pub

proc c_calc:pub { nick uhost handle chan text } {
  set text2 [join [lrange [split $text] 2 end]]
  if { ![string equal $text2 ""] } {
    regsub -all "pi" $text2 "3.1415926535897932" math
    if {![regexp -all {\[} $text2] || ![regexp -all {\]} $text2]} {
      if { [catch {expr $math} error] } {
        lappend output $error
      } else {
        if { [string match *c* [lindex [split [getchanmode $chan]] 0]] } {
          set text2 [stripcodes c $text2]
        }
        puthelp "PRIVMSG $chan :$text2 = [expr $math]"
        return 0
      }
    } else { lappend output "Those chars are not allowed." } 
  } else { lappend output "usage: calc <calculation>" }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putloglev c $chan "[lindex [split $text] 1]: $nick $uhost $handle $chan [join [lrange [split $text] 2 end]]"
}




#calc msg
bind msg fvlomn|fvlomn calc c_calc:msg

proc c_calc:msg { nick uhost handle text } {
  set text2 $text
  if { ![string equal $text2 ""] } {
    regsub -all "pi" $text2 "3.1415926535897932" math
    if {![regexp -all {\[} $text2] || ![regexp -all {\]} $text2]} {
      if { [catch {expr $math} error] } {
        lappend output $error
      } else { lappend output "$text2 = [expr $math]" }
    } else { lappend output "Those chars are not allowed." } 
  } else { lappend output "Usage: calc <calculation>" }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}



#calc dcc
bind dcc fvlomn|fvlomn calc c_calc:dcc

proc c_calc:dcc { handle idx text } {
  set text2 $text
  if { ![string equal $text2 ""] } {
    regsub -all "pi" $text2 "3.1415926535897932" math
    if {![regexp -all {\[} $text2] || ![regexp -all {\]} $text2]} {
      if { [catch {expr $math} error] } {
        putidx $idx $error
      } else { putidx $idx "$text2 = [expr $math]" }
    } else { putidx $idx "Those chars are not allowed." } 
  } else { putidx $idx "Usage: calc <calculation>" }
  return 1
}

