function injectcommentary($section, $talkline, $comment)
{
    global $session, $doublepost;
    // Make the comment pristine so that we match on it correctly.
    $comment = stripslashes($comment);
    $doublepost = 0;
    $emptypost = 0;
    $colorcount = 0;
    if ($comment != "") {
        $commentary = str_replace("`n", "", soap($comment));
        //maximum color code changes removed for performance reasons
        // $y = strlen($commentary);
        // for ($x=0;$x<$y;$x++){
        // if (substr($commentary,$x,1)=="`"){
        // $colorcount++;
        // if ($colorcount>=getsetting("maxcolors",10)){
        // $commentary = substr($commentary,0,$x).color_sanitize(substr($commentary,$x));
        // $x=$y;
        // }
        // $x++;
        // }
        // }
        $info = array();
        $info['rawcomment'] = $comment;
        $clanid = $session['user']['clanid'];
        if ($clanid && $session['user']['clanrank']) {
            $clansql = "SELECT clanname,clanshort FROM " . db_prefix("clans") . " WHERE clanid='{$clanid}'";
            $clanresult = db_query($clansql);
            $clanrow = db_fetch_assoc($clanresult);
            $info['clanname'] = $clanrow['clanname'];
            $info['clanshort'] = $clanrow['clanshort'];
            $info['clanid'] = $clanid;
            $info['clanrank'] = $session['user']['clanrank'];
        }
        if (!isset($session['user']['prefs']['ucol'])) {
            $session['user']['prefs']['ucol'] = false;
        } else {
            $info['talkcolour'] = $session['user']['prefs']['ucol'];
        }
        $args = array('commentline' => $commentary, 'commenttalk' => $talkline, 'info' => $info, 'name' => $session['user']['name'], 'section' => $section);
        $args = modulehook("postcomment", $args);
        //debug($args);
        if ($args['ignore'] == 1) {
            //A module tells us to ignore this comment, so we will
            return false;
        }
        $commentary = $args['commentline'];
        $talkline = $args['commenttalk'];
        $info = $args['info'];
        $name = $args['name'];
        $talkline = translate_inline($talkline);
        //Try to make it so that italics are always closed properly
        $italics = substr_count($commentary, "`i");
        if ($italics) {
            if ($odd = $italics % 2) {
                //odd number of italics - add one at the end
                $commentary .= "`i";
            }
        }
        //Clean up the comment a bit
        $commentary = preg_replace("'([^[:space:]]{45,45})([^[:space:]])'", "\\1 \\2", $commentary);
        $commentary = addslashes($commentary);
        // Sort out /game switches
        if (substr($commentary, 0, 5) == "/game" && ($session['user']['superuser'] & SU_IS_GAMEMASTER) == SU_IS_GAMEMASTER) {
            //handle game master inserts now, allow double posts
            injectsystemcomment($section, $commentary);
        } else {
            //check for double posts
            $commentbuffer = datacache("commentary/latestcommentary_" . $section, 60);
            if (is_array($commentbuffer)) {
                if ($commentbuffer[0]['comment'] == stripslashes($commentary)) {
                    $doublepost = true;
                }
            } else {
                $sql = "SELECT comment FROM " . db_prefix("commentary") . " WHERE section='{$section}' AND author='" . $session['user']['acctid'] . "' ORDER BY commentid DESC LIMIT 1";
                $result = db_query($sql);
                $row = db_fetch_assoc($result);
                db_free_result($result);
                if ($row['comment'] == stripslashes($commentary)) {
                    $doublepost = true;
                }
            }
            if (!$doublepost) {
                //Not a double post, inject the comment
                injectrawcomment($section, $session['user']['acctid'], $commentary, $session['user']['name'], $info);
                $session['user']['laston'] = date("Y-m-d H:i:s");
            }
        }
    }
}
function injectcommentary($section, $talkline, $comment, $schema = false)
{
    global $session, $doublepost, $translation_namespace;
    if ($schema === false) {
        $schema = $translation_namespace;
    }
    // Make the comment pristine so that we match on it correctly.
    $comment = stripslashes($comment);
    tlschema("commentary");
    $doublepost = 0;
    $emptypost = 0;
    $colorcount = 0;
    if ($comment != "") {
        $commentary = str_replace("`n", "", soap($comment));
        $y = strlen($commentary);
        for ($x = 0; $x < $y; $x++) {
            if (substr($commentary, $x, 1) == "`") {
                $colorcount++;
                if ($colorcount >= getsetting("maxcolors", 10)) {
                    $commentary = substr($commentary, 0, $x) . color_sanitize(substr($commentary, $x));
                    $x = $y;
                }
                $x++;
            }
        }
        $args = array('commentline' => $commentary, 'commenttalk' => $talkline);
        $args = modulehook("commentary", $args);
        //Begin CMJ Patch to allow players to use the /special switch
        if ($args['ignore'] == 1) {
            //Ignore this comment, it is likely a side-effect of using the Special switch
            return;
        }
        $commentary = $args['commentline'];
        $talkline = $args['commenttalk'];
        tlschema($schema);
        $talkline = translate_inline($talkline);
        tlschema();
        $commentary = preg_replace("'([^[:space:]]{45,45})([^[:space:]])'", "\\1 \\2", $commentary);
        $commentary = addslashes($commentary);
        // do an emote if the area has a custom talkline and the user
        // isn't trying to emote already.
        if ($talkline != "says" && substr($commentary, 0, 1) != ":" && substr($commentary, 0, 2) != "::" && substr($commentary, 0, 3) != "/me" && substr($commentary, 0, 5) != "/game") {
            $commentary = ":`3{$talkline}, \\\"`#{$commentary}`3\\\"";
        }
        if (substr($commentary, 0, 5) == "/game" && ($session['user']['superuser'] & SU_IS_GAMEMASTER) == SU_IS_GAMEMASTER) {
            //handle game master inserts now, allow double posts
            injectsystemcomment($section, $commentary);
        } else {
            $sql = "SELECT comment,author FROM " . db_prefix("commentary") . " WHERE section='{$section}' ORDER BY commentid DESC LIMIT 1";
            $result = db_query($sql);
            $row = db_fetch_assoc($result);
            db_free_result($result);
            if ($row['comment'] != stripslashes($commentary) || $row['author'] != $session['user']['acctid']) {
                injectrawcomment($section, $session['user']['acctid'], $commentary);
                $session['user']['laston'] = date("Y-m-d H:i:s");
            } else {
                $doublepost = 1;
            }
        }
        tlschema();
    }
}
function injectcommentary($section, $talkline, $comment)
{
    global $session, $doublepost;
    // Make the comment pristine so that we match on it correctly.
    $comment = stripslashes($comment);
    $doublepost = 0;
    $emptypost = 0;
    $colorcount = 0;
    if ($comment != "") {
        $commentary = str_replace("`n", "", soap($comment));
        //removed for performance
        // $y = strlen($commentary);
        // for ($x=0;$x<$y;$x++){
        // if (substr($commentary,$x,1)=="`"){
        // $colorcount++;
        // if ($colorcount>=getsetting("maxcolors",10)){
        // $commentary = substr($commentary,0,$x).color_sanitize(substr($commentary,$x));
        // $x=$y;
        // }
        // $x++;
        // }
        // }
        $info = array();
        $clanid = $session['user']['clanid'];
        if ($clanid) {
            $clansql = "SELECT clanname,clanshort FROM " . db_prefix("clans") . " WHERE clanid='{$clanid}'";
            $clanresult = db_query($clansql);
            $clanrow = db_fetch_assoc($clanresult);
            $info['clanname'] = $clanrow['clanname'];
            $info['clanshort'] = $clanrow['clanshort'];
            $info['clanid'] = $clanid;
        }
        $args = array('commentline' => $commentary, 'commenttalk' => $talkline, 'info' => $info, 'name' => $session['user']['name']);
        $args = modulehook("commentary", $args);
        if ($args['ignore'] == 1) {
            //Ignore this comment, it is likely a side-effect of using the Special switch
            return;
        }
        $commentary = $args['commentline'];
        $talkline = $args['commenttalk'];
        $info = $args['info'];
        $name = $args['name'];
        $talkline = translate_inline($talkline);
        //Clean up the comment a bit
        $commentary = preg_replace("'([^[:space:]]{45,45})([^[:space:]])'", "\\1 \\2", $commentary);
        $commentary = addslashes($commentary);
        // do an emote if the area has a custom talkline and the user
        // isn't trying to emote already.
        if ($talkline != "says" && substr($commentary, 0, 1) != ":" && substr($commentary, 0, 2) != "::" && substr($commentary, 0, 3) != "/me" && substr($commentary, 0, 5) != "/game") {
            $commentary = ":`3{$talkline}, \\\"`#{$commentary}`3\\\"";
        }
        // Sort out /game switches
        if (substr($commentary, 0, 5) == "/game" && ($session['user']['superuser'] & SU_IS_GAMEMASTER) == SU_IS_GAMEMASTER) {
            //handle game master inserts now, allow double posts
            injectsystemcomment($section, $commentary);
        } else {
            //This query checks for double posts
            $sql = "SELECT comment,author FROM " . db_prefix("commentary") . " WHERE section='{$section}' ORDER BY commentid DESC LIMIT 1";
            $result = db_query($sql);
            $row = db_fetch_assoc($result);
            db_free_result($result);
            if ($row['comment'] != stripslashes($commentary) || $row['author'] != $session['user']['acctid']) {
                //Not a double post, inject the comment
                injectrawcomment($section, $session['user']['acctid'], $commentary, $session['user']['name'], $info);
                $session['user']['laston'] = date("Y-m-d H:i:s");
            } else {
                $doublepost = 1;
            }
        }
    }
}