Omnimaga

Omnimaga => Site Feedback and Questions => OmnomIRC Development => Topic started by: Netham45 on April 15, 2011, 06:59:27 am

Title: Various bits of OmnomIRC Source Code
Post by: Netham45 on April 15, 2011, 06:59:27 am
Edit: SVN! http://ourl.ca/11696

Here's the new log parser for OmnomIRC. I'm planning to start open-sourcing components as soon as I'm not totally ashamed of the code in 'em. Regex's are awesome.

Log Parser
Spoiler For view_omnom.php:
Code: [Select]
<?PHP
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");  
?>
<link rel="stylesheet" type="text/css" href="http://209.90.113.101/omnimaganoie.css" />
<?PHP
$noShowNames = array("omnomirc","omnimaga","spybot45","spybot46");
function parseColors($colorStr)
{
$colorStr = htmlspecialchars($colorStr);
$colorStr = clickable_links($colorStr);
//$colorStr = preg_replace("/^  &lt;(.+?)&gt;/",'   &lt;<a target="_parent" href="http://www.omnimaga.org/index.php?action=ezportal;sa=page;p=13&userSearch=\1">\1</a>&gt;',$colorStr);
preg_match("/^  &lt;(.+?)&gt;/",$colorStr,$arrayName);
if (count($arrayName) > 0)
{
$colorStr = preg_replace("/^  &lt;(.+?)&gt;/",getOmnomNameColor($arrayName[1]),$colorStr);
}
$lcount = 0;
while(strpos($colorStr,"\x03") > 0)
{
preg_match("/(^.*)\x03([0-9]{1,2}),([0-9]{1,2})(.*)/",$colorStr,$arrayResults);
preg_match("/(^.*)\x03([0-9]{1,2})(.*)/",$colorStr,$arrayResults2);
if(count($arrayResults) > 4) //FG & BG
{
$colorStr = $arrayResults[1].'<span class="fg-'.$arrayResults[2]*1 .'"><span class="bg-'.$arrayResults[3]*1 .'">'.$arrayResults[4];
}
elseif(count($arrayResults2) > 3) //FG Only
{
$colorStr = $arrayResults2[1].'<span class="fg-' . $arrayResults2[2]*1 . '">'.$arrayResults2[3];
}
else //We have a color control character w/o a color, most clients interperet this as clear colors.
{
for($lcount;$lcount;$lcount--)
$colorStr=$colorStr."</span>";
$colorStr = preg_replace("/\x03/","",$colorStr,1);
$lcount--;
}
$lcount++;
}
for(;$lcount>=0;$lcount--)
$colorStr=$colorStr."</span>";
/*Strip codes*/
$colorStr = preg_replace("(\x03|\x02|\x1F|\x09|\x0F)","",$colorStr);
$colorStr = str_replace("\x07","&lt;",$colorStr); // Removes beeps and is functional!
return($colorStr);
}
function clickable_links($text) {
$text = str_replace("http://www.omnimaga.org/","h111://www.omnimaga.org/",$text);
$text = str_replace("http://omniurl.tk/","h111://omniurl.tk/",$text);
$text = eregi_replace('((h111://(www.omnimaga.org/|omniurl.tk))[[email protected]:;%_+.~#?&//=]+)', '<a target="_parent" href="\1">\1</a>', $text);
$text = eregi_replace('(((f|ht){1}(tp|tps)://)[[email protected]:;%_+.~#?&//=]+)', '<a target="_blank" href="\1">\1</a>', $text);
$text = eregi_replace('([[:space:]()[{}])(www.[[email protected]:;%_+.~#?&//=]+)', '\1<a target="_blank" href="http://\2">\2</a>', $text);
$text = str_replace("h111","http",$text);
return $text;
}
function getNameColor($name)
{
$rcolors = array(19, 20, 22, 24, 25, 26, 27, 28, 29);
$sum = $i = 0;
while (isset($name[$i]))
$sum += ord($name[$i++]);
$sum %= 9;
return '<span class="uName-'.$rcolors[$sum].'">'.$name.'</span>';
}
function getOmnomNameColor($name)
{
$rcolors = array(19, 20, 22, 24, 25, 26, 27, 28, 29);
$sum = $i = 0;
while (isset($name[$i]))
$sum += ord($name[$i++]);
$sum %= 9;
return '  &lt;<a target="_parent" href="http://www.omnimaga.org/index.php?action=ezportal;sa=page;p=13&userSearch='.$name.'"><span class="uName-'.$rcolors[$sum].'">'.$name.'</span></a>&gt';
}
$myFile = "#" . $_GET["log"] . "." . date("Ymd") . ".log";
$arrayFile = file($myFile);
$count = count($arrayFile);
if (isset($_GET["trim"]))
$startAt = $count - $_GET["trim"];
$topic = $output = "";
$highlight = "\xFF";
if (isset($_GET["high"]))
$highlight = base64_decode($_GET["high"]);
for($i = 0;$i<$count;$i++)
{
$curLine = $arrayFile[$i];
//Try to get the topic
preg_match("/(Topic is|changes topic to) '(.*.)'/",$curLine,$topicGet);
if(count($topicGet))
$topic = $topicGet[2];
if ($i < $startAt) continue;

preg_match("/(^.*)(\[[0-9]{2}:[0-9]{2}:[0-9]{2}\]).\<(.+?)>(.*)/",$curLine,$curLineParts); //Try to match color, timestamp, message, and name
if(count($curLineParts) > 3)
{
$curLineParts[3] = str_replace("@","",$curLineParts[3]);
$curLineParts[3] = str_replace("+","",$curLineParts[3]);
$output = $output . '<span class="irc-date">'.$curLineParts[2].'</span>';
if (strpos(strtolower($curLineParts[4]),strtolower($highlight),in_array(strtolower($curLineParts[3]),$noShowNames)?strpos($curLineParts[4]," ",5):0))
$output = $output . '<span style="font-weight:bold;color:#F44">';
if (!in_array(strtolower($curLineParts[3]),$noShowNames))
{
$output = $output . " &lt;" . getNameColor($curLineParts[3]) . "&gt;";
$curLineParts[4] = str_replace("<","\x07",$curLineParts[4]);
}
$output = $output . parseColors(" ".$curLineParts[1].$curLineParts[4]);
if (strpos($curLineParts[4],$highlight))
$output = $output . '</span>';
}
else
{
preg_match("/(^.*)(\[[0-9]{2}:[0-9]{2}:[0-9]{2}\])(.*)/",$curLine,$curLineParts); //Try to match color, timestamp and message, no name
if(count($curLineParts)>3)
{
$output = $output . '<span class="irc-date">'.$curLineParts[2].'</span>';
$output = $output . parseColors(" ".$curLineParts[1] . $curLineParts[3]);
}
else //Give up and print line
{
$output = $output . parseColors($curLine);
}
}
$output = $output . '<br/>';
}
?>
<span class="irc-topic">Current Topic:<?PHP echo parseColors($topic); ?></span><br/><br/>
<?PHP echo $output; ?>

The CSS for the previous document:
Spoiler For omnimaganoie.css:
Code: [Select]
body, td, th , tr     {font-size: 10px;font-family:verdana,sans-serif;}

.irc-date       {color: #000000;}
.irc-topic     {text-decoration :none;font-size:12;color:#333333;font-family:Arial;border-style:dashed;border-width:1px;width:99%;position:absolute;text-align:center;}
.irc-green     {color: #009200;}
.irc-black     {color: #000000;}
.irc-brown     {color: #7b0000;}
.irc-navy       {color: #00007b;}
.irc-brick     {color: #9c009c;}
.irc-red       {color: #ff0000;}
.irc-teal     {color: #1A5555;}

.fg-white, .fg-0   {color: #FFFFFF;}
.fg-black, .fg-1   {color: #000000;}
.fg-dkBlue, .fg-2   {color: #3636B2;}
.fg-dkgreen, .fg-3   {color: #2A8C2A;}
.fg-red, .fg-4   {color: #C33B3B;}
.fg-dkRed , .fg-5   {color: #C73232;}
.fg-dkPurple, .fg-6   {color: #80267F;}
.fg-orange, .fg-7   {color: #E19317;}
.fg-yellow, .fg-8   {color: #D9A641;}
.fg-green, .fg-9   {color: #3DCC3D;}
.fg-teal, .fg-10 {color: #1A5555;}
.fg-ltBlue, .fg-11 {color: #2F8C74;}
.fg-blue, .fg-12 {color: #4545E6;}
.fg-pink, .fg-13 {color: #B037B0;}
.fg-dkGray, .fg-14 {color: #4C4C4C;}
.fg-ltGray, .fg-15 {color: #959595;}

.bg-white, .bg-0   {}/*Placeholder*/
.bg-black, .bg-1   {background-color: #000000;}
.bg-dkBlue, .bg-2   {background-color: #3636B2;}
.bg-dkgreen, .bg-3   {background-color: #2A8C2A;}
.bg-red, .bg-4   {background-color: #C33B3B;}
.bg-dkRed , .bg-5   {background-color: #C73232;}
.bg-dkPurple, .bg-6   {background-color: #80267F;}
.bg-orange, .bg-7   {background-color: #E19317;}
.bg-yellow, .bg-8   {background-color: #D9A641;}
.bg-green, .bg-9   {background-color: #3DCC3D;}
.bg-teal, .bg-10 {background-color: #1A5555;}
.bg-ltBlue, .bg-11 {background-color: #2F8C74;}
.bg-blue, .bg-12 {background-color: #4545E6;}
.bg-pink, .bg-13 {background-color: #B037B0;}
.bg-dkGray, .bg-14 {background-color: #4C4C4C;}
.bg-ltGray, .bg-15 {background-color: #959595;}
Spoiler For Spybot45 code:
Code: (Spybot45.php) [Select]
<?PHP
$sqlConnection = mysql_connect("Omnimaga.org","omnimaga_Netham","-Snip-");
if (!$sqlConnection)
die("Could not connect to SQL DB.");
if (!mysql_select_db("omnimaga_forums",$sqlConnection)) die('Invalid query: ' . mysql_error());

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($params[$i],$sqlConnection);
$result = mysql_query(vsprintf($query,$args),$sqlConnection);
if (!$result)
die(mysql_error());
return $result;
}

function postMessage($Poster,$Topic,$tID,$mID,$board)
{
$frBoards = array('101','105','169','171');
$adminBoards = array('68','3','44');
$boardsToExclude = array('2','20','69','125','172','173');
if (in_array($board,$boardsToExclude))
return false;
if (in_array($board,$frBoards))
{
$WriteLine = sprintf("12(O)10 Nouveau message par03 %s10 dans04 %s12 http://omniurl.tk/%s/%s\n",$Poster,$Topic,$tID,$mID);
$fileName = "c:\\Omnimaga IRC Spybot\\fposts";
}
else if (in_array($board,$adminBoards))
{
$WriteLine = sprintf("12(O)10 New post by03 %s10 in04 %s12 http://omniurl.tk/%s/%s\n",$Poster,$Topic,$tID,$mID);
$fileName = "c:\\Omnimaga IRC Spybot\\aposts";
}
else
{
$WriteLine = sprintf("12(O)10 New post by03 %s10 in04 %s12 http://omniurl.tk/%s/%s\n",$Poster,$Topic,$tID,$mID);
$fileName = "c:\\Omnimaga IRC Spybot\\posts";
}
$fp = fopen($fileName, 'w');
fwrite($fp,$WriteLine);
fclose($fp);
}

$curMID = 0;

while(true)
{
usleep(1000000);
$message = mysql_fetch_array(sql_query("SELECT `id_msg`,`id_topic`,`posterName` FROM `omnitempboard_messages` WHERE id_msg = (SELECT MAX(`id_msg`) FROM `omnitempboard_messages`)"));
$mID = $message['id_msg'];

if ($mID == $curMID)
continue;

$curMID = $mID;

$tID = $message['id_topic'];
$mPoster = $message['posterName'];

$topic = mysql_fetch_array(sql_query("SELECT `id_board`,`id_first_msg` FROM `omnitempboard_topics` WHERE `id_topic` = %s",$tID));
$tBoard = $topic['id_board'];
$tFirstID = $topic['id_first_msg'];

$fMessage = mysql_fetch_array(sql_query("SELECT `subject` FROM `omnitempboard_messages` WHERE `id_msg` = %s",$tFirstID));
$tTopicName = $fMessage['subject'];
postMessage($mPoster,$tTopicName,$tID,$mID,$tBoard);
echo sprintf("New Post by %s in %s http://omniurl.tk/%s/%s Board ID:%s\n", $mPoster,$tTopicName,$tID,$mID,$tBoard);
}
?>


Edit: I suppose I should attach a license to this. Do whatever you want with the code, but I'd like credit somewhere if you use it. If you wish to use it for a corporate or profitable purpose, contact me for permission.
Title: Re: Various bits of OmnomIRC Source Code
Post by: Munchor on April 15, 2011, 07:56:59 am
That's good Netham45, I love open source, but why put the code in spoilers, though? It's hard to read :P
Title: Re: Various bits of OmnomIRC Source Code
Post by: Jim Bauwens on April 15, 2011, 03:34:03 pm
Yay, this is nice! Thanks Netham :)
Title: Re: Various bits of OmnomIRC Source Code
Post by: DJ Omnimaga on April 15, 2011, 04:55:54 pm
Nice! Are you planning to eventually release SpyBot45 as a SMF hack?

Hopefully people can maybe help on changing stuff or adding features too.
Title: Re: Various bits of OmnomIRC Source Code
Post by: Munchor on April 15, 2011, 04:56:58 pm
Nice! Are you planning to eventually release SpyBot45 as a SMF hack?

Hopefully people can maybe help on changing stuff or adding features too.

That would be a great idea, lots of downloads I'm sure!
Title: Re: Various bits of OmnomIRC Source Code
Post by: Juju on April 16, 2011, 12:09:35 am
Yep, can't wait to see the entire source code :D
Title: Re: Various bits of OmnomIRC Source Code
Post by: Netham45 on May 05, 2011, 03:10:47 am
Here's the interesting parts to iOmnom Mini (Note that this code was written in somewhat of a hurry, so the names of the files don't quite make sense, and I haven't refined the code.)


This only exists to be the target for the frame(holding the content div), and to handle the callback function.
Spoiler For chat.php:
Code: [Select]
<?PHP
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>
<html>
<head>
<title>chat</title>
<script type="text/javascript">
function callback(text)
{
document.getElementById("content").innerHTML = atob(text);
}
</script>
</head>
<body onload="window.location.hash='bottom';">
<div id="content">
<?PHP
define(SMF,"Yup.",true);
include("../Sources/Subs-Package.php");
$theURL = "http://209.90.113.101/IRC/EFNet/view_omnom.php?log=omnimaga&current=true&trim=17";
if (isset($_GET['high']))
$theURL = $theURL . "&high=" . $_GET['high'];
$contents = fetch_web_data($theURL);
echo $contents;
?>
</div>
<a name="bottom"/>
<script type="text/javascript" src="chat2.php">
</script>
</body>
</html>

This is where the real magic happens.

This page sleeps for 5 seconds on every request. It then sends the current chat view, and a bit of javascript to make another script tag to call itself again. That way it keeps a constant connection open, bypassing Opera Mini (and apparently the PSP's browser, according to Frey) limitations of no streaming AJAX and no programmatic refreshing.

Spoiler For chat2.php:
Code: [Select]
<?PHP
header('Content-type: text/javascript');
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
sleep(5);
define(SMF,"Yup.",true);
include("../Sources/Subs-Package.php");
$theURL = "http://209.90.113.101/IRC/EFNet/view_omnom.php?log=omnimaga&current=true&trim=17";
if (isset($_GET['high']))
$theURL = $theURL . "&high=" . $_GET['high'];
$contents = fetch_web_data($theURL);
echo 'callback("'.base64_encode($contents).'");';
?>
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';
script.src= 'chat2.php?uid=<?PHP echo gmdate("DdMYH:i:s");?>';
head.appendChild(script);
window.location.hash='a';
window.location.hash='bottom';

This one was altered from the live code to remove the signature generation code. It just handles the form for sending a message and creates the frame for chat.php.

It also handles logging in if you're not already.
Spoiler For index.php:
Code: [Select]
<?PHP
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
require(dirname(__FILE__) . '/../SSI.php');

?>
<html>
<head>
<title>iOmnom Mini</title>
</head>
<body>
<?PHP
if ($user_info['is_guest'])
{
ssi_login("http://www.omnimaga.org/iOmnom_Mini/index.php");
}
else
{
function sign($encstr) {
return "lolnope";
}
echo '
<script type="text/javascript">

</script>
<iframe src="chat.php#bottom" style="width:100%;height:75%;border:0;" scrolling="no" border=0>Please use a browser that supports frames.</iframe>
<form action="messagebounce.php">
<input type="hidden" name="name" value="'.$user_info[name].'"/>
<input type="hidden" name="chan" value="omnimaga"/>
<input type="hidden" name="signature" value="'.sign($user_info[name]).'"/>
<input type="text" name="message" maxlength=100 style="width:100%;height:10%;"/><br/>
<input type="Submit" value="Send" style="width:100%;height:10%;"/>
</form>
';
}
?>
</body>
</html>

This is just what sends the message to my server. Pretty simple.
Spoiler For messagebounce.php:
Code: [Select]
<?php
define
(SMF,"Yup.",true);
include(
"../Sources/Subs-Package.php");
fetch_web_data("http://209.90.113.101/irc/EFNet/message.php?message=" base64_encode(urldecode(stripslashes($_GET["message"]))) . "&signature=" $_GET["signature"] . "&name=" base64_encode(urldecode(stripslashes($_GET["name"]))) . "&chan=" $_GET["chan"]);
header("location:index.php");
?>

Title: Re: Various bits of OmnomIRC Source Code
Post by: Jim Bauwens on May 05, 2011, 03:21:05 am
Very nice! Thanks for the code :D
Title: Re: Various bits of OmnomIRC Source Code
Post by: Netham45 on May 10, 2011, 02:35:33 pm
New Spybot45 code posted. :D
Title: Re: Various bits of OmnomIRC Source Code
Post by: DJ Omnimaga on May 12, 2011, 04:09:44 am
This is nice that you are documenting/open-sourcing OmnomIRC. Hopefully maybe the idea can be re-implemented elsewhere.
Title: Re: Various bits of OmnomIRC Source Code
Post by: ruler501 on May 12, 2011, 08:43:30 am
Who knows maybe more forums will start having their own IRC channel you can talk in at the top.

I love that your open sourcing this.
Title: Re: Various bits of OmnomIRC Source Code
Post by: Netham45 on May 12, 2011, 10:13:27 pm
I woulda open-sourced the old one if the code wasn't so ugly. :P


mIRC bot! Using http://reko.tiira.net/mmysql/ for SQL connectivity.

Code: (aliases) [Select]
/sql_config {
  set %db omnomirc
  set %server -snip-
  set %uname -snip-
  set %pass -snip-
}
/sql_connect {
  if (%connection) echo Already connected! Not trying again.
  if (!%connection)  {
    sql_config
    set %connection $mysql_connect( %server , %uname , %pass )
    echo $iif(%connection,Connected Successfully,Failed to connect)
    echo $iif($mysql_select_db( %connection , %db ),DB Selected correctly!,Failed to select DB!)
  }
}
/sql_close {
  noop $mysql_close(%connection)
}
/sql_clean {
  var %sql SELECT MAX(line_number) FROM irc_lines
  var %result $mysql_query(%connection,%sql)
  noop $mysql_fetch_row(%result,row)
  set %lines $hget(row,1)
  set %sql DELETE * FROM irc_lines WHERE line_number < ?
  noop $mysql_exec(%connection, $calc(%lines-1000))
}

/update_names {
  if (!%connection)  sql_connect
  noop $mysql_exec(%connection, DELETE FROM irc_users)
  set %sql INSERT INTO irc_users (username,channel,online) VALUES (?,?,0)
  set %nickpos 1
  while ( %nickpos <= $nick( #omnimaga-test , 0 , a )) {
    noop $mysql_exec( %connection , %sql , $nick( $chan , %nickpos , a ) , #omnimaga-test )
    inc %nickpos 1
  }
}

/check_messages {
  if (!%connection) sql_connect
  var %sql = SELECT * FROM irc_outgoing_messages
  var %res = $mysql_query(%connection,%sql)
  while ($mysql_fetch_row(%res,row)) {
    if ($hget(row,action) == 0)    msg $hget(row, channel) < $+ $hget(row,nick) $+ > $hget(row,message)
    if ($hget(row,action) == 1)    msg $hget(row, channel) 6* $hget(row,nick) $hget(row,message)

  }
  var %sql = DELETE FROM irc_outgoing_messages
  noop $mysql_exec(%connection,%sql)
}
Code: (remote) [Select]
on *:TEXT:*:#:{
  if (!%connection)  sql_connect
  set %sql INSERT INTO irc_lines (name1,message,type,channel,time) VALUES (?,?,?,?,?)
  noop $mysql_exec(%connection,%sql,$nick,$1-,message,$chan,$chr(91) $+ $asctime(HH:nn:ss) $+ $chr(93))
}
on *:ACTION:*:#:{
  if (!%connection)  sql_connect
  set %sql INSERT INTO irc_lines (name1,message,type,channel,time) VALUES (?,?,?,?,?)
  noop $mysql_exec(%connection,%sql,$nick,$1-,action,$chan,$chr(91) $+ $asctime(HH:nn:ss) $+ $chr(93))
}
on *:JOIN:#:{
  if (!%connection)  sql_connect
  set %sql INSERT INTO irc_lines (name1,type,channel,time) VALUES (?,?,?,?)
  noop $mysql_exec(%connection,%sql,$nick,join,$chan,$chr(91) $+ $asctime(HH:nn:ss) $+ $chr(93))
}

on *:PART:#:{
  if (!%connection)  sql_connect
  set %sql INSERT INTO irc_lines (name1,type,channel,time) VALUES (?,?,?,?)
  noop $mysql_exec(%connection,%sql,$nick,part,$chan,$chr(91) $+ $asctime(HH:nn:ss) $+ $chr(93))
}
on *:QUIT:{
  if (!%connection)  sql_connect
  set %sql INSERT INTO irc_lines (name1,message,type,channel,time) VALUES (?,?,?,?,?)
  noop $mysql_exec(%connection,%sql,$nick,$1-,quit,$chan,$chr(91) $+ $asctime(HH:nn:ss) $+ $chr(93))
}

on *:RAWMODE:#:{
  if (!%connection)  sql_connect
  set %sql INSERT INTO irc_lines (name1,message,type,channel,time) VALUES (?,?,?,?,?)
  noop $mysql_exec(%connection,%sql,$nick,$1-,mode,$chan,$chr(91) $+ $asctime(HH:nn:ss) $+ $chr(93))
}

on *:KICK:#:{
  if (!%connection)  sql_connect
  set %sql INSERT INTO irc_lines (name1,name2,message,type,channel,time) VALUES (?,?,?,?,?,?)
  noop $mysql_exec(%connection,%sql,$nick,$knick,$1-,kick,$chan,$chr(91) $+ $asctime(HH:nn:ss) $+ $chr(93))
}
on *:NICK:{
  if (!%connection)  sql_connect
  set %sql INSERT INTO irc_lines (name1,name2,message,type,channel,time) VALUES (?,?,?,?,?,?)
  noop $mysql_exec(%connection,%sql,$nick,$newnick,$1-,nick,$chan,$chr(91) $+ $asctime(HH:nn:ss) $+ $chr(93))
}
on *:TOPIC:#:{
  if (!%connection)  sql_connect
  set %sql INSERT INTO irc_lines (name1,message,type,channel,time) VALUES (?,?,?,?,?)
  noop $mysql_exec(%connection,%sql,$nick,$1-,topic,$chan,$chr(91) $+ $asctime(HH:nn:ss) $+ $chr(93))
}
Title: Re: Various bits of OmnomIRC Source Code
Post by: DJ Omnimaga on May 13, 2011, 04:03:45 pm
Is that for IRC logs? I didn't know OmnomIRC now used its own MySQL database.
Title: Re: Various bits of OmnomIRC Source Code
Post by: ruler501 on May 13, 2011, 06:17:12 pm
I can't understand any of that code. i only program Python and C/C++
Title: Re: Various bits of OmnomIRC Source Code
Post by: Netham45 on May 13, 2011, 06:19:11 pm
Is that for IRC logs? I didn't know OmnomIRC now used its own MySQL database.

The IRC logs are still using files, same as before. OmnomIRC is using SQL, because it was considerably easier than keeping track of things in files, and should yield higher performance over time.
Title: Re: Various bits of OmnomIRC Source Code
Post by: Netham45 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
Title: Re: Various bits of OmnomIRC Source Code
Post by: DJ Omnimaga on June 16, 2011, 12:36:48 am
Cool to hear :D
Title: Re: Various bits of OmnomIRC Source Code
Post by: Netham45 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.
Title: Re: Various bits of OmnomIRC Source Code
Post by: DJ Omnimaga 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?
Title: Re: Various bits of OmnomIRC Source Code
Post by: Netham45 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.
Title: Re: Various bits of OmnomIRC Source Code
Post by: Munchor on June 24, 2011, 07:55:42 am
I guess this is not so important now with the svn right?
Title: Re: Various bits of OmnomIRC Source Code
Post by: DJ Omnimaga 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?
Title: Re: Various bits of OmnomIRC Source Code
Post by: Netham45 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.
Title: Re: Various bits of OmnomIRC Source Code
Post by: DJ Omnimaga 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.
Title: Re: Various bits of OmnomIRC Source Code
Post by: Sorunome on March 23, 2014, 05:49:14 am
Unstickied as its source is available here: https://github.com/Sorunome/OmnomIRC2