gCalc - All-around calculator

Making use of Google's calculator function, this sBNC script will serve as an easy way to get various math-related solutions, see examples below. You might ask, “Why doesn't it include the other google functions?”. The answer is that I don't think things such as regular google results belong on IRC. You will probably want to open your browser to look use the results anyway. That said. A future release may be extended to include spell-checking and definition lookups.

I am aware that mIRC add-ons for this purpose already exist, but this would be a way of adding such functionality to all the users on your sBNC instance, no matter what client they are using.

Requirements

Software Version Details
sBNC (w/TCL module) 1.1 Developed and tested on 1.2-841, but should work on 1.1 (unconfirmed)

Download

Latest version: Build 1 (2006-10-29) .zip (1.1kB)
Previous versions: None

Install

  • Download
  • Unzip
  • Place gcalc.tcl in scripts/
    • Decide if you want the direct /gcalc command (edit gcalc.tcl)
  • Add source scripts/gcalc.tcl to sbnc.tcl
  • Rehash TCL
  • Use

Usage

Default syntax:

/sbnc calc <query>

Optional direct command:

/gcalc <query>

Example

Query Return
2 + 2 2 + 2 = 4
123 DKK in EUR 123 Danish kroner = 16.4993591 Euros
5 ounces in ml 5 US fluid ounces = 147.867648 ml
MMVI in decimal MMVI = 2 006

Complete list of features in Google calculator

Source

Note that this source will/should be the development version, i.e. all changes noted in the history section, will have been made in this section.

gcalc.tcl

#
# @name       gCalc - All-round calculator
# @version    Build 2 (????-??-??)
# @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
# @author     David Lorentsen <zyberdog@quakenet.org>
# @www        http://wiki.zyberdog.dk/sbnc:gcalc
#
 
if {[namespace exists gcalc]} {
	namespace delete gcalc
}
 
namespace eval gcalc {
 
	# Configuration
 
	# Do you wanta direct /gcalc command?
	# 1 = enabled, 0 = disabled
	variable directcmd 1
 
	# End of Configuration
 
	# HTTP package required (included with tcl8.4 and above)
	package require http
 
	# Binds
	internalbind command [namespace current]::commands
	if {[string equal $directcmd 1]} {
		internalbind client [namespace current]::direct
	} else {
		internalunbind client [namespace current]::direct
	}
 
	# Procedures
	proc commands {client parameters} {
		if {[string equal -nocase [lindex $parameters 0] "help"]} {
			bncaddcommand calc User "Use Google Calculator" "Syntax: calc <query>\nCalculate <query> using Google."
		} elseif {[string equal -nocase [lindex $parameters 0] "calc"]} {
			if {[llength $parameters] < 2} {
				bncreply "Syntax: /sbnc calc <query>"
			} else {
				fetch bncreply [lrange $parameters 1 end]
			}
 
			haltoutput
		}
	}
 
	proc direct {client parameters} {
		if {[string equal -nocase [lindex $parameters 0] "GCALC"]} {
			if {[llength $parameters] < 2} {
				bncrnotc "Syntax: /gcalc <query>"
			} else {
				fetch bncrnotc [lrange $parameters 1 end]
			}
 
			haltoutput
		}
	}
 
	proc fetch {replymethod query} {
		::http::config -useragent "sBNC/[lindex [bncversion] 0]"
		set url "http://www.google.com/search?[::http::formatQuery hl en q $query]"
		set http  [::http::geturl $url]
		set html  [::http::data $http]
		if {[regexp -nocase -- {images/calc_img.+?<b>(.+?)</b>} $html - results]} {
			$replymethod [render $results]
		} else {
			$replymethod "No results found. Bad query?"
		}
		::http::cleanup $http
		return
	}
 
	proc render {result} {
		set result [string map {"&#215;" "x"} $result]
		set result [regsub -all "<sup>(\[^<\]+)</sup>" $result {^\1}]
		set result [regsub -all "<\[^>\]+>" $result ""]
 
		return $result
	}
 
	proc unload {} {
		internalunbind command [namespace current]::commands
		internalunbind client [namespace current]::direct
		namespace delete [namespace current]
 
		return "gcalc.tcl unloaded"
	}
 
}

History

Build 2 (????-??-??)

Release date Notes
????-??-?? Only minor changes, no point in releasing yet..
  • Made use of TCL namespaces
  • Changed http user-agent to something less deceiving
  • Variable tweak
  • Strip html tag from result
  • Properly display certain characters, like ”× 10^66

Build 1 (2006-10-29)

Release date Notes
2006-10-29 First public release
  • No prior releases to compare with.
sbnc/gcalc.txt · Last modified: 2006/12/04 14:53 by zyberdog