Przeglądaj źródła

only one connection allowed per user

Paul Klumpp 12 lat temu
rodzic
commit
caf3ed64c9
1 zmienionych plików z 27 dodań i 15 usunięć
  1. 27 15
      em_server.rb

+ 27 - 15
em_server.rb

@@ -53,8 +53,6 @@ $user_roles = Hash[
 
 
 
-
-
 module InterconnectionPointProtocolHandler
 	
 	@@connected_clients = Array.new
@@ -85,20 +83,12 @@ module InterconnectionPointProtocolHandler
 
 	# returns online users by default by searching through saved connections
 	def ousers
-		def online(user)
-			if ousers.include? user
-				return true
-			else
-				return false
-			end
-		end
-		
 		users = Array.new
 		@@connected_clients.each {|c| users.push(c.username)}
 		return users
 	end # of ousers
 	
-	### checkers getters
+	### checkers
 	def allowed_cmd(inputmessage)
 		my_cmds.each{|c|
 			#print "A #{c} B #{inputmessage}\n"
@@ -108,7 +98,16 @@ module InterconnectionPointProtocolHandler
 		}
 		return false
 	end
-		
+
+	def online(user)
+		if ousers.include? user
+			return true
+		else
+			return false
+		end
+	end
+	
+	
 
 	### actions
 	def put_log(msg)
@@ -153,6 +152,8 @@ module InterconnectionPointProtocolHandler
 		          }
 	end	# of write_role()
 	
+	
+	
 	# what happens with what input?!
 	def inputting(input)
 		
@@ -297,17 +298,28 @@ module InterconnectionPointProtocolHandler
 		if not entered_username? # oh, it seems, it's a new user
 			# this is the user name coming in!
 			username = data.chomp
+			
+			if online(username)
+				put_log "SYS User '#{username}' tried to double connect. Say bye bye."
+				send_data "SYS Hey '#{username}', only one connection allowed. Bye.\n"
+				EventMachine.add_timer 1, proc { close_connection }
+				
+				return false
+			end
+			
 			if not $user_roles.any? {|k, v| k.include? username} 
 				put_log "SYS User '#{username}' unknown to me. Saying bye."
-				send_data "SYS User '#{username}' unknown to me. Bye."
+				send_data "SYS User '#{username}' unknown to me. Bye.\n"
 				
 				# disconnect him
 				close_connection
 			else
+					
 				@username = username
+				
 				@@connected_clients.push(self)
 				
-				put_log("'#{@username}' connected. Online now: #{ousers.join(", ")}")
+				put_log("SYS '#{@username}' connected. Online now: #{ousers.join(", ")}")
 				
 				write_user("HELLO Hi user '#{@username}'! How are you? I'm '#{$version}'")
 				write_user("ROLES #{my_roles.join(", ")}")
@@ -331,7 +343,7 @@ module InterconnectionPointProtocolHandler
 	def unbind # a connection goes bye bye
 		if @username
 			@@connected_clients.delete(self)
-			put_log "'#{@username}' disconnected. Online now: #{ousers.join(", ")}"
+			put_log "SYS '#{@username}' disconnected. Online now: #{ousers.join(", ")}"
 		end
 		
 	end