* @copyright 2010, The Nystic Network
* @license http://creativecommons.org/licenses/by/3.0/
* @link http://wildphp.com (Visit for updated versions and more free scripts!)
* @version 1.0.0 (Last updated 03-20-2010)
*
*/
//So the bot doesnt stop.
set_time_limit(0);
ini_set('display_errors', 'on');
//Sample connection data.
$config = array(
'server' => 'chat.freenode.net',
'port' => 6667,
'channel' => '#flexget-bottest',
'name' => 'tv-helper2',
'nick' => 'tv-helper2',
'pass' => '',
);
/*
//Set your connection data.
$config = array(
'server' => 'example.com',
'port' => 6667,
'channel' => '#nystic_chat',
'name' => 'real name',
'nick' => 'user',
'pass' => 'pass',
);
*/
class IRCBot {
//This is going to hold our TCP/IP connection
var $socket;
//This is going to hold all of the messages both server and client
var $ex = array();
// This holds the last user to talk
var $lastuser;
/*
Construct item, opens the server connection, logs the bot in
@param array
*/
function __construct($config) {
$this->socket = fsockopen($config['server'], $config['port']);
$this->login($config);
$this->main($config);
}
/*
Logs the bot in on the server
@param array
*/
function login($config) {
$this->send_data('USER', $config['nick'].' wildphp.com '.$config['nick'].' :'.$config['name']);
$this->send_data('NICK', $config['nick']);
$this->join_channel($config['channel']);
}
/*
This is the workhorse function, grabs the data from the server and displays on the browser
*/
function main($config)
{
$data = fgets($this->socket, 256);
echo nl2br($data);
flush();
$this->ex = explode(' ', $data);
if($this->ex[0] == 'PING') {
$this->send_data('PONG', $this->ex[1]); //Plays ping-pong with the server to stay connected.
} else {
$this->lastuser = preg_replace("/:(.+)!.*/", "$1", $this->ex[0]);
echo "\n\nLast User: " . $this->lastuser ."\n\n";
$command = str_replace(array(chr(10), chr(13)), '', $this->ex[3]);
switch($command) //List of commands the bot responds to from a user.
{
case ':!tv':
$message = "";
for($i=4; $i <= (count($this->ex)); $i++)
{
$message .= $this->ex[$i]." ";
}
$this->tvshow($message);
if ($this->ex[2] == $config['nick']) {
echo "\nREPLYING TO USER\n";
$this->send_data('PRIVMSG ' . $this->lastuser . ' :', $this->tvinfo);
} else {
echo "\nREPLYING TO CHANNEL\n";
$this->send_data('PRIVMSG ' . $config['channel'] . ' :', $this->tvinfo);#
}
break;
case ':!tell':
$message = "";
for($i=5; $i <= (count($this->ex)); $i++)
{
$message .= $this->ex[$i]." ";
}
$this->tvshow($message);
$this->send_data('PRIVMSG ' . $config['channel'] . ' :', $this->ex[4] . ': ' . $this->tvinfo);
break;
}
}
$this->main($config);
}
function send_data($cmd, $msg = null) //displays stuff to the broswer and sends data to the server.
{
if($msg == null) {
fputs($this->socket, $cmd."\r\n");
echo ''.$cmd."
\n";
} else {
fputs($this->socket, $cmd.' '.$msg."\r\n");
echo ''.$cmd.' '.$msg."
\n";
}
}
function join_channel($channel) //Joins a channel, used in the join function.
{
if(is_array($channel)) {
foreach($channel as $chan) {
$this->send_data('JOIN', $chan);
}
} else {
$this->send_data('JOIN', $channel);
}
}
function tvshow($message) // Makes a query on tvrage
{
$show_mod = $message;
// Useragent
$useragent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.23 (KHTML, like Gecko) Ubuntu/10.10 Chromium/11.0.691.0 Chrome/11.0.691.0 Safari/534.23';
// TV Rage stuff
$key = 'tSZWVK3b5Tj1INLVY6gF';
$search = 'http://services.tvrage.com/myfeeds/search.php?key=' . $key . '&show=';
$info = 'http://services.tvrage.com/myfeeds/showinfo.php?key=' . $key . '&sid=';
$episodes = 'http://services.tvrage.com/myfeeds/episodeinfo.php?key=' . $key . '&sid=';
$episodeinfo = 'http://services.tvrage.com/myfeeds/episodeinfo.php?key=' . $key . '&sid=';
// Initialize objec
$ch = curl_init();
// Initial test
$query = $search . urlencode($show_mod);
curl_setopt($ch, CURLOPT_URL, $query);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$r = curl_exec($ch);
$showid = explode('', $r);
$showid = explode('', $showid[0]);
$showid = $showid[1];
// Fetch Show and Episode Info
$query = $info . $showid;
curl_setopt($ch, CURLOPT_URL, $query);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$r = curl_exec($ch);
$query = $episodes . $showid;
curl_setopt($ch, CURLOPT_URL, $query);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$s = curl_exec($ch);
//$showinfo = $this->xml2array($r);
$this->xml2array($r);
$showinfo = $this->xml_arr;
$showinfo = $showinfo['Showinfo'];
//$episodeinfo = $this->xml2array($s);
$this->xml2array($s);
$episodeinfo = $this->xml_arr;
$episodeinfo = $episodeinfo['show'];
$title = $showinfo['showname'];
$full_url = $showinfo['showlink'];
$genre = $showinfo['genres'];
if(is_array($genre)) {
$genre = $genre['genre'];
}
if(is_array($genre)) {
foreach($genre as $val) {
$genre_tmp .= $val . '/';
}
$genre = substr($genre_tmp, 0, -1);
}
$genre = 'Genre: ' . $genre;
$premiere = 'Premiere: ' . $showinfo['startdate'];
$ended = $showinfo['ended'];
if(is_array($ended)) {
$latest = 'Latest Episode: ';
} else {
$latest = 'Last Episode: ';
}
$latest .= $episodeinfo['latestepisode']['number'] . ' - ' . $episodeinfo['latestepisode']['title'] . ' (' . $episodeinfo['latestepisode']['airdate'] . ')';
if(is_array($ended)) {
$next = 'Next Episode: ' . $episodeinfo['nextepisode']['number'] . ' - ' . $episodeinfo['nextepisode']['title'] . ' (' . $episodeinfo['nextepisode']['airdate'] . ')';
} else {
$next = 'Ended: ' . $ended;
}
$network = $showinfo['network'];
if(is_array($ended)) {
$airs = '%C4<>%C Airs: ' . $showinfo['airday'] . 's at ' . $showinfo['airtime'] . ' (' . $showinfo['timezone'] .')';
$airs = '<> Airs: ' . $showinfo['airday'] . 's at ' . $showinfo['airtime'] . ' (' . $showinfo['timezone'] .')';
}
$output = "%C$title %C4<>%C $full_url %C4<>%C $genre %C4<>%C $premiere %C4<>%C $latest %C4<>%C $next %C4<>%C $network $airs";
$output = "$title <> $full_url <> $genre <> $premiere <> $latest <> $next <> $network $airs";
//echo $output;
$this->tvinfo = $output;
}
/**
* xml2array() will convert the given XML text to an array in the XML structure.
* Link: http://www.bin-co.com/php/scripts/xml2array/
* Arguments : $contents - The XML text
* $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value.
* $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance.
* Return: The parsed XML in an array form. Use print_r() to see the resulting array structure.
* Examples: $array = xml2array(file_get_contents('feed.xml'));
* $array = xml2array(file_get_contents('feed.xml', 1, 'attribute'));
*/
function xml2array($contents, $get_attributes=1, $priority = 'tag') {
if(!$contents) return array();
if(!function_exists('xml_parser_create')) {
//print "'xml_parser_create()' function not found!";
return array();
}
//Get the XML parser of PHP - PHP must have this module for the parser to work
$parser = xml_parser_create('');
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($contents), $xml_values);
xml_parser_free($parser);
if(!$xml_values) return;//Hmm...
//Initializations
$xml_array = array();
$parents = array();
$opened_tags = array();
$arr = array();
$current = &$xml_array; //Refference
//Go through the tags.
$repeated_tag_index = array();//Multiple tags with same name will be turned into an array
foreach($xml_values as $data) {
unset($attributes,$value);//Remove existing values, or there will be trouble
//This command will extract these variables into the foreach scope
// tag(string), type(string), level(int), attributes(array).
extract($data);//We could use the array by itself, but this cooler.
$result = array();
$attributes_data = array();
if(isset($value)) {
if($priority == 'tag') $result = $value;
else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
}
//Set the attributes too.
if(isset($attributes) and $get_attributes) {
foreach($attributes as $attr => $val) {
if($priority == 'tag') $attributes_data[$attr] = $val;
else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
}
}
//See tag status and do the needed.
if($type == "open") {//The starting of the tag ''
$parent[$level-1] = &$current;
if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
$current[$tag] = $result;
if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
$repeated_tag_index[$tag.'_'.$level] = 1;
$current = &$current[$tag];
} else { //There was another element with the same tag name
if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
$repeated_tag_index[$tag.'_'.$level]++;
} else {//This section will make the value an array if multiple tags with the same name appear together
$current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
$repeated_tag_index[$tag.'_'.$level] = 2;
if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}
}
$last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
$current = &$current[$tag][$last_item_index];
}
} elseif($type == "complete") { //Tags that ends in 1 line ''
//See if the key is already taken.
if(!isset($current[$tag])) { //New Key
$current[$tag] = $result;
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
} else { //If taken, put all things inside a list(array)
if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
// ...push the new element into that array.
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
if($priority == 'tag' and $get_attributes and $attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag.'_'.$level]++;
} else { //If it is not an array...
$current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $get_attributes) {
if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}
if($attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
}
}
} elseif($type == 'close') { //End of tag ''
$current = &$parent[$level-1];
}
}
//return($xml_array);
$this->xml_arr = $xml_array;
}
}
//Start the bot
$bot = new IRCBot($config);
?>