Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ***************************************************************
- --
- -- Copyright 2010 by Sean Conner. All Rights Reserved.
- --
- -- This program is free software; you can redistribute it and/or
- -- modify it under the terms of the GNU General Public License
- -- as published by the Free Software Foundation; either version 2
- -- of the License, or (at your option) any later version.
- --
- -- This program is distributed in the hope that it will be useful,
- -- but WITHOUT ANY WARRANTY; without even the implied warranty of
- -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- -- GNU General Public License for more details.
- --
- -- You should have received a copy of the GNU General Public License
- -- along with this program; if not, write to the Free Software
- -- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- --
- -- Comments, questions and criticisms can be sent to: [email protected]
- --
- -- ********************************************************************
- --[[ ----------------------------------------
- ; we check to see if the members table exists
- ; prior to creating it, so if we reload the
- ; program, we don't lose any current connections
- ;----------------------------------------]]
- if members == nil then
- members = {}
- end
- if handles == nil then
- handles = {}
- end
- -- **************************************************************
- local function login(socket)
- --[[
- socket:write("Handle you go by: ")
- return socket:read()
- --]]
- ---[[ --alternative version
- local handle
- repeat
- if handle ~= nil then
- socket:write(string.format("%s already taken, try again\n",handle))
- end
- socket:write("Handle you go by: ")
- handle = socket:read()
- until handles[handle] == nil
- handles[handle] = socket
- return handle
- --]]
- end
- -- **************************************************************
- local function wallaction(socket,who,everybody,me)
- for connection in pairs(members) do
- if connection ~= socket then
- connection:write(string.format("%s %s\n",who,everybody))
- else
- if me ~= nil then connection:write(string.format("%s\n",me)) end
- end
- end
- end
- -- ***************************************************************
- local function wall(socket,who,everybody,me)
- return wallaction(socket,who .. ":",everybody,me)
- end
- -- ***************************************************************
- function main(socket)
- local name = login(socket)
- members[socket] = name
- wallaction(socket,name,"is in da room!","You are in the room.")
- io.stdout:write(string.format("%s has joined the party!\n",name))
- while true do
- wall(socket,name,socket:read())
- end
- end
- -- ***************************************************************
- function fini(socket)
- io.stdout:write(string.format("%s has left the party!\n",members[socket]))
- wallaction(socket,members[socket],"has left the building!")
- handles[members[socket]] = nil
- members[socket] = nil
- end
- -- ***************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement