class PastoralServer < Framework public#{{{ def initialize()#{{{ @jid = 'pastoralchat@gmail.com/Home' @password = 'p4st0r4lsthlkj12' @jid = JID::new(@jid) @cl = Client::new(@jid) @cl.connect @cl.auth(@password) @allAnimals = [] print("Server started.\n") end#}}} def go#{{{ # get the animals going @allAnimals.each do |a| a.startActions.each do |s| handle s, a end end end#}}} def broadcast(str,sock,sendToSelf=true)#{{{ @allSocks.each do |s| s.terminal.send(str) if (s != @socket && s.user.sameCubeAs(sock.user.cube) && (sendToSelf || s != sock)) end end#}}} def getUsersPresent(cube)#{{{ users = [] @allSocks.each do |s| users.push s.user if s.user.sameCubeAs(cube) && s != @socket end users end#}}} def addAnimal(animal)#{{{ @allAnimals.push animal end#}}} #}}} private#{{{ def acceptConnection#{{{ newSock = @socket.accept newSock.sockType = SockTypeUser @allSocks.push newSock newSock.terminal.pushString welcomeMessage newSock.terminal.pushString getLine newSock.terminal.send newSock.describeCube broadcast "A guest has joined us.",newSock, false end#}}} def handle(str,sock)#{{{ $stderr.puts sock.user.name+" ("+sock.user.cube.x.to_s+","+sock.user.cube.y.to_s+","+sock.user.cube.z.to_s+"): "+str+"\n" if str.match('$') sock.terminal.rewriteScreen return end if str.match('^'+$cmdChar) sock.terminal.pushString(': '+str) tokens = str.sub($cmdChar,'').split(' ') handler = Handler.new(sock,tokens,@allSocks) eval('handler.'+tokens[0]) else str.chomp! sock.terminal.pushString("You say, \""+str+"\e[m\"") broadcast sock.user.name+" says, \""+str+"\e[m\"", sock, false end sock.terminal.rewriteScreen unless str.match('^'+$cmdChar+'quit') end#}}} def welcomeMessage#{{{ "\n\n\e[1mWelcome to my \"Pastoral Chat\" server.\e[0m\n\nThis software is still under serious development, so please excuse quirks, bugs, crashes, spontaneous combustion. If you encounter a weird bug, feel free to contact the developer (me@lucasoman.com). Questions, suggestions and praise can be directed there, as well. At any time, type !help for very sparse help info.\n" end#}}} #}}} end