www.pudn.com > coolchat.zip > chatkeepactive.pl


sub chatkeepactive 
{ 
        my $lowid = lc($UserData{'User'}); 
        my (%leftuser,@leftfilelist,$leaveuser)=(); 
        my $rightuser = 0; 
        my $roomchanged = 0; 
 
        &lock("$chatdatadir/$UserData{'Room'}$DATA"); 
        if(open(USERLIST,"$chatdatadir/$UserData{'Room'}$DATA")) 
        { 
                @userlines=; 
                close(USERLIST); 
                $mostoldtime = time; 
                $roomnumber = @userlines; 
                $newmaster  = 'sysop'; 
                if(open(USERLIST,">$chatdatadir/$UserData{'Room'}$DATA")) 
                { 
                   foreach $userline(@userlines) 
                   { 
                        ($userid,$entertime,$therandnum,$ip,$tails)=split(/:/,$userline,5); 
                        if($userid eq $lowid) 
                        { 
                              print USERLIST "$userid:".time.":$therandnum:$ip:$tails"; 
                              $rightuser = 1; 
                              next; 
                        } 
                        elsif($entertime + $MaxIdelTime < time) 
                        { 
                 	      $leaveuser .= "$userid "; 
                 	      push(@leftfilelist,"$Userdatapath/$userid.KEY"); 
                 	      $leftuser{$userid} = 1; 
                 	      $roomnumber--; 
                 	      $roomchanged = 1; 
                 	      next; 
                        } 
                        if($mostoldtime > $entertime) 
                        { 
                             $newmaster     = $userid; 
                             $mostoldtime   = $entertime; 
                        } 
                        print USERLIST $userline; 
                   } 
                   close USERLIST; 
                } 
                &unlock("$chatdatadir/$UserData{'Room'}$DATA"); 
 
                if($roomchanged eq 1) 
                { 
                   my $Nullroom = 0; 
                   &lock("$ChatRoomListFile"); 
                   if(open(ROOMLIST, "<$ChatRoomListFile")) 
                   {#Change the master and the number of the room 
	              @roomlines=; 
                      close ROOMLIST; 
                      if(open(ROOMLIST, ">$ChatRoomListFile")) 
                      { 
                          foreach $roomline(@roomlines) 
                          { 
               	               ($roomname,$usernum,$master,$visible,$attrib,$tails)=split(/:/,$roomline,6); 
               	               if($roomname eq $UserData{'Room'}) 
               	               { 
                                    if($roomnumber < 1) 
                                    {#Nobody there, delete the forbit file and the messages files 
                                          if(($UserData{'Room'} ne "main")&&($attrib ne 9)) 
                                          { 
				                 @filelist[0] = $ChaTempFilePath.$UserData{'Room'}.".out"; 
				                 @filelist[1] = $ChaTempFilePath.$UserData{'Room'}.".ip"; 
				                 @filelist[2]="$chatdatadir/$UserData{'Room'}$AllMsgExt"; 
				                 @filelist[3]="$chatdatadir/$UserData{'Room'}$DATA"; 
				                 @filelist[4]="$chatdatadir/$UserData{'Room'}$HEAD"; 
				                 @filelist[5]="$chatdatadir/$UserData{'Room'}$MSG"; 
				                 unlink(@filelist); 
				                 $Nullroom = 1; 
                                          } 
                                          else 
                                          { 
                                                 print ROOMLIST "$roomname:0:$master:$visible:$attrib:$tails"; 
                                          } 
                                    } 
                                    else 
                                    { 
                                           if(($leftuser{$master} eq 1)&&($attrib ne 9)) 
                                           {#Give the master right to the oldest man in the room 
                           	                  $master = lc($newmaster); 
                           	                  $mastermessage = " 现在本聊天室的室主是$master!"; 
                                           } 
                                           print ROOMLIST "$roomname:$roomnumber:$master:$visible:$attrib:$tails"; 
                                    } 
                               } 
                               else 
                               { 
                	             print ROOMLIST $roomline; 
                               } 
                           } 
                           close ROOMLIST; 
                        } 
                     } 
                     &unlock("$ChatRoomListFile"); 
                     if($Nullroom eq 0) 
                     { 
                           if($leaveuser ne "") 
                           { 
                                  $timestr = "[".&convert_time(time)."]"; 
                                  $leftmessage = "$leaveuser 离开我们的聊天室".$timestr; 
                                  &UpdateChatData(10, '-花无缺-',$UserData{'Room'},'-花无缺-',$leftmessage); 
                                  &UpdateChatData(40, '-花无缺-',$UserData{'Room'},'-花无缺-','Left'); 
                           } 
                     } 
                     unlink(@leftfilelist); 
                 } 
      } 
      &somerror("你未发言时间太长,在线数据出错!","$ChatURLpath",1) if($rightuser ne 1); 
      print "Content-type: text/html\nPragma: no-cache\n\n"; 
      my $randnum = int($AdNumber); 
      print ""; 
} 
1;