# math v1.0
# math.tcl http://fury.fi/~joep/eggdrop-scripts.php
#
# Simple math script
#
# Usage : !math <math>
#
# Example1: !math 1+1
# Example2: !math 3043*(202+34)
#
# Uses bc (must be installed). For exact usage read bc man page.
#
# Feel free to use & modify, but please keep the info about
# original author
#
#   joep@fury.fi
#
#############################################################################

#############################################################################
#
# Script configuration
#

# IRC Channels, where bot is allowed to show math results.
# Use only channels where the bot is on.
#
# Leave option "allowed_channels_math" empty (default) if you want this
# to work automatically on every bot's channel.
#
# Example1. allow only channels #pastilli ja #peelotus
# set allowed_channels_math { pastilli peelotus }
# set denied_channels_math { }
#
# Example2: deny channels #palle ja #tumplaus. allow everything else
# set allowed_channels_math { }
# set denied_channels_math { palle tumplaus }
#
# Default options allow all channels.
# Option denied_channels_math doesn't affect on MSG queries.
#
set allowed_channels_math { }
set denied_channels_math { }

# Do we require that user is on one of the allowed channels to work with MSG
# 1 = yes (default)
# 0 = no (everyone on IRC can make math queries!)
set required_on_channel_math 1

# math command
set math_command "laske"

# log all MSG queries ?
# 1 = yes (default)
# 0 = no
set log_queries_math 0

############################################################################
#                                                                          #
# Don't change anything below                                              #
#                                                                          #
############################################################################

# bindings
bind pub - !$math_command pub_math
bind msg - $math_command msg_math
bind msg - !$math_command msg_math

proc msg_math {nick uhost hand lause} {
global math_command allowed_channels_math required_on_channel_math log_queries_math
	set on_channel 0
	if { [llength $allowed_channels_math] == 0 } {
			# tutkitaan onko kyselijä samalla kanavalla kuin botti
			foreach kanava [channels] {
				if { [onchan $nick "$kanava"] } { set on_channel 1 }
				}
		} else {
			# katsotaan sallituiden kanavien perusteella
			foreach idx $allowed_channels_math {
				if { [onchan $nick "#$idx"] } { set on_channel 1 }
				}
		}
	if { $required_on_channel_math == 0 } { set on_channel 1 }
	if { $on_channel } {
		if { $log_queries_math == 1 } {
			putlog "$nick ($uhost) $math_command $lause"
			}
		set floodi [exec echo $lause | bc -l]
		putserv "PRIVMSG $nick :$lause = $floodi"
		}
}

proc pub_math {nick uhost hand chan lause} {
global allowed_channels_math denied_channels_math
	if { ([lsearch -exact $denied_channels_math [string trimleft [string tolower $chan] "#"]] != -1) } {
		return 0
		}
	set channel [string trimleft [strlwr $chan] "#"]
	if { ([lsearch -exact $allowed_channels_math $channel] != -1) || ([llength $allowed_channels_math] == 0) } {
		set floodi [exec echo $lause | bc -l]
		putserv "PRIVMSG $chan :$lause = $floodi"
		}
}

putlog "\002math v1.0\002 by joep@fury.fi"

