Author Topic: Various bits of OmnomIRC Source Code  (Read 15324 times)

0 Members and 1 Guest are viewing this topic.

Offline Netham45

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2103
  • Rating: +213/-4
  • *explodes*
    • View Profile
Re: Various bits of OmnomIRC Source Code
« Reply #15 on: June 15, 2011, 10:40:16 pm »
OMNOMNOMNOM

PHP Source code finished finally.

Managed to do it without threads, w00t.

Code: [Select]
<?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
Code: (run.bat) [Select]
@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
Omnimaga Admin

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55941
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Various bits of OmnomIRC Source Code
« Reply #16 on: June 16, 2011, 12:36:48 am »
Cool to hear :D

Offline Netham45

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2103
  • Rating: +213/-4
  • *explodes*
    • View Profile
Re: Various bits of OmnomIRC Source Code
« Reply #17 on: June 19, 2011, 11:13:20 pm »
iOmnom_Mini source in (most)entirety(removed the message signing functions for obvious reasons.)

The view_omnom.php file is available elsewhere in this thread, I believe. The message.php file will be available when I get the SVN set up for OmnomIRC.
« Last Edit: June 19, 2011, 11:15:07 pm by Netham45 »
Omnimaga Admin

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55941
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Various bits of OmnomIRC Source Code
« Reply #18 on: June 23, 2011, 07:26:06 pm »
By the way if someone installs this on the same server as his forum, will it slow it down?

Offline Netham45

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2103
  • Rating: +213/-4
  • *explodes*
    • View Profile
Re: Various bits of OmnomIRC Source Code
« Reply #19 on: June 23, 2011, 07:29:45 pm »
By the way if someone installs this on the same server as his forum, will it slow it down?

It'd probably triple the load.
Omnimaga Admin

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Various bits of OmnomIRC Source Code
« Reply #20 on: June 24, 2011, 07:55:42 am »
I guess this is not so important now with the svn right?

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55941
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Various bits of OmnomIRC Source Code
« Reply #21 on: June 24, 2011, 11:30:31 pm »
Hmm I see, so I guess if he runs a VPS it needs to be a good one x.x. How much RAM does this require?

Offline Netham45

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2103
  • Rating: +213/-4
  • *explodes*
    • View Profile
Re: Various bits of OmnomIRC Source Code
« Reply #22 on: June 24, 2011, 11:33:22 pm »
The RAM requirement isn't as high as the CPU requirement is. My MySQL server is only using about 60MB of RAM (only a small fraction of which is OmnomIRC),

The CPU requirement still isn't very high. It could quite possibly run just fine on shared hosting.
Omnimaga Admin

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55941
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Various bits of OmnomIRC Source Code
« Reply #23 on: June 25, 2011, 02:17:09 pm »
Ah, but the problem with shared hosting is that it can't host bots, right? Because the thing is when there's no internet activity, the connection stops. Someone tried to run an IRC bot on TI-Freakware server once, and the bot would disconnect every minute or so.

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: Various bits of OmnomIRC Source Code
« Reply #24 on: March 23, 2014, 05:49:14 am »
Unstickied as its source is available here: https://github.com/Sorunome/OmnomIRC2

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!