1
0

calc.tcl 701 B

12345678910111213141516171819202122232425262728293031
  1. # created by fedex
  2. bind pub - !calc safe_calc
  3. bind pub - .calc safe_calc
  4. setudef flag calc
  5. proc is_op {str} {
  6. return [expr [lsearch {{ } . + - * / ( ) %} $str] != -1]
  7. }
  8. proc safe_calc {nick uhost hand chan str} {
  9. if {![channel get $chan calc]} { return }
  10. foreach char [split $str {}] {
  11. if {![is_op $char] && ![string is integer $char]} {
  12. putserv "PRIVMSG $chan :$nick: Invalid expression for calc."
  13. return
  14. }
  15. }
  16. # make all values floating point
  17. set str [regsub -all -- {((?:\d+)?\.?\d+)} $str {[expr {\1*1.0}]}]
  18. set str [subst $str]
  19. if {[catch {expr $str} out]} {
  20. putserv "PRIVMSG $chan :$nick: Invalid equation."
  21. return
  22. } else {
  23. putserv "PRIVMSG $chan :$str = $out"
  24. }
  25. }