Hi,

I am trying to display a custom profile field instead of a username in the private messaging module - which is dependent on the buddylist module.

I've been looking at the code for both of them.

I've managed to change some stuff in the buddylist module ie the titles of the buddy listings to the username of the person whose buddies they are.

I have just started looking at the buddylist_views.inc file. Is it possible to add an array of values ie a profile field to this file and then reference these values from the buddylist module?

I'm generally trying to replace all instances of the username with a profile field value.

Any help would be great.

Thanks,

Sarah

Comments

marvil07’s picture

> I'm generally trying to replace all instances of the username with a profile field value.

Take a look at theme_username

scarer’s picture

i've looked at this before but the arrays and values that are in the code for each module call on values in databases etc.

i have already programmed this hack into my theme but thank you for the info.

i am trying to fix the chatroom module to only display a profile field value instead of a username on message posts.

this is the code i have so far:

// $Id: chatroom.module,v 1.59.2.67 2007/10/05 19:37:34 darrenoh Exp $

/**
 * @file
 * Enable chat room support in Drupal.
 */

/**
 * Implementation of hook_help().
 */
function chatroom_help($section) {
  switch ($section) {
    case 'admin/help#chatroom':
      return '<p />';
  }
}

/**
 * Implementation of hook_access().
 */
function chatroom_access($op, $node) {
  global $user;
  if ($op == 'create') {
    return user_access('create chat rooms');
  }
  if ($op == 'update' || $op == 'delete') {
    if (user_access('edit own chat rooms') && ($user->uid == $node->uid)) {
      return TRUE;
    }
  }
}

/**
 * Implementation of hook_perm().
 */
function chatroom_perm() {
  return array('access chat rooms', 'create chat rooms', 'edit own chat rooms', 'administer chats', 'administer chat rooms', 'create chats');
}

/**
 * Implementation of hook_menu().
 */
function chatroom_menu($may_cache) {
  $items = array();
  $items[] = array(
    'path' => 'admin/settings/chatroom',
    'callback' => 'drupal_get_form',
    'callback arguments' => array('chatroom_admin_settings'),
    'title' => t('Chat room'),
    'description' => t('Configure chat rooms.'),
    'access' => user_access('administer chat rooms')
  );
  $items[] = array(
    'path' => 'chatrooms',
    'callback' => 'chatroom_page',
    'access' => user_access('access chat rooms'),
    'title' => t('Chat rooms'),
    'type' => MENU_SUGGESTED_ITEM
  );
  $items[] = array(
    'path' => 'chatrooms/kicked',
    'callback' => 'chatroom_chat_kicked_user',
    'access' => user_access('access chat rooms'),
    'type' => MENU_CALLBACK
  );
  $items[] = array(
    'path' => 'chatrooms/chat',
    'callback' => 'chatroom_chat',
    'access' => user_access('access chat rooms'),
    'type' => MENU_CALLBACK
  );
  $items[] = array(
    'path' => 'chatrooms/archives',
    'callback' => 'chatroom_chat_archive',
    'access' => user_access('access chat rooms'),
    'type' => MENU_CALLBACK
  );
  return $items;
}

/**
 * Menu callback; display site-wide chat room settings.
 */
function chatroom_admin_settings() {
  $form['chatroom_auto_archive'] = array(
    '#type' => 'checkbox',
    '#title' => t('Automatically archive old messages.'),
    '#description' => t('If there are a lot of old messages, archiving will improve chat performance.'),
    '#default_value' => variable_get('chatroom_auto_archive', FALSE),
  );
  $form['chatroom_block_update_interval'] = array(
    '#type' => 'textfield',
    '#title' => t('Chat room block update interval'),
    '#default_value' => variable_get('chatroom_block_update_interval', 5),
    '#description' => t('Determines how often blocks should update active chat rooms, active chats, and on-line users.'),
    '#size' => 2,
    '#validate' => array('_chatroom_element_numeric_unsigned' => array()),
  );
  $form['chatroom_guest_user_prefix'] = array(
    '#type' => 'textfield',
    '#title' => t('Guest user prefix'),
    '#description' => t('Prefixed to guest ID to provide user name for anonymous users.'),
    '#default_value' => variable_get('chatroom_guest_user_prefix', t('guest-')),
    '#size' => 20,
  );
  $form['chatroom_chat_date_format'] = array(
    '#type' => 'textfield',
    '#title' => t('Chat date format'),
    '#attributes' => array('class' => 'custom-format'),
    '#default_value' => variable_get('chatroom_chat_date_format', '* \S\e\n\t \a\t G:i'),
    '#description' => t('Format for system time messages in chats. See the <a href="@url">PHP manual</a> for available options. This format is currently set to display as <span>%date</span>.', array('@url' => 'http://php.net/manual/function.date.php', '%date' => format_date(time(), 'custom', variable_get('chatroom_chat_date_format', '* \S\e\n\t \a\t G:i')))),
  );
  if (function_exists('_smileys_list')) {
    $form['chatroom_smileys_support'] = array(
      '#type' => 'fieldset',
      '#title' => t('Smileys module support'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['chatroom_smileys_support']['chatroom_smileys_enabled'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable Smileys module support.'),
      '#default_value' => variable_get('chatroom_smileys_enabled', FALSE),
    );
    $form['chatroom_smileys_support']['chatroom_smileys_showtextentry'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show smileys in text entry box.'),
      '#default_value' => variable_get('chatroom_smileys_enabled', FALSE) && variable_get('chatroom_smileys_showtextentry', FALSE),
      '#disabled' => !variable_get('chatroom_smileys_enabled', FALSE),
    );
  }
  $form['chatroom_alerts'] = array(
    '#type' => 'fieldset',
    '#title' => t('Chat alerts'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['chatroom_alerts']['chatroom_alerts'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable chat alerts.'),
    '#description' => t('Checking this box will allow users to turn on alerts for chat events.'),
    '#default_value' => variable_get('chatroom_alerts', FALSE),
  );
  $form['chatroom_alerts']['chatroom_alerts_default'] = array(
    '#type' => 'checkbox',
    '#title' => t('Turn alerts on by default.'),
    '#description' => t('Check this box if you want chats to open with alerts on.'),
    '#default_value' => variable_get('chatroom_alerts', FALSE) && variable_get('chatroom_alerts_default', FALSE),
    '#disabled' => !variable_get('chatroom_alerts', FALSE),
  );
  $form['chatroom_alerts']['chatroom_custom_sounds'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use custom sounds for chat alerts.'),
    '#description' => t('Check this box if you want to replace default chat alert sounds with your own MP3s.'),
    '#default_value' => variable_get('chatroom_alerts', FALSE) && variable_get('chatroom_custom_sounds', FALSE),
    '#disabled' => !variable_get('chatroom_alerts', FALSE),
  );
  $path = file_directory_path() .'/chatroom';
  $js = array();
  if (file_exists("$path/message.mp3")) {
    $js['messageSound'] = file_create_url("$path/message.mp3");
  }
  if (file_exists("$path/user.mp3")) {
    $js['userSound'] = file_create_url("$path/user.mp3");
  }
  $form['chatroom_alerts']['chatroom_message_alert_upload'] = array(
    '#type' => 'file',
    '#title' => t('Custom new message sound'),
    '#description' => isset($js['messageSound']) ? t('Replace the custom new message sound with a new MP3. !Listen to current file.', array('!Listen' => l(t('Listen'), $js['messageSound'], array('id' => 'sound_message')))) : t('Replace the default new message sound with your own MP3.'),
    '#size' => 30,
  );
  $form['chatroom_alerts']['chatroom_user_alert_upload'] = array(
    '#type' => 'file',
    '#title' => t('Custom new user sound'),
    '#description' => isset($js['userSound']) ? t('Replace the custom new user sound with a new MP3. !Listen to current file.', array('!Listen' => l(t('Listen'), $js['userSound'], array('id' => 'sound_user')))) : t('Replace the default new user sound with your own MP3.'),
    '#size' => 30,
  );
  if (!empty($js)) {
    global $base_path;
    $js['basePath'] = $base_path;
    $js['chatroomBase'] = drupal_get_path('module', 'chatroom');
    drupal_add_js(array('chatroom' => $js), 'setting');
    drupal_add_js(drupal_get_path('module', 'chatroom') .'/soundmanager2.js');
    drupal_add_js('$("#sound_message").attr("href", "javascript:Drupal.chatroom.soundManager.play(\'message\')")', 'inline', 'footer');
    drupal_add_js('$("#sound_user").attr("href", "javascript:Drupal.chatroom.soundManager.play(\'user\')")', 'inline', 'footer');
  }
  $form['#attributes'] = array('enctype' => 'multipart/form-data');
  return system_settings_form($form);
}

function chatroom_admin_settings_validate($form_id, $form_values) {
  $path = file_directory_path();
  if (file_check_directory($path, FILE_CREATE_DIRECTORY)) {
    $path .= '/chatroom';
    if (file_check_directory($path, FILE_CREATE_DIRECTORY)) {
      if ($user_sound = file_check_upload('chatroom_user_alert_upload')) {
        if ($user_sound->filemime != 'audio/mpeg') {
          form_set_error('chatroom_alerts][chatroom_user_alert_upload', t('New user sound must be an MP3 file.'));
        }
      }
      if ($message_sound = file_check_upload('chatroom_message_alert_upload')) {
        if ($message_sound->filemime != 'audio/mpeg') {
          form_set_error('chatroom_alerts][chatroom_message_alert_upload', t('New message sound must be an MP3 file.'));
        }
      }
    }
  }
}

function chatroom_admin_settings_submit($form_id, $form_values) {
  $path = file_directory_path() .'/chatroom';
  if (file_check_upload('chatroom_user_alert_upload')) {
    file_save_upload('chatroom_user_alert_upload', "$path/user.mp3", FILE_EXISTS_REPLACE);
  }
  if (file_check_upload('chatroom_message_alert_upload')) {
    file_save_upload('chatroom_message_alert_upload', "$path/message.mp3", FILE_EXISTS_REPLACE);
  }
  system_settings_form_submit($form_id, $form_values);
}

function _chatroom_element_numeric_unsigned($element) {
  if (is_numeric($element['#value'])) {
    if ($element['#value'] == 0) {
      form_error($element, t('The block update interval cannot be zero.'));
    }
    elseif ($element['#value'] < 0) {
      form_error($element, t('The block update interval cannot be negative.'));
    }
  }
  else {
    form_error($element, t('The block update interval must be a number.'));
  }
}

function chatroom_file_download($file) {
  if (strpos($file, 'chatroom/') === 0 && pathinfo($file, PATHINFO_EXTENSION) == 'mp3') {
    return array('Content-type: audio/mpeg');
  }
}

/**
 * Implementation of hook_node_info().
 */
function chatroom_node_info() {
  return array(
    'chatroom' => array(
      'name' => t('Chat room'),
      'module' => 'chatroom',
      'description' => t('A chat room provides access to chats and chat archives.'),
    ),
  );
}

/**
 * Implementation of hook_form().
 */
function chatroom_form(&$node) {
  global $user;
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#default_value' => check_plain($node->title),
    '#required' => TRUE
  );
  $form['body_filter']['body'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#default_value' => $node->body,
    '#rows' => 3,
    '#description' => t('Describe your chat room so other people will know if they want to join.'),
  );
  $form['body_filter']['format'] = filter_form($node->format);
  $form['kicked_out_message'] = array(
    '#type' => 'textarea',
    '#title' => t('Chat room kicked out message'),
    '#default_value' => $node->chatroom->kicked_out_message,
    '#rows' => 3,
    '#description' => t('This text will appear on the page kicked out users are sent to. Defaults to, "You have been kicked out of %chat for misbehaving."', array('%chat' => t('chat-name'))),
  );
  $form['banned_message'] = array(
    '#type' => 'textarea',
    '#title' => t('Chat room banned message'),
    '#default_value' => $node->chatroom->banned_message,
    '#rows' => 3,
    '#description' => t('This text will appear on the page banned users are sent to. Defaults to, "You have been banned from %chatroom."', array('%chatroom' => t('chat-room'))),
  );
  if (!empty($node->chatroom->banned_users)) {
    $form['chatroom_banned_users'] = array(
      '#type' => 'fieldset',
      '#title' => t('Manage banned users'),
      '#collapsible' => TRUE,
    );
    foreach ($node->chatroom->banned_users as $banned_user) {
      $banned_users[$banned_user->uid] = check_plain($banned_user->name);
    }
    $form['chatroom_banned_users']['unban_list'] = array(
      '#type' => 'checkboxes',
      '#options' => $banned_users,
      '#description' => t('Check the users you would like to unban')
    );
  }
  $form['chat_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Chat settings'),
    '#collapsible' => TRUE,
  );
  $form['chat_settings']['poll_freq'] = array(
    '#type' => 'select',
    '#title' => t('Update frequency'),
    '#default_value' => empty($node->chatroom->poll_freq) ? 1 : $node->chatroom->poll_freq / 1000,
    '#options' => drupal_map_assoc(range(1,10)),
    '#description' => t('How many seconds between each request for updates from the server.'),
  );
  $form['chat_settings']['idle_freq'] = array(
    '#type' => 'select',
    '#title' => t('Idle time'),
    '#default_value' => empty($node->chatroom->idle_freq) ? 60 : $node->chatroom->idle_freq / 1000,
    '#options' => drupal_map_assoc(array(20, 40, 60, 80, 100, 120, 140, 160, 180)),
    '#description' => t('How many seconds between each message before a last message time is shown in the chat.'),
  );
  $old_msg_range = array();
  for ($i = 1; $i <= 25; $i++) {
    $old_msg_range[$i] = $i * 10;
  }
  $form['chat_settings']['old_msg_count'] = array(
    '#type' => 'select',
    '#title' => t('Old messages'),
    '#description' => t('How many old messages to show when entering a chat.'),
    '#default_value' => empty($node->chatroom->old_msg_count) ? 20 : $node->chatroom->old_msg_count,
    '#options' => drupal_map_assoc($old_msg_range),
  );
  if (!empty($node->chatroom->chats)) {
    foreach ($node->chatroom->chats as $chat) {
      if ($chat->section != 'archives') {
        $chats[$chat->ccid] = check_plain($chat->chatname);
      }
      else {
        $closed_chats[$chat->ccid] = check_plain($chat->chatname);
      }
    }
    if (!empty($chats)) {
      $form['chatroom_chats'] = array(
        '#type' => 'fieldset',
        '#title' => t('Manage open chats'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
      );
      $form['chatroom_chats']['chat_list'] = array(
        '#type' => 'checkboxes',
        '#options' => $chats,
        '#description' => t('Check the chats you would like to close')
      );
    }
    if (!empty($closed_chats)) {
      $form['closed_chats'] = array(
        '#type' => 'fieldset',
        '#title' => t('Manage archived chats'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
      );
      $form['closed_chats']['closed_chat_list'] = array(
        '#type' => 'checkboxes',
        '#options' => $closed_chats,
        '#description' => t('Check the chats you would like to delete')
      );
    }
  }
  return $form;
}

/**
 * Implementation of hook_insert()
 */
function chatroom_insert($node) {
  $result = db_query("
    INSERT INTO {chatroom}
    (nid, poll_freq, idle_freq, old_msg_count, kicked_out_message, banned_message, modified)
    VALUES (%d, %d, %d, %d, '%s', '%s', %d)
  ", array(
    $node->nid,
    1000 * $node->poll_freq,
    1000 * $node->idle_freq,
    $node->old_msg_count,
    $node->kicked_out_message,
    $node->banned_message,
    time()
  ));
  if ($result) {
    chatroom_block_update_cache('chatrooms');
  }
}

/**
 * Implementation of hook_update()
 */
function chatroom_update($node) {
  db_query("
    UPDATE {chatroom}
    SET
      poll_freq = %d,
      idle_freq = %d,
      old_msg_count = %d,
      kicked_out_message = '%s',
      banned_message = '%s',
      modified = %d
    WHERE nid = %d
  ", array(
    1000 * $node->poll_freq,
    1000 * $node->idle_freq,
    $node->old_msg_count,
    $node->kicked_out_message,
    $node->banned_message,
    time(),
    $node->nid,
  ));
  if (isset($node->chat_list)) {
    foreach ($node->chat_list as $chat_id) {
      if ($chat_id > 0) {
        chatroom_archive_chat($chat_id);
        file_delete(_chatroom_get_cache_file("chat.$chat_id"));
        db_query("
          INSERT INTO {chatroom_msg_archive} (cmid, ccid, uid, msg_type, msg, session_id, recipient, modified)
          SELECT * FROM {chatroom_msg} WHERE ccid = %d
        ", $chat_id);
      }
    }
    chatroom_block_update_cache('chatrooms');
    chatroom_block_update_cache('chats');
  }
  if (isset($node->closed_chat_list)) {
    foreach ($node->closed_chat_list as $chat_id) {
      if (!empty($chat_id)) {
        chatroom_chat_delete($chat_id);
      }
    }
  }
  if (isset($node->unban_list)) {
    db_query('DELETE FROM {chatroom_ban_list} WHERE crid = %d AND uid IN (%s)', $node->chatroom->crid, implode(',', $node->unban_list));
  }
}

/**
 * Implementation of hook_delete().
 */
function chatroom_delete(&$node) {
  db_query('DELETE FROM {chatroom} WHERE nid = %d', $node->nid);
  db_query("DELETE FROM {chatroom_chat} WHERE crid = %d", $node->chatroom->crid);
  db_query('DELETE FROM {chatroom_ban_list} WHERE crid = %d', $node->chatroom->crid);
  if (isset($node->chatroom) && !empty($node->chatroom->chats)) {
    $ccids = implode(',', array_keys($node->chatroom->chats));
    db_query('DELETE FROM {chatroom_msg} WHERE ccid IN (%s)', $ccids);
    db_query('DELETE FROM {chatroom_online_list} WHERE ccid IN (%s)', $ccids);
    db_query('DELETE FROM {chatroom_msg_archive} WHERE ccid IN (%s)', $ccids);
    foreach ($node->chatroom->chats as $chat_id => $name) {
      file_delete(_chatroom_get_cache_file("chat.$chat_id"));
    }
  }
  chatroom_block_update_cache('chatrooms');
  chatroom_block_update_cache('chats');
}

/**
 * Implementation of hook_load().
 */
function chatroom_load($node) {
  $chatroom->chatroom = db_fetch_object(db_query('SELECT * FROM {chatroom} WHERE nid = %d', $node->nid));
  if (!empty($chatroom->chatroom)) {
    $chatroom->chatroom->banned_users = chatroom_get_banned_users($chatroom->chatroom->crid);
    // If the user is banned, don't load chats.
    if (!chatroom_is_banned_user($chatroom->chatroom->crid)) {
      $chatroom->chatroom->chats = chatroom_get_room_summary($chatroom->chatroom->crid);
    }
  }
  return $chatroom;
}

/**
 * returns a the list of chats for a given room
 */
function chatroom_get_room_summary($room_id) {
  $result = db_query("
    SELECT (
      SELECT COUNT(*) FROM {chatroom_msg}
      WHERE ccid = cc.ccid AND recipient = ''
    ) + (
      SELECT COUNT(*) FROM {chatroom_msg_archive}
      WHERE ccid = cc.ccid AND recipient = ''
    ) AS msg_count, MAX(cm.cmid) AS last_cmid, cc.ccid, cc.chatname, cc.when_archived
    FROM {chatroom} AS cr
    INNER JOIN {chatroom_chat} AS cc ON cc.crid = cr.crid
    LEFT JOIN {chatroom_msg} AS cm ON cm.ccid = cc.ccid AND cm.recipient = ''
    WHERE cr.crid = %d
    GROUP BY cc.ccid, cc.chatname, cc.when_archived
  ", $room_id, $room_id, $room_id);
  $chats = array();
  $msg_ids = array();
  while ($chat = db_fetch_object($result)) {
    if (isset($chat->when_archived)) {
      $chat->section = 'archives';
      $chat->msg_info = t('Archived on !date.', array('!date' => format_date($chat->when_archived, 'medium')));
    }
    else {
      $chat->section = 'chat';
      if ($chat->last_cmid) {
        $msg_ids[] = $chat->last_cmid;
      }
      else {
        $chat->msg_info = t('No messages');
      }
    }
    $chats[$chat->ccid] = $chat;
  }
  if (!empty($msg_ids)) {
    $result = db_query('
      SELECT cm.*, col.guest_id, u.name FROM {chatroom_msg} cm
      LEFT JOIN {chatroom_online_list} col ON col.session_id = cm.session_id
      LEFT JOIN {users} u ON u.uid = cm.uid
      WHERE cm.cmid IN (%s)
    ', implode(',', $msg_ids));
    while ($msg = db_fetch_object($result)) {
      foreach ($chats as $i => $chat) {
        if ($chat->last_cmid == $msg->cmid) {
          $chats[$i]->msg_info = chatroom_get_msg_info($msg);
        }
      }
    }
  }
  return $chats;
}

/**
 * Format a message for display in a summary table.
 */
function chatroom_get_msg_info($msg) {
  if (function_exists('_smileys_list') && variable_get('chatroom_smileys_enabled', FALSE)) {
    $msg->msg = smileys_filter_process($msg->msg);
  }
  $output = $msg->msg .'<br />';
  $displayName = user_load(array('uid' => $msg->uid));
  /*$output .= t('Posted by <strong>!user</strong> on !date', array(
    '!user' => $msg->uid ? l($msg->name, "user/$msg->uid") : variable_get('chatroom_guest_user_prefix', t('guest-')) . $msg->guest_id,
    '!date' => format_date($msg->modified, 'medium'),
  */
  $output .= t('Posted by <strong>!user</strong> on !date', array(
    '!user' => $msg->uid ? l($displayName->profile_displayName, "user/$msg->uid") : variable_get('chatroom_guest_user_prefix', t('guest-')) . $msg->guest_id,
    '!date' => format_date($msg->modified, 'medium'),
  ));
  return $output;
}

/**
 * Gets a list of banned users for a given chat room.
 */
function chatroom_get_banned_users($crid) {
  $sql = '
    SELECT cbl.uid, u.name FROM {chatroom_ban_list} cbl
    INNER JOIN {users} u ON u.uid = cbl.uid
    WHERE cbl.crid = %d
  ';
  $banned_users = array();
  if ($result = db_query($sql, $crid)) {
    if ($banned_user = db_fetch_object($result)) {
      $displayName = user_load(array('uid' => $banned_user));
      $banned_users[] = $displayName->profile_displayName;
    }
  }
  return $banned_users;
}

/**
 * Implementation of hook_view().
 */
function chatroom_view($node, $teaser = FALSE, $page = FALSE) {
  $node = node_prepare($node);

  if ($page) {
    $bc = drupal_get_breadcrumb();
    if (!empty($bc)) {
      $bc[] = l('Chat rooms', 'chatrooms');
      drupal_set_breadcrumb($bc);
    }
  }
  if (!$teaser) {
    // if the user is banned, just tell them why
    if (chatroom_is_banned_user($node->chatroom->crid)) {
      $node->content['body']['#value'] = !empty($node->chatroom->banned_message) ? $node->chatroom->banned_message : t('You have been banned from %chatroom.', array('%chatroom' => $node->title));
    }
    else {
      // if the user can create chats, show the form
      if (user_access('create chats')) {
        $node->content['add_chat'] = array(
          '#value' => drupal_get_form('chatroom_create_chat_form', $node->chatroom->crid),
          '#weight' => 1,
        );
      }
      // if there are some chats, build some tables to display them
      if (!empty($node->chatroom->chats) > 0) {
        foreach ($node->chatroom->chats as $chat) {
          $type = $chat->section == 'chat' ? 'open' : 'archived';
          $rows[$type][] = array(
            array('data' => l($chat->chatname, "chatrooms/$chat->section/$chat->ccid")),
            array('data' => $chat->msg_count),
            array('data' => $chat->msg_info)
          );
        }
        if (!empty($rows['open'])) {
          $node->content['open_chats']['#weight'] = 2;
          $node->content['open_chats']['title'] = array(
            '#value' => '<h2>'. t('Open chats in this room') .'</h2>',
            '#weight' => 0,
          );
          $node->content['open_chats']['table'] = array(
            '#value' => theme('table', array(t('Chat name'), t('Message count'), t('Last message')), $rows['open']),
            '#weight' => 1,
          );
        }
        if (!empty($rows['archived'])) {
          $node->content['archived_chats']['#weight'] = 3;
          $node->content['archived_chats']['header'] = array(
            '#value' => '<h2>'. t('Archived chats in this room') .'</h2>',
            '#weight' => 0,
          );
          $node->content['archived_chats']['table'] = array(
            '#value' => theme('table', array(t('Chat name'), t('Message count'), t('When archived')), $rows['archived']),
            '#weight' => 1,
          );
        }
      }
    }
  }
  return $node;
}

/**
 * returns a create chat form
 */
function chatroom_create_chat_form($room_id) {
  $form['chatroom_create_chat'] = array(
    '#type' => 'fieldset',
    '#title' => t('Create a new chat'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE
  );
  $form['chatroom_create_chat']['chat_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Chat name'),
    '#size' => 30,
    '#required' => TRUE,
    '#description' => t('Enter the name for the chat'),
  );
  $form['chatroom_create_chat']['room_id'] = array(
    '#type' => 'hidden',
    '#value' => $room_id
  );
  $form['chatroom_create_chat']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Create chat')
  );
  return $form;
}

/**
 * validates attempt to create chat - checks this name is not already in use
 */
function chatroom_create_chat_form_validate($f_id, $f_values) {
  $crid = $f_values['room_id'];
  $name = $f_values['chat_name'];
  $sql = "SELECT ccid FROM {chatroom_chat} WHERE crid = %d AND chatname = '%s' AND when_archived IS NULL";
  if (db_num_rows(db_query_range($sql, $crid, $name, 0, 1))) {
    form_set_error('chatroom_chat_name', t('A chat called %name already exists.', array('%name' => $name)));
  }
}

/**
 * creates a chat
 */
function chatroom_create_chat_form_submit($f_id, $f_values) {
  global $user;
  $created = db_query("
    INSERT INTO {chatroom_chat} (crid, uid, chatname, modified)
    VALUES (%d, %d, '%s', %d)
  ", $f_values['room_id'], $user->uid, $f_values['chat_name'], time());
  if (!$created) {
    drupal_set_message(t("There was an error creating your chat"), 'error');
  }
  else {
    chatroom_block_update_cache('chats');
    chatroom_block_update_cache('chatrooms');
  }
}

/**
 * Archive old chat.
 */
function chatroom_archive_chat($chat_id) {
  db_query('UPDATE {chatroom_chat} SET when_archived = %d WHERE ccid = %d', time(), $chat_id);
  $result = db_query('
    INSERT INTO {chatroom_msg_archive}
    (cmid, ccid, uid, msg_type, msg, session_id, recipient, modified)
    SELECT * FROM {chatroom_msg} WHERE ccid = %d
  ', $chat_id);
  if ($result) {
    db_query('DELETE FROM {chatroom_msg} WHERE ccid = %d', $chat_id);
  }
  else {
    db_query('UPDATE {chatroom_chat} SET when_archived = NULL WHERE ccid = %d', $chat_id);
  }
  chatroom_block_update_cache('chats');
  chatroom_block_update_cache('chatrooms');
  return $result;
}

/**
 * Implementation of hook_block().
 */
function chatroom_block($op = 'list', $delta = 0, $edit = array()) {
  $types = array(
    array('chats', t('Chat Room: active chats')),
    array('chat_rooms', t('Chat Room: active chat rooms')),
    array('online_chat_users', t('Chat Room: chat on-line list')),
    array('online_site_users', t('Chat Room: site-wide on-line list')),
    array('command_list', t('Chat Room: chat commands')),
  );
  switch ($op) {
    case 'list':
      $blocks[0]['info'] = $types[0][1];
      $blocks[1]['info'] = $types[1][1];
      $blocks[2]['info'] = $types[2][1];
      $blocks[3]['info'] = $types[3][1];
      $blocks[4]['info'] = $types[4][1];
      return $blocks;
    case 'configure':
      switch ($types[$delta][0]) {
        case 'chats':
          $items = t('chats');
          break;
        case 'chat_rooms':
          $items = t('chat rooms');
      }
      if (isset($items)) {
        $form["chatroom_block_{$types[$delta][0]}"] = array(
          '#type' => 'select',
          '#title' => t('Number of !items to display', array('!items' => $items)),
          '#default_value' => variable_get("chatroom_block_{$types[$delta][0]}", 5),
          '#options' => drupal_map_assoc(range(1, 15))
        );
        return $form;
      }
    case 'save':
      variable_set("chatroom_block_{$types[$delta][0]}", $edit["chatroom_block_{$types[$delta][0]}"]);
      break;
    case 'view':
      if (user_access('access chat rooms')) {
        if ($types[$delta][0] == 'command_list') {
          $block = theme('chatroom_block_commands');
        }
        else {
          // Hack to set up cache files - can't find a way to do this on
          // enable/disable of chat room blocks.
          if ($types[$delta][0] != 'online_chat_users' && !@file_exists(_chatroom_get_cache_file($types[$delta][0]))) {
            chatroom_block_update_cache($types[$delta][0]);
          }
          switch ($types[$delta][0]) {
            case 'chats':
              $block = theme('chatroom_block_chats');
              break;
            case 'chat_rooms':
              $block = theme('chatroom_block_rooms');
              break;
            case 'online_chat_users':
              $block = theme('chatroom_block_chat_online_list');
              break;
            case 'online_site_users':
              global $user;
              $block = theme('chatroom_block_site_online_list', $user->uid);
              break;
          }
          if (!empty($block['content'])) {
            chatroom_block_settings();
          }
        }
        return $block;
      }
  }
}

/**
 * List chat commands.
 */
function theme_chatroom_block_commands() {
  if (preg_match('/^chatrooms\/chat\/(\d+)/', $_GET['q'], $matches)) {

    $displayName = user_load(array('uid' => 'username'));

    /*$content = '/msg <em>'. t('username') .'</em> '. t('message');*/

    $content = '/msg <em>'. t($displayName->profile_displayName) .'</em> '. t('message');

    if (user_access('administer chats')) {
      $content .= '<br />/kick <em>'. t('username') .'</em>';
      $content .= '<br />/ban <em>'. t('username') .'</em>';
    }
    $content .= '<br />/me '. t('message');
    $content .= '<br />/away';
    $content .= '<br />/back';
    return array(
      'content' => $content,
      'subject' => t('Chat commands'),
    );
  }
}

/**
 * List site-wide chats.
 */
function theme_chatroom_block_chats() {
  if ($_GET['q'] != 'chatrooms') {
    $content = '<ul class="menu" id="chatroom-sitewide-chats">';
    $chats = chatroom_get_active_chats(0, variable_get('chatroom_block_chats', 5));
    if ($chats) {
      foreach ($chats as $chat) {
        $chat_link = l($chat->chatname, "chatrooms/chat/$chat->ccid");
        $room_link = l($chat->room_name, "node/$chat->nid");
        $content .= '<li id="chat_'. $chat->ccid .'">'. $chat_link .'<br />';
        $content .= '<span class="chatroomLink">'. t('in') .' '. $room_link .'</span></li>';
      }
    }
    else {
      $content .= '<li id="chat_empty"><em>'. t('There are no active chats.') .'</em></li>';
    }
    $content .= '</ul>';
    return array(
      'content' => $content,
      'subject' => t('Active chats'),
    );
  }
}

/**
 * gets a list of active chats
 */
function chatroom_get_active_chats($start = NULL, $end = NULL, $save_query = TRUE) {
  global $user;
  $sql = "
    SELECT cc.*, pv.value, nr.title AS room_name, cr.nid FROM {chatroom_chat} cc
    INNER JOIN {chatroom} cr ON cr.crid = cc.crid
    INNER JOIN {node_revisions} nr ON nr.nid = cr.nid
    INNER JOIN {node} n ON n.nid = nr.nid
    WHERE cc.when_archived IS NULL
    ORDER BY cc.modified DESC
  ";
  if ($save_query) {
    $sql = db_rewrite_sql($sql);
    variable_set("chatroom_get_active_chats_query_{$user->uid}", $sql);
  }
  else {
    $sql = _chatroom_variable_get("chatroom_get_active_chats_query_{$user->uid}", $sql);
  }
  if (isset($start) && isset($end)) {
    $result = db_query_range($sql, $start, $end);
  }
  else {
    $result = db_query($sql);
  }
  if (db_num_rows($result) > 0) {
    $chats = array();
    while ($chat = db_fetch_object($result)) {
      $chats[] = $chat;
    }
    return $chats;
  }
  return FALSE;
}

/**
 * List chat rooms.
 */
function theme_chatroom_block_rooms() {
  if ($_GET['q'] != 'chatrooms') {
    $content = '<ul class="menu" id="chatroom-sitewide-chatrooms">';
    $rooms = chatroom_get_chatroom_list(FALSE, 0, variable_get('chatroom_block_chat_rooms', 5));
    if (empty($rooms)) {
      $content .= '<li id="chatroom_empty"><em>'. t('There are no active chat rooms.') .'</em></li>';
    }
    else {
      foreach ($rooms as $room) {
        $content .= '<li id="chatroom_'. $room->nid .'">'. l($room->title, "node/$room->nid") .'</li>';
      }
    }
    $content .= '</ul>';
    return array(
      'content' => $content,
      'subject' => t('Active chat rooms'),
    );
  }
}

/**
 * Get a list of on-line users in a given chat.
 */
function theme_chatroom_block_chat_online_list() {
  if (!preg_match('/^chatrooms\/chat\/(\d+)/', $_GET['q'], $matches)) {
    return array(false, false);
  }
  $chat_id = $matches[1];
  $cache_file = _chatroom_get_cache_file("chat.$chat_id");
  $cache_timestamp = @filemtime($cache_file);
  $users = chatroom_chat_get_online_list($chat_id, $cache_timestamp, 0);
  $title = '';
  $content = '';
  if (!empty($users)) {
    $chatname = db_result(db_query("SELECT chatname FROM {chatroom_chat} WHERE ccid = %d", $chat_id));
    $title = t('Who is on line in %chat', array('%chat' => $chatname));
    $content = '<ul class="menu" id="chatroom-online">';
    foreach ($users as $user) {
      $content .= '<li id="'. $user['guestId'] .'"';
      $content .= $user['away'] ? ' class="chatroom-user-away">' : '>';
      $content .= '<a href="javascript:Drupal.chatroom.chat.selectUser(\''. $user['user'] .'\')">'. $user['user'] .'</a></li>';
    }
    $content .= '</ul>';
    if (variable_get('chatroom_alerts', FALSE)) {
      $checked = variable_get('chatroom_alerts_default', FALSE) ? ' checked' : '';
      $content .= '<div id="chatroom-user-options">';
      $content .= '<label><input type="checkbox"'. $checked .' id="chatroom-user-alert" /> ';
      $content .= t('Alert me if new users enter.') .'</label></div>';
    }
  }
  return array(
    'content' => $content,
    'subject' => $title,
  );
}

/**
 * outputs html for list of online users for the whole site
 */
function theme_chatroom_block_site_online_list($uid) {
  chatroom_site_update_online_time($uid);
  $users = chatroom_get_site_online_list($uid);
  $content = '<ul class="menu" id="chatroom-sitewide-online">';
  if (!empty($users)) {
    foreach ($users as $user) {
      $content .= '<li id="user-li-'. $user->uid .'"><strong>'. check_plain($user->name) .'</strong></li>';
    }
  }
  else {
    $content .= '<li id="no_users"><em>'. t('There are no other users on line.') .'</em></li>';
  }
  $content .= '</ul>';
  return array(
    'content' => $content,
    'subject' => t('On-line users'),
  );
}

/**
 * Add settings to block pages.
 */
function chatroom_block_settings() {
  // only do this once per request
  static $state_set = FALSE;
  if (!$state_set) {
    $state_set = TRUE;
    global $user, $base_url;
    // need some css for the user list display when not in chat room chat page
    if (substr($_GET['q'], 0, strlen('chatrooms/chat/')) != 'chatrooms/chat/') {
      drupal_add_css(drupal_get_path('module', 'chatroom') .'/chatroom.css');
    }
    $module_base = drupal_get_path('module', 'chatroom');
    $chats_cache_file = _chatroom_get_cache_file('chats');
    $rooms_cache_file = _chatroom_get_cache_file('chatrooms');
    $js = array(
      'drupalBase' => realpath('.'),
      'baseUrl' => $base_url,
      'updateUrl' => $base_url .'/'. $module_base .'/chatroomread.php',
      'chatroomBase' => $module_base,
      'roomBase' => url('node/'),
      'chatBase' => url('chatrooms/chat/'),
      'chatsCacheFile' => $chats_cache_file,
      'roomsCacheFile' => $rooms_cache_file,
      'userBase' => drupal_get_path('module', 'user'),
      'blockUpdateInterval' => variable_get('chatroom_block_update_interval', 5) * 1000,
      'chatTimestamp' => @file_exists($chats_cache_file) ? @filemtime($chats_cache_file) : time() - 5,
      'roomTimestamp' => @file_exists($rooms_cache_file) ? @filemtime($rooms_cache_file) : time() - 5,
      'uid' => (int) $user->uid,
      'usersMessage' => t('There are no other users on line.'),
      'chatsMessage' => t('There are no active chats.'),
      'roomsMessage' => t('There are no active chat rooms.'),
    );
    $drupal_js = drupal_add_js();
    foreach ($drupal_js['setting'] as $setting) {
      if (is_array($setting['chatroom'])) {
        foreach ($js as $key => $value) {
          if (array_key_exists($key, $setting['chatroom'])) {
            unset($js[$key]);
          }
        }
      }
    }
    drupal_add_js(array('chatroom' => $js), 'setting');
    drupal_add_js(drupal_get_path('module', 'chatroom') .'/chatroom.block.js');
  }
}

/**
 * Menu callback; prints a chat room listing.
 */
function chatroom_page() {
  foreach (chatroom_get_chatroom_list(TRUE) as $room) {
    $tree[$room->nid] = node_load($room->nid);
  }
  return theme('chatroom_display', $tree);
}

/**
 * Format the chat room listing.
 *
 * @ingroup themeable
 */
function theme_chatroom_display($tree) {
  global $user;
  // chat room list, chats list, and 'add new chat' link

  if (!empty($tree)) {
    $output  = '<div id="chatroom">';
    $output .= '<ul>';

    if (user_access('create chat rooms')) {
      $output .= '<li>'. l(t('Post a new chat room.'), "node/add/chatroom") .'</li>';
    }
    else if ($user->uid) {
      //
    }
    else {
      $output .= '<li>'. t('<a href="!login">Login</a> to post a new chat room.', array('!login' => url('user/login'))) .'</li>';
    }
    $output .= '</ul>';
    $output .= theme('chatroom_list', $tree);
    $output .= '</div>';
  }
  else {
    drupal_set_title(t('No chat rooms defined'));
    $output = '';
  }

  return $output;
}

/**
 * Format the chat room table.
 *
 * @ingroup themeable
 */
function theme_chatroom_list($tree) {
  global $user;

  if ($tree) {
    $header = array(t('Chat room'), t('Chats'), t('Messages'), t('Last message'));
    foreach ($tree as $room) {
      $description  = "<div>\n";
      $description .= ' <div class="name">'. l($room->title, "node/$room->nid").'</div>';

      if ($room->body) {
        $description .= ' <div class="description">'. $room->body ."</div>\n";
      }
      $description .= "</div>\n";

      $rows[] = array(array('data' => $description, 'class' => 'container', 'colspan' => '4'));

      if (isset($room->chatroom->chats)) {
        foreach ($room->chatroom->chats as $id => $chat) {
          $description  = "<div>\n";
          if (isset($chat->when_archived)) {
            //$name = user_load(array('uid' => $chat->chatname));
            $description .= ' <div class="name">'. l($chat->chatname, "chatrooms/archives/$chat->ccid") ."</div>\n";
            //$description .= ' <div class="name">'. l($name->profile_displayName, "chatrooms/archives/$chat->ccid") ."</div>\n";

          }
          else {
            //$name = user_load(array('uid' => $chat->chatname));
            $description .= ' <div class="name">'. l($chat->chatname, "chatrooms/chat/$chat->ccid") ."</div>\n";
            //$description .= ' <div class="name">'. l($name->profile_displayName, "chatrooms/chat/$chat->ccid") ."</div>\n";
          }
          $description .= "</div>\n";

          $row[] = array('data' => '&nbsp;');
          $row[] = array('data' => $description, 'class' => 'chatroom-chat');
          $row[] = array('data' => $chat->msg_count, 'class' => 'chatroom-msg-count');
          $row[] = array('data' => $chat->msg_info, 'class' => 'chatroom-msg-info');
          $rows[] = $row;
          unset($row);
        }
      }
    }
    return theme('table', $header, $rows);
  }
}

/**
 * tells a kicked out user not to be a knob
 */
function chatroom_chat_kicked_user($chat_id = FALSE) {
  if ($chat = chatroom_chat_get_from_id($chat_id)) {
    if (isset($chat->when_archived)) {
      drupal_goto("chatrooms/archives/$chat_id", NULL, NULL, 301);
    }
    else {
      if (chatroom_is_banned_user($chat->crid)) {
        $content = theme('chatroom_chat_banned_user', $chat);
      }
      else {
        $content = theme('chatroom_chat_kicked_user', $chat);
      }
      return $content;
    }
  }
  else {
    drupal_not_found();
  }
}

/**
 * Get HTML for kick message.
 *
 * @ingroup themeable
 */
function theme_chatroom_chat_kicked_user($chat) {
  $msg = $chat->kicked_out_message ? $chat->kicked_out_message : t('You have been kicked out of %chat for misbehaving.', array('%chat' => $chat->chatname));
  return '<div id="chatroom-kicked-msg">'. $msg .'</div>';
}

/**
 * Get HTML for ban message.
 *
 * @ingroup themeable
 */
function theme_chatroom_chat_banned_user($chat) {
  $msg = $chat->banned_message ? $chat->banned_message : t('You have been banned from %chatroom.', array('%chatroom' => $chat->chatroom_name));
  return '<div id="chatroom-banned-msg">'. $msg .'</div>';
}

/**
 * loads the UI for a chat, and registers this user as online
 */
function chatroom_chat($chat_id = FALSE) {
  if ($chat = chatroom_chat_get_from_id($chat_id)) {
    if (isset($chat->when_archived)) {
      drupal_goto("chatrooms/archives/$chat_id", NULL, NULL, 301);
    }
    else {
      $content = '';
      if (chatroom_is_banned_user($chat->crid)) {
        $content .= theme('chatroom_chat_banned_user', $chat);
      }
      else {
        if (variable_get('chatroom_auto_archive', FALSE)) {
          chatroom_archive_old_msgs($chat_id);
        }
        chatroom_chat_register_user($chat->ccid);
        chatroom_chat_set_cache($chat->ccid);
        drupal_add_css(drupal_get_path('module', 'chatroom') .'/chatroom.css');
        chatroom_chat_settings($chat);
        $bc = drupal_get_breadcrumb();
        $bc[] = l($chat->chatroom_name, "node/$chat->nid");
        drupal_set_breadcrumb($bc);
        $displayName = user_load(array('uid' => $chat->chatname));
        //drupal_set_title(check_plain($chat->chatname));
        drupal_set_title(check_plain($displayName->profile_displayName));
        $content .= theme('chatroom_chat', $chat);
      }
      return $content;
    }
  }
  else {
    drupal_not_found();
  }
}

/**
 * Get HTML for chat.
 *
 * @ingroup themeable
 */
function theme_chatroom_chat($chat) {
  $html = '<p>'. l(t('View old messages'), "chatrooms/archives/{$chat->ccid}") .'</p>';
  $html .= '<div id="chatroom-container">';
  $html .= theme('chatroom_chat_board');
  $html .= theme('chatroom_chat_textentry');
  $html .= '</div>';
  return $html;
}

/**
 * Move old messages to archive.
 */
function chatroom_archive_old_msgs($chat_id) {
  if ($chat_id != FALSE) {
    global $user;
    $msgs = chatroom_chat_get_latest_msgs($chat_id, $user->uid);
    $result = db_query("
      INSERT INTO {chatroom_msg_archive}
      (cmid, ccid, uid, msg_type, msg, session_id, recipient, modified)
      SELECT * FROM {chatroom_msg}
      WHERE ccid = %d AND cmid < %d
    ", $chat_id, $msgs[0]['id']);
    if ($result) {
      db_query("DELETE FROM {chatroom_msg} WHERE ccid = %d AND cmid < %d", $chat_id, $msgs[0]['id']);
    }
  }
}

/**
 * Handles update or insert for user when loading a chat room.
 *
 * mysql 3.23 compatability patch form pacheco - thanks!
 * http://drupal.org/node/70949
 */
function chatroom_chat_register_user($chat_id) {
  global $user;
  $cache_file = _chatroom_get_cache_file("chat.$chat_id");
  chatroom_chat_update_cache($cache_file);
  $result = db_query_range("
    SELECT coid FROM {chatroom_online_list}
    WHERE ccid = %d AND uid = %d AND session_id = '%s'
  ", $chat_id, $user->uid, session_id(), 0, 1);
  if (db_num_rows($result)) {
    chatroom_chat_update_online_time($chat_id);
  }
  else {
    $is_admin = user_access('administer chats');
    $result = db_query_range("
      SELECT coid FROM {chatroom_online_list}
      WHERE ccid = %d AND session_id = '%s'
    ", $chat_id, session_id(), 0, 1);
    if (db_num_rows($result)) {
      db_query("
        UPDATE {chatroom_online_list} SET uid = %d, is_admin = %d
        WHERE ccid = %d AND session_id = '%s'
      ", $user->uid, $is_admin, $chat_id, session_id());
    }
    else {
      db_lock_table('chatroom_online_list');
      $result = db_query("SELECT COALESCE(MAX(guest_id) + 1, 1) FROM {chatroom_online_list} WHERE ccid = %d", $chat_id);
      if ($result && db_num_rows($result)) {
        db_query("
          INSERT INTO {chatroom_online_list}
          (ccid, uid, is_admin, session_id, guest_id, modified)
          VALUES (%d, %d, %d, '%s', %d, %d)
        ", $chat_id, $user->uid, $is_admin, session_id(), db_result($result), time());
      }
      db_unlock_tables();
    }
  }
}

/**
 * Add settings to chat page.
 */
function chatroom_chat_settings($chat) {
  global $user, $base_url, $base_path;
  $chatroom_base = drupal_get_path('module', 'chatroom');
  $cache_file = _chatroom_get_cache_file("chat.{$chat->ccid}");
  $cache_timestamp = @filemtime($cache_file);
  $users = chatroom_chat_get_online_list($chat->ccid, $cache_timestamp, 0);
  if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
    $timezone = $user->timezone;
  }
  else {
    $timezone = variable_get('date_default_timezone', 0);
  }
  $js = array(
    'chatId' => $chat->ccid,
    'lastMsgId' => 0,
    'cacheTimestamp' => $cache_timestamp,
    'updateCount' => 0,
    'updateUrl' => $base_url .'/'. $chatroom_base .'/chatroomread.php',
    'kickUrl' => url('chatrooms/kicked/'),
    'drupalBase' => realpath('.'),
    'baseUrl' => $base_url,
    'userBase' => drupal_get_path('module', 'user'),
    'chatroomBase' => $chatroom_base,
    'userUrl' => url('user/'),
    'chatCacheFile' => $cache_file,
    'chatUpdateInterval' => (int) $chat->poll_freq,
    'idleInterval' => (int) $chat->idle_freq,
    'chatUsers' => $users,
    'userColours' => _chatroom_load_hex_colours(),
    'timezone' => $timezone,
    'joinMessage' => t(' has joined the chat'),
    'leaveMessage' => t(' has left the chat'),
    'awayMessage' => t(' is away'),
    'backMessage' => t(' is back'),
    'basePath' => $base_path,
  );
  if (function_exists('_smileys_list') && variable_get('chatroom_smileys_enabled', FALSE)) {
    $js['smileysBase'] = drupal_get_path('module', 'smileys');
    $js['smileysMarker'] = '------';
  }
  $drupal_js = drupal_add_js();
  foreach ($drupal_js['setting'] as $setting) {
    if (is_array($setting['chatroom'])) {
      foreach ($js as $key => $value) {
        if (array_key_exists($key, $setting['chatroom'])) {
          unset($js[$key]);
        }
      }
    }
  }
  drupal_add_js(drupal_get_path('module', 'chatroom') .'/chatroom.js');
  if (variable_get('chatroom_alerts', FALSE)) {
    $path = file_directory_path() .'/chatroom';
    if (variable_get('chatroom_custom_sounds', FALSE)) {
      $js['messageSound'] = file_exists("$path/message.mp3") ? file_create_url("$path/message.mp3") : "$chatroom_base/sounds/message.mp3";
      $js['userSound'] = file_exists("$path/user.mp3") ? file_create_url("$path/user.mp3") : "$chatroom_base/sounds/user.mp3";
    }
    else {
      $js['messageSound'] = "$chatroom_base/sounds/message.mp3";
      $js['userSound'] = "$chatroom_base/sounds/user.mp3";
    }
    drupal_add_js(drupal_get_path('module', 'chatroom') .'/soundmanager2.js');
  }
  drupal_add_js(array('chatroom' => $js), 'setting');
}

/**
 * Get an array of dark hex values for user colours.
 */
function _chatroom_load_hex_colours() {
  $colours = array();
  $hex = array('00', '33', '66', '99', 'CC', 'FF');
  foreach ($hex as $rkey => $rvalue) {
    foreach ($hex as $gkey => $gvalue) {
      foreach ($hex as $bkey => $bvalue) {
        if ($rkey + $gkey + $bkey + 3 < 8) {
          $colour->colour = '#'. $rvalue . $gvalue . $bvalue;
          $colour->unUsed = TRUE;
          $colours[] = $colour;
        }
      }
    }
  }
  return $colours;
}

/**
 * returns html for the message board
 */
function theme_chatroom_chat_board() {
  return '<div id="chatroom-board"></div>';
}

/**
 * returns html for the text entry widget
 * themeable
 */
function theme_chatroom_chat_textentry() {
  $output  = '<div class="clear-both"></div>'."\n";
  $output .= '<div id="chatroom-textentry">'."\n";
  $output .= '<span class="chatroom-textentry-title">'. t('Type your message') ."</span><br/><br/>\n";
  $output .= '<input type="text" id="chatroom-msg-input" size="50" maxlength="160" />'."\n";
  $output .= '<input type="submit" id="chatroom-msg-submit" value="'. t('Send') .'" />'."\n";
  $output .= chatroom_smileys_get_textentry();
  $output .= '<div class="chatroom-textentry-options">';
  $output .= '<table><tr>';
  $output .= '<td><label><input type="checkbox" id="chatroom-msg-away" /> '. t('Show me as away.') .'</label></td>';
  if (variable_get('chatroom_alerts', FALSE)) {
    $checked = variable_get('chatroom_alerts_default', FALSE) ? ' checked' : '';
    $output .= '<td><label><input type="checkbox"'. $checked .' id="chatroom-msg-alert" /> '. t('Alert me if new messages are received.') .'</label></td>';
  }
  $output .= '</table>';
  $output .= "</div>\n";
  $output .= "</div>\n";
  return $output;
}

/**
 * smileys module support
 * return text entry smileys list
 */
function chatroom_smileys_get_textentry() {
  if (function_exists('_smileys_list') && variable_get('chatroom_smileys_enabled', FALSE)) {
    $hidden = variable_get('chatroom_smileys_showtextentry', FALSE) ? '' : '-hidden';
    $content = '<div class="chatroom-textentry-smileys'. $hidden .'">';
    $list = _smileys_list();
    foreach ($list as $smiley) {
      $acronym = array_shift(explode(' ', $smiley->acronyms));
      $content .= $hidden ? '' : '<a href="javascript:Drupal.chatroom.chat.smileyInsert(\''. $acronym .'\')">';
      $content .= '<img id="_'. md5($acronym) .'" ';
      $content .= 'src="'. check_url(base_path() . $smiley->image) .'" ';
      $content .= 'title="'. check_plain($smiley->acronyms) .'" ';
      $content .= 'alt="'. $acronym .'" />';
      $content .= $hidden ? '' : '</a>';
    }
    $content .= '</div>';
    return $content;
  }
}

/**
 * loads an archived chat
 */
function chatroom_chat_archive($chat_id = FALSE) {
  if ($chat = chatroom_chat_get_from_id($chat_id)) {
    $content = '';
    if (chatroom_is_banned_user($chat->crid)) {
      $content = theme('chatroom_chat_banned_user', $chat);
    }
    else {
      drupal_add_css(drupal_get_path('module', 'chatroom') .'/chatroom.css');
      $bc = drupal_get_breadcrumb();
      $bc[] = l($chat->chatroom_name, "node/$chat->nid");
      drupal_set_breadcrumb($bc);
      //$name = user_load(array('uid' => $chat->chatname));
      drupal_set_title(check_plain($chat->chatname));
      //drupal_set_title(check_plain($name->profile_displayName));
      $content = theme('chatroom_chat_archive', $chat);
      if (user_access('administer chat rooms') && isset($chat->when_archived)) {
        $content .= drupal_get_form('chatroom_form_chat_delete', $chat_id);
      }
      elseif (user_access('administer chat rooms')) {
        $content .= drupal_get_form('chatroom_form_chat_archive', $chat_id);
      }
    }
    return $content;
  }
  else {
    drupal_not_found();
  }
}

/**
 * form for archived chat view to delete that chat
 *
 * @param integer $chat_id
 * @return array
 */
function chatroom_form_chat_delete($chat_id = NULL) {
  $form = array();
  $form['chat_id'] = array (
    '#type' => 'value',
    '#value' => $chat_id,
  );
  $form['delete'] = array(
    '#type' => 'submit',
    '#value' => t('Delete this chat'),
  );
  return $form;
}

/**
 * Delete an archived chat
 *
 * @param array $form_id
 * @param array $form_values
 * @return string
 */
function chatroom_form_chat_delete_submit($form_id, $form_values) {
  $chat = chatroom_chat_get_from_id($form_values['chat_id']);
  chatroom_chat_delete($form_values['chat_id']);
  return "node/$chat->nid";
}

/**
 * form for archived chat view of open chat to archive that chat
 *
 * @param integer $chat_id
 * @return array
 */
function chatroom_form_chat_archive($chat_id) {
  $form['chat_id'] = array (
    '#type' => 'value',
    '#value' => $chat_id,
  );
  $form['delete'] = array(
    '#type' => 'submit',
    '#value' => t('Archive this chat'),
  );
  return $form;
}

/**
 * Archive an open chat.
 *
 * @param array $form_id
 * @param array $form_values
 * @return string
 */
function chatroom_form_chat_archive_submit($form_id, $form_values) {
  chatroom_archive_chat($form_values['chat_id']);
  file_delete(_chatroom_get_cache_file('chat.'. $form_values['chat_id']));
  db_query("
    INSERT INTO {chatroom_msg_archive} (cmid, ccid, uid, msg_type, msg, session_id, recipient, modified)
    SELECT * FROM {chatroom_msg} WHERE ccid = %d
  ", $form_values['chat_id']);
  $chat = chatroom_chat_get_from_id($form_values['chat_id']);
  chatroom_block_update_cache('chatrooms');
  chatroom_block_update_cache('chats');
}

/**
 * delete a single chat from the database
 *
 * @param int $chat_id
 * @return mixed
 */
function chatroom_chat_delete($chat_id) {
  db_query('DELETE FROM {chatroom_chat} WHERE ccid = %d', $chat_id);
  db_query('DELETE FROM {chatroom_msg_archive} WHERE ccid = %d', $chat_id);
  db_query('DELETE FROM {chatroom_online_list} WHERE ccid = %d', $chat_id);
}

/**
 * Get HTML for chat archive page.
 *
 * @ingroup themeable
 */
function theme_chatroom_chat_archive($chat) {
  $html = '';
  if (isset($chat->when_archived)) {
    $html = '<p>'. t('Archived on !date.', array('!date' => format_date($chat->when_archived, 'medium'))) .'</p>';
  }
  else {
    $html = '<p>'. l(t('Join !chat', array('!chat' => $chat->chatname)), "chatrooms/chat/$chat->ccid") .'</p>';
  }
  if ($msgs = chatroom_get_all_msgs($chat->ccid)) {
    $html .= '<div id="chatroom-container-archive"><table><tbody valign="top">';
    foreach ($msgs as $msg) {
      $name = $msg->name ? $msg->name : variable_get('chatroom_guest_user_prefix', t('guest-')) . $msg->guest_id;
      $displayProfileInfo = user_load(array('uid' => $name));
      drupal_set_title(check_plain($chat->chatname));
      $html .= '<tr><td><span class="chatroom-archive-date">'. format_date($msg->modified, 'small') .'</span></td>';
      if ($msg->msg_type == 'me') {
        $html .= '<td><p class="chatroom-old-me-msg">';
        //$html .= "* $name {$msg->msg}</p></td>";
        $html .= "* $displayProfileInfo->profile_displayName {$msg->msg}</p></td>";
      }
      else {
        $html .= '<td><p class="chatroom-old-msg">';
        if (empty($msg->recipient)) {
          if ($previous != $name) {
            //$html .= '<span class="header">'."$name:</span>";
            $html .= '<span class="header">'."$displayProfileInfo->profile_displayName:</span>";
            //$previous = $name;
            $previous = $displayProfileInfo->profile_displayName;

          }
        }
        else {
          $html .= '<span class="header">'."$name ";
          $recipient = db_result(db_query("
            SELECT pv.value FROM {chatroom_online_list} col
            LEFT JOIN {users} u ON u.uid = col.uid INNER JOIN {profile_values} pv ON pv.uid = u.uid
            WHERE col.ccid = %d AND col.guest_id = %d AND pv.fid = '23'
          ", $msg->ccid, $msg->guest_id));
          $recipient = empty($recipient) ? variable_get('chatroom_guest_user_prefix', t('guest-')) . $msg->recipient : $recipient;
          if ($msg->session_id == session_id() && $recipient != $name) {
            $html .= '<span class="chatroom-private">(privately to '. $recipient .')</span>:</span> ';
          }
          else {
            $html .= '<span class="chatroom-private">(privately)</span>:</span> ';
          }
        }
        $html .= "{$msg->msg}</p></td>";
      }
    }
    $html .= '</tbody></table></div>';
    $limit = chatroom_chat_old_msg_limit($chat->ccid);
    $html .= theme('pager', array(), $limit);
  }
  else {
    $html .= '<p>'. t('This chat contains no messages.') .'</p>';
  }
  return $html;
}

/**
 * get all msgs for a given chat
 */
function chatroom_get_all_msgs($chat_id) {
  global $user;
  $msgs = FALSE;
  $limit = chatroom_chat_old_msg_limit($chat_id);
  if (!empty($limit)) {
    $query = "
      (
        SELECT cma.*, u.name, col.guest_id FROM {chatroom_msg_archive} cma
        LEFT JOIN {chatroom_online_list} col ON col.session_id = cma.session_id AND col.ccid = cma.ccid
        LEFT JOIN {users} u ON u.uid = cma.uid
        WHERE cma.ccid = %d AND (
          cma.recipient = '' OR cma.session_id = '%s' OR cma.recipient IN (
            SELECT guest_id FROM {chatroom_online_list}
            WHERE session_id = '%s' OR (uid = %d AND uid > 0)
          ) OR (cma.uid = %d AND cma.uid > 0)
        )
      )
      UNION
      (
        SELECT cm.*, u.name, col.guest_id FROM {chatroom_msg} cm
        LEFT JOIN {chatroom_online_list} col ON col.session_id = cm.session_id AND col.ccid = cm.ccid
        LEFT JOIN {users} u ON u.uid = cm.uid
        WHERE cm.ccid = %d AND (
          cm.recipient = '' OR cm.session_id = '%s' OR cm.recipient IN (
            SELECT guest_id FROM {chatroom_online_list}
            WHERE session_id = '%s' OR (uid = %d AND uid > 0)
          ) OR (cm.uid = %d AND cm.uid > 0)
        )
      )
      ORDER BY cmid ASC
    ";
    $count_query = "
      SELECT
      (
        SELECT COUNT(*) FROM {chatroom_msg_archive}
        WHERE ccid = %d AND (
          recipient = '' OR session_id = '%s' OR recipient IN (
            SELECT guest_id FROM {chatroom_online_list}
            WHERE session_id = '%s' OR (uid = %d AND uid > 0)
          ) OR (uid = %d AND uid > 0)
        )
      )
      +
      (
        SELECT COUNT(*) FROM {chatroom_msg}
        WHERE ccid = %d AND (
          recipient = '' OR session_id = '%s' OR recipient IN (
            SELECT guest_id FROM {chatroom_online_list}
            WHERE session_id = '%s' OR (uid = %d AND uid > 0)
          ) OR (uid = %d AND uid > 0)
        )
      )
    ";
    $arguments = array(
      $chat_id,
      session_id(),
      session_id(),
      $user->uid,
      $user->uid,
      $chat_id,
      session_id(),
      session_id(),
      $user->uid,
      $user->uid,
    );
    $result = pager_query($query, $limit, 0, $count_query, $arguments);
    $msgs = array();
    while ($message = db_fetch_object($result)) {
      if (function_exists('_smileys_list') && variable_get('chatroom_smileys_enabled', FALSE)) {
        $message->msg = smileys_filter_process($message->msg);
      }
      $msgs[] = $message;
    }
  }
  return $msgs;
}

/**
 * updates chat's cache file modified time
 */
function chatroom_chat_update_cache($cache_file) {
  if (@touch($cache_file)) {
    return @filemtime($cache_file);
  }
  return FALSE;
}

/**
 * update a users online timestamp value
 */
function chatroom_chat_update_online_time($chat_id) {
  global $user;
  db_query("
    UPDATE {chatroom_online_list} SET modified = %d
    WHERE ccid = %d AND uid = %d AND session_id = '%s'
  ", time(), $chat_id, $user->uid, session_id());
}

/**
 * Get all messages for chat room after last message.
 */
function chatroom_chat_get_latest_msgs($chat_id, $uid, $last_msg_id = 0, $timezone = 0, $smileys = FALSE) {
  global $user;
  $msgs = array();
  $limit = chatroom_chat_old_msg_limit($chat_id, $last_msg_id);
  if (!empty($limit)) {
    $result = db_query_range("
      SELECT cm.*, u.name, col.guest_id FROM {chatroom_msg} cm
      INNER JOIN {chatroom_online_list} col ON col.session_id = cm.session_id AND col.ccid = cm.ccid
      LEFT JOIN {users} u ON u.uid = cm.uid
      WHERE cm.ccid = %d AND (
        cm.recipient = '' OR cm.session_id = '%s' OR cm.recipient IN (
          SELECT guest_id FROM {chatroom_online_list}
          WHERE session_id = '%s' OR (uid = %d AND uid > 0)
        ) OR (cm.uid = %d AND cm.uid > 0)
      )
      ORDER BY cm.cmid DESC
    ", array(
      $chat_id,
      session_id(),
      session_id(),
      $user->uid,
      $user->uid,
    ), 0, $limit);
    while ($message = db_fetch_object($result)) {
      if ($smileys) {
        $message->msg = chatroom_smileys_filter_process($message->msg);
      }
      $name = empty($message->name) ? _chatroom_variable_get('chatroom_guest_user_prefix', 'guest-') . $message->guest_id : $message->name;
      $time = gmdate(_chatroom_variable_get('chatroom_chat_date_format', '* \S\e\n\t \a\t G:i'), $message->modified + $timezone);
      $msg = array(
        'id' => (int) $message->cmid,
        'text' => stripslashes(str_replace(array("\\", '"'), array("\\\\", '\"'), $message->msg)),
        'user' => $name,
        'time' => $time,
        'recipient' => $message->recipient,
        'type' => $message->msg_type,
      );
      $msgs[] = $msg;
    }
    $msgs = array_reverse($msgs);
  }
  return $msgs;
}

/**
 * Get the number of old messages to show when entering a chat
 */
function chatroom_chat_old_msg_limit($chat_id, $last_msg_id = 0) {
  $limit = db_result(db_query('
    SELECT cr.old_msg_count FROM {chatroom} cr
    INNER JOIN {chatroom_chat} cc ON cc.crid = cr.crid
    WHERE cc.ccid = %d
  ', $chat_id));
  $count = db_result(db_query("SELECT COUNT(*) FROM {chatroom_msg} WHERE ccid = %d AND cmid > %d", $chat_id, $last_msg_id));
  $count += db_result(db_query("SELECT COUNT(*) FROM {chatroom_msg_archive} WHERE ccid = %d AND cmid > %d", $chat_id, $last_msg_id));
  return $count > $limit ? $limit : $count;
}

/**
 * Replacement for variable_get because configuration variables are not loaded in session bootstrap
 */
function _chatroom_variable_get($name, $default) {
  $result = db_query_range("SELECT value FROM {variable} WHERE name = '%s'", $name, 0, 1);
  if (db_num_rows($result)) {
    $default = unserialize(db_result($result));
  }
  return $default;
}

/**
 * smileys module support - process smileys filtering
 * this is modified from smileys.module
 * - because we don't have full bootstrap
 * - because we don't want to send html, just data about image
 */
function chatroom_smileys_filter_process($text) {
  if (function_exists('_smileys_list') && _chatroom_variable_get('chatroom_smileys_enabled', FALSE)) {
    $text = ' '. $text .' ';
    $list = _smileys_list();
    $marker = '------';
    foreach ($list as $smiley) {
      $acronyms = explode(" ", $smiley->acronyms);
      $alt = str_replace('\\', '\\\\', check_plain($smiley->description));
      foreach ($acronyms as $acronym) {
        if ($smiley->standalone) {
          $replace = "\\1{$marker}_". md5($acronyms[0]) ."$marker\\2";
          $text = eregi_replace("([ ,\.\?!:\(\)\r\n\<\>])". preg_quote($acronym) ."([ ,\.\?!:\(\)\r\n\<\>])", $replace, $text);
        }
        else {
          $replace = $marker .'_'. md5($acronyms[0]) . $marker;
          $text = eregi_replace(preg_quote($acronym), $replace, $text);
        }
      }
    }
    $text = substr($text, 1, -1);
  }
  return $text;
}

/**
 * sets the chat's cache file by touching it
 */
function chatroom_chat_set_cache($chat_id) {
  $chat_file = _chatroom_get_cache_file("chat.$chat_id");
  if (!file_exists($chat_file)) {
    @touch($chat_file);
  }
}

/**
 * Returns a chat room object for $chat_id.
 */
function chatroom_chat_get_from_id($chat_id, $save_query = TRUE) {
  if ($chat_id !== FALSE) {
    global $user;
    $sql = "
      SELECT
        cc.*,
        n.title AS chatroom_name,
        n.nid,
        cr.poll_freq,
        cr.idle_freq,
        cr.kicked_out_message,
        cr.banned_message
      FROM {chatroom_chat} cc
      INNER JOIN {chatroom} cr ON cr.crid = cc.crid
      INNER JOIN {node} n ON n.nid = cr.nid
      WHERE cc.ccid = %d
    ";
    if ($save_query) {
      $sql = db_rewrite_sql($sql);
      variable_set("chatroom_chat_get_from_id_query_{$user->uid}", $sql);
    }
    else {
      $sql = _chatroom_variable_get("chatroom_chat_get_from_id_query_{$user->uid}", $sql);
    }
    $result = db_query_range($sql, $chat_id, 0, 1);
    return db_fetch_object($result);
  }
  return FALSE;
}

/**
 * updates a users online time
 */
function chatroom_site_update_online_time($uid) {
  db_query('UPDATE {users} SET access = %d WHERE uid = %d', time(), $uid);
}

/**
 * updates a block cache file
 */
function chatroom_block_update_cache($type) {
  @touch(_chatroom_get_cache_file($type));
}

/**
 * get block cache file
 */
function _chatroom_get_cache_file($type) {
  $path = file_directory_temp() .'/drupal.chatroom.'. session_name();
  if (file_check_directory($path, TRUE)) {
    $path .= "/$type";
  }
  else {
    $path .= ".$type";
  }
  return $path;
}

/**
 * gets a list of online users, not including the current user
 */
function chatroom_get_site_online_list($uid) {
  $users = array();
  $time_period = 2 * _chatroom_variable_get('chatroom_block_update_interval', 5);
  $result = db_query("SELECT u.uid, u.name, pv.value FROM {users} u JOIN {profile_values} pv ON u.uid = pv.uid WHERE access >= %d AND uid != 0 AND pv.fid = '23'", time() - $time_period);
  while ($user = db_fetch_object($result)) {
    if ($uid != $user->uid) {
      $users[] = $user;
    }
  }
  return $users;
}

/**
 * Checks if the user is banned from the chat room - returns false if not.
 */
function chatroom_is_banned_user($crid) {
  global $user;
  static $result = NULL;
  if (!is_null($result)) {
    return $result;
  }
  if ($user->uid == 0) {
    $result = FALSE;
  }
  else {
    $result = db_num_rows(db_query_range("SELECT crid FROM {chatroom_ban_list} WHERE crid = %d AND uid = %d", $crid, $user->uid, 0, 1));
  }
  return $result;
}

/**
 * returns the list of available chat rooms
 */
function chatroom_get_chatroom_list($archived = FALSE, $start = NULL, $end = NULL, $save_query = TRUE) {
  global $user;
  $sql = '
    SELECT nr.*, COUNT(cc.ccid) AS chat_count FROM {chatroom} cr
    INNER JOIN {node_revisions} nr ON nr.nid = cr.nid
    INNER JOIN {node} n ON n.nid = nr.nid
    INNER JOIN {chatroom_chat} cc ON cc.crid = cr.crid
  ';
  $sql .= $archived ? '' : ' WHERE cc.when_archived IS NULL';
  $sql .= ' GROUP BY cr.crid ORDER BY n.sticky DESC, nr.timestamp DESC';
  $variable = 'chatroom_get_chatroom_list_';
  $variable .= $archived ? "archived_query_{$user->uid}" : "query_{$user->uid}";
  if ($save_query) {
    $sql = db_rewrite_sql($sql);
    variable_set($variable, $sql);
  }
  else {
    $sql = _chatroom_variable_get($variable, $sql);
  }
  if (isset($start) && !empty($end)) {
    $result = db_query_range($sql, $start, $end);
  }
  else {
    $result = db_query($sql);
  }
  $rooms = array();
  if (db_num_rows($result) > 0) {
    while ($room = db_fetch_object($result)) {
      $rooms[] = $room;
    }
  }
  return $rooms;
}

/**
 * Get a list of online users in a given chat.
 * Return the current user at the top of the list.
 * Users who do not update within five seconds after the last message is
 * sent are dropped from the online list.
 */
/*
function chatroom_chat_get_online_list($chat_id, $cache_timestamp, $update_count = 7) {
  $users = array();
  if ($chat = chatroom_chat_get_from_id($chat_id, FALSE) && !isset($chat->when_archived)) {
    if ($cache_timestamp < time() - 5) {
      db_query("UPDATE {chatroom_chat} SET modified = %d WHERE ccid = %d", $cache_timestamp, $chat_id);
    }
    $result = db_query("
      SELECT
        col.uid,
        col.session_id,
        col.guest_id,
        col.away,
        cc.chatname,
        du.name
      FROM {chatroom_online_list} col
      INNER JOIN {chatroom_chat} cc ON cc.ccid = col.ccid
      LEFT JOIN {users} du ON du.uid = col.uid
      WHERE col.ccid = %d AND col.modified > cc.modified
    ", $chat_id);
    while ($row = db_fetch_object($result)) {
      $name = $row->name ? check_plain($row->name) : _chatroom_variable_get('chatroom_guest_user_prefix', 'guest-') . $row->guest_id;
      $user = array(
        'user' => $name,
        'uid' => $row->uid,
        'guestId' => $row->guest_id,
        'away' => (int) $row->away,
      );
      if ($row->session_id == session_id()) {
        $user['self'] = TRUE;
        $current_user = $user;
      }
      else {
        $users[] = $user;
      }
    }
    if (isset($current_user)) {
      array_unshift($users, $current_user);
    }
    if ($update_count < 7) {
      $users = array($current_user);
    }
  }
  return $users;
}
*/

function chatroom_chat_get_online_list($chat_id, $cache_timestamp, $update_count = 7) {
  $users = array();
  if ($chat = chatroom_chat_get_from_id($chat_id, FALSE) && !isset($chat->when_archived)) {
    if ($cache_timestamp < time() - 5) {
      db_query("UPDATE {chatroom_chat} SET modified = %d WHERE ccid = %d", $cache_timestamp, $chat_id);
    }
    $result = db_query("
      SELECT
        col.uid,
        col.session_id,
        col.guest_id,
        col.away,
        cc.chatname,
        du.name,
        pv.value
      FROM {chatroom_online_list} col
      INNER JOIN {chatroom_chat} cc ON cc.ccid = col.ccid
      LEFT JOIN {users} du ON du.uid = col.uid RIGHT JOIN {profile_values} pv ON du.uid = pv.uid
      WHERE col.ccid = %d AND col.modified > cc.modified AND pv.fid = '23'
    ", $chat_id);
    while ($row = db_fetch_object($result)) {
      $name = $row->value ? check_plain($row->value) : _chatroom_variable_get('chatroom_guest_user_prefix', 'guest-') . $row->guest_id;
      $user = array(
        'user' => $row->value,
        'uid' => $row->uid,
        'guestId' => $row->guest_id,
        'away' => (int) $row->away,
      );
      if ($row->session_id == session_id()) {
        $user['self'] = TRUE;
        $current_user = $user;
      }
      else {
        $users[] = $user;
      }
    }
    if (isset($current_user)) {
      array_unshift($users, $current_user);
    }
    if ($update_count < 7) {
      $users = array($current_user);
    }
  }
  return $users;
}


Again, any help would be great!

Sarah

marvil07’s picture

Status: Active » Closed (won't fix)

also take a look to realname module