Icecast.tcl

            #Tido's Icecast2 Script
#Version 1.0RC1
#February 26th, 2006
######################################################################
#Credits:
#Influenced by Domsen's shoutcast1.03.tcl
#Special thanks to the TCL MAN files :D
######################################################################
#What is this:
#Ice2.tcl is a parser/reporter script for Icecast2 internet radio stations. It's slightly similar to Domsen's
# shoutcast1.03.tcl script except this is for Icecast2 servers and not Shoutcast servers. It tells members of
# a room if the stream is online, what artist-song it is playing, how many listeners there are, what the
# stream url is, who the DJ is, and various other tidbits.

#Ice2.tcl was origionally created by Tido to work with The Jedi Academy's (www.thejediacademy.net) Icecast2
# server. It was hosted by Henkes on JARadiobot in #ja-radio on Quakenet. I made this script work for the JA's
# situation and haven't been able to test it on any other Icecast2 server! It worked for me for a long time but
# it might not work for you! As far as I know, this is the only Eggdrop TCL script for Icecast2 in existance.
#
#Ice2.tcl has only been tested on icecast2 version 2.3.1 (linux/freebsd) with Ogg Vorbis and MP3 encoding. It is
# completely dependent on the output of Icecast2's status.xsl, so if that is changed the script will not work.
# One of the nice things about Ice2.tcl is it doesn't require the server's admin password to work unlike other
# internet radio scripts!
#
#E-mail Tido @ tidokenobi@gmail.com for questions/comments/suggestions
######################################################################
#Changelog:
#v1.0RC1 - First public release candidate
######################################################################
#Change these varibles to the one for your stream/bot/channel

#Name of Radio Station
set streamname ""

#IP of the Icecast server.
set streamip ""

#Port that Icecast is using, default is 8000.
set streamport ""

#Main channel for the radio bot.
set radiochan ""

#Other channel that the bot can advertise to. Bot must be in this channel for this to work. Ice2.tcl only
# sends stream advertisements to this channel and does not send song info.
set otherchan ""

#URL/Link to the stream for listening. This is what listeners need to click to tune in.
set streamurl "http://stream.zeno.fm/hefn9cs3dxhvv"

#How often the bot checks the stream when it knows it is down in minutes. Recommend 1 minute.
set offlinetimer "1"

#How often the bot checks the stream when it knows it is online in seconds. Recommend 15 seconds.
set onlinetimer "10"

#Default interval for how often the bot advertises (in minutes). You want to set it to something that isn't
# pure spammage.
set adtimer "10"

#Enables advertising set to the above frequency. 1 for ON and 0 for OFF. This reminds people that the stream
# is online.
set enableadvertise "1"

#Enables Special Announcement, 1 for ON and 0 for OFF. Special announcements are displayed every 720 minutes.
# This feature of the script is very undeveloped and I don't recommend using it.
set specialannounce "0"

#Special Announcement Message
set announcemsg "SPECIAL ANNOUNCEMENT! Our Channel Radio is now Online"

################################################################
#  Don't edit past this stuff unless you're Tido or Henkes :P  #
################################################################


# Binds
bind pub - "!commands" showcommands
#Shows a list of all commands

bind pub - "!help" showcommands
#Same as !commands

bind pub - "!status" status
#Displays the status of the stream

bind pub D "!djon" dj_on
#If the user has the D flag, this sets them as the DJ

bind pub D "!djoff" dj_off
#Removes the user as the DJ

bind pub - "!listeners" listenercheck
#Reports how many listeners there are

bind pub D "!advertise" toggle_advertise
#Turns on/off advertising

bind pub D "!forceadvertise" forceadvertise
#Forces an advertising message to be sent

bind msg D "djforceoff" dj_force_off
#Force-remove a DJ with !djforceoff <nick>, must have D flag

bind pub - "!dj" dj
#Reports the current DJ

bind pub - "!request" request
#Sends a request to the current DJ. !request <artist/song>

bind msg - "request" msg_request
#/msg version of request

bind pub - "!version" iceversion
#Displays the Ice2.tcl version

bind pub m "!newdj" newdj
#Creates a new DJ by adding the D flag

# Varible Resets
set ice2version "1.0RC1 - 02.26.06"
set streamstatus "0"
set djnickname ""
set dj ""
set oldsong ""
set newsong ""
set newlistener ""
set oldlistener "0"
set forceadsent "0"
set sessionpeak "0"

# Check to make sure StatusCheck timer isn't running when bot rehashes.
if {![info exists statuscheck_running]} {
  timer $offlinetimer [list statuscheck]
  set statuscheck_running 1
}

# Check to make sure Special Announce timer isn't running when bot rehashes.
if {![info exists specialannounce_running]} {
  if {$specialannounce == "1"} {
    timer 720 [list specialmessage]
    set specialannounce_running 1
  }
}


# Check to make sure Advertise timer isn't running when bot rehashes.
if {![info exists adtimer_running]} {
  if {$enableadvertise == "1"} {
    timer $adtimer [list advertise]
    set adtimer_running 1
  }
}

# Output for !help or !showcommands
proc showcommands {nick uhost hand chan arg} {
  global ice2version streamname botnick
  putserv "notice $nick :>>> $botnick Commands - $ice2version<<<"
  putserv "notice $nick :!status >>> Displays the stream's status. If online it shows the song, # of listeners, and the current DJ."
  putserv "notice $nick :!dj >>> Shows current DJ."
  putserv "notice $nick :!listeners >>> Shows the current number of listeners tuned into $streamname."
  putserv "notice $nick :!request (artist+track) >>> Sends a request to the DJ's queue. Example: !request Oasis - Wonderwall"
  putserv "notice $nick :/msg $botnick request (artist+track) >>> Another way to send a request that isn't visable to everyone in the channel."
}

# Turns on and off Advertising. Also lets you set the interval: !advertise X
proc toggle_advertise {nick uhost hand chan arg} {
  global radiochan enableadvertise adtimer
  if {$enableadvertise == "1"} {
    set enableadvertise "0"
    set timerinfo [gettimerid]
    killtimer $timerinfo
    putserv "PRIVMSG $chan :Advertising OFF"
  } else {
    set enableadvertise "1"
    if {$arg == ""} {
      putserv "PRIVMSG $chan :Advertising ON. Frequency set to $adtimer minutes."
    } else {
      set adtimer $arg
      putserv "privmsg $chan :Advertising ON. Frequency changed to $adtimer minutes."
      timer $adtimer [list advertise]
    }
  }
}

# Function that finds out the ID of the advertising timer.
proc gettimerid {} {
    set adtimerinfo [timers]
    set loc1 [string first "advertise" $adtimerinfo]
    set loc1 [expr $loc1 + 10]
    set str1 [string range $adtimerinfo $loc1 999]
    set endloc [string first "\}" $str1]
    set endloc [expr $endloc -1]
    set timerinfo [string range $str1 0 $endloc]
    return $timerinfo
}

# Messages that are displayed when Advertising is enabled.
proc advertise {} {
  global radiochan streamstatus otherchan enableadvertise adtimer forceadsent streamurl streamname
  if {$streamstatus != "0" && $enableadvertise == "1"} {
    putserv "PRIVMSG $radiochan :$streamname is currently broadcasting live! Listen in @ $streamurl"
    putserv "PRIVMSG $otherchan :$streamname is currently broadcasting live! Listen in @ $streamurl"
    if {$forceadsent == "0"} {timer $adtimer [list advertise]} else {set forceadsent "0"}
    return 1
  }
  return 0
}

# Forces the advertising messages to appear
proc forceadvertise {nick uhost hand chan arg} {
  global streamstatus enableadvertise forceadsent
  if {$streamstatus == "0"} {
    putserv "notice $nick :The stream isn't on-air. Unable to advertise."
    return 0
  } else {
    if {$enableadvertise == "0"} {
      putserv "notice $nick :Advertising isn't enabled."
      return 0
    } else {
      set forceadsent "1"
      set forceadvertised [advertise]
      if {$forceadvertised == "1"} {
        putserv "notice $nick :Done!"
      } else {
        putserv "notice $nick :Advertising message was not sent!"
      }
    }
  }
}

# Special Announcement Message.
proc specialmessage {} {
  global radiochan specialannounce announcemsg
  putserv "PRIVMSG $radiochan : $announcemsg"
  timer 720 [list specialmessage]
  return 0
}

# StatusCheck
# Function that takes the information from Icecast_Online and creates the proper responses.
proc statuscheck {} {
  global radiochan streamstatus newsong oldsong newlistener oldlistener sessionpeak dj enableadvertise otherchan onlinetimer offlinetimer streamurl streamname

  if {$streamstatus == "0"} {
    set oldstatus "0"
  } else {
    set oldstatus "1"
  }
  set newstatus "[icecast_online]"
  if {$newstatus =="0" && $oldstatus == "0"} {
    timer $offlinetimer [list statuscheck]
  }
  if {$newstatus == "1" && $oldstatus == "0"} {
    putserv "PRIVMSG $radiochan :$streamname is now ON-AIR!! Click to listen: $streamurl"
    putlog "(RADIO) On-Air detected."
    utimer $onlinetimer [list statuscheck]
    if {$enableadvertise == "1"} {
      putserv "PRIVMSG $otherchan :$streamname is now ON-AIR!! Click to listen: $streamurl"

    }
  }
  if {$newstatus == "0" && $oldstatus == "1"} {
    putserv "PRIVMSG $radiochan :$streamname is now off-air."
    set oldlistener "0"
    set sessionpeak "0"
    if {$enableadvertise == "1"} {
      putserv "PRIVMSG $otherchan :$streamname is now off-air."
    }
    putlog "(RADIO) Off-Air detected."
    timer $offlinetimer [list statuscheck]
  }
  if {$newstatus == "1" && $oldstatus == "1"} {
    utimer $onlinetimer [list statuscheck]
    if {$newlistener != $oldlistener} {
      putserv "notice $dj :$newlistener listeners."
      if {$newlistener > $sessionpeak} {
         putserv "PRIVMSG $radiochan :$streamname currently has $newlistener listeners!"
         set sessionpeak $newlistener
      }
      set oldlistener "$newlistener"
    }
    if {$newsong != $oldsong} {
      putserv "PRIVMSG $radiochan :Now playing on $streamname: $newsong"
      set oldsong "$newsong"
    }
  }
}
  
# Icecast_Online
# This is the HTTP Parser that gathers the various data from the status.xsl file.
proc icecast_online { } {
  global streamip streamport streamstatus newsong newlistener
  set pagedata ""
  if {[catch {set sock [socket $streamip $streamport] } sockerror]} {
    putlog "error: $sockerror"
    return 0
  } else {
    puts $sock "GET /status.xsl HTTP/1.1"
    puts $sock "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.9)"
    puts $sock "Host: $streamip"
    puts $sock "Connection: close"
    puts $sock ""
    flush $sock
    while {![eof $sock]} { append pagedata "[read $sock]" }
  }
  if {[string match *streamdata* $pagedata] == 1} {
    set streamstatus "1"
    set songlocation [string first "Current Song:" $pagedata]
    set songdata1 [string range $pagedata $songlocation 99999]
    set location2 [string first "</tr>" $songdata1]
    set songdata2 [string range $songdata1 0 $location2]
    set songdata3 [string range $songdata2 41 9999]
    set location3 [string first "</td>" $songdata3]
    set location3 [expr $location3 - 1]
    set newsong [string range $songdata3 0 $location3]

    set llocation [string first "Listeners:" $pagedata]
    set countdata1 [string range $pagedata $llocation 99999]
    set llocation2 [string first "</tr>" $countdata1]
    set countdata2 [string range $countdata1 0 $llocation2]
    set countdata3 [string range $countdata2 38 9999]
    set llocation3 [string first "</td>" $countdata3]
    set llocation3 [expr $llocation3 - 1]
    set newlistener [string range $countdata3 0 $llocation3]
    close $sock
    return 1
    
  } else {
    set streamstatus "0"
    close $sock
    return 0
  }
}

# !status function. Diplays the current status of the stream.
proc status {nick uhost hand chan arg} {
  global dj radiochan newsong newlistener streamstatus streamurl streamname
  if {$streamstatus == 1} {
    putserv "PRIVMSG $chan :$streamname is currently broadcasting live @ $streamurl"
    if {$newsong != ""} {putserv "PRIVMSG $chan :The current song is |$newsong| with $newlistener listeners."}
    if {$dj != ""} {putserv "PRIVMSG $chan :The DJ is $dj"}
  } else {
    putserv "PRIVMSG $chan :$streamname is currently offline."
  } 
}

# !request X: sends requests to the DJ.
proc request {nick uhost hand chan arg} {
  global dj radiochan
  if {$dj == ""} {
    putserv "notice $nick :There isn't a DJ logged in at the moment to take requests."
  } else {
    if {$arg == ""} {
      putserv "notice $nick :You didn't request anything!"
    } else {
      putserv "privmsg $dj :REQUEST($nick)=> $arg"
      putserv "notice $nick :Request sent to $dj :)"
    }
  }
}

# /msg $botnick request X: sends the request to the DJ privately.
proc msg_request {nick uhost hand arg} {
  global dj radiochan
  if {$dj == ""} {
    putserv "notice $nick :There isn't a DJ logged in at the moment to take requests."
  } else {
    if {$arg == ""} {
      putserv "notice $nick :You didn't request anything!"
    } else {
      putserv "privmsg $dj :REQUEST($nick)=> $arg"
      putserv "notice $nick :Request sent to $dj :)"
    }
  }
}

# !djon (name): enables DJ status for someone with the D tag
proc dj_on {nick uhost hand chan arg} {
  global dj radiochan djnickname streamurl streamname
  if {$dj != "" && $dj != "$nick" && [onchan $djnickname $radiochan] != "0"} {
    putserv "notice $nick :There is already a DJ active!"
    return 0
  }
  if {$arg == "" } { set djnickname $nick} else {set djnickname $arg}
  pushmode $radiochan +o $nick
  putserv "TOPIC $radiochan :$streamname || Click here to Listen: $streamurl || Program Schedule: http://www.thejediacademy.net/forums_detail_page.php?f_id=13532 || Current DJ: $djnickname"
  putserv "PRIVMSG $radiochan :$djnickname is now rocking the turntables, enjoy."
  set dj "$nick"
  putlog "(RADIO) DJ-ON: [nick2hand $nick $chan]"
}

# !djoff: Disables DJ status.
proc dj_off {nick uhost hand chan arg} {
  global dj radiochan djnickname streamurl streamname
  if {$dj != $nick} {
    putserv "notice $nick :You are not the current DJ or you changed your nickname since becoming one."
  } else {
    set dj ""
    set djnickname ""
    pushmode $radiochan -o $nick
    putserv "TOPIC $radiochan :$streamname || Click here to Listen: $streamurl || Program Schedule: http://www.thejediacademy.net/forums_detail_page.php?f_id=12083 || Current DJ: none"
    putserv "notice $nick :Your DJ'ness has been deactivated"
    putlog "(RADIO) DJ-OFF: [nick2hand $nick $chan]"
  }
}

# /msg $botnick djforceoff [nick]: Forces off a DJ from having DJ status.
proc dj_force_off {nick uhost hand arg} {
  global djnickname dj radiochan streamurl streamname
  if {$dj == ""} {
    putserv "notice $nick :No DJ currently set to boot off."
    return 0
  } else {
    if {$arg == ""} {
      putserv "notice $nick :You didn't specify a DJ to boot off."
      return 0
    } else {
      set dj ""
      set djnickname ""
      pushmode $radiochan -o $arg
      putserv "TOPIC $radiochan :$streamname || Click here to Listen: $streamurl || Program Schedule: http://www.thejediacademy.net/forums_detail_page.php?f_id=12083 || Current DJ: none"
      putserv "notice $nick :DJ status removed for $arg."
      putserv "notice $arg :Your DJ'ness was removed by $nick."
      putlog "(RADIO) DJ-FORCEOFF: $arg booted by [nick2hand $nick $chan]"
    }
  }
}

# !dj: displays who the current DJ is.
proc dj {nick uhost hand chan arg} {
  global djnickname radiochan streamname
  if {$djnickname == ""} {
    putserv "PRIVMSG $chan :There currently are no DJs logged in at the moment."
  } else {
    putserv "PRIVMSG $chan :$djnickname is the current $streamname DJ!"
  }
}

# !listeners: displays how many current listeners there are.
proc listenercheck {nick uhost hand chan arg} {
  global newlistener radiochan streamstatus streamname
  if {$streamstatus == "1"} {
    if {$newlistener == "0"} {
      putserv "PRIVMSG $chan :There aren't any listeners tuned into $streamname :(."
    } elseif {$newlistener =="1"} {
      putserv "PRIVMSG $chan :There is 1 listener tuned into $streamname."
    } else {
      putserv "PRIVMSG $chan :There are $newlistener listeners tuned into $streamname."
    }
  } else {
    putserv "PRIVMSG $chan :$streamname isn't on-air."
  }
}

# !newdj [nick] [handle] [host]: Adds a new user to the bot and gives them the DJ flag (D).
proc newdj {nick host hand chan arg} {
  global radiochan streamname
  set nickname [lindex $arg 0]
  set newhand [lindex $arg 1]
  set newhost [lindex $arg 2]
  if {[validuser $nickname]} {
    putserv "privmsg $nick :There is already a user existing by that handle!"
    return 0
  }
  if {$nickname == ""} {
    putserv "privmsg $nick :You must enter their nickname handle and host!"
    return 0
  }
  if {$newhand == ""} {
    putserv "privmsg $nick :You must enter their nickname handle and host!"
    return 0
  }
  if {$newhost == ""} {
    putserv "privmsg $nick :You must enter their nickname handle and host!"
    return 0
  }
  if {([onchan $nickname $chan])} {
    adduser $newhand $newhost
    chattr $newhand +D
    putserv "privmsg $radiochan :Added $nickname ($newhand) as a $streamname DJ!"
  }
}

# !version: displays the current version of the Ice2.tcl script.
proc iceversion {nick host hand chan arg} {
   global ice2version radiochan botnick
   if {$chan == $radiochan} {
     putserv "PRIVMSG $radiochan :I am $botnick running Ice2.tcl Version $ice2version by Tido and Henkes."
   }
}

putlog "Ice2 Version $ice2version by Tido and Henkes is loaded! :)"

        

Leave a Reply