Advertisement
Guest User

Simple multiuser chat room script.

a guest
Aug 21st, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.08 KB | None | 0 0
  1. -- ***************************************************************
  2. --
  3. -- Copyright 2010 by Sean Conner.  All Rights Reserved.
  4. --
  5. -- This program is free software; you can redistribute it and/or
  6. -- modify it under the terms of the GNU General Public License
  7. -- as published by the Free Software Foundation; either version 2
  8. -- of the License, or (at your option) any later version.
  9. --
  10. -- This program is distributed in the hope that it will be useful,
  11. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. -- GNU General Public License for more details.
  14. --
  15. -- You should have received a copy of the GNU General Public License
  16. -- along with this program; if not, write to the Free Software
  17. -- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18. --
  19. -- Comments, questions and criticisms can be sent to: [email protected]
  20. --
  21. -- ********************************************************************
  22.  
  23.     --[[ ----------------------------------------
  24.     ; we check to see if the members table exists
  25.     ; prior to creating it, so if we reload the
  26.     ; program, we don't lose any current connections
  27.     ;----------------------------------------]]
  28.  
  29. if members == nil then
  30.   members = {}
  31. end
  32.  
  33. if handles == nil then
  34.   handles = {}
  35. end
  36.  
  37. -- **************************************************************
  38.  
  39. local function login(socket)
  40.  
  41.   --[[
  42.   socket:write("Handle you go by: ")
  43.   return socket:read()
  44.   --]]
  45.  
  46.   ---[[ --alternative version
  47.  
  48.   local handle
  49.  
  50.   repeat
  51.     if handle ~= nil then
  52.       socket:write(string.format("%s already taken, try again\n",handle))
  53.     end
  54.     socket:write("Handle you go by: ")
  55.     handle = socket:read()
  56.   until handles[handle] == nil
  57.  
  58.   handles[handle] = socket
  59.   return handle
  60.  
  61.   --]]
  62.  
  63. end
  64.  
  65. -- **************************************************************
  66.  
  67. local function wallaction(socket,who,everybody,me)
  68.   for connection in pairs(members) do
  69.     if connection ~= socket then
  70.       connection:write(string.format("%s %s\n",who,everybody))
  71.     else
  72.       if me ~= nil then connection:write(string.format("%s\n",me)) end
  73.     end
  74.   end
  75. end
  76.  
  77. -- ***************************************************************
  78.  
  79. local function wall(socket,who,everybody,me)
  80.   return wallaction(socket,who .. ":",everybody,me)
  81. end
  82.  
  83. -- ***************************************************************
  84.  
  85. function main(socket)
  86.   local name      = login(socket)
  87.   members[socket] = name
  88.  
  89.   wallaction(socket,name,"is in da room!","You are in the room.")
  90.   io.stdout:write(string.format("%s has joined the party!\n",name))
  91.  
  92.   while true do
  93.     wall(socket,name,socket:read())
  94.   end
  95. end
  96.  
  97. -- ***************************************************************
  98.  
  99. function fini(socket)
  100.   io.stdout:write(string.format("%s has left the party!\n",members[socket]))
  101.   wallaction(socket,members[socket],"has left the building!")
  102.   handles[members[socket]] = nil
  103.   members[socket] = nil
  104. end
  105.  
  106. -- ***************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement