# exchange.tcl v0.3.2 - a little tool for converting currency's
# Copyright (C) krikkit - krikkit@gmx.net
# --> http://www.krikkit.net/
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#
# my first simple tcl script for eggdrop 1.6.x which queries the xe.com webpage to get live 
# mid-market exchange rates of all common currency's
#
# Syntax: !e <value> <origin-currency> <wanted currency>
#         !elist - for a list of the most common supported currency's
#         !uptime - to view servers uptime...
#
# by krikkit - krikkit@gmx.net - http://www.krikkit.net
#
# Changelog
#
# 2002-08-16 v0.2 by krikkit
# 2002-08-19 v0.2.1 by krikkit  ...shorter output to public channel...
# 2002-10-17 v0.2.2 by krikkit  ...fixed exec security hole... see http://forum.egghelp.org/viewtopic.php?t=2273 for details...
# 2003-01-22 v0.2.3 by krikkit  ...fix, because of change in xe.com website
# 2003-02-01 v0.3.0 by krikkit  a little bit of code cleanup, added some configure options to the header
# 2004-04-27 v0.3.1 by krikkit  fixed the default hitline to avoid codepage problems with the (r) sign (thx to Micha for bugreport)
# 2005-07-03 v0.3.2 by krikkit just fixed the link to the moved currency page, no functional changes
#
# START CONFIG ===============================================================
# 
# path to lynx  (you MUST have installed lynx on your shell, otherwise
#		 the script won't work)
set lynx_path "/usr/bin/lynx"

# path to uptime executable
set uptime_path "/usr/bin/uptime"

# specify the trigger string
set mytrigger "!e"

# the line to search for in the output page
#set hitline "*xe.com  Universal Currency Converter ® Results*"
set hitline "*xe.com  Universal Currency Converter*"

# the three parts of the querysting
# example of the finals URL		
#		set myurl "http://www.xe.com/ucc/convert.cgi?Amount=10000&From=EUR&To=USD"
set querystring1 "http://www.xe.com/ucc/convert.cgi?Amount="
set querystring2 "&From="
set querystring3 "&To="

# show warning about obsolent currency's 1==show waring, 0== no warning
set show_obsolete_warning "0" 

# show live-string ( e.g.: Live [16]mid-market rates as of 2003.02.01 12:24:22 GMT. )
set show_live "1" 

# enables debug output
set show_debug "0"

# END CONFIG =================================================================


# do not change this...
set myversion "0.3.1"

# global myversion hitline querystring1 querystring2 querystring3 show_obsolete_warning mytrigger show_debug show_live lynx_path uptime_path


bind pub - $mytrigger pub:exch
bind msg - $mytrigger msg:exch
bind pub - !elist pub:elist
bind msg - !elist msg:elist
bind pub - !uptime pub:uptime
bind msg - !uptime msg:uptime


proc mk_inptcheck {txt} { 
  	global myversion hitline querystring1 querystring2 querystring3 show_obsolete_warning mytrigger show_debug show_live lynx_path uptime_path
foreach char {">" "<" "|" "&"} { 
    if [string match "*$char*" $txt] {return 1} 
  } 
  return 0 
} 


proc pub:exch { nick uhost handle channel arg } {
	global myversion hitline querystring1 querystring2 querystring3 show_obsolete_warning mytrigger show_debug show_live lynx_path uptime_path
	if {[llength $arg]!=3} {
		putserv "NOTICE $nick :Krikkit's Currency Converter $myversion "
		putserv "NOTICE $nick :Syntax: $mytrigger <value> <origin-currency> <wanted currency>"
		putserv "NOTICE $nick :example: $mytrigger 100 USD EUR"
		putserv "NOTICE $nick : "
		putserv "NOTICE $nick :for a list of the most common supported currency's try !elist"
		putserv "NOTICE $nick :for the full list of supported currency's please look an http://www.krikkit.net/currencys.html"
	} else {
		
		set ende "...so long and thanks for all the fish"


		  if {([mk_inptcheck [lindex $arg 0]] || [mk_inptcheck [lindex $arg 1]] || [mk_inptcheck [lindex $arg 2]])} { 
		    puthelp "NOTICE $nick :  TERMINATED: Your input contains illegal characters." 
		    puthelp "NOTICE $nick :  Unable to process '|', '<', '>' and '&'." 
		    puthelp "NOTICE $nick :  Please modify your searchterms and try again." 
		    return 
		  } 

		set query $querystring1
		append query [lindex $arg 0]
		append query $querystring2
		append query [lindex $arg 1]
		append query $querystring3
		append query [lindex $arg 2]
		
		if {$show_debug} {
			putserv "NOTICE $nick :querystring: $query"
		}
		
		set filetemp "exchange-script-temp"
		set fs [open $filetemp w]
		puts $fs [exec $lynx_path -preparsed -dump $query]
		close $fs

		set fs [open $filetemp r]
		
		set line ""
		set templine ""
		set muell ""
		
		gets $fs muell
		gets $fs muell
		if { ![string match "*The following error occurred:*" $muell] } {
		
		while {![string match $hitline $muell] && ![eof $fs]} {
			gets $fs muell
		}
		
		gets $fs line1
		gets $fs line2
		gets $fs line3
		gets $fs line4
		gets $fs line5
		gets $fs line6
		gets $fs line7

		close $fs
		
		if { [string match "*WARNING*" $line2] && [string match "*WARNING*" $line3] } {
			append line4 $line5
			append line4 $line6
			if {$show_live} {
				putserv "PRIVMSG $channel :$line1"
			}
			if {$show_obsolete_warning} {
				putserv "PRIVMSG $channel :$line2"
				putserv "PRIVMSG $channel :$line3"
			}
			putserv "PRIVMSG $channel :$line4"
			putserv "PRIVMSG $channel :$line7"

#			putserv "PRIVMSG $channel : case 2 warnings"
		}

		if { [string match "*WARNING*" $line2] && ![string match "*WARNING*" $line3] } {
			append line3 $line4
			append line3 $line5
			if {$show_live} {
				putserv "PRIVMSG $channel :$line1"
			}
			if {$show_obsolete_warning} {
				putserv "PRIVMSG $channel :$line2"
			}
			putserv "PRIVMSG $channel :$line3"
			putserv "PRIVMSG $channel :$line6"

#			putserv "PRIVMSG $channel : case nur 1 warning"
		}
		if { ![string match "*WARNING*" $line2] && ![string match "*WARNING*" $line3] } {
			append line2 $line3
			append line2 $line4
			if {$show_live} {
				putserv "PRIVMSG $channel :$line1"
			}
			putserv "PRIVMSG $channel :$line2"
			putserv "PRIVMSG $channel :$line5"
#			putserv "PRIVMSG $channel : case kein warning"
		}
		} else {
		putserv "PRIVMSG $channel :i don't know this currency... see http://www.krikkit.net/currencys.html or !elist for a full list of supported currency's"
		}
#		putserv "PRIVMSG $channel :$ende"
	}
}


proc msg:exch { nick uhost handle arg } {
	global myversion hitline querystring1 querystring2 querystring3 show_obsolete_warning mytrigger show_debug show_live lynx_path uptime_path
	if {[llength $arg]!=3} {
		putserv "NOTICE $nick :Krikkit's Currency Converter $myversion "
		putserv "NOTICE $nick :Syntax: $mytrigger <value> <origin-currency> <wanted currency>"
		putserv "NOTICE $nick :example: $mytrigger 100 USD EUR"
		putserv "NOTICE $nick : "
		putserv "NOTICE $nick :for a list of the most common supported currency's try !elist"
		putserv "NOTICE $nick :for the full list of supported currency's please look an http://www.krikkit.net/currencys.html"
	} else {
		
		set ende "...so long and thanks for all the fish"

		  if {([mk_inptcheck [lindex $arg 0]] || [mk_inptcheck [lindex $arg 1]] || [mk_inptcheck [lindex $arg 2]])} { 
		    puthelp "NOTICE $nick :  TERMINATED: Your input contains illegal characters." 
		    puthelp "NOTICE $nick :  Unable to process '|', '<', '>' and '&'." 
		    puthelp "NOTICE $nick :  Please modify your searchterms and try again." 
		    return 
		  } 


		set query $querystring1
		append query [lindex $arg 0]
		append query $querystring2
		append query [lindex $arg 1]
		append query $querystring3
		append query [lindex $arg 2]
	
		if {$show_debug} {
			putserv "NOTICE $nick :querystring: $query"
		}
		
		set filetemp "exchange-script-temp"
		set fs [open $filetemp w]
		puts $fs [exec $lynx_path -preparsed -dump $query]
		#puts $fs [exec $uptime_path]
		close $fs

		set fs [open $filetemp r]
		
		set line ""
		set templine ""
		set muell ""

		gets $fs muell
		gets $fs muell
		if { ![string match "*The following error occurred:*" $muell] } {
		
		while {![string match $hitline $muell] && ![eof $fs] } {
			gets $fs muell
		}
		
		gets $fs line1
		gets $fs line2
		gets $fs line3
		gets $fs line4
		gets $fs line5
		gets $fs line6
		gets $fs line7

		close $fs
		
		if { [string match "*WARNING*" $line2] && [string match "*WARNING*" $line3] } {
			append line4 $line5
			append line4 $line6
			if {$show_live} {
				putserv "NOTICE $nick :$line1"
			}
			if {$show_obsolete_warning} {
				putserv "NOTICE $nick :$line2"
				putserv "NOTICE $nick :$line3"
			}
			putserv "NOTICE $nick :$line4"
			putserv "NOTICE $nick :$line7"

#			putserv "NOTICE $nick : case 2 warnings"
		}

		if { [string match "*WARNING*" $line2] && ![string match "*WARNING*" $line3] } {
			append line3 $line4
			append line3 $line5
			if {$show_live} {
				putserv "NOTICE $nick :$line1"
			}
			if {$show_obsolete_warning} {
				putserv "NOTICE $nick :$line2"
			}
			putserv "NOTICE $nick :$line3"
			putserv "NOTICE $nick :$line6"
#			putserv "NOTICE $nick : case nur 1 warning"

		}
		if { ![string match "*WARNING*" $line2] && ![string match "*WARNING*" $line3] } {
			append line2 $line3
			append line2 $line4
			if {$show_live} {
				putserv "NOTICE $nick :$line1"
			}
			putserv "NOTICE $nick :$line2"
			putserv "NOTICE $nick :$line5"
#			putserv "NOTICE $nick : case kein warning"
		}
		} else {
		putserv "NOTICE $nick :i don't know this currency... see http://www.krikkit.net/currencys.html or !elist for a full list of supported currency's"
		}			
#		putserv "NOTICE $nick :$ende"
	}
}


proc pub:uptime { nick uhost handle channel arg } {
	global myversion hitline querystring1 querystring2 querystring3 show_obsolete_warning mytrigger show_debug show_live lynx_path uptime_path
	set ausgabe [exec $uptime_path]
	set ausgabemsg "Zaphod's current uptime is:"
	putserv "PRIVMSG $channel :$ausgabemsg"
	putserv "PRIVMSG $channel :$ausgabe"
}


proc msg:uptime { nick uhost handle arg } {
	global myversion hitline querystring1 querystring2 querystring3 show_obsolete_warning mytrigger show_debug show_live lynx_path uptime_path
	set ausgabe [exec $uptime_path]
	set ausgabemsg "Zaphod's current uptime is:"
	putserv "NOTICE $nick :$ausgabemsg"
	putserv "NOTICE $nick :$ausgabe"

}

proc pub:elist { nick uhost handle channel arg } {
	global myversion hitline querystring1 querystring2 querystring3 show_obsolete_warning mytrigger show_debug show_live lynx_path uptime_path
putserv "PRIVMSG $channel :listing the most common supported currency's:"
putserv "PRIVMSG $channel : "
putserv "PRIVMSG $channel :EUR Euro"
putserv "PRIVMSG $channel :USD United States Dollars"
putserv "PRIVMSG $channel :CAD Canada Dollars      "
putserv "PRIVMSG $channel :GBP United Kingdom Pounds"
putserv "PRIVMSG $channel :DEM Germany Deutsche Marks"
putserv "PRIVMSG $channel :FRF France Francs"
putserv "PRIVMSG $channel :JPY Japan Yen"
putserv "PRIVMSG $channel :ITL Italy Lire"
putserv "PRIVMSG $channel :CHF Switzerland Francs"
putserv "PRIVMSG $channel :ARS Argentina Pesos"
putserv "PRIVMSG $channel :AUD Australia Dollars"
putserv "PRIVMSG $channel :ATS Austria Schillings"
putserv "PRIVMSG $channel :CZK Czech Republic Koruny"
putserv "PRIVMSG $channel :DKK Denmark Kroner"
putserv "PRIVMSG $channel :FIM Finland Markkaa"
putserv "PRIVMSG $channel :FRF France Francs"
putserv "PRIVMSG $channel :DEM Germany Deutsche Marks"
putserv "PRIVMSG $channel :GRD Greece Drachmae"
putserv "PRIVMSG $channel :NLG Holland (Netherlands) Guilders"
putserv "PRIVMSG $channel :HUF Hungary Forint"
putserv "PRIVMSG $channel :ILS Israel New Shekels"
putserv "PRIVMSG $channel :JPY Japan Yen"
putserv "PRIVMSG $channel :RUR Russia Rubles"
putserv "PRIVMSG $channel :SEK Sweden Kronor"
}

proc msg:elist { nick uhost handle arg } {
	global myversion hitline querystring1 querystring2 querystring3 show_obsolete_warning mytrigger show_debug show_live lynx_path uptime_path
putserv "NOTICE $nick :listing the most common supported currency's:"
putserv "NOTICE $nick : "
putserv "NOTICE $nick :EUR Euro"
putserv "NOTICE $nick :USD United States Dollars"
putserv "NOTICE $nick :CAD Canada Dollars      "
putserv "NOTICE $nick :GBP United Kingdom Pounds"
putserv "NOTICE $nick :DEM Germany Deutsche Marks"
putserv "NOTICE $nick :FRF France Francs"
putserv "NOTICE $nick :JPY Japan Yen"
putserv "NOTICE $nick :ITL Italy Lire"
putserv "NOTICE $nick :CHF Switzerland Francs"
putserv "NOTICE $nick :ARS Argentina Pesos"
putserv "NOTICE $nick :AUD Australia Dollars"
putserv "NOTICE $nick :ATS Austria Schillings"
putserv "NOTICE $nick :CZK Czech Republic Koruny"
putserv "NOTICE $nick :DKK Denmark Kroner"
putserv "NOTICE $nick :FIM Finland Markkaa"
putserv "NOTICE $nick :FRF France Francs"
putserv "NOTICE $nick :DEM Germany Deutsche Marks"
putserv "NOTICE $nick :GRD Greece Drachmae"
putserv "NOTICE $nick :NLG Holland (Netherlands) Guilders"
putserv "NOTICE $nick :HUF Hungary Forint"
putserv "NOTICE $nick :ILS Israel New Shekels"
putserv "NOTICE $nick :JPY Japan Yen"
putserv "NOTICE $nick :RUR Russia Rubles"
putserv "NOTICE $nick :SEK Sweden Kronor"
}



putlog "Krikkit's Exchange Script $myversion - LOADED!"
