<?php

/**
 * Implementation of hook_schema().
 */
function spambot_schema() {
  $schema = array();
  $schema['node_spambot'] = array(
    'description' => t('Node table to track author IP addresses. For use by spambot only.'),
    'fields' => array(
      'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'uid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'hostname' => array( 'type' => 'varchar', 'length' => 128, 'not null' => FALSE),
    ),
    'primary key' => array('nid'),
    'indexes' => array(
      'uid' => array('uid'),
    ),
  );
  return $schema;
}

/**
 * Implementation of hook_uninstall().
 */
function spambot_uninstall() {
  db_query("DELETE FROM {variable} WHERE name LIKE 'spambot_%'");
}

/**
 * Migrate settings from previous version of spambot (6.x-2.0)
 */
function spambot_update_6300() {
  $ret = array();

  // In previous versions of spambot, the default message was 'Blacklisted. Now go away!'
  // If no custom message was configured, then configure it to 'Blacklisted. Now go away!'
  $message = variable_get('spambot_blocked_message', FALSE);
  if (!$message) {
    variable_set('spambot_blocked_message', t('Blacklisted. Now go away!'));
  }

  // Previous versions of spambot blacklisted on any of the three criteria
  variable_set('spambot_criteria_email', TRUE);
  variable_set('spambot_criteria_username', TRUE);
  variable_set('spambot_criteria_ip', TRUE);
  return $ret;
}

function spambot_update_6301() {
  $ret = array();
  // Change criteria settings from booleans to numbers
  if (variable_set('spambot_criteria_email', TRUE)) {
    variable_set('spambot_criteria_email', 1);
  }  
  if (variable_set('spambot_criteria_username', FALSE)) {
    variable_set('spambot_criteria_username', 1);
  }
  if (variable_set('spambot_criteria_ip', FALSE)) {
    variable_set('spambot_criteria_ip', 1);
  }
  return $ret;
}

function spambot_update_7101() {
  $messages = array();

  variable_del('spambot_user_register_report');
  $messages[] = t('Deleted variable <em>spambot_user_register_report.</em>');

  $message = variable_get('spambot_blocked_message', FALSE);
  if ($message !== FALSE) {
    variable_set('spambot_blocked_message_email', $message);
    variable_set('spambot_blocked_message_username', $message);
    variable_set('spambot_blocked_message_ip', $message);
    variable_del('spambot_blocked_message');
    $messages[] = t('Transferred user registration blocked message to new format.');
  }

  // Create new table node_spambot
  if (!db_table_exists('node_spambot')) {
    $schema = array();
    $schema['node_spambot'] = array(
      'description' => t('Node table to track author IP addresses. For use by spambot only.'),
      'fields' => array(
        'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
        'uid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
        'hostname' => array( 'type' => 'varchar', 'length' => 128, 'not null' => FALSE),
      ),
      'primary key' => array('nid'),
      'indexes' => array(
        'uid' => array('uid'),
      ),
    );
    db_create_table('node_spambot', $schema['node_spambot']);
    $messages[] = t('Created new table <em>node_spambot</em>.');
  }
  
  return join('<br />', $messages);
}
