Index: chatroom.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/chatroom/Attic/chatroom.js,v
retrieving revision 1.43.2.5
diff -u -p -r1.43.2.5 chatroom.js
--- chatroom.js	8 Feb 2007 17:54:32 -0000	1.43.2.5
+++ chatroom.js	23 Feb 2007 19:28:02 -0000
@@ -55,6 +55,10 @@ var chatroomMsgCallback = function(respo
           chatroom.cacheTimestamp = resArray[i].cacheTimestamp;
           continue;
         }
+        if (typeof resArray[i].time != 'undefined') {
+          chatroom.time = resArray[i].time;
+          continue;
+        }
         if (typeof resArray[i].msgs != 'undefined') {
           chatroomUpdateMsgList(resArray[i].msgs);
           continue;
@@ -440,7 +444,7 @@ function chatroomUpdateChatOnlineList(up
     }
     $('chatroom-online').removeChild($(chatroom.userList[usersToDelete[i][0]].sessionId));
     chatroom.userList.splice(usersToDelete[i][0], 1);
-    if (chatroom.updateCount > 5) {
+    if (chatroom.time > (chatroom.cacheTimestamp + 5) && chatroom.updateCount > 5) {
       chatroomWriteSystemMsg(usersToDelete[i][1], 'leave');
     }
   }
@@ -468,7 +472,7 @@ function chatroomUpdateChatOnlineList(up
         addClass(li, 'chatroom-user-away');
       }
       $('chatroom-online').appendChild(li);
-      if (chatroom.updateCount > 5) {
+      if (chatroom.time > (chatroom.cacheTimestamp + 5) && chatroom.updateCount > 5) {
         chatroomWriteSystemMsg(updateUsers[i].user, 'join');
       }
     }
Index: chatroom.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/chatroom/Attic/chatroom.module,v
retrieving revision 1.50.2.42
diff -u -p -r1.50.2.42 chatroom.module
--- chatroom.module	23 Feb 2007 01:59:48 -0000	1.50.2.42
+++ chatroom.module	23 Feb 2007 19:28:34 -0000
@@ -695,9 +695,10 @@ function chatroom_block_chat_online_list
   if (!preg_match('/^chatrooms\/chat\/(\d+)/', $_GET['q'], $matches)) {
     return array(false, false);
   }
-  $title   = '';
+  $title = '';
   $content = '';
-  $cache_timestamp = @file_exists($cache_file) ? @filemtime($cache_file) : time();
+  $cache_file = file_directory_temp() .'/drupal_chat_cache/'. chatroom_chat_get_cache_file_name($chat->ccid);
+  $cache_timestamp = @filemtime($cache_file);
   if ($users = chatroom_chat_get_online_list($matches[1], $cache_timestamp)) {
     $content = '<ul class="menu" id="chatroom-online">';
     foreach ($users as $ol_user) {
@@ -761,29 +762,28 @@ function chatroom_block_add_state_js() {
     theme_add_style(drupal_get_path('module', 'chatroom') .'/chatroom.css');
   }
 
-  // get some link information
-  $chat_base   = url('chatrooms/chat/');
-  $room_base   = url('node/');
-  $module_base = drupal_get_path('module', 'chatroom');
-  $user_base   = drupal_get_path('module', 'user');
-  $block_url   = $base_url .'/'. $module_base .'/chatroomread.php';
-
-  // setup the cache file paths
   $chat_cache_file = chatroom_block_get_cache_file('chats');
   $room_cache_file = chatroom_block_get_cache_file('chatrooms');
 
-  // setup some timing variables
-  $poll_freq = variable_get('chatroom_block_poll_freq', 5000);
-  $timestamp = time() - (60 * 60);
-
-  // put it all together
-  $js = <<<JS
-var chatroomBlock = {blockUrl:'$block_url',moduleBase:'$module_base',smileysModuleBase:'$smileys_module_base',roomBase:'$room_base',
-                     chatBase:'$chat_base',chatCacheFile:'$chat_cache_file',roomCacheFile:'$room_cache_file',userBase:'$user_base',
-                     updateInterval:$poll_freq,chatTimestamp:$timestamp,roomTimestamp:$timestamp,uid:$user->uid,siteBase:'$site_base'};
-JS;
+  $js = array(
+    'blockUrl' => $base_url .'/'. $module_base .'/chatroomread.php',
+    'moduleBase' => drupal_get_path('module', 'chatroom'),
+    'smileysModuleBase' => '',
+    'roomBase' => url('node/'),
+    'chatBase' => url('chatrooms/chat/'),
+    'chatCacheFile' => $chat_cache_file,
+    'roomCacheFile' => $room_cache_file,
+    'userBase' => drupal_get_path('module', 'user'),
+    'updateInterval' => variable_get('chatroom_block_poll_freq', 5000),
+    'chatTimestamp' => @file_exists($chat_cache_file) ? @filemtime($chat_cache_file) : time() - 5,
+    'roomTimestamp' => @file_exists($room_cache_file) ? @filemtime($room_cache_file) : time() - 5,
+    'uid' => $user->uid,
+    'siteBase' => '',
+  );
+  $js = (object) $js;
+  $js = drupal_to_js($js);
 
-  drupal_set_html_head('<script type="text/javascript">'. $js .'</script>');
+  drupal_set_html_head('<script type="text/javascript">var chatroomBlock = '. $js .';</script>');
   drupal_add_js(drupal_get_path('module', 'chatroom') .'/chatroom.block.js');
 }
 
@@ -949,6 +949,8 @@ function chatroom_archive_old_msgs($chat
 function chatroom_chat_register_user($chat_id) {
   global $user;
   $session_id = session_id();
+  $cache_file = file_directory_temp() .'/drupal_chat_cache/'. chatroom_chat_get_cache_file_name($chat_id);
+  chatroom_chat_update_cache($cache_file);
   $result = db_query("SELECT coid
                       FROM   {chatroom_online_list}
                       WHERE  ccid = %d
@@ -979,27 +981,26 @@ function chatroom_chat_register_user($ch
  * helper function to add some state information to a chat page
  */
 function chatroom_chat_add_state_js($chat, $session_id) {
-  global $user;
-  global $base_url;
-
-  $users_js = '[]';
-  $cache_timestamp = @file_exists($cache_file) ? @filemtime($cache_file) : time();
-  if ($users = chatroom_chat_get_online_list($chat->ccid, $cache_timestamp)) {
-    $users_js = chatroom_chat_get_online_list_js($users);
-  }
-  $kick_url       = url('chatrooms/kicked/user');
-  $ban_url        = url('chatrooms/banned/user');
-  $user_url       = url('user/');
-  $chatroom_base  = drupal_get_path('module', 'chatroom');
-  $chat_url       = $base_url .'/'. $chatroom_base .'/chatroomread.php';
-  $user_base      = drupal_get_path('module', 'user');
-  $cache_file     = file_directory_temp() .'/drupal_chat_cache/'. chatroom_chat_get_cache_file_name($chat->ccid);
-  $smileys_base   = drupal_get_path('module', 'smileys') ? "'". drupal_get_path('module', 'smileys') ."'" : 'false';
-  $smileys_marker = '------';
-  $timestamp      = 0;
-  $online_list    = variable_get('chatroom_online_list', 0) ? 'true' : 'true';
+  global $user, $base_url;
 
-  // timezone support
+  $chatroom_base = drupal_get_path('module', 'chatroom');
+  $cache_file = file_directory_temp() .'/drupal_chat_cache/'. chatroom_chat_get_cache_file_name($chat->ccid);
+  $cache_timestamp = @filemtime($cache_file);
+  $users = chatroom_chat_get_online_list($chat->ccid, $cache_timestamp);
+  $ol_users = array();
+  if (!empty($users)) {
+    foreach ($users as $ol_user) {
+      $name = $ol_user->name ? check_plain($ol_user->name) : "guest-{$ol_user->guest_id}";
+      $ol_user = array(
+        'user' => $name,
+        'sessionId' => $ol_user->session_id,
+        'uid' => $ol_user->uid,
+        'away' => $ol_user->away,
+      );
+      $ol_user = (object) $ol_user;
+      $ol_users[] = $ol_user;
+    }
+  }
   if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
     $timezone = $user->timezone;
   }
@@ -1007,30 +1008,32 @@ function chatroom_chat_add_state_js($cha
     $timezone = variable_get('date_default_timezone', 0);
   }
 
-  $js = <<<JS
-var chatroom = {chatId         : $chat->ccid,
-                lastMsgId      : 0,
-                cacheTimestamp : $timestamp,
-                updateCount    : 0,
-                chatUrl        : '$chat_url',
-                kickUrl        : '$kick_url',
-                banUrl         : '$ban_url',
-                userBase       : '$user_base',
-                chatroomBase   : '$chatroom_base',
-                smileysBase    : $smileys_base,
-                smileysMarker  : '$smileys_marker',
-                userUrl        : '$user_url',
-                chatCacheFile  : '$cache_file',
-                updateInterval : $chat->poll_freq,
-                idleInterval   : $chat->idle_freq,
-                userList       : $users_js,
-                onlineList     : $online_list,
-                userColours    : [],
-                sessionId      : '$session_id',
-                timezone       : $timezone
-                };
-JS;
-  drupal_set_html_head('<script type="text/javascript">'. $js .'</script>');
+  $js = array(
+    'chatId' => $chat->ccid,
+    'lastMsgId' => 0,
+    'cacheTimestamp' => 0,
+    'updateCount' => 0,
+    'chatUrl' => $base_url .'/'. $chatroom_base .'/chatroomread.php',
+    'kickUrl' => url('chatrooms/kicked/user'),
+    'banUrl' => url('chatrooms/banned/user'),
+    'userBase' => drupal_get_path('module', 'user'),
+    'chatroomBase' => $chatroom_base,
+    'smileysBase' => drupal_get_path('module', 'smileys'),
+    'smileysMarker' => '------',
+    'userUrl' => url('user/'),
+    'chatCacheFile' => $cache_file,
+    'updateInterval' => $chat->poll_freq,
+    'idleInterval' => $chat->idle_freq,
+    'userList' => $ol_users,
+    'onlineList' => variable_get('chatroom_online_list', 0),
+    'userColours' => array(),
+    'sessionId' => $session_id,
+    'timezone' => $timezone,
+  );
+  $js = (object) $js;
+  $js = drupal_to_js($js);
+
+  drupal_set_html_head('<script type="text/javascript">var chatroom = '. $js .';</script>');
 }
 
 /**
@@ -1148,18 +1151,13 @@ function chatroom_chat_read_msgs($chat_i
     $msg_js = '[]';
   }
   $users_js = '[]';
-  $cache_timestamp = @file_exists($cache_file) ? @filemtime($cache_file) : time();
+  $cache_timestamp = @filemtime($cache_file);
   if ($users = chatroom_chat_get_online_list($chat_id, $cache_timestamp)) {
     $users_js = chatroom_chat_get_online_list_js($users, true);
   }
-  $cache_js = '';
-  if (empty($last_msg_id)) {
-    $cache_timestamp = chatroom_chat_update_cache($cache_file);
-  }
-  if ($cache_timestamp) {
-    $cache_js = ",{cacheTimestamp:$cache_timestamp}";
-  }
-  return chatroom_send_ajax_response("[$msg_js, $users_js $cache_js]");
+  $cache_js = $cache_timestamp ? ",{cacheTimestamp:$cache_timestamp}" : '';
+  $time = ",{time:". time() ."}";
+  return chatroom_send_ajax_response("[$msg_js, $users_js $cache_js $time]");
 }
 
 /**
@@ -1189,16 +1187,15 @@ function chatroom_chat_write_msg($chat_i
     $msg_js = '[]';
   }
   $users_js = '[]';
-  $cache_timestamp  = @file_exists($cache_file) ? @filemtime($cache_file) : time();
+  $cache_timestamp  = @filemtime($cache_file);
   if ($users = chatroom_chat_get_online_list($chat_id, $cache_timestamp)) {
     $users_js = chatroom_chat_get_online_list_js($users, true);
   }
   $cache_js = '';
   $cache_timestamp = chatroom_chat_update_cache($cache_file);
-  if ($cache_timestamp) {
-    $cache_js = ",{cacheTimestamp:$cache_timestamp}";
-  }
-  return chatroom_send_ajax_response("[$msg_js, $users_js $cache_js]");
+  $cache_js = ",{cacheTimestamp:$cache_timestamp}";
+  $time = ",{time:". time() ."}";
+  return chatroom_send_ajax_response("[$msg_js, $users_js $cache_js $time]");
 }
 
 /**
@@ -1219,16 +1216,15 @@ function chatroom_chat_set_away_status($
     $msg_js = '[]';
   }
   $users_js = '[]';
-  $cache_timestamp  = @file_exists($cache_file) ? @filemtime($cache_file) : time();
+  $cache_timestamp  = @filemtime($cache_file);
   if ($users = chatroom_chat_get_online_list($chat_id, $cache_timestamp)) {
     $users_js = chatroom_chat_get_online_list_js($users, true);
   }
   $cache_js = '';
   $cache_timestamp = chatroom_chat_update_cache($cache_file);
-  if ($cache_timestamp) {
-    $cache_js = ",{cacheTimestamp:$cache_timestamp}";
-  }
-  return chatroom_send_ajax_response("[$msg_js, $users_js $cache_js]");
+  $cache_js = ",{cacheTimestamp:$cache_timestamp}";
+  $time = ",{time:". time() ."}";
+  return chatroom_send_ajax_response("[$msg_js, $users_js $cache_js $time]");
 }
 
 /**
@@ -1630,8 +1626,8 @@ function chatroom_get_chatroom_list($sta
  * gets a list of online users in a given chat
  * returns the current user at the top of the list
  */
-function chatroom_chat_get_online_list($chat_id, $cache_timestamp = NULL) {
-  if (empty($cache_timestamp)) {
+function chatroom_chat_get_online_list($chat_id, $cache_timestamp = FALSE) {
+  if (!$cache_timestamp) {
     $cache_timestamp = time();
   }
   $result = db_query('SELECT     col.uid,
