Kaynağa Gözat

good working version

Paul Klumpp 12 yıl önce
ebeveyn
işleme
26f26a94da
1 değiştirilmiş dosya ile 175 ekleme ve 26 silme
  1. 175 26
      sockcentral.rb

+ 175 - 26
sockcentral.rb

@@ -8,7 +8,7 @@ require 'digest/md5'
 # .. allowed commands .. ROLES = CAPABILITIES
 # normal users have ROLE broadcast. Roles are defined on a per-user basis .. in a config.
 
-$version = "0.11big_yadda"
+$version = "0.15TCPServer"
 $debug = 0
 
 $role_commands = Hash[
@@ -54,6 +54,7 @@ $user_roles = Hash[
 STDOUT.sync = true
  
 
+# display loop ... it is displaying the display_queue 
 def displaying(client, user)
 	
 	#client.puts "#{user} connected."
@@ -80,23 +81,13 @@ end
 def write_user(user, input)
 	
 	if online_users.user(user)
-		sometime = "#{Time.now.strftime("%Y-%m-%d %H:%M:%S %z")}"
+		sometime = "#{Time.now.utc.strftime("%Y-%m-%d %H:%M:%S %z")}"
 		line = "#{sometime}: #{input}"
 	
 		$display_queue[user].push(line)
 	end
 end
 
-def inputting(user, input)
-	
-	write_user(user, "You said: #{input}")
-	
-	if input =~ /^w (.+)/
-		input = $1
-		write_user("paul_dev_eggdrop", "he said: #{input}")
-	end
-end
-
 
 def kill_conn(c, user, t_display)
 	sleep 0.5
@@ -125,7 +116,7 @@ def online_users
 	
 end
 
-def write_role(role, input)
+def write_role(role, input, *exempts)
 	
 	#find users with role
 	users = Array.new
@@ -145,11 +136,170 @@ def write_role(role, input)
 	
 	# now write to them
 	lala.each {|user|
-	           write_user(user, input)
+	          if not exempts.include? user then
+							write_user(user, input)
+	          end
 	          }
 	
 end
 
+def allowed_cmd(user, inputmessage)
+	
+	my_cmds(user).each{|c|
+    #print "A #{c} B #{inputmessage}\n"
+		if inputmessage =~ /^#{c}/
+			return true
+		end
+	}
+	return false
+
+end
+
+# what happens with what input?!
+def inputting(user, input)
+	
+	write_user(user, "SYS You typed: #{input}")
+	
+	if input =~ /^([A-Z_]+)/
+		cmd = $1
+	end
+	# now we have the cmd .. or not ;)
+	
+	if input =~ /^[A-Z_]+ (.+)/
+		payload = $1
+	end
+	# now we can use payload
+
+	if allowed_cmd(user, cmd)
+		
+		### typical system commands follow
+		if cmd == "PING"
+			write_user(user, "PONG")
+			
+		elsif cmd == "C"
+			
+			if payload =~ /^(.+)$/
+				write_role($default_role, "C #{user}: #{$1}")
+				
+			else
+				write_user(user, "SYS Format is C <chattext>")
+			end
+			
+	  elsif cmd == "WHO"
+			write_user(user, "WHO_RE #{online_users.join(", ")}")
+			
+		elsif cmd == "PART"
+			write_user(user, "SYS Goodbye '#{user}'.")
+			write_role($default_role, "PARTED User '#{user}' just left the party.", user)
+			return false
+			
+		###now for the good stuff, broadcasting
+		elsif cmd == "REQ_BC"
+			
+			# help with format .. but send the raw payload
+			if payload =~ /^(.+),(.+),(.+),'(.+)','(.+)'$/
+				#              n    f    s     ni     txt
+				error = 0
+				# $&
+				# The string matched by the last successful pattern match in this scope, or nil if the last pattern match failed. (Mnemonic: like & in some editors.) This variable is r
+				network = $1
+				freqname = $2
+				source = $3
+				nickname = $4
+				text = $5
+				
+				if not network =~ /^(QWalt)|(QDEV)/
+					write_user(user, "SYS Network name #{network} is unknown: QWalt or QDEV allowed.")
+					error = 1
+				end
+				
+				if not freqname =~ /^(-qw-)|(-spam-)|(-dev-)/
+					write_user(user, "SYS Frequency name is unknown #{freqname}")
+					error = 1
+				end
+				
+				if not source =~ /^(#.+)|(qw:\/\/)|(http:\/\/)/
+					write_user(user, "SYS Source string is not in the form ^(#.+)|(qw:\/\/)|(http:\/\/)  was: #{source}")
+					error = 1
+				end
+				
+				if not nickname =~ /^.+/ 
+					write_user(user, "SYS Nickname string is not in the form ^.+  #{nickname}")
+					error = 1
+				end
+				
+				if not text =~ /^.+/
+					write_user(user, "SYS Message string is not in the form ^.+ #{text}")
+					error = 1
+				end
+				
+			else # of check syntax
+				write_user(user, "SYS Command format is REQ_BC <network>,<frequency>,<source/channel>,'<nickname>','<message>'")
+				error = 1
+			end
+			
+			if error == 0
+				
+				# send REQ_BC to the corresponding role.
+				bcid = Digest::MD5.hexdigest("%d %s %s %s %s %s" % [Time.now.utc.to_i, network, freqname, source, nickname, text])
+				
+				# so it only reaches the issuer of REQ_BC
+				write_user(user, "BC_ID #{bcid} for: #{network},#{freqname},#{source}")
+				$broadcasts[bcid] = user
+				
+				finalmessage = "BC %s %s,%s,%s,'%s','%s'" % [bcid, network, freqname, source, nickname, text]
+				write_role("broadcast", finalmessage, user) # write to the role with the exempt of myself.
+				
+				
+			end 
+			#end of REQ_BC here..
+			
+		elsif cmd == "BC_RE"
+				
+			if payload =~ /^(.+) (.+)=(\d+),(.+)=(\d+)$/
+				
+				bcid = $1
+				user_string = $2
+				user_count = $3
+				item_string = $4
+				item_count = $5
+				
+				payload = "%s %s=%d,%s=%d" % [bcid, user_string, user_count, item_string, item_count]
+				# according bcid it is possible to get the originating user.. so send only to his topic!
+				
+				if $broadcasts[bcid]
+					to_user = $broadcasts[bcid]
+					
+					write_user(user, "SYS Broadcast reply bcid: #{bcid} underway to #{to_user}.")
+					write_user(to_user, "#{cmd} #{payload}")
+				else
+					write_user(user, "SYS Broadcast reply bcid: #{bcid} underway.")
+					write_role("broadcast", "#{cmd} #{payload}", user)
+				end
+				
+			else # of bc_re format check
+				put_log "SYS Format is BC_RE <bcid> <userstring>=<usercount>,<itemstring>=<itemcount>"
+			end
+		
+		end
+		
+			
+	else  # of if allowed command
+		write_user(user, "SYS Command #{input} not allowed, your commands are: #{my_cmds(user).join(", ")}.")
+	end  # of if allowed command
+	
+end
+
+def my_roles(user)
+	return $user_roles[user]
+end
+
+def my_cmds(user)
+	myroles = my_roles(user)
+	mycmds = myroles.collect {|v| $role_commands[v]}.flatten
+	return mycmds
+end
+
 def thinking(c, user, t_display)
 	
 	put_log "#{user} connected. Online now: #{online_users.join(", ")}"
@@ -160,25 +310,23 @@ def thinking(c, user, t_display)
 		
 		return false
 	end
-	
-	my_roles = $user_roles[user]
-	my_cmds = my_roles.collect {|v| $role_commands[v]}.flatten
+
 	
 	write_user(user, "HELLO Hi user '#{user}'! How are you? I'm #{$version}")
-	write_user(user, "ROLES #{my_roles.join(", ")}")
-	write_user(user, "COMMANDS #{my_cmds.join(", ")}")
-	write_user(user, "SYS User '#{user}' just joined the party.")
-
-	write_role("broadcast", "to the broadcasting role")
+	write_user(user, "ROLES #{my_roles(user).join(", ")}")
+	write_user(user, "COMMANDS #{my_cmds(user).join(", ")}")
+	write_role("everyone", "JOINED User '#{user}' just joined the party.", user)
 	
 	# reading the client input ... to write it somewhere, possibly to the client himself.
 	begin
-		while input = c.gets.chomp
-			inputting(user, input)
+		input = ""
+		while input != "PART" do
+			input = c.gets.chomp
+			execution = inputting(user, input)
 		end
 	rescue Exception => e  
-		#puts e.message  
-		#puts e.backtrace.inspect  
+		puts e.message  
+		puts e.backtrace.inspect  
 		
 		put_log "#{user} lost connection."
 		
@@ -188,6 +336,7 @@ end
 
 def spawn_server
 	$display_queue = Hash.new
+	$broadcasts = Hash.new
 	
 	server = TCPServer.new 7337
 	Thread.abort_on_exception = true