function addcommentary()
{
    global $session, $emptypost, $afk, $dni;
    $section = httppost('section');
    $talkline = httppost('talkline');
    $schema = httppost('schema');
    $comment = trim(httppost('insertcommentary'));
    $counter = httppost('counter');
    $remove = URLDecode(httpget('removecomment'));
    $restore = URLDecode(httpget('restorecomment'));
    //debug(httpallpost());
    if (httpget("bulkdelete")) {
        $everything = httpallpost();
        foreach ($everything as $key => $val) {
            if (substr($key, 0, 14) == "deletecomment_") {
                $del = str_replace("deletecomment_", "", $key);
                removecommentary($del, "Mass deletion by " . $session['user']['name'], $session['user']['acctid']);
            }
        }
    }
    if (!$comment) {
        return false;
    }
    if ($session['user']['chatloc'] == "DNI") {
        $dni = true;
    }
    if ($comment == strtoupper($comment)) {
        //this is an all-uppercase entry.  Do not add this comment to the database; instead, check it for built-in stuff like AFK and GREM, then run it through the commentarycommand hook
        if ($comment == "AFK" || $comment == "BRB") {
            $session['user']['chatloc'] = "AFK";
            $afk = true;
            output("`0`n`c`bYou are Away From the Keyboard until you load another page.`b`c`n");
            return false;
        }
        if ($comment == "DNI") {
            if ($session['user']['chatloc'] == "DNI") {
                $session['user']['chatloc'] = $section;
                $dni = false;
                output("`0`n`c`bYou are no longer in Do Not Interrupt status.`b`c`n");
            } else {
                $session['user']['chatloc'] = "DNI";
                $dni = true;
                output("`0`n`c`bYou are in Do Not Interrupt status.  Type DNI again to leave.`b`nDNI status is used for whenever you're doing or saying something that means other players shouldn't try to interact with you.  For example, when two or more characters are chatting just outside of the main group of characters, and other characters shouldn't be able to hear them.`c`n");
            }
            return false;
        }
        if ($comment == "GREM") {
            //handle deleting the player's last comment
            $sql = "SELECT * FROM " . db_prefix("commentary") . " WHERE author='" . $session['user']['acctid'] . "' ORDER BY commentid DESC LIMIT 1";
            $result = db_query($sql);
            while ($row = db_fetch_assoc($result)) {
                $now = time();
                $then = strtotime($row['postdate']);
                $ago = $now - $then;
                if ($ago < 120) {
                    removecommentary($row['commentid'], "Typo Gremlin", $session['user']['acctid']);
                    output("`0`n`c`bA nearby Typo Gremlin notices the peculiar tastiness of your previous comment.  Within moments, a small horde of them have descended upon your words, and consumed them.`b`c`n");
                } else {
                    output("`0`n`c`bThe Typo Gremlins turn up their nose at your latest comment - it's just too old.  They have no taste for stale words.`b`c`n");
                }
            }
            return false;
        }
        $hookcommand = array('command' => $comment, 'section' => $section);
        $returnedhook = modulehook("commentarycommand", $hookcommand);
        if (!$returnedhook['processed']) {
            debug($returnedhook);
            output("`c`b`JCommand Not Recognized`b`0`nWhen you type in ALL CAPS, the game doesn't think you're talking to other players; it thinks you're trying to perform an action within the game.  For example, typing `#GREM`0 will remove the last comment you posted, as long as you posted it less than two minutes ago.  Typing `#AFK`0 or `#BRB`0 will turn your online status bar grey, so that people know you're `#A`0way `#F`0rom the `#K`0eyboard (or, if you prefer, that you'll `#B`0e `#R`0ight `#B`0ack).  Typing `#DNI`0 will let other players know that you're busy talking to one particular player - maybe somewhere off-camera - and that you don't want to be interrupted right now.`nSome areas have special hidden commands or other easter eggs that you can hunt for.  This time around, you didn't trigger anything special.`c`0`n");
        }
        return false;
    }
    if ($remove > 0) {
        removecommentary($remove, "Moderated by " . $session['user']['name'], $session['user']['acctid']);
    }
    if ($restore > 0) {
        restorecommentary($restore, "Restored by " . $session['user']['name'], $session['user']['acctid']);
    }
    if (array_key_exists('commentcounter', $session) && $session['commentcounter'] == $counter) {
        if ($section || $talkline || $comment) {
            $tcom = color_sanitize($comment);
            if ($tcom == "" || $tcom == ":" || $tcom == "::" || $tcom == "/me") {
                $emptypost = 1;
            } else {
                injectcommentary($section, $talkline, $comment);
            }
        }
    }
}
function addcommentary()
{
    global $session, $emptypost, $afk, $dni;
    $section = httppost('section');
    $talkline = httppost('talkline');
    $schema = httppost('schema');
    $comment = trim(httppost('insertcommentary'));
    $counter = httppost('counter');
    $remove = URLDecode(httpget('removecomment'));
    $restore = URLDecode(httpget('restorecomment'));
    //debug(httpallpost());
    if (httpget("bulkdelete")) {
        $everything = httpallpost();
        foreach ($everything as $key => $val) {
            if (substr($key, 0, 14) == "deletecomment_") {
                $del = str_replace("deletecomment_", "", $key);
                removecommentary($del, "Mass deletion by " . $session['user']['name'], $session['user']['acctid']);
            }
        }
    }
    if ($remove > 0) {
        removecommentary($remove, "Moderated by " . $session['user']['name'], $session['user']['acctid']);
    }
    if ($restore > 0) {
        restorecommentary($restore, "Restored by " . $session['user']['name'], $session['user']['acctid']);
    }
    if (!$comment) {
        return false;
    }
    if ($session['user']['chatloc'] == "DNI") {
        $dni = true;
    }
    $colors = array("1" => "colDkBlue", "2" => "colDkGreen", "3" => "colDkCyan", "4" => "colDkRed", "5" => "colDkMagenta", "6" => "colDkYellow", "7" => "colDkWhite", "~" => "colBlack", "!" => "colLtBlue", "@" => "colLtGreen", "#" => "colLtCyan", "\$" => "colLtRed", "%" => "colLtMagenta", "^" => "colLtYellow", "&" => "colLtWhite", ")" => "colLtBlack", "e" => "colDkRust", "E" => "colLtRust", "g" => "colXLtGreen", "G" => "colXLtGreen", "j" => "colMdGrey", "k" => "colaquamarine", "K" => "coldarkseagreen", "l" => "colDkLinkBlue", "L" => "colLtLinkBlue", "m" => "colwheat", "M" => "coltan", "p" => "collightsalmon", "P" => "colsalmon", "q" => "colDkOrange", "Q" => "colLtOrange", "R" => "colRose", "T" => "colDkBrown", "t" => "colLtBrown", "V" => "colBlueViolet", "v" => "coliceviolet", "x" => "colburlywood", "X" => "colbeige", "y" => "colkhaki", "Y" => "coldarkkhaki");
    if (substr($comment, 0, 9) == "/chatcol ") {
        $ucol = substr($comment, 9, 1);
        if (!isset($colors[$ucol])) {
            output("`c`b`4Invalid default talk colour`b`nYou entered an invalid colour code, please try again.`0`c`n");
            return false;
        } else {
            $session['user']['prefs']['ucol'] = $ucol;
            output_notl("`c`bDefault talk colour changed`b`n`" . $ucol . "This is your new default commentary dialogue colour.  When you type in commentary areas, this colour will show up automatically.  If you're an Extra-Awesome Site Supporter, you can also change the colour of your character's dialogue during quests, monster fights and other in-game areas using the /talkcol switch, in the same way you just used the /chatcol switch.`0`c`n");
            return false;
        }
    } else {
        if (substr($comment, 0, 9) == "/talkcol ") {
            $ucol = substr($comment, 9, 1);
            if (!isset($colors[$ucol])) {
                output("`c`b`4Invalid default talk colour`b`nYou entered an invalid colour code, please try again.`0`c`n");
                return false;
            } else {
                $session['user']['prefs']['ugcol'] = $ucol;
                if ($session['user']['donation'] >= 2000) {
                    output_notl("`c`bDefault talk colour changed`b`n`" . $ucol . "This is your new default in-game dialogue colour.  This is the colour we'll use to represent your character's dialogue during quests, monster encounters, and other in-game things.  If you choose a colour commonly used by monsters or other characters, you might have problems figuring out who's talking - if that's the case, you can reset to the default with \"/talkcol #\".  You can also change your default colour for commentary areas by using the /chatcol switch, in the same way you just used /talkcol.`0`c`n");
                } else {
                    output("`c`bNot enough Supporter Points`b`nSorry, but due to the extra system load that the /talkcol switch uses, this feature is restricted to players with more than 2,000 Supporter Points.`0`c`n");
                }
                return false;
            }
        }
    }
    if ($comment == strtoupper($comment)) {
        //this is an all-uppercase entry.  Do not add this comment to the database; instead, check it for built-in stuff like AFK and GREM, then run it through the commentarycommand hook
        if ($comment == "AFK" || $comment == "BRB") {
            $session['user']['chatloc'] = "AFK";
            $afk = true;
            output("`0`n`c`bYou are Away From the Keyboard until you load another page.`b`c`n");
            return false;
        }
        if ($comment == "DNI") {
            if ($session['user']['chatloc'] == "DNI") {
                $session['user']['chatloc'] = $section;
                $dni = false;
                output("`0`n`c`bYou are no longer in Do Not Interrupt status.`b`c`n");
            } else {
                $session['user']['chatloc'] = "DNI";
                $dni = true;
                output("`0`n`c`bYou are in Do Not Interrupt status.  Type DNI again to leave.`b`nDNI status is used for whenever you're doing or saying something that means other players shouldn't try to interact with you.  For example, when two or more characters are chatting just outside of the main group of characters, and other characters shouldn't be able to hear them.`c`n");
            }
            return false;
        }
        if ($comment == "GREM") {
            //handle deleting the player's last comment
            $sql = "SELECT * FROM " . db_prefix("commentary") . " WHERE author='" . $session['user']['acctid'] . "' ORDER BY commentid DESC LIMIT 1";
            $result = db_query($sql);
            while ($row = db_fetch_assoc($result)) {
                $now = time();
                $then = strtotime($row['postdate']);
                $ago = $now - $then;
                if ($ago < 120) {
                    removecommentary($row['commentid'], "Typo Gremlin", $session['user']['acctid']);
                    output("`0`n`c`bA nearby Typo Gremlin notices the peculiar tastiness of your previous comment.  Within moments, a small horde of them have descended upon your words, and consumed them.`b`c`n");
                } else {
                    output("`0`n`c`bThe Typo Gremlins turn up their nose at your latest comment - it's just too old.  They have no taste for stale words.`b`c`n");
                }
            }
            return false;
        }
        $hookcommand = array('command' => $comment, 'section' => $section);
        $returnedhook = modulehook("commentarycommand", $hookcommand);
        if (!$returnedhook['skipcommand']) {
            //if for some reason you're going to involve a command that can be a mix of upper and lower case, set $args['skipcommand'] and $args['ignore'] to true and handle it in postcomment instead.
            if (!$returnedhook['processed']) {
                output("`c`b`JCommand Not Recognized`b`0`nWhen you type in ALL CAPS, the game doesn't think you're talking to other players; it thinks you're trying to perform an action within the game.  For example, typing `#GREM`0 will remove the last comment you posted, as long as you posted it less than two minutes ago.  Typing `#AFK`0 or `#BRB`0 will turn your online status bar grey, so that people know you're `#A`0way `#F`0rom the `#K`0eyboard (or, if you prefer, that you'll `#B`0e `#R`0ight `#B`0ack).  Typing `#DNI`0 will let other players know that you're busy talking to one particular player - maybe somewhere off-camera - and that you don't want to be interrupted right now.`nSome areas have special hidden commands or other easter eggs that you can hunt for.  This time around, you didn't trigger anything special.`c`0`n");
            }
            return false;
        }
    }
    if ($section || $talkline || $comment) {
        $tcom = color_sanitize($comment);
        if ($tcom == "" || $tcom == ":" || $tcom == "::" || $tcom == "/me") {
            $emptypost = 1;
        } else {
            $comment = comment_sanitize($comment);
            injectcommentary($section, $talkline, $comment);
        }
    }
}