Posted to tcl by Simbaa at Tue Oct 17 17:11:53 GMT 2017view pretty

###############################################################################################################################
#                                                                                                                             # 
# LoReZ ShoutCast Script v2.1 (sc.tcl)                                                                                        #
# Email: lorez@bigvibez.com                                                                                                   #
# IRC: #bigvibez @ Quakenet                                                                                                   #
#                                                                                                                             #
# Please contact me if you notice any bugs                                                                                    #
#                                                                                                                             #
# Installation: Put the script and the config file into your scripts directory, add "source scripts/sc.tcl" to your eggdrop   #
#               config and rehash the bot. (http package is REQUIRED for this script to work)                                 #                                                                                              #
#                                                                                                                             #
###############################################################################################################################

###############################################################################################################################
#                                                          START OF TCL CODE                                                  #
###############################################################################################################################

package require http
namespace eval sc {
    variable vars
    variable sets
    variable triggers
    variable texts
    variable offadv 0
    source scripts/sc.cfg
    proc init {} {
	set start [clock clicks]
	set sc::offadv 0
	bind time - "* * * * *" sc::check
	bind nick - * sc::nick
	foreach x [array names sc::triggers] {
	    if {$sc::sets(pub) == 1} {
		bind pub [expr {($x == "kick") || ($x == "setdj") || ($x == "unsetdj") ? "$sc::vars(flag)" : "-"}] $sc::vars(trigger)$sc::triggers($x) "sc::pub $x"
	    }
	    if {$sc::sets(msg) == 1} {
		bind msg [expr {($x == "kick") || ($x == "setdj") || ($x == "unsetdj") ? "$sc::vars(flag)" : "-"}] $sc::vars(trigger)$sc::triggers($x) "sc::msg $x"
	    }
	}
	if {$sc::sets(adv) == 1} {
	    timer $sc::sets(advtime) "sc::post privmsg none all {$sc::texts(adv)}"
	}
	putlog "sc.tcl v2.1 by LoReZ successfully initialized in [expr {([clock clicks]-$start)/1000.0}]ms"	
    }
    proc pub {text nick uhost hand chan args} {
	if {([lsearch -exact [string tolower $sc::vars(chans)] [string tolower $chan]] != -1) || ($sc::vars(chans) == "")} {
	    sc::cmd $text $nick $chan $args
	}
    }
    proc msg {text nick uhost hand args} {
	sc::cmd $text $nick none $args
    }
    proc nick {nick uhost hand chan newnick} {
	set tmp [open $sc::vars(file) r]
	gets $tmp streamdata
	gets $tmp songdata
	gets $tmp djdata
	close $tmp
	if {$nick == [lindex $djdata 0]} {
	    set tmp [open $sc::vars(file) w+]
	    puts $tmp $streamdata
	    puts $tmp $songdata
	    puts $tmp "$newnick [expr {([lindex $djdata 1] == $nick) ? $newnick : [lindex $djdata 1]}] [lrange $djdata 2 end]"
	    close $tmp
	    if {([lindex $djdata 1] == $nick) && ($sc::sets(djtopchg) == 1)} {
		sc::post topic none all "$sc::texts(djtopic)"
	    }
	}	    
    }
    proc cmd {text nick chan args} {
	if {[file exists $sc::vars(file)] != 1} {
	    sc::check * * * * *
	}
	set tmp [open $sc::vars(file) r]
	gets $tmp streamdata
	gets $tmp songdata
	gets $tmp djdata
	close $tmp
	switch -exact $text {
	    help {
		set sc::offadv 1
		putquick "privmsg $nick :Available Commands:"
		foreach x [array names sc::triggers] {
		    if {$x != "help"} {
			if {(([matchattr [nick2hand $nick $chan] +$sc::vars(flag) $chan] != 1) && ($x != "kick") && ($x != "setdj") && ($x != "unsetdj")) || ([matchattr [nick2hand $nick $chan] +$sc::vars(flag) $chan] == 1)} {
			    set sc::offadv 1
			    putquick "privmsg $nick :$sc::vars(trigger)[expr {($x == "setdj") ? "$sc::triggers($x) <djname> <showname>" : [expr {(($x == "wish") || ($x == "greet")) ? "$sc::triggers($x) <$x>" : "$sc::triggers($x)"}]}]"  
			}
		    }
		}
		set sc::offadv 1
		putquick "privmsg $nick :End of Commands"
	    }
	    kick {
		putquick "privmsg $nick :Source successfully kicked"
		if {[lindex $streamdata 15] == 1} {
		    set sock [socket $sc::vars(ip) $sc::vars(port)]
		    puts $sock "GET /admin.cgi?mode=kicksrc HTTP/1.1"
		    puts $sock "User-Agent:Mozilla"
		    puts $sock "Host: $sc::vars(ip)"
		    puts $sock "Authorization: Basic $sc::vars(adpass)"
		    puts $sock ""
		    flush $sock
		}
	    }
	    setdj {
		putquick "privmsg $nick :New DJ set"
		if {[lindex $streamdata 15] == 1} {
		    set tmp [open $sc::vars(file) w+]
		    puts $tmp $streamdata
		    puts $tmp $songdata
		    puts $tmp "$nick [expr {([lindex $args 0] == "{}") ? $nick : [lindex [lindex $args 0] 0]}] [lrange [lindex $args 0] 1 end]"
		    close $tmp
		    if {$sc::sets(anndj) == 1} {
			sc::post privmsg none all "$sc::texts(djchg)"
		    }
		    if {$sc::sets(djtopchg) == 1} {
			sc::post topic none all "$sc::texts(djtopic)"
		    }
		}
	    }
	    unsetdj {
		putquick "privmsg $nick :DJ unset"
		if {[lindex $streamdata 15] == 1} {
		    set tmp [open $sc::vars(file) w+]
		    puts $tmp $streamdata
		    puts $tmp $songdata
		    puts $tmp "none none none"
		    close $tmp
		    if {$sc::sets(anndj) == 1} {
			sc::post privmsg none all "$sc::texts(djchg)"
		    }
		    if {$sc::sets(djtopchg) == 1} {
			sc::post topic none all "$sc::texts(ontopic)"
		    }
		}
	    }
	    default {
		if {($text != "wish") && ($text != "greet")} {
		    sc::post privmsg $nick $chan "$sc::texts($text)"
		} else {
		    if {[lindex $streamdata 15] == 1} {
			if {([lindex $args 0] == "{}")} {
			putquick "privmsg $nick :Nothing specified"
			} else {
			    if {[lindex $djdata 1] == "none"} {
				putquick "privmsg $sc::vars(intern) :[string totitle $text] by $nick: $args"
			    } else {
				putquick "privmsg [expr {($sc::sets(wishgreet) == 1) ? $sc::vars(intern) : [lindex $djdata 0]}] :[string totitle $text] by $nick: $args"
				putquick "privmsg $nick :[string totitle $text] sent to [expr {($sc::sets(wishgreet) == 1) ? $sc::vars(intern) : [lindex $djdata 0]}]"				    
			    }
			}
		    } else {
			sc::post privmsg $nick none "$sc::texts(offcmd)"
		    }
		}
	    }
	}
	putlog "sc.tcl v2.1 $nick used $sc::vars(trigger)$sc::triggers($text)"
    }           
    proc check {min hour day month year} {
	set start [clock clicks]
	if {[file exists $sc::vars(file)] == 1} {
	    set tmp [open $sc::vars(file) r]
	    gets $tmp olddata
	    gets $tmp songdata
	    gets $tmp djdata
	    close $tmp
	} else {
	    set olddata "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
	    set djdata "none none none"
	}
	http::config -useragent "Mozilla 5.0"
	set con [http::geturl http://$sc::vars(ip):$sc::vars(port)/admin.cgi?pass=$sc::vars(pass)&mode=viewxml&page=0 -timeout 10000]
	set urldata [http::data $con]
	http::cleanup $con 
	foreach {y z} {21 ! 22 \" 23 # 24 \$ 25 % 26 + 27 ' 28 \( 29 \) 2A * 2B + 2C , 2E . 2F / 3A : 3B \; 3C < 3D = 3E > 3F ? 40 @ 5B \[ 5C \\ 5D \] 5F _ 60 ` C0 À C1 Á C2 Â C3 Ã C4 Ä C5 Å C6 Æ C7 Ç C8 È C9 É CA Ê CB Ë CC Ì CD Í CE Î CF Ï D1 Ñ D2 Ò D3 Ó D4 Ô D5 Õ D6 Ö D9 Ù DA Ú DB Û DC Ü DD Ý DF ß E0 à E1 á E2 â E3 ã E4 ä E5 å E6 æ E7 ç E8 è E9 é EA ê EB ë EC ì ED í EE î EF ï F0 ð F1 ñ F2 ò F3 ó F4 ô F5 õ F6 ö F9 ù FA ú FB û FC ü FD ý FE þ FF ÿ} {
	    regsub -all "&#x$y;" $urldata "$z" urldata 
	}
	regsub -all ".*<SHOUTCASTSERVER>" $urldata "" data1
	regsub -all "<WEBDATA>.*" $data1 "" data1
	regsub -all ".*<SONGHISTORY>" $urldata "\{" data2
	regsub -all "</SONGHISTORY>.*" $data2 " \}" data2
	set x 0
	foreach y {CURRENTLISTENERS PEAKLISTENERS MAXLISTENERS REPORTEDLISTENERS AVERAGETIME SERVERGENRE SERVERURL SERVERTITLE SONGTITLE SONGURL IRC ICQ AIM WEBHITS STREAMHITS STREAMSTATUS BITRATE CONTENT VERSION SONG PLAYEDAT TITLE} {
	    if {$x < 19} {
		regsub -all ".*<$y>" $data1 "" data10
		regsub -all "</$y>.*" $data10 "" data10
		lappend streamdata [expr {(($data10 == "0") || ($data10 == "")) && ([string match -nocase *listeners "$y"] != 1) && ([string match -nocase *hits "$y"] != 1) && ([string match -nocase *time "$y"] != 1) ? "N/A" : $data10}]
	    }
	    if {$x > 18} {
		regsub -all "<$y>" $data2 "\{" data2
		regsub -all "</$y>" $data2 "\} " data2
	    }
	    incr x
	}
	set tmp [open $sc::vars(file) w+]
	puts $tmp $streamdata
	puts $tmp $data2
	puts $tmp $djdata
	close $tmp
	if {[lindex $olddata 15] != [lindex $streamdata 15]} {
	    if { ($sc::sets(ann) == 1)} {
		set sc::offadv [expr {([lindex $streamdata 15] == 1) ? 0 : 1}]
		sc::post privmsg none all "[expr {([lindex $streamdata 15] == 1) ? $sc::texts(on) : $sc::texts(off)}]"
	    }
	    if {$sc::sets(topchg) == 1} {
		set sc::offadv [expr {([lindex $streamdata 15] == 1) ? 0 : 1}]
		sc::post topic none all "[expr {([lindex $streamdata 15] == 1) ? $sc::texts(ontopic) : $sc::texts(offtopic)}]"
	    }
	}
	foreach {x y z} {8 anntrack songchg 0 annlist listchg 1 annpeak peakchg 16 annbitrate bitratechg} {
	    if {([lindex $olddata $x] != [lindex $streamdata $x]) && ($sc::sets($y) == 1)} {
		sc::post privmsg all all "$sc::texts($z)"
	    }
	}
	putlog "sc.tcl v2.1 received information from $sc::vars(ip):$sc::vars(port) in [expr {([clock clicks]-$start)/1000.0}]ms"
    }              
    proc post {mode nick chan text} {
	set tmp [open $sc::vars(file) r]
	gets $tmp streamdata
	gets $tmp songdata
	gets $tmp djdata
	close $tmp
	if {([lindex $streamdata 15] == 1) || ($sc::offadv == 1)} {
	    foreach x [lindex $songdata 0] {
		append lastsongs "[lindex $x 1] :: "
	    }
	    lappend streamdata $sc::vars(ip) $sc::vars(port) $sc::vars(desc) ":: $lastsongs" [expr {(([lindex $djdata 0] == "none") || ([lindex $djdata 0] == "{}") || ([lindex $djdata 0] == "")) ? "No DJ" : [lindex $djdata 0]}] [expr {(([lindex $djdata 1] == "none") || ([lindex $djdata 1] == "{}") || ([lindex $djdata 1] == "")) ? "No DJ" : [lindex $djdata 1]}] [expr {(([lindex $djdata 2] == "none") || ([lindex $djdata 2] == "{}") || ([lindex $djdata 2] == "")) ? "No Show" : [lrange $djdata 2 end]}] [expr {([lindex $streamdata 15] == 1) ? "online" : "offline"}]
	    set x 0
	    foreach y {listeners peak max unique avgtime genre url title song songurl irc icq aim webhits streamhits streamstatus bitrate content version ip port desc lastsongs djnick dj showname status} {
		regsub -all "%$y" $text "[lindex $streamdata $x]" text
		incr x
	    }
	    if {$mode == "topic"} {
		foreach chans [channels] {
		    if {$sc::vars(chans) == ""} {
			if {$sc::sets(qtopic) == 1} {
			    putquick "privmsg TheQBot@CServe.quakenet.org :SETTOPIC $chans $text"
			} else {
			    putquick "$mode $chans :$text"
			}
		    } else {
			if {([lsearch -exact [string tolower $sc::vars(chans)] [string tolower $chans]] != -1)} {
			    if {$sc::sets(qtopic) == 1} {
				putquick "privmsg TheQBot@CServe.quakenet.org :SETTOPIC $chans $text"
			    } else {
				putquick "$mode $chans :$text"
			    }
			}		
		    }
		}
	    } else {
		if {($sc::sets(reply) != 1) || ($chan == "none")} {
		    putquick "$mode $nick :$text"
		}
		if {(($sc::sets(reply) == 1) && ($chan != "none")) || ($chan == "all")} {
		    foreach chans [channels] {
			if {$sc::vars(chans) == ""} {
			    putquick "$mode $chans :$text"
			} else {
			    if {([lsearch -exact [string tolower $sc::vars(chans)] [string tolower $chans]] != -1)} {
				putquick "$mode $chans :$text"
			    }
			}
		    }
		}
	    }
	} else {
	    putquick "$mode [expr {((($sc::sets(reply) != 1) || ($chan == "none")) && ($chan != "all")) ? $nick : $chan}] :$sc::texts(offcmd)"
	}
	set sc::offadv 0
    }
    sc::init
}

###############################################################################################################################
#                                                           END OF TCL CODE                                                   #
###############################################################################################################################