View source  
  <?php
function messaging_schema() {
  $schema['messaging_store'] = array(
    'description' => 'Stores queued messages to be sent or sent messages as logs.',
    'fields' => array(
      'mqid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Unique message id.',
      ),
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {user}.uid for destination if it is a unique destination.',
      ),
      'sender' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {user}.uid who sent the message if any.',
      ),
      'method' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Messaging send method key.',
      ),
      'language' => array(
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Language code.',
      ),
      'destination' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Destination identifier, it may be an email address.',
      ),
      'subject' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Message subject, single text line.',
      ),
      'body' => array(
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'big',
        'description' => 'Message body, multiple text line.',
      ),
      'params' => array(
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'Additional serialized parameters.',
      ),
      'created' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Unix timestamp, when the message was created/stored.',
      ),
      'sent' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Unix timestamp, when the message was sent.',
      ),
      'cron' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'Will be 1 if row marked for processing on cron.',
      ),
      'queue' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'Will be 1 if this is a queued message.',
      ),
      'log' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'Will be 1 if this is a message log.',
      ),
      'error' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'Error code.',
      ),
      'data' => array(
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'Additional serialized parameters.',
      ),
    ),
    'primary key' => array(
      'mqid',
    ),
    'indexes' => array(
      'cron' => array(
        'cron',
      ),
      'queue' => array(
        'queue',
      ),
      'log' => array(
        'log',
      ),
    ),
  );
  $schema['messaging_destination'] = array(
    'description' => 'Stores message destination for users and non users.',
    'fields' => array(
      'mdid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Unique destination id.',
      ),
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {user}.uid for destination if it belongs to a registered user.',
      ),
      'type' => array(
        'type' => 'varchar',
        'length' => 60,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Destination type: mail, xmpp, etc.',
      ),
      'address' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Destination identifier, it may be an email address.',
      ),
      'status' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
        'size' => 'tiny',
        'description' => 'Destination status: 1= active, 0 = inactive.',
      ),
      'sent' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Unix timestamp, when the last message was sent to this address.',
      ),
    ),
    'primary key' => array(
      'mdid',
    ),
    'indexes' => array(
      'uid' => array(
        'uid',
      ),
      'address' => array(
        'address',
      ),
    ),
  );
  return $schema;
}
function messaging_install() {
  
  drupal_install_schema('messaging');
}
function messaging_uninstall() {
  
  drupal_uninstall_schema('messaging');
  
  if ($format = variable_get('messaging_default_filter', 0)) {
    db_query('DELETE FROM {filters} WHERE format = %d', $format);
    db_query('DELETE FROM {filter_formats} WHERE format = %d', $format);
  }
  
  variable_del('messaging_debug');
  variable_del('messaging_default_filter');
  variable_del('messaging_default_method');
  variable_del('messaging_log');
  variable_del('messaging_process_limit');
  db_query("DELETE FROM {variable} WHERE name LIKE 'messaging_method_%'");
}
function messaging_update_method_disable($method, $replace) {
  
  if ($method == messaging_method_default()) {
    variable_set('messaging_default_method', $replace);
  }
  messaging_update_method_update($method, $replace);
  return $replace;
}
function messaging_update_method_replace($method, $default = TRUE) {
  $replace = NULL;
  
  if ($method_group = messaging_method_info($method, 'group')) {
    foreach (messaging_method_info(NULL, 'group') as $index => $group) {
      if ($group == $method_group && $method != $index) {
        $replace = $index;
        break;
      }
    }
  }
  
  if (empty($replace) && $default) {
    if ($method == messaging_method_default()) {
      $info = messaging_method_info();
      unset($info[$method]);
      $replace = key($info);
    }
    else {
      $replace = messaging_method_default();
    }
  }
  return $replace;
}
function messaging_update_method_update($old, $new) {
  
  if ($old == variable_get('messaging_default_method', NULL)) {
    if ($new) {
      variable_set('messaging_default_method', $new);
    }
    else {
      variable_del('messaging_default_method');
    }
  }
  
  if ($new) {
    $old_type = messaging_method_info($old, 'address_type');
    $new_type = messaging_method_info($new, 'address_type');
    if ($old_type && $new_type && $old_type != $new_type && ($field = messaging_address_info($new_type, 'field_name')) && ($table = messaging_address_info($new_type, 'field_table'))) {
      $sql = "INSERT INTO {messaging_destination} (uid, type, address)";
      if ($new_type == 'user') {
        
        $sql .= " SELECT d1.uid, '%s', d1.uid FROM {messaging_destination} d1";
      }
      else {
        $sql .= " SELECT d1.uid, '%s', t.{$field} FROM {messaging_destination} d1";
        $sql .= " INNER JOIN {" . $table . "} t ON d1.uid = t.uid";
        
      }
      
      $sql .= " LEFT JOIN {messaging_destination} d2 ON d1.uid = d2.uid AND d2.type = '%s'";
      $sql .= " WHERE d1.uid > 0 AND d1.type = '%s' AND d2.uid IS NULL";
      db_query($sql, $new_type, $new_type, $old_type);
      if ($created = db_affected_rows()) {
        drupal_set_message(t("Created @count user destinations for new sending method.", array(
          '@count' => $created,
        )));
      }
    }
  }
  
  module_invoke_all('messaging', 'method update', $old, $new);
  
  messaging_store()
    ->delete_multiple(array(
    'method' => $old,
  ));
}
function messaging_update_1() {
  $ret = array();
  if ($settings = variable_get('messaging_methods', array())) {
    foreach ($settings as $key => $info) {
      $info['subject_filter'] = $info['filter'];
      variable_set('messaging_method_' . $key, $info);
      $ret[] = array();
    }
    drupal_set_message('Your messaging methods have been updated. Please review the messaging settings.');
  }
  return $ret;
}
function messaging_update_2() {
  $ret = array();
  drupal_install_schema('messaging_store');
  return $ret;
}
function messaging_update_6001() {
  $ret = array();
  return $ret;
}
function messaging_update_6002() {
  $ret = array();
  module_load_all();
  if (module_exists('messaging_phpmailer')) {
    $replace['html_mail'] = 'phpmailer';
  }
  if (module_exists('messaging_mime_mail') && !module_exists('messaging_mail')) {
    $replace['mail'] = 'mimemail';
  }
  if (!empty($replace)) {
    foreach ($replace as $old => $new) {
      if ($settings = variable_get('messaging_method_' . $old, NULL)) {
        variable_set('messaging_method_' . $new, $settings);
        variable_del('messaging_method_' . $old);
      }
      messaging_update_method_update($old, $new);
      $ret[] = array(
        'success' => TRUE,
        'query' => "Replaced sending method {$old} by {$new}",
      );
    }
    drupal_set_message('Please, check all your messaging settings for sending methods.');
  }
  return $ret;
}
function messaging_update_6003() {
  $ret = array();
  db_change_field($ret, 'messaging_store', 'params', 'params', array(
    'type' => 'text',
    'not null' => TRUE,
    'size' => 'big',
    'serialize' => TRUE,
  ));
  return $ret;
}
function messaging_update_6004() {
  $ret = array();
  db_add_index($ret, 'messaging_store', 'cron', array(
    'cron',
  ));
  db_add_index($ret, 'messaging_store', 'queue', array(
    'queue',
  ));
  db_add_index($ret, 'messaging_store', 'log', array(
    'log',
  ));
  return $ret;
}
function messaging_update_6005() {
  $ret = array();
  $ret[] = update_sql('UPDATE {messaging_store} SET log = 1 WHERE log > 1');
  $ret[] = update_sql('DELETE FROM {messaging_store} WHERE log = 1 AND queue = 0 AND sent < %d', time() - variable_get('messaging_log', 0));
  return $ret;
}
function messaging_update_6006() {
  $ret = array();
  $table = drupal_get_schema('messaging_destination', TRUE);
  db_create_table($ret, 'messaging_destination', $table);
  
  variable_set('messaging_update_6006', 1);
  return $ret;
}
function messaging_update_6007() {
  $ret = array();
  db_add_field($ret, 'messaging_store', 'data', array(
    'type' => 'text',
    'not null' => TRUE,
    'size' => 'big',
    'serialize' => TRUE,
  ));
  db_add_field($ret, 'messaging_store', 'error', array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'size' => 'tiny',
  ));
  db_add_field($ret, 'messaging_store', 'language', array(
    'type' => 'varchar',
    'length' => 12,
    'not null' => TRUE,
    'default' => '',
  ));
  return $ret;
}
function messaging_update_6008() {
  messaging_include('text.inc');
  foreach (module_invoke_all('messaging', 'send methods') as $method => $info) {
    $properties = variable_get('messaging_method_' . $method, array()) + array(
      'name' => $info['name'],
    );
    $properties['filter'] = isset($info['filter']) ? $info['filter'] : 'messaging_plaintext';
    variable_set('messaging_filters_' . $method, $properties);
  }
  variable_del('messaging_default_filter');
  return array();
}
function messaging_update_6009() {
  $ret = array();
  if (!variable_get('messaging_update_6006', 0)) {
    
    @db_drop_unique_key($ret, 'messaging_destination', 'destination');
  }
  return $ret;
}
function messaging_update_6010() {
  $ret = array();
  if (!variable_get('messaging_update_6006', 0) && db_column_exists('messaging_destination', 'method')) {
    foreach (messaging_method_info() as $method => $info) {
      $type = isset($info['address_type']) ? $info['address_type'] : $info['group'];
      
      $ret[] = update_sql("UPDATE {messaging_destination} SET type = '{$type}' WHERE method = '{$method}'");
    }
  }
  return $ret;
}
function messaging_update_6011() {
  return _messaging_update_duplicate_destinations();
}
function messaging_update_6012() {
  $ret = array();
  if (db_table_exists('messaging_message_parts') && !db_column_exists('messaging_message_parts', 'language')) {
    module_load_install('messaging_template');
    $ret = messaging_template_update_6001();
  }
  return $ret;
}
function messaging_update_6013() {
  $ret = array();
  if (db_table_exists('messaging_message_parts') && !db_column_exists('messaging_message_parts', 'format')) {
    module_load_install('messaging_template');
    $ret = messaging_template_update_6002();
  }
  return $ret;
}
function messaging_update_6014() {
  $ret = array();
  
  drupal_flush_all_caches();
  return $ret;
}
function _messaging_update_duplicate_destinations() {
  $ret = array();
  
  $ret[] = update_sql("UPDATE {messaging_destination} d INNER JOIN {messaging_destination} d2 ON d.uid = d2.uid AND d.address = d2.address AND d.type = d2.type AND d.mdid > d2.mdid SET d.type = ''");
  
  $ret[] = update_sql("DELETE FROM messaging_destination WHERE type = ''");
  return $ret;
}