view calc.tcl @ 117:d40a0f3af7ab

urllog: Add some entities for conversion.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 13 Oct 2011 20:13:31 +0300
parents 593874678e45
children 88ce9376f22d
line wrap: on
line source

##########################################################################
#
# Calc v0.4 by Matti 'ccr' Hamalainen <ccr@tnsp.org>
# (C) Copyright 2008-2011 Tecnic Software productions (TNSP) 
#
# A simple calculator for Eggdrop.
#
# This script is freely distributable under GNU GPL (version 2) license.
#
##########################################################################

# Error message for when no arguments given to the command
set calc_err_noargs "Laalis?"

set calc_preferredmsg "PRIVMSG"


##########################################################################
# No need to look below this line
##########################################################################
set calc_message "Calc v0.4 by ccr/TNSP"

#set calc_reps [split "\$|| ||^|**|pi|3.14159265|e|2.71828183" "|"]
set calc_reps [split "\$||\;||^|**|pi|3.141592653589793238|PI|3.141592653589793238|e|2.71828183" "|"]

putlog "$calc_message"
bind pub - !laske calc_pubmsg
bind msg - laske calc_msg


# ------------------------------------------------------------------------
proc calc_smsg {udest umsg} {
  global calc_preferredmsg
  putserv "$calc_preferredmsg $udest :$umsg"
}

# ------------------------------------------------------------------------
proc calc_do {args} {
  global calc_reps

  # This is something rather perverse
  set equ [join [join [string map $calc_reps $args] ""] ""]

  # Use 'expr' to calculate the result, but catch any exceptions / errors
  if {[catch { set res [expr $equ] } errmsg]} {
    return $errmsg
  } else {
    return "$equ = $res"
  }
}

# ------------------------------------------------------------------------
proc calc_pubmsg {nick uhost hand chan args} {
  global calc_err_noargs

  if {$args == {} || $args == ""} {
    calc_smsg $chan "$calc_err_noargs"
    return 1
  }

  set result [calc_do $args]
  calc_smsg $chan "$nick, $result"
  return 1
}

# -------------------------------------------------------------------------  
proc calc_msg {nick uhost hand args} {
  global calc_err_noargs

  if {$args == {} || $args == ""} {
    calc_smsg $nick "$calc_err_noargs"
    return 1
  }
  
  set result [calc_do $args]
  calc_smsg $nick "$result"
  return 1
}

# -------------------------------------------------------------------------