-
-
Save tpokorra/1c88c3b7a4a56401484c to your computer and use it in GitHub Desktop.
phpIrcBot based on code from http://php4you.de/page/22/+PHPIrc-bot (GPL). used for OpenPetra IRC Chat, 3 day logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PHPIrc_bot - (c) 2006 Thorben Gartmann - type "less $PATH_TO_PHPIrc_bot/GPL" for more info. | |
<?php | |
/* | |
PHPIrc_bot - an IRC bot writen in PHP | |
Copyright (C) 2006 Thorben Gartmann | |
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 | |
*/ | |
include_once("IRC.php"); | |
class IRC_Bot { | |
public $server; | |
public $port = 6667; | |
public $nickname = "PHPIrc_Bot"; | |
public $username = "PHPIrc_Bot"; | |
public $hostname = "HOST"; | |
public $servername = "SERVER"; | |
public $realname = "Botti der Bot"; | |
public $irc; | |
private $hooks = array(); | |
private $privmsg_hooks = array(); | |
function run() { | |
$this->irc = IRC::factory($this->server, | |
$this->port, | |
$this->nickname, | |
$this->username, | |
$this->hostname, | |
$this->servername, | |
$this->realname); | |
while (true) { | |
$input = $this->irc->get(); | |
$this->do_hooks($input); | |
} | |
} | |
function add_hook($command, $function) { | |
$this->hooks[] = array("command" => $command, "function" => $function); | |
} | |
function add_privmsg_hook($command, $function, $channel = "*") { | |
$this->privmsg_hooks[] = array("command" => $command, "function" => $function, "channel" => $channel); | |
} | |
function do_hooks($input) { | |
foreach ($this->hooks AS $hook) { | |
if ($hook["command"] == $input["command"]) { | |
call_user_func($hook["function"], | |
$this->irc, | |
$input["command"], | |
$input["params"], | |
$input["prefix"]); | |
} | |
} | |
if ($input["command"] == "PRIVMSG") { | |
foreach ($this->privmsg_hooks AS $hook) { | |
$string = $hook["channel"] . " :" . $hook["command"]; | |
$string2 = strstr($input["params"], ":"); | |
if ($hook["channel"] != "*" && | |
substr($input["params"], 0, strlen($string)) == $string) { | |
call_user_func($hook["function"], | |
$this->irc, | |
substr($input["params"], strlen($string) + 1), | |
$input["prefix"], | |
$hook["channel"]); | |
} elseif (substr($string2, 0, strlen($hook["command"]) + 1) == ":" . $hook["command"]) { | |
$params_pos = @strpos($input["params"], $hook["command"]) + strlen($hook["command"]) + 1; | |
$params = substr($input["params"], $params_pos); | |
$channel = substr($input["params"], 0, strpos($input["params"], " ")); | |
call_user_func($hook["function"], | |
$this->irc, | |
$params, | |
$input["prefix"], | |
$channel); | |
} | |
} | |
} | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$bot->add_hook("JOIN", "php4youBot_hello"); | |
function php4youBot_hello($object, $command, $params, $prefix) { | |
global $owner; | |
global $c; | |
global $channel; | |
$nick = IRC::get_nick($prefix); | |
# $object->privmsg($channel, ":Welcome to this channel, $nick! Type \"" . $c . "info\" to know more about this channel and the 3 day logging."); | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$bot->add_privmsg_hook($c . "info", "php4youBot_info"); | |
function php4youBot_info($object, $params, $prefix, $channel) { | |
global $info; | |
if (trim($params) == "") { | |
$i = 1; | |
foreach ($info AS $key=>$value) { | |
$i++; | |
$object->privmsg($channel, ":" . chr(3) . $i . "##" . chr(3) . chr(2) . $key . chr(2) . ": " . $value); | |
} | |
} elseif (trim($params) == "short") { | |
$text = ""; | |
foreach ($info AS $key=>$value) { | |
$text .= $key . " "; | |
} | |
$object->privmsg($channel, ":" . $text); | |
} elseif (isset($info[trim($params)])) { | |
$object->privmsg($channel, ":" . chr(3) . rand(0, 15) . "##" . chr(3) . chr(2) . $params . chr(2) . ": " . $info[$params]); | |
} else { | |
$object->privmsg($channel, ":" . chr(21) . "\"$params\"" . chr(21) . ": 404 - Not found"); | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$bot->add_hook("422", "php4youBot_init"); | |
$bot->add_hook("376", "php4youBot_init"); | |
function php4youBot_init($object, $command, $params, $prefix) { | |
global $channel; | |
global $c; | |
$object->join($channel); | |
$object->away(":logging the past 3 days to http://irc.openpetra.org; Help command: " . $c . "info"); | |
#admin_notice("Online Status: 1"); | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$bot->add_hook("PRIVMSG", "php4youBot_msglog"); | |
$bot->add_hook("JOIN", "php4youBot_msglog"); | |
$bot->add_hook("PART", "php4youBot_msglog"); | |
$bot->add_hook("NICK", "php4youBot_msglog"); | |
$bot->add_hook("QUIT", "php4youBot_msglog"); | |
function php4youBot_msglog($object, $command, $params, $prefix) { | |
global $log_format; | |
$nick = IRC::get_nick($prefix); | |
$dest = substr($params, 0, strpos($params, " ")); | |
$msg = trim(substr($params, strpos($params, " :") + 1)); | |
if ($msg[0] == ":") $msg = substr($msg, 1); | |
# echo gmdate($log_format)." command: ".$command. " params: ". trim($params). " prefix: ". $prefix. "\n"; | |
$line = "<tr><td nowrap='nowrap'>".gmdate("d.m.Y H:i")."</td>"; | |
$nickhtml = "<a title=\"$prefix\">$nick</a>"; | |
if ($command == "NICK") $line .= "<td align=right>*</td><td style=\"color:lime\">$nickhtml is now known as: $msg</td>"; | |
if ($command == "JOIN") $line .= "<td align=right>*</td><td style=\"color:lime\">$nickhtml entered the channel</td>"; | |
if ($command == "PART") $line .= "<td align=right>*</td><td style=\"color:gray\">$nickhtml has left the channel: $msg</td>"; | |
if ($command == "QUIT") $line .= "<td align=right>*</td><td style=\"color:gray\">$nickhtml has quit: $msg</td>"; | |
if (strpos($msg, ":") > 0 && strpos(substr($msg, 0, strpos($msg, ":")), " ")===false) $msg = "<font style=\"color:red; font-weight:bold\">".substr($msg, 0, strpos($msg, ":"))."</font>".substr($msg, strpos($msg, ":")); | |
if ($command == "PRIVMSG") $line .= "<td style=\"color:blue\" align=right>$nickhtml</td><td>$msg</td>"; | |
$line .= "</tr>\n"; | |
$logfilepath = "irclogs/"; | |
$file = $logfilepath."logfile_".gmdate("Y-m-d").".html"; | |
if (!file_exists($file)) | |
{ | |
$oldfile = $logfilepath."logfile_".gmdate("Y-m-d", strtotime('-3 day')).".html"; | |
if (file_exists($oldfile)) | |
{ | |
echo "deleting old file $oldfile\n"; | |
unlink($oldfile); | |
} | |
file_put_contents($file, "<table>\n", FILE_APPEND); | |
} | |
file_put_contents($file, $line, FILE_APPEND); | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if [ "`ps xaf | grep openpetralogbot | grep -v grep`" == "" ] | |
then | |
cd /home/timotheusp/irc/phpircbot | |
php openpetralogbot.php& | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
PHPIRC-bot - an IRC bot writen in PHP | |
Copyright (C) 2006 Thorben Gartmann | |
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 | |
*/ | |
class IRC { | |
public $server; | |
public $port; | |
public $nickname; | |
public $username; | |
public $hosthostname; | |
public $servername; | |
public $realname; | |
private $stack = array(); | |
private $handle; | |
public $errno; | |
public $errstr; | |
static function factory($server, | |
$port = 6667, | |
$nick = "phpirc", | |
$user = "phpirc", | |
$host = "host", | |
$servername = "server", | |
$realname = "php irc") { | |
// New IRC Object | |
$irc = new IRC; | |
// Save data | |
$irc->server = $server; | |
$irc->port = $port; | |
$irc->nickname = $nick; | |
$irc->username = $user; | |
$irc->hostname = $host; | |
$irc->servername = $servername; | |
$irc->realname = $realname; | |
// Connect to $server with $port | |
$irc->connect(); | |
// Register | |
$irc->pass(md5(rand(1, 1000))); | |
$irc->nick($nick); | |
$irc->user($user, $host, $servername, ":" . $realname); | |
// Return the object | |
return $irc; | |
} | |
function connect() { | |
// Connect | |
$this->handle = fsockopen($this->server, $this->port, $this->errno, $this->errstr); | |
// Check if connection is okay. | |
if (!is_resource($this->handle)) { | |
die("error with connection"); | |
} | |
return 1; | |
} | |
function __call($command, $params) { | |
// Generate data | |
$data = strtoupper($command) . " " . implode(" ", $params) . "\n"; | |
// Send data | |
fwrite($this->handle, $data); | |
} | |
function get() { | |
if (empty($this->stack)) { | |
// Get data | |
stream_set_timeout($this->handle, 60*5); //60*5 seconds read timeout, we are waiting for a PING | |
if (strlen($data = fread($this->handle, 100000)) == 0) | |
{ | |
die ("we expected a PING"); | |
} | |
// Split data | |
$data = explode("\n", $data); | |
unset($data[count($data) - 1]); | |
$this->stack = $data; | |
} | |
$line = array_shift($this->stack); | |
echo $line . "\n"; | |
return self::analyse($line, $this); | |
} | |
function action($dest, $text) { | |
$this->privmsg($dest, ":" . chr(1) . "ACTION " . $text . chr(1)); | |
} | |
static function analyse($data, $object) { | |
// Check if data is empty | |
if ($data == "") { | |
return 0; | |
} | |
// Get prefix | |
if (substr($data, 0, 1) == ":") { | |
$retval["prefix"] = substr($data, 1, strpos($data, " ")); | |
$data = substr($data, strpos($data, " ") + 1); | |
} | |
// Get command | |
$retval["command"] = substr($data, 0, strpos($data, " ")); | |
$retval["command"] = strtoupper($retval["command"]); | |
// Get params | |
$retval["params"] = substr($data, strpos($data, " ") + 1); | |
// Response to pings | |
if ($retval["command"] == "PING") { | |
$object->pong($retval["params"]); | |
} | |
return $retval; | |
} | |
static function get_nick($prefix) { | |
$nick = substr($prefix, 0, strpos($prefix, "!")); | |
$nick = str_replace(":", "", $nick); | |
return $nick; | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
PHPIrc_bot - an IRC bot writen in PHP | |
Copyright (C) 2006 Thorben Gartmann | |
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 | |
*/ | |
include("BOT.php"); | |
$bot = new IRC_Bot; | |
// Config for the bot | |
$bot->server = "irc.freenode.net"; | |
$bot->port = "6667"; | |
$bot->nickname = "OP3DayLogBot"; | |
$bot->realname = "See http://irc.openpetra.org for log"; | |
$nick = $bot->nickname; | |
$channel = "#openpetra"; | |
$c = "$"; | |
// Some config for hooks | |
$owner = "timotheusp"; | |
$quitmsg = "bot is leaving"; | |
$log_format = "d.m. H:i"; | |
$info["owner"] = $owner; | |
$info["website"] = "irc.openpetra.org"; | |
$info["channel"] = "#openpetra @ irc.freenode.net"; | |
$info["version"] = "PHPIrc_bot 0.2"; | |
#$info["commands"] = $c . "coffee, " . $c . "info, " . $c . "echo, " . $c . "mypenis, " . $c . "html"; | |
#$info["admin_commands"] = "$c" . "quit, ". $c . "rejoin, " . $c . "nick, " . $c . "php, " . $c . "mode [nick]"; | |
$info["notice"] = "You can download the logs of the past 3 days at http://irc.openpetra.org"; | |
$dir = "bot_functions/"; | |
$dirh = opendir($dir); | |
while ($file = readdir($dirh)) { | |
if (substr($file, -4) == ".php") { | |
include_once($dir . $file); | |
} | |
} | |
closedir($dirh); | |
#while(true) | |
$bot->run(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment