#tcptests
#2003-03-03
#by Gh0stWriteR
#v0.83

#      ,,        ,,
#    ,MMMM,    ,MMMM,                  ,lll,    ,
#   ,MM'MMM   ,MMM'MM,                ll ,ll   iii
#   MM'  MM, ,MM   'MM               ,ll,lll    '            ''
#   MM   'MM,MM'    MM    ,ee,  rr,rrlllll'   ,ii,   ,nnnnnn,'' ,ss
#  ,MM    'MMM'     MM    e::e  rrr' 'll,    ,i'ii   n'nn''nn  ,ss'
# ,MM'              MM,   :ee:  rr'   'll,   ii ii, ,n n'  nn, ss:
#MMM'               'MM, ,e''e,,rr     'll, ,ii 'ii,nn n   'nn,'ssss,
#MM'                 'MMeee  eerrr      'lllii'  'iinn n    nnn  :sss
#                                                                sss'
#      ,R==( by Gh0stWriteR Mar'03 E-Mail: clankrogoth@gmx.net )==s'
#    ,RR'
# ,RRRRRRRRRR, 
# RRR'RR' 'RRR,
# RR  RR   'RRRR            [TCL EggdropScript]
# RR  RR    :RRR
# RR  RR  ,RRRR'
# RR  RR ,RRR'
# RR  RRRRR:
#,RR  RR 'RRR,   ,eee, vv,  ,vv ,eee, nnn,nnn,  ,gggggg  ,eee,
#RRR ,RR  'RRR   ee:ee 'vv  vv' ee:ee 'nnnn'nn  ggg' gg  ee:ee
#RR' RRR   'RR,  eeee'  vv,,vv  eeee'  'nn' nn  gg'  gg  eeee'
#RR,RRR'    RRR, ee:    'vvvv'  ee:     nn  nn, gg,  gg, ee:
#'RRRR'     'RRR 'eeee   'vv'   'eeee   nn  nnn 'ggg'ggg,'eeee .gggg
#                                                  ,gggggggggggggg'
#                                      ,ggggggggggggggggg
#                         ,ggggggggggggggg'        ,gg
#                   ,gggggggggggg'                ,ggg
#               gggggggg:                       ,gggg'  
#                'ggggggggg,                  ,gggg'
#                     'ggggggggg,       ,ggggggg'
#                            'ggggggggggggg'
#
#
#/////////////////////////////////////////////////////////////////
#// RADIO Script for Shoutcaserver V0.83 BETA                    //
#// Name: MR {Merlins Revenge}                                  //
#// Script by Gh0stWriteR                                       //
#// EMail: clankrogoth@gmx.net                                  //
#/////////////////////////////////////////////////////////////////
#
# -----------------------------------------------------DESCRIPTION
# this script connects directly to the radio stream on the 
# shoutcastserver and parses it... not more not less. a timout
# will handle the case if no title information where send with
# the stream. notice that due to the TCP handling in TCL the bot
# will HANG while reading this stream. That means:
# 
# DONT USE THIS SCRIPT ON PROTECTION BOTS OR OTHER CHANNEL
# CRITICAL BOTS
#
# while the bot listens to the stream no commands will accepted
# nor any event handling - in normal cases its not critical - but
# i write it down here ... 
#
# ----------------------------------------------------------GREETS
# fly to (alphabetically order):
# Andohr, D3f, Deelen, Igor, Magic, Maxix, MOB, Naikos,  
# Purple, SF, Stage, Zweistein ..... and many more
#
# very special Thanks fly to:
# Thermi - for Shellsupply, 
# ZeiRAM - for helpin me out all the time and high-level 
#          knowledge
# Crazyman - for splatting Naikos in Quake :-)
# L0RE - for special coolness and high-level knowledge
# Hippo - for his Angel Bot - great work
#
#
# ----------------------------------------------------------TESTED
# Tested on Windows Eggdrop too (W2k SP3) 1.6.7
#
# ------------------------------------------------------------TODO
# - layout changes
#
#
# ---------------------------------------------------------HISTORY
# 04/03/2003
# fix hang bug while listen to windows shoutcast servers
# adding multi channel support - with notice bugfix
# adding ICQ IRC HP STREAMURL AIM output on join
# 03/03/2003
# rewrite the script - now it parses the stream for informations
# this was done due to heavy requests from ppl all over the world
# 10/01/1999
# wrong targetchan onjoin notice bug detected and fixed
# minimize DNS lookups
# fix IP lookup - bug 
# V0.7 09/30/1999
# DNS Lookup added
# comparing source IP of the shoutcast server with the user in
#     the targetchan added
# no DNS host - bug detected
# no moderator on chan - server online bug detected and fixed
# 09/28/1999
# fixed logging (keep logspace)
# C - song bug detected - possible bug of the trim procedures
# too long Song names bug detected and fixed
# too long Server title bug detected and fixed
# uppercase public cmds bug fixed
# 09/26/1999
# Infos added
# V0.6 09/24/1999
# output design changed
# V0.5 09/24/1999
# merge public, join and main script into one radioscript
# V0.4 09/23/1999
# Public cmds added
# V0.3 09/22/1999
# user join information added
# V0.2 09/21/1999
# optimized for lynx - shell script added (shradio)
# V0.1 09/20/1999
# early buggy script (analyses orginal HTML code - but try to use 
# wget failed with shoutcast)


#=========>> NOTICE !! this is STILL a BETA Version <<============
#==>       so if u are not familiar with TCL EMail me          <==



#========SETTINGS========================================
#time the script will be executed (default every minute)
#  make sure that min h d month have 2 and year 4 chars
#  or a *
#                     ,--------- Minute
#                     |  ,------- Hour
#                     |  |  ,----- Day
#                     |  |  |  ,--- Month
#                     |  |  |  |  ,- Year
#                     V  V  V  V  V
set gst_scs_exectime "* * * * *"

# channel where the msgs will be posted ("#first #second ...")
set gst_scs_channels "#byte7f.de #byte7f"

# server the bot will connect on (the shoutcast radioserver)
set gst_scs_server "64.236.34.141"

# specifies the tcp port where the bot will connect on
set gst_scs_serverport "80"

# specifies a subdirectory at the shoutcast webserver (if nothing then put a / there)
set gst_scs_serversubdir "/stream/1020"

# disables log output
set gst_scs_silencescript "0"

# timeout limit after which the bot disconnects from the stream - dont set it too high
# - dunno why but the bot will hang =)
set gst_scs_limit "2500"


#========SCRIPT==========================================
#=            DO NOT EDIT BELOW THIS LINE !!!           =
#=               OR MALFUNCTION MAY CAUSE               =
#========================================================
set gst_scs_servertitle "-"
set gst_scs_servergenre "-"
set gst_scs_serverurl "-"
set gst_scs_serverhp "-"
set gst_scs_serveraim "-"
set gst_scs_servericq "-"
set gst_scs_serverirc ""
set gst_scs_oldtitle ""

bind time - "$gst_scs_exectime" gst_scs_serverconnect
bind join - * gst_scs_userjoin


proc gst_scs_userjoin {nick uhost handle chan} {
	global gst_scs_server
	global gst_scs_serverport
	global gst_scs_serversubdir
	global gst_scs_servertitle
	global gst_scs_channels
	global gst_scs_servergenre
	global gst_scs_serverhp
	global gst_scs_serverurl
	global gst_scs_serverirc
	global gst_scs_servericq
	global gst_scs_serveraim
	
	
	foreach c $gst_scs_channels {
		if {$chan == $c} {
			
			# edit here to change the layout of the on join greet
			putserv "NOTICE $nick : Current Server: $gst_scs_servertitle"
			putserv "NOTICE $nick : StreamURL: http://$gst_scs_server:$gst_scs_serverport$gst_scs_serversubdir"
			putserv "NOTICE $nick : Genre: $gst_scs_servergenre HP: $gst_scs_serverhp IRC: $gst_scs_serverirc ICQ: $gst_scs_servericq AIM: $gst_scs_serveraim"
			# stop editing here :-)
		}
	}
}


proc gst_scs_serverconnect {nick uhost hand chan text} {
	global gst_scs_server
	global gst_scs_serverport
	global gst_scs_serversubdir
	global gst_scs_oldtitle
	global gst_scs_channels
	global gst_scs_silencescript
	global gst_scs_servertitle
	global gst_scs_servergenre
	global gst_scs_serverurl
	global gst_scs_serverhp
	global gst_scs_servericq
	global gst_scs_serveraim
	global gst_scs_serverirc gst_scs_limit
	
	
	if {$gst_scs_silencescript == "0"} {putlog "GSTSCS: try connection to http://$gst_scs_server:$gst_scs_serverport$gst_scs_serversubdir"}
	
	
	if {[catch {set sock [socket $gst_scs_server $gst_scs_serverport] } sockerror]} {
		putlog "GSTSCS: Could not connect: $sockerror"
		return
	}
	if {$gst_scs_silencescript == "0"} {putlog "GSTSCS: sucessfull"}
	
	
	#test 
	#fconfigure $sock -translation platform -buffering none -blocking 1
	fconfigure $sock -buffersize 80 -blocking 1
	
	puts $sock "GET $gst_scs_serversubdir HTTP/1.0"
	puts $sock "Host:$gst_scs_server"
	puts $sock "ACCEPT:*/*"
	puts $sock "User-Agent:RMA/1.0 (compatible; RealMedia)"
	puts $sock "Icy-Metadata:1"
#	puts $sock "Connection: Keep-Alive"
	puts $sock ""
	flush $sock
	
	set connected 0
	#do until socket closed or procedure returns or timeout occurs
	set counter 0
	while {[eof $sock] != 1} {
		set rc [gets $sock]
	
		
		# check the server response
		if {$connected == 0} {
			if {[lindex $rc 0] != "ICY"} {
				putlog $rc
				putlog "GSTSCS: no Winamp-Shoutcast Stream"
				close $sock
				return
			} else {
				if {[lindex $rc 1] != "200"} {
					putlog $rc
					putlog "GSTSCS: Winamp-Shoutcast Stream sends not a 200 OK Code"
					close $sock
					return
				}
				set connected 1
			}
		}
		
		if [string match "*icy*" $rc] {
			if [string match "*icy-name:*" $rc] {
				set gst_scs_servertitle [string range $rc [expr [string first ":" $rc]+1] end]
			}
			if [string match "*icy-genre:*" $rc] {
				set gst_scs_servergenre [string range $rc [expr [string first ":" $rc]+1] end]
			}
			if [string match "*icy-irc:*" $rc] {
				set gst_scs_serverirc [string range $rc [expr [string first ":" $rc]+1] end]
			}
			if [string match "*icy-url:*" $rc] {
				set gst_scs_serverhp [string range $rc [expr [string first ":" $rc]+1] end]
			}
			if [string match "*icy-icq:*" $rc] {
				set gst_scs_servericq [string range $rc [expr [string first ":" $rc]+1] end]
			}
			if [string match "*icy-aim:*" $rc] {
				set gst_scs_serveraim [string range $rc [expr [string first ":" $rc]+1] end]
			}
		}
	
		
		
		
		# checks the stream after the bot is connected with and parse for stream title
		if [string match "*StreamTitle*" $rc] {
			if {$gst_scs_silencescript == "0"} {putlog "GSTSCS: Title found"}
			set temp [string range $rc [expr [string first "StreamTitle='" $rc]+13] end]
			set temp2 [string range $temp 0 [expr [string first "'" $temp]-1]]
			
			if {[string match $temp2 $gst_scs_oldtitle]} {
				
			} else {
				foreach c $gst_scs_channels {

					# edit here to change the layout of the on join greet
					putserv "PRIVMSG $c : Current Title: $temp2"
					# stop editing here =)

				}
				set gst_scs_oldtitle $temp2
			}
			close $sock
			return
		}
		

		# timeout handling
		incr counter
		if {$counter >= $gst_scs_limit} {
			putlog "GSTSCS: TIMEOUT FORCED !!! - SORRY NO TITLE INFORMATION FOUND IN THE STREAM"
			close $sock; return 0
		}

	}
	

}




putlog "--==-- SHOUTCAST SCRIPT 0.83 BETA by \002Gh0stWriteR\002 loaded"