Advertisement
Mackan90096

Chatty

Mar 29th, 2015
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.96 KB | None | 0 0
  1. --[[
  2. Copyright 2015 Max Thor <thormax5@gmail.com>
  3.  
  4.    Licensed under the Apache License, Version 2.0 (the "License");
  5.    you may not use this file except in compliance with the License.
  6.    You may obtain a copy of the License at
  7.  
  8.        http://www.apache.org/licenses/LICENSE-2.0
  9.  
  10.    Unless required by applicable law or agreed to in writing, software
  11.    distributed under the License is distributed on an "AS IS" BASIS,
  12.    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.    See the License for the specific language governing permissions and
  14.    limitations under the License.
  15.    ]]--
  16.  
  17. for k,v in ipairs(rs.getSides()) do
  18.     if(peripheral.getType(v) == "modem") then
  19.       modem = peripheral.wrap(v)
  20.     end
  21. end
  22.  
  23. local msg = ""
  24. local name = ""
  25. local cmd = ""
  26. local args
  27.  
  28. local channel = 1
  29. local cmds = {
  30.     ["me"] = {
  31.         ["does"] = function(args)
  32.             send('* '..name..' '..args)
  33.         end
  34.     },
  35.     ["nick"] = {
  36.         ["does"] = function(nc)
  37.             send(name..' is now known as '..nc)
  38.             name=nc
  39.         end
  40.     }
  41. }
  42.  
  43. local history = {}
  44.  
  45. function initConf()
  46.   --modem.open(channel)
  47. end
  48.  
  49. function send(msg)
  50.    --modem.transmit(channel,channel,msg)
  51. end
  52.  
  53. function input()
  54.   while true do  
  55.     _, p1 = os.pullEvent("key")
  56.     if(p1 == 28) then
  57.       local inp = read()
  58.       local cmd,args = inp:match("^/(%w+)%s*(.-)$")
  59.       if cmd then
  60.         if(cmds[cmd]) then
  61.             cmds[cmd]['does'](args)
  62.         else
  63.                print("Unknown command: "..cmd)
  64.         end
  65.       else
  66.         send("<"..name.."> "..inp)
  67.       end
  68.     end
  69.   end
  70. end
  71.  
  72. function chat()
  73.   while true do
  74.     e, p1, p2, p3, p4 = os.pullEventRaw()
  75.     if(e == "modem_message") then
  76.       print(p4)
  77.     elseif(e == "terminate") then
  78.       send(name.." has left")
  79.     end
  80.   end
  81. end
  82.  
  83. function init()
  84.   term.clear()
  85.   print("Chatty")
  86.   write("Name > ")
  87.   name = read()
  88.   initConf()
  89.   send(name.." has joined!")
  90.   parallel.waitForAny(chat,input)
  91. end
  92.  
  93. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement