<?php

if(!defined('IN_MYBB'))
	die('This file cannot be accessed directly.');

$plugins->add_hook('datahandler_pm_validate', 'nocaps_run');
$plugins->add_hook('datahandler_post_validate_thread', 'nocaps_run');
$plugins->add_hook('datahandler_post_validate_post', 'nocaps_run');

function nocaps_info()
{
	return array(
		'name'			=> 'No ALL-CAPS in Posts/PMs',
		'description'	=> 'Tries to fix up posts/PMs (and thread/post/PM subjects) posted in all capitals (ie Caps-lock on).',
		'website'		=> 'http://mybbhacks.zingaburga.com/',
		'author'		=> 'ZiNgA BuRgA',
		'authorsite'	=> 'http://zingaburga.com/',
		'version'		=> '1.03',
		'compatibility'	=> '1*',
		'guid'			=> '' // bc26d291b63d606fe9442d140987fed7
	);
}

function nocaps_parse(&$message, $issubj=false)
{
	// if a message, we'll allow all caps if there's only one word
	if(!$issubj && (!my_strpos($message, ' ') && !my_strpos($message, "\t") && !my_strpos($message, "\n")))
		return $message;
	
	// okay, now check if all caps
	if($message != my_strtoupper($message))
	//if(preg_match('#[a-z]#', $message))
		return $message;
	
	// so the message _is_ all in caps...
	if(function_exists('mb_convert_case'))
		return mb_convert_case($message, MB_CASE_TITLE);
	else
		return ucwords(my_strtolower($message));
}


function nocaps_run(&$ph)
{
	if($ph->data['subject'])
		$ph->data['subject'] = nocaps_parse($ph->data['subject'], true);
	$ph->data['message'] = nocaps_parse($ph->data['message']);
	if(defined('THIS_SCRIPT') && THIS_SCRIPT == 'xmlhttp.php') {
		// editing a post on xmlhttp may display different stuff due to it parsing the post using the global variable
		$GLOBALS['message'] = $ph->data['message'];
	}
}

?>