From 82a524ef5496e0f16549e1e002f30ec2e6c234e1 Mon Sep 17 00:00:00 2001 From: moodler Date: Wed, 11 Aug 2004 14:45:40 +0000 Subject: [PATCH] Latest changes to the chat daemon ... user window now working Needs: - testing - beeps in daemon mode (you can type "beep all" though) --- mod/chat/chatd.php | 239 ++++++++++++++++----------------- mod/chat/gui_sockets/index.php | 4 +- mod/chat/lib.php | 49 ++++--- 3 files changed, 151 insertions(+), 141 deletions(-) diff --git a/mod/chat/chatd.php b/mod/chat/chatd.php index 5c6a43500a..76c7125c0e 100755 --- a/mod/chat/chatd.php +++ b/mod/chat/chatd.php @@ -67,30 +67,100 @@ class ChatDaemon { var $message_queue = array(); // Holds messages that we haven't committed to the DB yet + function update_lastmessageping($sessionid, $time = NULL) { + // TODO: this can and should be written as a single UPDATE query + if(empty($this->sets_info[$sessionid])) { + trace('update_lastmessageping() called for an invalid SID: '.$sessionid, E_USER_WARNING); + return false; + } + + $now = time(); + if(empty($time)) { + $time = $now; + } + + // We 'll be cheating a little, and NOT updating lastmessageping + // as often as we have to, so we can save on DB queries (imagine MANY users) + $this->sets_info[$sessionid]['chatuser']->lastmessageping = $time; + + // This will set it just fine for bookkeeping purposes. + if($now - $this->sets_info[$sessionid]['lastinfocommit'] > $this->live_data_update_threshold) { + // commit to permanent storage + trace('Committing volatile lastmessageping for session '.$sessionid); + $this->sets_info[$sessionid]['lastinfocommit'] = $now; + update_record('chat_users', $this->sets_info[$sessionid]['chatuser']); + } + return true; + } + function get_user_window($sessionid) { + + global $CFG, $THEME; + + static $str; + + $info = &$this->sets_info[$sessionid]; + $oldlang = chat_language_override($info['lang']); + + $timenow = time(); + + if (empty($str)) { + $str->idle = get_string("idle", "chat"); + $str->beep = get_string("beep", "chat"); + $str->day = get_string("day"); + $str->days = get_string("days"); + $str->hour = get_string("hour"); + $str->hours = get_string("hours"); + $str->min = get_string("min"); + $str->mins = get_string("mins"); + $str->sec = get_string("sec"); + $str->secs = get_string("secs"); + } + ob_start(); - echo "\n\n"; - - // - /* - echo ""; - */ - // - - //echo "\n\n"; - echo "\n\n"; - - // - echo '

User window for session '.$sessionid.'

'; - echo '

The time is '.time().'

'; - // + echo ''; + echo ''; + + echo ''; + if(empty($this->sets_info)) { + // No users + echo ''; + } + else { + foreach ($this->sets_info as $usersid => $userinfo) { + $lastping = $timenow - $userinfo['chatuser']->lastmessageping; + echo '"; + } + } + echo '
 
'; + + echo ''; + print_user_picture($userinfo['user']->id, 0, $userinfo['user']->picture, false, false, false); + echo ""; + echo "

"; + echo fullname($userinfo['user'])."
"; + echo "$str->idle: ".format_time($lastping, $str).""; + //echo " id&groupid=$groupid\">$str->beep"; + echo "

"; + echo "
'; echo "\n\n"; + + chat_language_restore($oldlang); return ob_get_clean(); } @@ -156,13 +226,27 @@ class ChatDaemon { $header .= "Cache-Control: no-cache, must-revalidate\n"; $header .= "Expires: Wed, 4 Oct 1978 09:32:45 GMT\n"; $header .= "Content-Length: ".strlen($content)."\n"; - $header .= "Refresh: 3; url=http://$CFG->chat_serverhost:$CFG->chat_serverport/?win=users&". + $header .= "Refresh: $CFG->chat_refresh_userlist; URL=http://$CFG->chat_serverhost:$CFG->chat_serverport/?win=users&". "chat_sid=".$sessionid."&groupid=".$this->sets_info[$sessionid]['groupid']."\n"; $header .= "\n"; // That's enough headers for one lousy dummy response + trace('writing users http response to handle '.$handle); chat_socket_write($handle, $header . $content); +/* + $header = "HTTP/1.1 200 OK\n"; + $header .= "Connection: close\n"; + $header .= "Date: ".date('r')."\n"; + $header .= "Server: Moodle\n"; + $header .= "Content-Type: text/html\n"; + $header .= "Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT\n"; + $header .= "Cache-Control: no-cache, must-revalidate\n"; + $header .= "Expires: Wed, 4 Oct 1978 09:32:45 GMT\n"; + $header .= "\n"; + trace('writing users http response to handle '.$handle); + chat_socket_write($handle, $header); +*/ break; case CHAT_SIDEKICK_MESSAGE: // Incoming message @@ -186,12 +270,7 @@ class ChatDaemon { $this->message_broadcast($msg, $this->sets_info[$sessionid]['user']); // Update that user's lastmessageping - // TODO: this can and should be written as a single UPDATE query - $user = get_record('chat_users', 'sid', $sessionid); - if($user !== false) { - $user->lastmessageping = $msg->timestamp; - update_record('chat_users', $user); - } + $this->update_lastmessageping($sessionid, $msg->timestamp); // We did our work, but before slamming the door on the poor browser // show the courtesy of responding to the HTTP request. Otherwise, some @@ -261,7 +340,13 @@ class ChatDaemon { } $this->conn_sets[$sessionid] = $this->conn_half[$sessionid]; + + // This whole thing needs to be purged of redundant info, and the + // code base to follow suit. But AFTER development is done. $this->sets_info[$sessionid] = array( + 'lastinfocommit' => 0, + 'courseid' => $course->id, + 'chatuser' => $chatuser, 'chatid' => $chatuser->chatid, 'user' => $user, 'userid' => $chatuser->userid, @@ -301,7 +386,7 @@ class ChatDaemon { // Is the main connection ready? if(isset($this->conn_sets[$sessionid])) { // Yes, so dispatch this sidekick now and be done with it - trace('Dispatching sidekick immediately'); + //trace('Dispatching sidekick immediately'); $this->dispatch_sidekick($handle, $type, $sessionid, $customdata); $this->dismiss_ufo($handle, false); } @@ -411,7 +496,7 @@ class ChatDaemon { socket_getpeername($newconn->handle, &$newconn->ip, &$newconn->port); $id = $this->new_ufo_id(); - trace('UFO #'.$id.': connection from '.$newconn->ip.' on port '.$newconn->port.', '.$newconn->handle); + //trace('UFO #'.$id.': connection from '.$newconn->ip.' on port '.$newconn->port.', '.$newconn->handle); $this->conn_ufo[$id] = $newconn->handle; } @@ -447,7 +532,8 @@ class ChatDaemon { { // Simply give them the message - $output = chat_format_message_manually($message, 0, $sender, $info['userid'], $info['lang']); + $output = chat_format_message_manually($message, 0, $sender, $info['user'], $info['lang']); + trace('Delivering message "'.$output->text.'" to '.$this->conn_sets[$sessionid][CHAT_CONNECTION_CHANNEL]); if($output->beep) { chat_socket_write($this->conn_sets[$sessionid][CHAT_CONNECTION_CHANNEL], ''); @@ -505,9 +591,9 @@ $DAEMON = New ChatDaemon; $DAEMON->socket_active = false; $DAEMON->trace_level = E_ALL; $DAEMON->socketserver_refresh = 10; -$DAEMON->rememberlast = 10; $DAEMON->can_daemonize = function_exists('pcntl_fork'); $DAEMON->beepsoundsrc = $CFG->wwwroot.'/mod/chat/beep.wav'; +$DAEMON->live_data_update_threshold = 30; /// Check the parameters ////////////////////////////////////////////////////// @@ -747,96 +833,5 @@ function chat_socket_write($connection, $text) { return false; } -function pusers($chatid, $groupid) { -/// Delete users who are using text version and are old - -global $CFG, $str; - -static $lastupdate = 0; -static $outputcache = NULL; - -//chat_delete_old_users(); - - -/// Print headers -ob_start(); - -//print_header(); - -$timenow = time(); - - -// WARNING!!!! THESE ARE HERE TO REMOVE NOTICES!!! -// THEY SHOULD BE FIXED! -$chat_sid = 0; -$chat->course = 0; - -// Keep the results cached for 5 seconds -if($timenow - 5 > $lastupdate || empty($outputcache)) { - - if (empty($str)) { - $str->idle = get_string("idle", "chat"); - $str->beep = get_string("beep", "chat"); - $str->day = get_string("day"); - $str->days = get_string("days"); - $str->hour = get_string("hour"); - $str->hours = get_string("hours"); - $str->min = get_string("min"); - $str->mins = get_string("mins"); - $str->sec = get_string("sec"); - $str->secs = get_string("secs"); - } - - /// Get list of users - - if (!$chatusers = chat_get_users($chatid, $groupid)) { - print_string("errornousers", "chat"); - die('no users'); - //return ob_get_clean(); - //exit; - } - - echo "\n"; - echo "\n"; - echo "\n"; - //echo ''."\n\n"; - //for ($i = 0; $i < 100; ++$i) { - // echo "\n"; - //} - - echo "\n\n"; - foreach ($chatusers as $chatuser) { - $lastping = $timenow - $chatuser->lastmessageping; - echo "\n\n"; - } - echo "
"; - echo "id&course=$chat->course','user$chatuser->id','');\" href=\"$CFG->wwwroot/user/view.php?id=$chatuser->id&course=$chat->course\">"; - print_user_picture($chatuser->id, 0, $chatuser->picture, false, false, false); - echo ""; - echo "

"; - echo fullname($chatuser)."
"; - echo "$str->idle: ".format_time($lastping, $str).""; - echo " id&groupid=$groupid\">$str->beep"; - echo "

"; - echo "
\n
\n"; - - //window.setTimeout('reloadme', 1000) - echo "\n\n"; - - $lastupdate = $timenow; - return $outputcache = ob_get_clean(); -} -else { - // Return cached output - trace('User window cached return'); - return $outputcache; -} - -} ?> diff --git a/mod/chat/gui_sockets/index.php b/mod/chat/gui_sockets/index.php index e3ea7c5c93..4d19d0a59a 100644 --- a/mod/chat/gui_sockets/index.php +++ b/mod/chat/gui_sockets/index.php @@ -66,9 +66,9 @@ require_once('../../../config.php'); chat_serverhost:$CFG->chat_serverport?win=chat&$params"; ?>" NAME="msg" scrolling="auto" noresize marginwidth="2" marginheight="0"> - - diff --git a/mod/chat/lib.php b/mod/chat/lib.php index 40e158cdfb..a32e9b4e60 100644 --- a/mod/chat/lib.php +++ b/mod/chat/lib.php @@ -599,8 +599,27 @@ function chat_display_version($version, $browser) } +function chat_language_override($language) { + // Override the highest-ranking language variable from current_language() + // And save it so we can restore it again afterwards + global $CFG; -function chat_format_message_manually($message, $courseid, $sender, $currentuserid, $language = NULL) { + $oldlang = empty($CFG->courselang) ? NULL : $CFG->courselang; + $CFG->courselang = $language; + + return $oldlang; +} + +function chat_language_restore($language = NULL) { + // Restore the highest-ranking language variable from current_language() + global $CFG; + + if(!empty($language)) { + $CFG->courselang = $language; + } +} + +function chat_format_message_manually($message, $courseid, $sender, $currentuser, $language = NULL) { global $CFG; $output = New stdClass; @@ -610,21 +629,17 @@ function chat_format_message_manually($message, $courseid, $sender, $currentuser $language = current_language(); } - // Override the highest-ranking language variable from current_language() here - // And save it so we can restore it again afterwards - - $oldcfglang = empty($CFG->courselang) ? NULL : $CFG->courselang; - $CFG->courselang = $language; + $oldcfglang = chat_language_override($language); // Get some additional info now that the language has been correctly set // But before that :-) let's override get_user_timezone() for this call... messy stuff... - $tz = ($sender->timezone == 99) ? $CFG->timezone : $sender->timezone; + $tz = ($currentuser->timezone == 99) ? $CFG->timezone : $currentuser->timezone; $message->strtime = userdate($message->timestamp, get_string('strftimemessage', 'chat'), $tz); $message->picture = print_user_picture($sender->id, 0, $sender->picture, false, true, false); if ($courseid) { - $message->picture = "<a target=\"_new\" href=\"$CFG->wwwroot/user/view.php?id=$sender->id&course=$courseid\">$picture</a>"; + $message->picture = "<a target=\"_new\" href=\"$CFG->wwwroot/user/view.php?id=$sender->id&course=$courseid\">$message->picture</a>"; } // Start processing the message @@ -642,12 +657,14 @@ function chat_format_message_manually($message, $courseid, $sender, $currentuser return $output; } - // It's not a system event, so format it nicely + // It's not a system event $text = $message->message; - convert_urls_into_links($text); - replace_smilies($text); - $text = filter_text($text, $courseid); + + /// Parse the text to clean and filter it + + $options->para = false; + $text = format_text($text, FORMAT_MOODLE, $options, $courseid); // And now check for special cases @@ -661,7 +678,7 @@ function chat_format_message_manually($message, $courseid, $sender, $currentuser $output->beep = true; // (eventually this should be set to // to a filename uploaded by the user) - } else if ($beepwho == $currentuserid) { // current user + } else if ($beepwho == $currentuser->id) { // current user $outinfo = $message->strtime.': '.get_string('messagebeepsyou', 'chat', fullname($sender)); $outmain = ''; $output->beep = true; @@ -700,9 +717,7 @@ function chat_format_message_manually($message, $courseid, $sender, $currentuser $output->html .= "</font></td></tr></table>"; // Don't forget to reset the language before returning!!! - if(!empty($oldcfglang)) { - $CFG->courselang = $oldcfglang; - } + chat_language_restore($oldcfglang); return $output; } @@ -718,7 +733,7 @@ function chat_format_message($message, $courseid=0) { return "Error finding user id = $message->userid"; } - return chat_format_message_manually($message, $courseid, $user, $USER->id); + return chat_format_message_manually($message, $courseid, $user, $USER); } -- 2.39.5