OMNOMNOMNOM
PHP Source code finished finally.
Managed to do it without threads, w00t.
<?PHP
function sql_query()
{
global $sqlConnection;
$params = func_get_args();
$query = $params[0];
$args = Array();
for ($i=1;$i<count($params);$i++)
$args[$i-1] = mysql_real_escape_string(trim($params[$i]),$sqlConnection);
$result = mysql_query(vsprintf($query,$args),$sqlConnection);
if (!$result)
die(mysql_error() . "Query: " . vsprintf($query,$args));
return $result;
}
function sendLine($line)
{
global $socket;
$line = trim($line) . "\n";
socket_write($socket,$line);
echo "<<" . $line;
}
function getMessage($parts,$start,$trim)
{
if($trim)
$message = substr($parts[$start++],1);
for ($i = $start; $i < count($parts);$i++)
$message = $message . " " . $parts[$i];
$message = trim($message);
return $message;
}
function parseMsg($allMessage)
{
global $socket,$hasIdent,$ident;
$lines = explode("\n",$allMessage);
foreach ($lines as $Message)
{
$parts = explode(" ",$Message);
preg_match("/:(.*)!(.*)@(.*)/",$parts[0],$info);
$channel = strtolower($parts[2]);
$isChan = (substr($channel,0,1)=="#");
if (strtolower($parts[0]) == "ping")
sendLine("PONG " . trim($parts[1]) . "\n");
switch(strtolower($parts[1]))
{
case "privmsg":
if ($parts[3] == ":-snip-") sendLine(getMessage($parts,4,false));
if (!$isChan) break;
$message = getMessage($parts,3,true);
if (preg_match("/ACTION (.*)/",$message,$messageA))
addLine($info[1],'','action',$messageA[1],$channel);
else
addLine($info[1],'','message',$message,$channel);
break;
case "join":
addLine($info[1],'','join','',$channel);
break;
case "part":
$message = getMessage($parts,3,true);
addLine($info[1],'','part',$message,$channel);
break;
case "mode":
if (!$isChan) break;
$message = getMessage($parts,3,false);
addLine($info[1],'','mode',$message,$channel);
break;
case "kick":
$message = getMessage($parts,4,true);
addLine($info[1],$parts[3],"kick",$message,$channel);
break;
case "quit":
$message = getMessage($parts,2,true);
addLine($info[1],'',"quit",$message,'');
break;
case "topic":
$message = getMessage($parts,3,true);
addLine($info[1],'',"topic",$message,'');
break;
case "nick":
$message = getMessage($parts,2,true);
addLine($info[1],$message,"nick",'','');
break;
case "376":
sendLine("JOIN #omnimaga\n");
break;
}
}
}
function addLine($name1,$name2,$type,$message,$channel)
{
global $socket;
$curPosArr = mysql_fetch_array(sql_query("SELECT MAX('line_number') FROM `irc_lines`"));
$curPos = $curPosArr[0]+ 1;
sql_query("INSERT INTO `irc_lines` (`name1`,`name2`,`message`,`type`,`channel`,`time`) VALUES ('%s','%s','%s','%s','%s','%s')",$name1,$name2,$message,$type,$channel,date("[H:i:s]"));
sql_query("DELETE FROM `irc_lines` WHERE `line_number` < %s",$curPos - 1000);
}
function processMessages()
{
$res = sql_query("SELECT * FROM irc_outgoing_messages");
while ($row = mysql_fetch_array($res))
{
if ($row['action'] == 0)
sendLine("PRIVMSG $row[channel] :12(O)<$row[nick]> $row[message]");
if ($row['action'] == 1)
sendLine("PRIVMSG $row[channel] :12(O)6* $row[nick] $row[message]");
}
sql_query("DELETE FROM `irc_outgoing_messages`");
}
?>
<?PHP
error_reporting(0);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!socket_connect($socket,"irc.mzima.net",6667))
die("Could not connect. Error: " . socket_last_error());
socket_set_nonblock($socket);
$sqlConnection = mysql_connect("-snip-","-snip-","-snip-");
if (!$sqlConnection)
die("Could not connect to SQL DB.");
if (!mysql_select_db("omnomirc",$sqlConnection)) die('Invalid query: ' . mysql_error());
sql_query("DELETE FROM `irc_outgoing_messages`");
$ident = "PASS none\nUSER OmnomIRC OmnomIRC OmnomIRC :OmnomIRC\nNICK OmnomIRC\n";
sleep(1);
sendLine($ident);
while (true)
{
if ($recBuf = socket_read($socket,1024))
{
echo ">>" . $recBuf;
parseMsg($recBuf);
}
$errorcode = socket_last_error();
socket_clear_error();
if (!$recBuf && $errorcode == 0)
die("Connection lost!");
processMessages();
usleep(2000);
}
socket_close($socket);
?>
Running this with
@echo off
:run
time /T
echo Starting...
php OmnomIRC.php
echo DIED! Sleeping for 30 seconds...
choice /T 30 /c as /D a /n /m "Died! Restarting in 30 seconds..."
goto run