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


sub joinroom 
{ 
	&get_form_data; 
        $NewRoom = $formdata{'message'}; 
         
        &somerror("出错了, 你必须输入聊天室的名称!") if($NewRoom eq ""); 
 
        if($UserData{'Room'} eq $NewRoom) 
        { 
	    &chatshowmsg; 
            exit 0; 
        } 
 
        if($UserData{'Group'} ne 99) 
        { 
            MyDo $do_CheckIDIP; 
            &somerror("对不起, 你不能使用$NewRoom这个聊天室的名称!")       if(&checkforbidroom($NewRoom) ne 0); 
            &somerror("对不起, 你的ID或者IP在聊天室$NewRoom已经被管理员暂时锁住,不能使用!")    if(&checkforbididip($UserData{'User'},$NewRoom) ne 0); 
        } 
 
        MyDo $do_getlastmsgnum; 
        my $newlastmsg = &GetLastMsgNum($NewRoom); 
 
        &updateonline($NewRoom); 
 
        print "Content-type: text/html\nPragma: no-cache\n"; 
        &SetCookies(0,'Room',$NewRoom); 
        print "\n"; 
        print ""; 
} 
 
sub updateonline 
{ 
    my($room) = shift; 
    my($lowid,$entertime,$usernum,$insertsite,$leaveuser,$hastheuser,$Nullroom,$createroom,$newroomnum,$oldroomnum,@leftfilelist,$leftuser,%leftuserlist,%newroomleftuser) = (); 
 
    $lowid=lc($UserData{'User'}); 
 
    $hastheuser = 0; 
    $Nullroom   = 0; 
    $entertime = time; 
    $createroom = 1; 
    $newroomnum = 0; 
    $oldroomnum = 0; 
    @leftfilelist   = (); 
 
    MyDo $do_finduser; 
        
    #First,add user to new room user list 
    &lock("$chatdatadir/$room$DATA"); 
    if(-e "$chatdatadir/$room$DATA") 
    {#Enter a exist room 
       $createroom = 0; 
       if(open(USERLIST,"<$chatdatadir/$room$DATA")) 
       { 
           @userlines=; 
	   close(USERLIST); 
           $usernum = @userlines; 
           $newroomnum = $usernum; 
       } 
       else 
       { 
    	  &unlock("$chatdatadir/$room$DATA"); 
    	  &somerror("系统正忙, 请稍候再试!"); 
       } 
    
       $insertsite = &finduser($lowid,$usernum); 
 
       #Add user to user list 
       if(($insertsite eq $usernum)&&($insertsite ne -1)) 
       {#Add at the tail 
           if(open(USERLIST,">>$chatdatadir/$room$DATA")) 
           { 
              print USERLIST "$lowid:$entertime:$UserData{'Key'}:$ENV{'REMOTE_ADDR'}:$UserData{'Nick'}:$entertime:0:E\n"; 
	      close USERLIST; 
	      &unlock("$chatdatadir/$room$DATA"); 
           } 
           else 
           { 
    	       &unlock("$chatdatadir/$room$DATA"); 
    	       &somerror("系统正忙, 请稍候再试!"); 
           } 
       } 
       elsif(open(USERLIST,">$chatdatadir/$room$DATA")) 
       {#Insert 
            $entertime = time; 
            $newroommaster = "sysop"; 
            for($i = 0;$i<$MID;$i++) 
            { 
                 ($userid,$oldtime) = split(/:/,$userlines[$i],3); 
                 if($oldtime + $MaxIdelTime < $entertime) 
                 { 
                 	$leaveuser .= "$userid "; 
                 	push(@leftfilelist,"$Userdatapath/$userid.KEY"); 
                 	$newroomleftuser{$userid} = 1; 
                 	$newroomnum--; 
                 	next; 
                 } 
                 if($entertime > $oldtime) 
                 {#Find the oldest user in the room. 
                        $newroommaster = $userid; 
                        $entertime = $oldtime; 
                 } 
                 print USERLIST "$userlines[$i]"; 
            } 
            if($insertsite == -1) 
            { 
            	 $newroomnum--; 
            	 $MID++; 
            } 
            print USERLIST "$lowid:$entertime:$UserData{'Key'}:$ENV{'REMOTE_ADDR'}:$UserData{'Nick'}:$entertime:0:E\n"; 
            for( $i = $MID; $i < $usernum; $i++) 
            { 
                 ($userid,$oldtime) = split(/:/,$userlines[$i],3); 
                 if($oldtime + $MaxIdelTime < $entertime) 
                 { 
                 	$leaveuser .= "$userid "; 
                 	push(@leftfilelist,"$Userdatapath/$userid.KEY"); 
                 	$newroomleftuser{$userid} = 1; 
                 	$newroomnum--; 
                 	next; 
                 } 
                 if($entertime > $oldtime) 
                 {#Find the oldest user in the room. 
                        $newroommaster = $userid; 
                        $entertime = $oldtime; 
                 } 
                 print USERLIST "$userlines[$i]"; 
            } 
            close USERLIST; 
            &unlock("$chatdatadir/$room$DATA"); 
      } 
      else 
      { 
    	    &unlock("$chatdatadir/$room$DATA"); 
    	    &somerror("系统正忙, 请稍候再试!"); 
      } 
      &unlock("$chatdatadir/$room$DATA"); 
    } 
    else 
    {#New room 
    	if(open(USERLIST,">$chatdatadir/$room$DATA")) 
    	{ 
    		print USERLIST "$lowid:$entertime:$UserData{'Key'}:$ENV{'REMOTE_ADDR'}:$UserData{'Nick'}:$entertime:0:E\n"; 
    		close USERLIST; 
    	} 
        if(open(MSG,">$chatdatadir/$room$MSG")) 
        { 
             	print MSG "0\n"; 
             	close MSG; 
        } 
        unlink("$chatdatadir/$room$AllMsgExt"); 
    } 
    &unlock("$chatdatadir/$room$DATA"); 
    $newroomnum++; 
 
    #Second, remove user from oldroom user list 
    &lock("$chatdatadir/$UserData{'Room'}$DATA"); 
    if(open(USERLIST,"<$chatdatadir/$UserData{'Room'}$DATA")) 
    {#Del user from the onlinelist 
	     @userlines=; 
	     close(USERLIST); 
	     $oldroomnum = @userlines; 
             if(open(USERLIST,">$chatdatadir/$UserData{'Room'}$DATA")) 
             { 
                $newmaster = "sysop"; 
                $entertime = time; 
	        foreach $userline(@userlines) 
	        { 
                      ($userid,$idnum,$tails)=split(/:/,$userline,3); 
                      if(lc($userid) eq $lowid) 
                      { 
                      	  ($keynum)=split(/:/,$tails,2); 
                      	  if($keynum eq $UserData{'Key'}) 
                      	  { 
                      	  	$hastheuser = 1; 
                      	  	$oldroomnum --; 
                      	  	next; 
                      	  } 
                      } 
                      elsif($idnum + $MaxIdelTime < $entertime) 
                      { 
                 	    push(@leftfilelist,"$Userdatapath/$userid.KEY"); 
                 	    $leftuserlist{$userid} = 1; 
                 	    $leftuser .= "$userid"; 
                 	    $hastheuser = 1; 
                 	    $oldroomnum--; 
                 	    next; 
                      } 
                      print USERLIST $userline; 
                      if($entertime > $idnum) 
                      {#Find the oldest user in the room. 
                          	$newmaster = $userid; 
                          	$entertime = $idnum; 
                      } 
                } 
                close(USERLIST); 
             } 
    } 
    &unlock("$chatdatadir/$UserData{'Room'}$DATA"); 
 
    #Final, modify the room user number 
    if($hastheuser eq 1) 
    { 
           &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($oldroomnum < 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(($leftuserlist{$master} eq 1)&&($attrib ne 9)) 
                           {#Give the master right to the oldest man in the room 
                           	$master = lc($newmaster); 
                           	$mastermessage = " The new master of this room is $master!"; 
                           } 
                           print ROOMLIST "$roomname:$oldroomnum:$master:$visible:$attrib:$tails"; 
                        } 
                   } 
                   elsif($roomname eq $room) 
                   { 
                   	$createroom=0; 
                        if($attrib ne 9) 
                        { 
                        	if($newroomnum eq 1) 
                        	{ 
                        		$master = $lowid; 
                        	} 
                        	elsif($newroomleftuser{$master} eq 1) 
                        	{ 
                        		$master = lc($newroommaster); 
                        		$newroommastermessage = " The new master of this room is $master!"; 
                        	} 
                        } 
                   	print ROOMLIST "$roomname:$newroomnum:$master:$visible:$attrib:$tails"; 
                   } 
                   else 
                   { 
                	print ROOMLIST $roomline; 
                   } 
                } 
                if($createroom eq 1) 
                { 
                	print ROOMLIST "$room:1:$lowid:1:0:0:0:0:0:E\n"; 
                } 
                close ROOMLIST; 
              } 
           } 
           &unlock("$ChatRoomListFile"); 
    } 
 
    unless(-e "$chatdatadir/$room$HEAD") 
    { 
    	&lock("$chatdatadir/$room$HEAD"); 
        if(open(Header,">$chatdatadir/$room$HEAD")) 
        { 
        	print Header "聊天广场 -- $room"; 
        	close Header; 
        } 
        &unlock("$chatdatadir/$room$HEAD"); 
   } 
   $timestr = "[".&convert_time(time)."]"; 
   $entermessage = "$UserData{'Nick'}($UserData{'User'}) 进入我们的聊天室. $newroommastermessage".$timestr; 
   if($leaveuser ne "") 
   { 
        $leftmessage = "$leaveuser离开我们的房间".$timestr; 
        &UpdateChatData(11, '-Billow-',$room,'-Billow-',$leftmessage); 
   } 
 
   &UpdateChatData(10, $UserData{'User'},$room,$UserData{'Nick'},$entermessage); 
   if($Nullroom eq 0) 
   { 
        $leftmessage = "$UserData{'Nick'}($UserData{'User'}) 离开我们的聊天室".$timestr; 
        &UpdateChatData(11, $UserData{'User'},$UserData{'Room'},$UserData{'Nick'},$leftmessage) if($Nullroom eq 0); 
        if($leftuser ne "") 
        { 
             $leftmessage = "$leftuser 离开我们的聊天室".$timestr; 
             &UpdateChatData(11, '-Billow-',$UserData{'Room'},'-Billow-',$leftmessage); 
        } 
        &UpdateChatData(40, '-Billow-',$UserData{'Room'},'-Billow-','Left'); 
   } 
   &UpdateChatData(20, $UserData{'User'},$room,'-Billow-','Enter'); 
   &UpdateChatData(40, $UserData{'User'},$room,'-Billow-','Enter'); 
   &UpdateChatData(30, $UserData{'User'},$room,$UserData{'Nick'},'Enter',$UserData{'User'},1); 
   unlink(@leftfilelist); 
} 
1; #return true