Here's a quick and easy method that has pretty much gotten rid of all my spam comments. It stops comments with embedded links from being posted by anonymous users.
// hook comment creation
function wcic_form_comment_form_alterTHEME_OR_MODULE_NAME_form_comment_form_alter(&$form, &$form_state, $form_id) {
$form['#validate'][] = 'wcic_comment_exclude_spam_with_link';'THEME_OR_MODULE_NAME_comment_exclude_spam_with_link';
}
// function to weed out spam comments with links submitted by anonymous users
function wcic_comment_exclude_spam_with_linkTHEME_OR_MODULE_NAME_comment_exclude_spam_with_link($form, &$form_state) {
$comment_body = $form_state['values']['comment_body'][LANGUAGE_NONE][0];
if (isset($comment_body['format'])) {
$comment_text = check_markup($comment_body['value'],$comment_body['format']);
}
else {
$comment_text = check_plain($comment_body['value']);
}
if(preg_match("/href=/i",$comment_text)) {
if(isset($_SERVER['REMOTE_ADDR'])) {
$user_ip = $_SERVER['REMOTE_ADDR'];
if(!user_is_logged_in()) {
$watchdog_message = "Attempted anonymous link, IP: " . $user_ip;
watchdog("content",$watchdog_message,NULL,WATCHDOG_ERROR);
form_set_error('comment_body', t('Embedded links are not allowed for anonymous users'));
}
}
}
}
There is a watchdog message that you can use to ban the IP address if you wish. This code goes in youra custom module or the template.php file for your theme. My theme I'm using a module as this is wcicfunctionality only. You will want to replace wcic Replace THEME_OR_MODULE_NAME with the name of your own theme nameor module.